Color Contrast

PROBLEM: given a color, call it color1, with RGB values of r, g, and b, then find the best choice of color2 as white or black to contrast with color1.

SOLUTION (as pseudo code)

If 0.3 * r + 0.59 * g + 0.11 * b is less than 128 Then
    color2 = white
    color2 = black
End If

HOW DOES IT WORK? I have no idea, I just know that I paid two different companies to develop an algorithm to do this for a client of mine some years ago and this was not only the better of the two, it was also given to me practically free because it is a public domain algorithm that the company I hired got out of some obscure magazine that one of their developers knew about.

Please pass this along to anyone who needs it.