How the contrast ratio is calculated
WCAG compares the relative luminance of the two colours: how bright each one is, on a scale from 0 for black to 1 for white. Each sRGB channel is first converted from its gamma-encoded value to linear light, then the three channels are weighted by how sensitive the eye is to each. Green counts for most of the result and blue for very little, which is why a saturated blue can carry white text that a yellow of the same lightness cannot.
The ratio runs from 1:1, for two identical colours, to 21:1, for black on white. The result is compared with each threshold without rounding: the Understanding documents give 4.499:1 as an example that does not meet 4.5:1. This checker therefore truncates the displayed value to two decimals, so a ratio just below a threshold never displays as meeting it.
Linearise each channel
c = value / 255 if c <= 0.04045: c / 12.92 else: ((c + 0.055) / 1.055) ^ 2.4The 0.04045 threshold is the one in the current WCAG 2.2 text. Versions before May 2021 printed 0.03928, taken from an older sRGB document. The W3C notes the change has no practical effect on the results.
Relative luminance
L = 0.2126 × R + 0.7152 × G + 0.0722 × BR, G and B are the linearised channels. White has a luminance of 1 and black of 0.
Contrast ratio
(L1 + 0.05) / (L2 + 0.05)L1 is the lighter colour and L2 the darker, so the order you enter the colours in does not change the ratio.
Translucent colours
seen = alpha × colour + (1 − alpha) × backgroundWhat users see is the colour after blending. A translucent text colour is painted over the background, and a translucent background over white, then rounded to whole 8-bit values as the browser and axe-core do.