WCAG 2.2 contrast ratio calculator for deterministic accessibility assertions.
Implements the W3C relative luminance formula and contrast-ratio formula exactly as specified in WCAG 2.2 Success Criterion 1.4.3 (Contrast — Minimum).
Usage
iex> Sigra.A11y.Contrast.ratio("#1d4ed8", "#ffffff")
5.17... # >= 4.5 passes WCAG AA for normal textReferences
Summary
Functions
Returns the WCAG contrast ratio between two hex colors.
Returns the WCAG relative luminance for a hex color string.
Functions
Returns the WCAG contrast ratio between two hex colors.
The ratio is always >= 1.0. Direction (fg vs bg) does not matter — the
function returns the same value regardless of argument order.
Returns {:error, reason} when either color is malformed.
Thresholds (WCAG 2.2)
| Standard | Normal text | Large text / bold |
|---|---|---|
| AA | 4.5:1 | 3.0:1 |
| AAA | 7.0:1 | 4.5:1 |
Examples
iex> Sigra.A11y.Contrast.ratio("#000000", "#ffffff")
21.0
iex> Sigra.A11y.Contrast.ratio("#1d4ed8", "#ffffff")
5.17...
Returns the WCAG relative luminance for a hex color string.
Accepts #rrggbb and #RRGGBB forms (6-digit hex with leading #).
Returns a float in [0.0, 1.0] on success, or {:error, reason} when the
color string is malformed.
Examples
iex> Sigra.A11y.Contrast.relative_luminance("#000000")
0.0
iex> Sigra.A11y.Contrast.relative_luminance("#ffffff")
1.0