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??
The WCAG relative luminance definition says:
relative luminance
the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest whiteNOTE
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 R_{sRGB} <= 0.03928 then R = R_{sRGB}/12.92 else R = ((R_{sRGB}+0.055)/1.055) ^ 2.4
- if G_{sRGB} <= 0.03928 then G = G_{sRGB}/12.92 else G = ((G_{sRGB}+0.055)/1.055) ^ 2.4
- if B_{sRGB} <= 0.03928 then B = B_{sRGB}/12.92 else B = ((B_{sRGB}+0.055)/1.055) ^ 2.4
and R_{sRGB}, G_{sRGB}, and B_{sRGB} are defined as:
- R_{sRGB} = R_{8bit}/255
- G_{sRGB} = G_{8bit}/255
- B_{sRGB} = B_{8bit}/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 | R_{8bit} | G_{8bit} | B_{8bit} | -> | R_{sRGB} | G_{sRGB} | B_{sRGB} |
---|---|---|---|---|---|---|---|
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 ;)
Discussion (8)
There's a tool here which will show you contrast ratios of up to five colours at a time, along with Black and White: reallybigshoe.co.uk/visualiser/ind...
Full disclosure: it's my tool.
Hey Julian, this is a pretty handy tool! Thanks!
This is actually super useful if you're creating a style guide for a website, or adjusting a theme for an already existing framework. Nice work!
yep, I wanted to find some colors that work with both day and night modes!
Awesome to see how you calculate this using the standards. ππ»
Wow, now I know that at least you read it through! Thanks! π
YW.
But it's you who should be thanked (thank you π).
We need more of these "gut feeling" things to be defined SMART. It really helps starters and pro's to make their interfaces more readable and accessible.
Thanks so much for this post! I love that you walked through the calculations. It helped me check my own math. Also glad you included your codepen because I found a syntax error in my math π