"Best color" is very subjective and contextual. It depends on what effect you want: if you need the highest contrast, find additional colors (this will give you red to green, yellow to blue, etc.). If you want the colors to be βsimilar,β look for similar harmonies. If you want to solve only black and white, measure the brightness (hamstergene placed a very good shape for it).
Wherever you go, the HSV color model is the key.
Getting additional or similar colors is trivial (for example, hue_text = (hue_bg + 180) % 360 OR hue_text = (hue_bg + 30) % 360 ).
You can also experiment with value (lightness) and saturation for better contrast. For example, v_text = 1 - v_bg can give you dark text on a bright background and vice versa (pay attention to midtones!). It does not have to be linear - you can also use the step function, for example: if v_bg < 0.5 then v_text = 1 else v_text = 0 or if s_bg < 0.5 then s_text = 1 else s_text = 0 (bright pale).
Here are just some hints. In a word: It depends!
Google for color theory and color harmonies. Some links:
http://www.tigercolor.com/color-lab/color-theory/color-harmonies.htm
http://www.colormatters.com/color-and-design/basic-color-theory
Konrad garus
source share