""" Verifies the contrast ratios asserted in docs/design/DASHBOARD-STANDARD.md §8 against the actual token values in app/globals.css (:root = light, .dark = dark). Run from the portal root (solvway-admin-portal/): python3 scripts/check-contrast.py Exits non-zero on any failure, so it can gate CI. The tokens used to live in src/styles/_variables.scss; that file went with the Tailwind 3 port and the script had been failing to start since. It now reads the real token file. """ import re, pathlib, sys css = pathlib.Path('app/globals.css').read_text() def palette(selector): """Reads the hex custom properties declared in one top-level `selector { ... }` block.""" m = re.search(r'(?m)^' + re.escape(selector) + r'\s*\{(.*?)^\}', css, re.S) if not m: raise SystemExit(f'Could not find the `{selector}` block in app/globals.css') return dict(re.findall(r'--([\w-]+):\s*(#[0-9a-fA-F]{6})\b', m.group(1))) light, dark = palette(':root'), palette('.dark') def lum(hexstr): c = [int(hexstr[i:i+2], 16)/255 for i in (1, 3, 5)] c = [x/12.92 if x <= 0.03928 else ((x+0.055)/1.055)**2.4 for x in c] return 0.2126*c[0] + 0.7152*c[1] + 0.0722*c[2] def ratio(a, b): la, lb = lum(a), lum(b) hi, lo = max(la, lb), min(la, lb) return (hi + 0.05) / (lo + 0.05) # (foreground, background, minimum, label) CHECKS = [ ('foreground', 'background', 4.5, 'body text on page'), ('foreground', 'card', 4.5, 'body text on card'), ('muted-foreground', 'card', 4.5, 'secondary text on card'), ('muted-foreground', 'background', 4.5, 'secondary text on page'), ('primary-foreground', 'primary', 4.5, 'text on primary button'), ('primary', 'card', 3.0, 'primary fill against card'), # `accent` is deliberately NOT checked against the card surface. It is a # decorative fill that always carries its own foreground, and WCAG 1.4.11's # 3:1 applies to boundaries and graphics that convey information — not to a # filled surface whose text is separately verified below. Anything that has # to carry meaning in the accent family uses `accent-strong`, which is # checked strictly. ('accent-foreground', 'accent', 4.5, 'text on accent fill'), ('accent-strong', 'card', 4.5, 'accent as text/stroke on card'), ('accent-strong', 'background', 4.5, 'accent as text/stroke on page'), ('secondary-foreground', 'secondary', 4.5, 'text on secondary fill'), ('positive', 'card', 4.5, 'positive delta on card'), ('negative', 'card', 4.5, 'negative delta on card'), ('warning', 'card', 4.5, 'warning text on card'), ('input', 'card', 3.0, 'form-control border on card'), ('input', 'background', 3.0, 'form-control border on page'), ('ring', 'card', 3.0, 'focus ring on card'), ('chart-1', 'card', 3.0, 'chart series 1'), ('chart-2', 'card', 3.0, 'chart series 2'), ('chart-3', 'card', 3.0, 'chart series 3'), ('chart-4', 'card', 3.0, 'chart series 4'), ('chart-5', 'card', 3.0, 'chart series 5'), # The contrast colour (dusty blue, approved 2026-10-22): fill/stroke on cards, and text on its # own soft fill (pills). ('contrast', 'card', 3.0, 'contrast as icon/stroke on card'), ('contrast-foreground', 'contrast-soft', 4.5, 'text on contrast pill'), ('contrast-foreground', 'card', 4.5, 'contrast as text on card'), # Type pills that reuse chart series as text on a 10% tint of themselves. ('chart-2', 'card', 4.5, 'teal pill text on card'), ('chart-4', 'card', 4.5, 'violet pill text on card'), ('table-head-foreground', 'table-head', 4.5, 'table header text on its gold row'), ] fails = 0 for mode, tokens in (('LIGHT', light), ('DARK', dark)): print(f'\n--- {mode} ---') for fg, bg, minimum, label in CHECKS: if fg not in tokens or bg not in tokens: print(f' ?? {label}: token missing'); continue r = ratio(tokens[fg], tokens[bg]) ok = r >= minimum if not ok: fails += 1 print(f' {"PASS" if ok else "FAIL"} {r:5.2f}:1 (min {minimum}) {label}') print(f'\n{"ALL PASS" if fails == 0 else str(fails) + " FAILURE(S)"}') sys.exit(1 if fails else 0)