The United Nations web accessibility standards page (http://www.un.org/webaccessibility/1_visual/13_colourcontrast.shtml) does indicate a standard for ensuring proper text contrast on websites, keeping in mind that some users are colorblind. This might be particularly important for you since some of your users may be colorblind as well (would be hard to tell a red ball from a green background if they have the same luminance).
The UN page points to the Juicy Studios Luminosity Colour Contrast Ratio Analyser at (http://juicystudio.com/services/luminositycontrastratio.php#specify) which states text on background must have a contrast ratio of 4.5:1, except for what I believe to be similar to your case:
- Large Text: Large-scale text and images of large-scale text have a contrast ratio of at least 3:1
Now what's a contrast ratio? Following hyperlinks further (this is fun!) we get to a W3 page that defines contrast ratio as:
Contrast Ratio = (L1 + 0.05)/(L2 + 0.05)
Where L1 and L2 are the relative luminences of the colors. L1 is the more luminant color, L2 is the less luminant color. Again, we follow the link to the same W3 page that specifies:
L = 0.2126 * R + 0.7152 * G + 0.0722 * B where R, G and B are defined as:
if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4
if GsRGB <= 0.03928 then G = GsRGB/12.92 else G = ((GsRGB+0.055)/1.055) ^ 2.4
if BsRGB <= 0.03928 then B = BsRGB/12.92 else B = ((BsRGB+0.055)/1.055) ^ 2.4
and RsRGB, GsRGB, and BsRGB are defined as:
RsRGB = R8bit/255
GsRGB = G8bit/255
BsRGB = B8bit/255
NOTENOTE: The caret symbol `^^ above refers to exponentiation (x²)
An we have a pretty good framework for a contrast algorithm (that's colorblind friendly!)
- Determine R, G, B values for each color
- Determine Luminance for each color
- If the Contrast Ratio of the more luminant color to the least luminant color is less than 3, reroll!