The Mathematical Foundation of Relative Luminance
Human eyes perceive green wavelengths much brighter than blue wavelengths. Simple RGB distance averages fail to measure true visual contrast. The W3C specification defines relative luminance by first linearizing gamma values and applying spectral weighting:
function luminance(r, g, b) {
const [rs, gs, bs] = [r, g, b].map(c => {
c = c / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
}
Calculating Contrast Ratios
Contrast ratio is given by:
\[\text{CR} = \frac{L_1 + 0.05}{L_2 + 0.05}\]where \(L_1\) is the relative luminance of the lighter of the colors, and \(L_2\) is the relative luminance of the darker color. The ratio ranges from 1:1 (zero contrast) to 21:1 (pure black on pure white).