SpunkArt
Color, art and images

How to check color contrast with the WCAG formula

Contrast ratio tells you how readable text is against its background. It comes from a short formula that you can run by hand: convert each color to relative luminance, then compare the two numbers.

Contrast ratio checker

Sample text in the chosen colors.

The two formulas

The Web Content Accessibility Guidelines (WCAG) define contrast with two steps. First you find the relative luminance of each color, a number from 0 for black to 1 for white. Then you put the lighter and darker values into the ratio formula.

Step 1: linearise each channel

Screen values are gamma-encoded, so a channel value of 128 is not half as bright as 255. Divide each red, green and blue value by 255 to get a number c between 0 and 1, then undo the encoding:

if c ≤ 0.04045: linear = c ÷ 12.92
otherwise: linear = ((c + 0.055) ÷ 1.055)2.4

Step 2: weight the channels

The eye is most sensitive to green and least sensitive to blue, so the channels get different weights:

L = 0.2126 × R + 0.7152 × G + 0.0722 × B

Step 3: compare

contrast ratio = (Llighter + 0.05) ÷ (Ldarker + 0.05)

The 0.05 stands in for light reflected from the screen. It also stops the formula from dividing by zero for pure black. The result runs from 1:1 (same color) to 21:1 (black on white).

Worked example: #1a5fb4 text on white. The HEX code gives red 26, green 95, blue 180. Values are rounded to four decimals.

  • Red: 26 ÷ 255 = 0.1020, linear 0.0103, weighted 0.2126 × 0.0103 = 0.0022
  • Green: 95 ÷ 255 = 0.3725, linear 0.1144, weighted 0.7152 × 0.1144 = 0.0818
  • Blue: 180 ÷ 255 = 0.7059, linear 0.4564, weighted 0.0722 × 0.4564 = 0.0330
  • Luminance L = 0.0022 + 0.0818 + 0.0330 = 0.1170
  • White has L = 1, so the ratio is (1 + 0.05) ÷ (0.1170 + 0.05) = 6.29:1

The pass levels

What you checkLevel AALevel AAA
Body text4.5:17:1
Large text (about 24px, or about 18.66px bold)3:14.5:1
Icons, input borders, focus rings3:1no separate level

Example pairs, computed

All ratios below are against the second color and rounded to two decimals.

TextBackgroundRatioBody text AA?
#000000#ffffff21.00:1Yes
#595959#ffffff7.00:1Yes, also AAA
#767676#ffffff4.54:1Yes, just
#777777#ffffff4.48:1No, just short
#e66100#ffffff3.46:1No; large text only
#ffcc00#00000013.89:1Yes
#ffcc00#ffffff1.51:1No

The #767676 and #777777 rows show why you should not round up. A grey one step lighter falls below 4.5:1. The yellow rows show that a bright color can be excellent on black and nearly invisible on white.

Common mistakes

  • Averaging the RGB values. A plain average ignores gamma and the green weighting. Orange #e66100 and blue #1a5fb4 have similar averages but very different luminance.
  • Checking only the brand color. Check every real pair: link text, placeholder text, disabled buttons, text over photos.
  • Forgetting hover and focus states. A button can pass at rest and fail when its background lightens on hover.
  • Trusting transparency. Text at 60% opacity has a blended color. Work out the blended color first, then check it.

Questions people ask

Why does the checker not round 4.48 up to 4.5?

The guideline sets a minimum. A ratio of 4.48:1 is below 4.5:1, so it fails. The checker on this page decides pass or fail from the exact value and shows it rounded to two decimals. If rounding would show exactly 4.50 for a value just below it, the checker shows a third decimal instead.

Does a high ratio guarantee readable text?

No. Thin fonts, very small sizes and busy backgrounds can still be hard to read. The ratio is a minimum check, not a full readability test.

Is the formula the same for dark mode?

Yes. The formula does not care which color is the text. Light text on a dark background uses the same two luminance values.