## DEV Community is a community of 642,858 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading... # Which Colors Look Good on Black and White? Ben Szabo
CSS Bender
Updated on ・3 min read

Imagine you have to find a color that looks good both on black and white. By "looking good" I mean conforming at least WCAG AA Contrast (Minimum), which is a ratio of 4.5:1.

tl;dr: Scroll to the end to get the list of colors

# So how is color contrast ratio calculated?

The WCAG contrast ratio definition says:

contrast ratio
(L1 + 0.05) / (L2 + 0.05), where

• L1 is the relative luminance of the lighter of the colors, and
• L2 is the relative luminance of the darker of the colors.

NOTE
Contrast ratios can range from 1 to 21 (commonly written 1:1 to 21:1).

Simple math, right? You take two values and divide them.

# But how is the relative luminance calculated??

relative luminance
the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white

NOTE
For the sRGB colorspace, the relative luminance of a color is defined as L = 0.2126 * R + 0.7152 * G + 0.0722 * B where R, G and B are defined as:

• if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4
• if GsRGB <= 0.03928 then G = GsRGB/12.92 else G = ((GsRGB+0.055)/1.055) ^ 2.4
• if BsRGB <= 0.03928 then B = BsRGB/12.92 else B = ((BsRGB+0.055)/1.055) ^ 2.4

and RsRGB, GsRGB, and BsRGB are defined as:

• RsRGB = R8bit/255
• GsRGB = G8bit/255
• BsRGB = B8bit/255

Don't get bamboozled by the equations, it's still simple math! The last three lines are just about normalizing the color channels by dividing them with 255, here are some examples:

Color R8bit G8bit B8bit -> RsRGB GsRGB BsRGB
Black 0 0 0 0 0 0
White 255 255 255 1 1 1
Red 255 0 0 1 0 0
Purple 128 0 128 0.502 0 0.502
Orange 255 165 0 1 0.647 0

Then getting the R, G, and B values is done by a simple division (divide by 12.92) if the normalized value is less than or equal to 0.03928, or applying the exponential formula above if the normalized value is greater than 0.03928. The final step is summing these three values with different weights.

# Let's calculate!

It's not hard to see that the relative luminance of black is 0, and white is 1. Calculating their contrast ratio gives (1 + 0.05) / (0 + 0.05) = 21. We can calculate any other color's contrast ratio against black and white by the following (where "c" is the color's relative luminance):

• against black: (c + 0.05) / (0 + 0.05)
• against white: (1 + 0.05) / (c + 0.05)

We have a requirement of a contrast ratio of at least 4.5, therefore:

• against black: (c + 0.05) / (0 + 0.05) >= 4.5
• against white: (1 + 0.05) / (c + 0.05) >= 4.5

Solving the two inequalities give:

• against black: c >= 0.175
• against white: 0.1833 >= c

In the end we get the relative luminance requirement of:

0.175 <= c <= 0.1833

# The list of colors

Finally, I created a pen which iterates through the RGB color space, from black to white with increments of 0x11 = 17 per channel. Iterating through all of the colors would take some time and CPU, and I'm sure listing ~300k colors wouldn't be practical. So the pen iterates through the colors which can be described with 3-digit hexa notation, and lists 76 colors that matches our requirements. Use them wisely ;)