Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

How the WCAG contrast ratio is calculated (with a 20-line JavaScript implementation)

Every contrast checker answers the same question: does this text color pass on this background? The answer is a number like 4.6:1, and behind it there are about ten lines of math from WCAG 2.x. Once you know them, you ca

Every contrast checker answers the same question: does this text color pass on this background? The answer is a number like 4.6:1, and behind it there are about ten lines of math from WCAG 2.x. Once you know them, you can check colors right in your code, write a lint rule, or generate an accessible palette instead of copying hex codes into an online tool.

Step 1: relative luminance

WCAG compares colors by their relative luminance: how bright a color looks to the eye, from 0 (black) to 1 (white).

  1. Take each sRGB channel (0–255) and divide it by 255.
  2. Undo the sRGB gamma curve: values up to 0.04045 are divided by 12.92, larger ones become ((c + 0.055) / 1.055) ^ 2.4.
  3. Mix the channels with weights that follow human vision: L = 0.2126 R + 0.7152 G + 0.0722 B.

Green gets most of the weight because our eyes are most sensitive to it. That is why pure green #00ff00 is far brighter than pure blue #0000ff, even though both have one channel at 255.

The WCAG text itself uses 0.03928 as the threshold in step 2, a value from an older sRGB draft. For 8-bit colors it makes no difference: no channel value from 0 to 255 falls between the two thresholds.

Step 2: the ratio

contrast = (L1 + 0.05) / (L2 + 0.05)

L1 is the lighter color, L2 the darker one. The result runs from 1:1 (same color) to 21:1 (black on white). The 0.05 stands for the ambient light reflected by a real screen, so very dark colors do not produce an infinite ratio.

Step 3: the thresholds

What AA AAA
Normal text 4.5:1 7:1
Large text (at least 24px, or 18.66px bold) 3:1 4.5:1
UI components and meaningful graphics 3:1 β€”

The text rules are success criteria 1.4.3 and 1.4.6; the one for icons, input borders and focus indicators is 1.4.11.

The code

function hexToRgb(hex) {
  const h = hex.replace('#', '')
  const full = h.length === 3 ? [...h].map((c) => c + c).join('') : h
  const n = parseInt(full, 16)
  return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
}

function luminance([r, g, b]) {
  const linear = (v) => {
    const c = v / 255
    return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
  }
  return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b)
}

function contrastRatio(foreground, background) {
  const a = luminance(hexToRgb(foreground))
  const b = luminance(hexToRgb(background))
  const [light, dark] = a > b ? [a, b] : [b, a]
  return (light + 0.05) / (dark + 0.05)
}

function wcag(ratio) {
  return {
    aaText: ratio >= 4.5,
    aaLargeText: ratio >= 3,
    aaaText: ratio >= 7,
    aaaLargeText: ratio >= 4.5,
    uiComponents: ratio >= 3,
  }
}

const ratio = contrastRatio('#767676', '#ffffff')
console.log(ratio.toFixed(2), wcag(ratio)) // 4.54 β€” the lightest gray that passes AA on white

Four mistakes that make a checker lie

Rounding before comparing. A ratio of 4.497 printed as "4.5" still fails AA. Compare the raw number and round only what you show.

Ignoring transparency. rgba(0, 0, 0, 0.5) on white is not black. Blend the color with its background first (channel = fg * alpha + bg * (1 - alpha)), then measure.

Text over images and gradients. One ratio is not enough: check the lightest part of the background that the text actually sits on.

Treating WCAG 3 as current. WCAG 3 and its perceptual contrast model, APCA, are still drafts. Accessibility rules and audits today refer to WCAG 2.x, so 4.5:1 is still the number to hit.

Try it without code

If you only need to check a pair of colors, the free contrast checker at Nexfield Academy uses the same luminance formula. It shows the ratio, the AA and AAA verdicts for normal and large text, and a rating while you pick colors. The interface is in Russian, but the numbers read the same in any language.

I build the learning platform at Nexfield Academy, an online school of design. Accessibility questions come up in every UI course we teach, so we keep small tools like this one free.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.