views:

680

answers:

3

Given a system (a website for instance) that lets a user customize the background color for some section but not the font color (to keep number of options to a minimum), is there a way to programmatically determine if a "light" or "dark" font color is necessary?

I'm sure there is some algorithm, but I don't know enough about colors, luminosity, etc to figure it out on my own.

+14  A: 

I encountered similar problem. I had to find a good method of selecting contrastive font color to display text labels on colorscales/heatmaps. It had to be universal method and generated color had to be "good looking", which means that simple generating complementary color was not good solution - sometimes it generated strange, very intensive colors that were hard to watch and read.

After long hours of testing and trying to solve this problem, I found out that the best solution is to select white font for "dark" colors, and black font for "bright" colors.

Here's an example of function I am using in C#:

Color ContrastColor(Color color)
        {
            int d = 0;

            // Counting the perceptive luminance - human eye favors green color... 
            double a = 1 - ( 0.299 * color.R + 0.587 * color.G + 0.114 * color.B)/255;

            if (a < 0.5)
                d = 0; // bright colors - black font
            else
                d = 255; // dark colors - white font

            return  Color.FromArgb(d, d, d);
        }

This was tested for many various colorscales (rainbow, grayscale, heat, ice, and many others) and is the only "universal" method I found out.

Edit
Changed the formula of counting a to "perceptive luminance" - it really looks better! Already implemented it in my software, looks great.

Gacek
It's probably not important, but you might want a better function to compute the brightness http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
jleedev
Thanks! Very useful (+1).
Gacek
This looks like it'll be perfect.
Joseph Daigle
Improved the code - now it's even better
Gacek
A: 

Take a look at this Question here. It calculates the complementary color of any given color. This should do the trick for you.

Best wishes,
Fabin

halfdan
That won't really be pleasant too look at. Yellow on purple, etc. etc.. Eww!
Marcus Lindblom
You're definetely right with that point. Gacek's solution is smarter.
halfdan
A: 

If you're manipulating color spaces for visual effect it's generally easier to work in HSL (Hue Separation and Lightness) than RGB. Moving colours in RGB to give naturally pleasing effects tends to be quite conceptually difficult, whereas converting into HSL, manipulating there, then converting back out again is more intuitive in concept and invariably gives better looking results.

Wikipedia has a good introduction to HSL and the closely related HSV. And there's free code around the net to do the conversion (for example here is a javascript implementation)

What precise transformation you use is a matter of taste, but personally I'd have thought reversing the Hue and Lightness components would be certain to generate a good high contrast colour as a first approximation, but you easily go for more subtle effects.

Cruachan