🔍ContrastScope

About ContrastScope

WCAG 2.1 Contrast Formula

The contrast ratio between two colors is calculated using the relative luminance formula from the W3C WCAG 2.1 specification:

  1. For each sRGB channel (R, G, B), normalize to 0–1:c = channelValue / 255
  2. Linearize each channel:
    if c ≤ 0.03928 → c / 12.92
    else → ((c + 0.055) / 1.055)^2.4
  3. Compute relative luminance:
    L = 0.2126·R + 0.7152·G + 0.0722·B
  4. Contrast ratio:
    ratio = (L_lighter + 0.05) / (L_darker + 0.05)

The ratio ranges from 1.0 (identical colors) to 21.0 (black vs white).

Compliance Thresholds

CategoryAA (min ratio)AAA (min ratio)
Normal text (< 24px / < 18.66px bold)4.5:17:1
Large text (≥ 24px / ≥ 18.66px bold)3:14.5:1
UI components (§1.4.11 Non-text Contrast)3:1N/A

Find-Nearest-Pass Algorithm

The “Fix” button on audit pairs uses an HSL-based binary search to find the minimum lightness adjustment that achieves AA compliance (4.5:1):

  1. Determine direction: lighten or darken the foreground based on which moves luminance away from the background.
  2. Binary search on the HSL lightness channel (0–100) for the smallest change that reaches the target ratio.
  3. Return the adjusted hex — perceptually the closest passing color.

If the target ratio is unreachable (e.g. extremely close colors with no valid lightness solution), no adjustment is made.