Farol.Contrast (Farol v0.2.0)

Copy Markdown View Source

WCAG 2.2 contrast math over hex colors.

The formula is the one from the spec: convert each sRGB channel to linear light, weigh them into relative luminance (0.2126 R + 0.7152 G + 0.0722 B), then ratio = (L1 + 0.05) / (L2 + 0.05), lighter over darker. The result lands between 1:1 (same color) and 21:1 (black on white).

Level AA asks for 4.5:1 for normal text and 3:1 for large text and UI components. Level AAA raises those to 7:1 and 4.5:1.

Summary

Functions

Checks a foreground/background pair against level AA.

Parses #rgb or #rrggbb into an {r, g, b} tuple of 0..255.

The contrast ratio between two hex colors, rounded to two decimals. Accepts #rgb and #rrggbb, case insensitive.

Functions

aa?(fg, bg, opts \\ [])

@spec aa?(String.t(), String.t(), keyword()) :: {:ok, float()} | {:error, term()}

Checks a foreground/background pair against level AA.

large: true relaxes the threshold to 3:1, for text at least 24px, or 18.66px bold (the WCAG definition of large scale).

parse_hex(arg1)

Parses #rgb or #rrggbb into an {r, g, b} tuple of 0..255.

ratio(fg, bg)

@spec ratio(String.t(), String.t()) :: {:ok, float()} | {:error, :invalid_color}

The contrast ratio between two hex colors, rounded to two decimals. Accepts #rgb and #rrggbb, case insensitive.