The algorithm for obtaining the best text color

I am looking for an algorithm to get the best text color (most pleasing to the eye) from a given background color.

Any idea?

+8
algorithm colors
source share
2 answers

"Best color" is very subjective and contextual. It depends on what effect you want: if you need the highest contrast, find additional colors (this will give you red to green, yellow to blue, etc.). If you want the colors to be β€œsimilar,” look for similar harmonies. If you want to solve only black and white, measure the brightness (hamstergene placed a very good shape for it).

Wherever you go, the HSV color model is the key.

Getting additional or similar colors is trivial (for example, 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 can give you dark text on a bright background and vice versa (pay attention to midtones!). It does not have to be linear - you can also use the step function, for example: 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 (bright pale).

Here are 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

+12
source share

There is no better for everyone.

Say, if you need to make sure the text is easy to read, the following simple formula works 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, but I think any of them will work).

+6
source share

All Articles