Human eyes don't perceive brightness linearly, so you can't just average RGB channels. WCAG gamma-corrects each sRGB channel to linear light, computes relative luminance as 0.2126R + 0.7152G + 0.0722B, adds 0.05 to both the lighter and darker luminance, and divides: (L1+0.05)/(L2+0.05).
There are three thresholds: 4.5:1 for AA on normal text, 3:1 for AA on large text (18pt+, or 14pt bold), and 7:1 for AAA on normal text. Pure black on pure white tops out at the theoretical maximum, 21:1.
Every "contrast checker" plugin in every design tool runs this exact formula — running the numbers instead of eyeballing a color pair is the single biggest reducer of accessibility incidents.
When to use
Check it before shipping any color pair that carries meaning — text, icons, form borders.