"""Python port of the data-viz palette validator (no node on this machine). Implements the computable checks: OKLCH lightness band, chroma floor, OKLab Delta E under Machado-Oliveira-Fernandes severity-1.0 CVD simulation, the normal-vision floor, and WCAG contrast against the chart surface. python -m src.palette_check "#2a78d6,#eb6834" --mode light --pairs all """ from __future__ import annotations import argparse import itertools import math BAND = {"light": (0.43, 0.77), "dark": (0.48, 0.67)} CHROMA_FLOOR = 0.10 CVD_TARGET, CVD_FLOOR = 8.0, 6.0 NORMAL_FLOOR = 15.0 CONTRAST_MIN = 3.0 DEFAULT_SURFACE = {"light": "#fcfcfb", "dark": "#1a1a19"} MACHADO = { "protan": [[0.152286, 1.052583, -0.204868], [0.114503, 0.786281, 0.099216], [-0.003882, -0.048116, 1.051998]], "deutan": [[0.367322, 0.860646, -0.227968], [0.280085, 0.672501, 0.047413], [-0.011820, 0.042940, 0.968881]], "tritan": [[1.255528, -0.076749, -0.178779], [-0.078411, 0.930809, 0.147602], [0.004733, 0.691367, 0.303900]], } def _hex2srgb(h): h = h.strip().lstrip("#") return [int(h[i:i + 2], 16) / 255 for i in (0, 2, 4)] def _s2lin(c): return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4 def lin(h): return [_s2lin(c) for c in _hex2srgb(h)] def rel_lum(h): r, g, b = lin(h) return 0.2126 * r + 0.7152 * g + 0.0722 * b def contrast(a, b): hi, lo = sorted([rel_lum(a), rel_lum(b)], reverse=True) return (hi + 0.05) / (lo + 0.05) def oklab_from_lin(rgb): r, g, b = rgb l = (0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b) ** (1 / 3) m = (0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b) ** (1 / 3) s = (0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b) ** (1 / 3) return (0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s, 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s) def oklab(h): return oklab_from_lin(lin(h)) def oklch(h): L, a, b = oklab(h) return L, math.hypot(a, b) def simulate(h, kind): r, g, b = lin(h) M = MACHADO[kind] return [min(1.0, max(0.0, M[i][0] * r + M[i][1] * g + M[i][2] * b)) for i in range(3)] def dE(c1, c2, kind=None): a = oklab_from_lin(simulate(c1, kind)) if kind else oklab(c1) b = oklab_from_lin(simulate(c2, kind)) if kind else oklab(c2) return 100 * math.dist(a, b) def validate(colors, mode="light", surface=None, pairs="adjacent", verbose=True): surface = surface or DEFAULT_SURFACE[mode] lo, hi = BAND[mode] fails, warns = [], [] if verbose: print(f"{'slot':<5}{'hex':<10}{'L':>7}{'C':>7}{'contrast':>10} check") for i, c in enumerate(colors, 1): L, C = oklch(c) ct = contrast(c, surface) flag = [] if not (lo <= L <= hi): flag.append(f"FAIL L band [{lo},{hi}]") fails.append(f"slot {i} L={L:.3f}") if C < CHROMA_FLOOR: flag.append("FAIL chroma") fails.append(f"slot {i} C={C:.3f}") if ct < CONTRAST_MIN: flag.append(f"WARN contrast {ct:.2f} (relief rule: label directly)") warns.append(f"slot {i} contrast={ct:.2f}") if verbose: print(f"{i:<5}{c:<10}{L:>7.3f}{C:>7.3f}{ct:>10.2f} " f"{'; '.join(flag) or 'ok'}") plist = (list(itertools.combinations(range(len(colors)), 2)) if pairs == "all" else [(i, i + 1) for i in range(len(colors) - 1)]) worst_cvd, worst_norm = 1e9, 1e9 worst_cvd_pair = worst_norm_pair = None for i, j in plist: cvd = min(dE(colors[i], colors[j], "protan"), dE(colors[i], colors[j], "deutan")) nrm = dE(colors[i], colors[j]) if cvd < worst_cvd: worst_cvd, worst_cvd_pair = cvd, (i + 1, j + 1) if nrm < worst_norm: worst_norm, worst_norm_pair = nrm, (i + 1, j + 1) if verbose: print(f"\npairlist: {pairs} ({len(plist)} pairs)") print(f"worst CVD dE = {worst_cvd:5.1f} (slots {worst_cvd_pair}) " f"target>={CVD_TARGET} floor>={CVD_FLOOR}") print(f"worst normal dE= {worst_norm:5.1f} (slots {worst_norm_pair}) " f"hard floor>={NORMAL_FLOOR}") if worst_cvd < CVD_FLOOR: fails.append(f"CVD dE {worst_cvd:.1f} < {CVD_FLOOR}") elif worst_cvd < CVD_TARGET: warns.append(f"CVD dE {worst_cvd:.1f} in warn band " f"(needs secondary encoding)") if worst_norm < NORMAL_FLOOR: fails.append(f"normal-vision dE {worst_norm:.1f} < {NORMAL_FLOOR}") if verbose: print(f"\nRESULT: {'FAIL' if fails else 'PASS'}" + (f" fails={fails}" if fails else "") + (f" warns={warns}" if warns else "")) return not fails, fails, warns if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument("colors") ap.add_argument("--mode", default="light") ap.add_argument("--surface", default=None) ap.add_argument("--pairs", default="adjacent") a = ap.parse_args() ok, _, _ = validate([c.strip() for c in a.colors.split(",") if c.strip()], a.mode, a.surface, a.pairs) raise SystemExit(0 if ok else 1)