There is some information in the Web Content Accessibility Guidelines (WCAG) 2.0 (http://www.w3.org/TR/2008/REC-WCAG20-20081211)
-
Visual contrast: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast
-
Contrast ratio: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
-
Relative luminance : http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
There’s a good example in this site but he calculate where two colors are enough, not how to get them.
To choose a color with good contrast, I’d go with complementary colors: for example, choose the random color A, transform it to a HSV space, get the complementary hue.
Complementary hue: after you transform color from RGB to HSV, complementary hue will be 180 degrees appart (or 0.5, in a 0-1 normalized hue value). This site has something about it in PHP