How the WCAG contrast formula works
The WCAG contrast formula computes relative luminance for two colors, then calculates the ratio between the lighter and darker values. Relative luminance uses the standard sRGB luminance coefficients: R×0.2126 + G×0.7152 + B×0.0722 (after gamma correction). This formula gives a single number that ranges from 0 (black) to 1 (white). The contrast ratio is then (L1 + 0.05) / (L2 + 0.05). Understanding this formula explains several practical points: First, neutral colors (greys with equal R, G, B values) have predictable luminance values that are easy to reason about. Second, saturated colors of the same lightness value in HSL do not have the same luminance — a pure red (H:0, S:100%, L:50%) is darker in perceived luminance than a pure yellow (H:60, S:100%, L:50%) because the luminance formula weights green very heavily. This is why bright yellow text fails contrast checks against white even though it looks similar in HSL lightness. Designers working with saturated colors should always verify contrast with an actual calculation tool rather than estimating from HSL values.
