1. How WCAG 2.2 Calculates Contrast Under the Hood
The Web Content Accessibility Guidelines (WCAG) determine color contrast by computing the difference in Relative Luminance between two colors. Relative luminance measures the relative brightness of any point in a colorspace, normalized to 0 for the darkest black and 1 for the lightest white.
Because the human visual cortex does not perceive red, green, and blue light with equal intensity (the eye is dramatically more sensitive to green wavelengths than blue), WCAG 2.2 weights RGB channels using the standardized sRGB coefficients:
Step 1: Convert sRGB 8-bit to Linear Luminance:
If (Channel / 255) ≤ 0.04045: C_linear = (Channel / 255) / 12.92
Else: C_linear = (((Channel / 255) + 0.055) / 1.055) ^ 2.4
Step 2: Calculate Relative Luminance (L):
L = 0.2126 × R_linear + 0.7152 × G_linear + 0.0722 × B_linear
Step 3: Calculate Contrast Ratio:
Ratio = (L1 + 0.05) / (L2 + 0.05) [where L1 ≥ L2]
The constant offset + 0.05 accounts for ambient flare in standard viewing conditions. The resulting ratio ranges from 1.00:1 (identical colors with zero contrast) up to 21.00:1 (pure black on pure white).