Intelligent Color Detection

We are working on a medical scheduling system that allows users to define colors to show on the calendar based on a certain status. The issue we have is that if the user picks a dark background color and we are using a dark font, the font doesn't show. Likewise if they pick a light color and we are using a light font. For a variety of reasons, we don't want to have the user setting both colors.

But, we wonder if there is a particular pattern to the hex codes of colors. Perhaps if it is less than a given value, a dark font should be used and a light font used otherwise?

So, is there an intelligent way to programatically pick the font color based on the user's choice of background color?

Amy


You can use the hex codes to mathematically calculate the contrast between two colors. With that information the question is just which color of text (white or black) has more contrast with the chosen background color. This site shows how to write that code in PHP or Javascript, but it could easily be adapted to other languages.

http://24ways.org/2010/calculating-color-contrast


Color brightness: http://www.webmasterworld.com/forum88/9769.htm

But, actually, why not to just invert background and use resulting color is a foreground?


For your first question, the hex values of color codes are in RGB format I believe. This means that the first byte is the amount of red-ness in the color; the second byte is the amount of green-ness in the color; and the third byte is the amount of blue-ness in the color.

If you wanted to try and guess a font color, you could try and see how close to #FFFFFF it is, and use a dark color if it is less than #777777.

I'm not a expert in colors by any means, so I won't guarantee the correctness of this answer.

链接地址: http://www.djcxy.com/p/15264.html

上一篇: 用不透明度找到“等价”的颜色

下一篇: 智能颜色检测