>I need to be able to determine the forecolor when a user selects the backcolor. This is to be able to display the text as readable based on what the backcolor is. Just taking the inverse color works for the outer color spectrum but when it is in the middle this does not work well.
>
>Does anyone have any ideas on how to calculate the best readable forecolor based on the backcolor setting? Thanks.
Inverse color is the easiest thing to do: bitxor(nColor, 0xffffff), but the inverse of #808080 is #7f7f7f, which looks pretty much the same. IOW, any color that's close to the middle (and #808080 and #7f7f7f aren't colors at all - they are grays) will have an inverse that's just as close to the middle, and the distance between such a color and its inverse in the color space may fall below any readability standard.
So if the solution that Tracy posted looks like a cheap way out (the result is again not a color, but a black or a white), it's actually the best way out. In many cases you actually don't want the contrary color, because it would wear and (literally?) tear the eyes of the user. While it may help some of them, it may irritate others. So I'd go with that simple black and white answer to this color question.