Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good text foreground color for a given background color

I'm drawing a color selection button and I'm looking for a nice and simple formula to get a good text color (foreground) for a given background color in RGB.

A simple try would be to just take the complement color but this will produce an odd looking button for colors like pure blue or pure red.
Is there something well known that does this?

If it matters at all, I'm using QT.

like image 739
shoosh Avatar asked Jun 03 '09 19:06

shoosh


People also ask

What font color is best for a colorful background?

Contrast between the foreground and background is one of the most important factors for the ease of reading. If coloured text is used on a bright background the contrast will be weak, for optimal contrast results is white text against dark colored backgrounds.

How do you choose foreground and background color?

Hold down the Alt (Win) or Option (Mac) key, and then click to change the background color. Click on a color swatch in the Swatches panel to change the foreground color. (Mac) key, and then click to change the background color. Click the Foreground or Background thumbnail to choose the color's destination.

What is the best background Colour for reading text?

The contrast between the color of the backdrop and the color of the body text should be at least 80%. Since the contrast between foreground and background is critical to readability, black-and-white is your best bet.

Which color is used for foreground color?

Using the Foreground / Background Tool The basic idea of this tool is simple. The foreground controls what color your brush or pencil will be, while the background color erases any added color and replaces it with the background color, which is white by default.


2 Answers

For maximum legibility, you want maximum brightness contrast without getting into hues which don't work together. The most consistent way to do this is to stick with black or white for the text color. You might be able to come up with more aesthetically pleasing schemes, but none of them will be more legible.

To pick between black or white, you need to know the brightness of the background. This gets a little more complicated, due to two factors:

  • The perceived brightness of the individual primaries red, green, and blue are not identical. The quickest advice I can give is to use the traditional formula to convert RGB to gray - R*0.299 + G*0.587 + B*0.114. There are lots of other formulas.

  • The gamma curve applied to displays makes the middle gray value higher than you'd expect. This is easily solved by using 186 as the middle value rather than 128. Anything less than 186 should use white text, anything greater than 186 should use black text.

like image 87
Mark Ransom Avatar answered Sep 25 '22 02:09

Mark Ransom


I'm no expert on programming things related to RGB, but from a designer's perspective, often the most readable color will be just a much lighter (if the background color is dark) or darker (if the background color is light) version of the same shade.

Basically you'd take your RGB values and if they're closer to 0 (dark) you'd push them each up by an equal amount for your foreground color, or vice versa if it's a light BG.

Complement colors can actually be really painful on the eyes for readability.

like image 45
Gabriel Hurley Avatar answered Sep 23 '22 02:09

Gabriel Hurley