I'm looking for an algorithm to get the best text color (most pleasing to the eye) from a given background color.
Any idea ?
Black text on a white background provides maximal value contrast and, therefore, optimal readability for body text. Black text on a white background provides maximal value contrast and, therefore, optimal readability for body text.
With a black background the lettering tends to stand out more onto to background than with other colored backgrounds. Black is one of the few surfaces that lets other colored text work great together.
Their general findings were: 1) Black and white were consistently rated as the most readable; 2) Color combinations that included black were rated more readable than those that did not; and 3) Darker text on lighter backgrounds were rated higher than lighter text on darker backgrounds.
On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a color. You can also use the formatting options on the Mini toolbar to quickly format text. The Mini toolbar appears automatically when you select text.
"The best color" is very subjective and context dependent. It depends on what effect you want: If you want the highest contrast possible, look for complementary colors (that would give you red on green, yellow on blue etc.). If you want colors that are "similar", look for analogous harmonies. If you want to decide between black and white only, measure brightness (hamstergene posted a very good formula for it).
Wherever you go, HSV color model is the key.
Getting complementary or analogous colors is trivial (e.g. hue_text = (hue_bg + 180) % 360
OR hue_text = (hue_bg + 30) % 360
).
You can also experiment with value (lightness) and saturation for better contrast. For example, v_text = 1 - v_bg
could give you dark text on bright background and vice versa (watch out for mid tones!). It doesn't have to be linear - you also could go for a step function like: if v_bg < 0.5 then v_text = 1 else v_text = 0
, or if s_bg < 0.5 then s_text = 1 else s_text = 0
(vibrant on pale).
That's just some hints. In a word: It depends!
Google for color theory and color harmonies. Some links:
http://www.tigercolor.com/color-lab/color-theory/color-harmonies.htm
http://www.colormatters.com/color-and-design/basic-color-theory
There is no best for everyone.
Say if you need to make sure text will be easily readable, the following simple formula worked well for me:
textColor = brightness(backColor) > 0.5 ? black : white;
where brightness is defined as
brightness(R,G,B) = 0.299*R + 0.587*G + 0.114*B
(there are several definitions for “brightness”, I used this one but I think any one would work).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With