feat(tools): theme design audit + item 28 punch list (slice a)
All checks were successful
Check / eval (push) Successful in 3m0s

tools/audit-theme-design.py (report-only, maintainer-run): for every
themes/*.json it checks OKLCH lightness architecture (raised bg stack,
fg hierarchy), WCAG contrast beyond the guarded pairs, status-color
hue families, accent harmony, protanopia/deuteranopia
distinguishability (Machado matrices), and ANSI slot semantics the
greeter/tty now rely on. Ran it over all 21 palettes + a visual pass
over representative previews; findings curated identity-aware (lumon/
vantablack/white/hackerman exemptions are identity, not defects) into
the P1-P3 punch list now recorded in BACKLOG item 28: two palettes
with literally invisible subtext (summer-day, flexoki-light — the
item-25/27 trap at its source), two inverted bg stacks (miasma,
kanagawa), muted/warn floors, a systemic "status is never color-only"
rule, the light-theme ANSI convention decision, and bar grouping
rhythm notes.

Verified: V0 (py_compile; the tool is report-only, deliberately not a
checks.* gate until slice b adopts rules + exemptions).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Bernardo Magri
2026-07-05 18:45:56 +01:00
parent d1d6a09d9d
commit abc953ea84
3 changed files with 223 additions and 4 deletions

148
tools/audit-theme-design.py Executable file
View File

@@ -0,0 +1,148 @@
#!/usr/bin/env python3
"""Item 28 slice (a): design-theory audit of every themes/*.json.
Reports (never fails): OKLCH lightness architecture, extended WCAG
contrast pairs, hue-family sanity for status colors, accent harmony,
CVD (protanopia/deuteranopia) distinguishability, ANSI slot semantics.
"""
import json, math, sys
from pathlib import Path
# ── color math ───────────────────────────────────────────────────────
def srgb_to_linear(c):
c /= 255.0
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
def hex_rgb(h):
h = h.lstrip("#")
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
def hex_lin(h):
return tuple(srgb_to_linear(c) for c in hex_rgb(h))
def oklab(h):
r, g, b = hex_lin(h)
l = 0.4122214708*r + 0.5363325363*g + 0.0514459929*b
m = 0.2119034982*r + 0.6806995451*g + 0.1073969566*b
s = 0.0883024619*r + 0.2817188376*g + 0.6299787005*b
l, m, s = l ** (1/3), m ** (1/3), s ** (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 oklch(h):
L, a, b = oklab(h)
C = math.hypot(a, b)
H = math.degrees(math.atan2(b, a)) % 360
return L, C, H
def de(h1, h2): # OKLab euclidean distance
a, b = oklab(h1), oklab(h2)
return math.dist(a, b)
def wcag_lum(h):
r, g, b = hex_lin(h)
return 0.2126*r + 0.7152*g + 0.0722*b
def ratio(f, b):
hi, lo = sorted((wcag_lum(f), wcag_lum(b)), reverse=True)
return (hi + 0.05) / (lo + 0.05)
# Machado et al. 2009, severity 1.0, applied in linear RGB.
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)]
def cvd_hex(h, M):
r, g, b = hex_lin(h)
sim = [max(0.0, min(1.0, M[i][0]*r + M[i][1]*g + M[i][2]*b)) for i in range(3)]
def enc(c):
c = 12.92*c if c <= 0.0031308 else 1.055 * c ** (1/2.4) - 0.055
return max(0, min(255, round(c*255)))
return "#%02x%02x%02x" % tuple(enc(c) for c in sim)
def hue_in(H, lo, hi):
return lo <= H <= hi if lo <= hi else (H >= lo or H <= hi)
ROLES = ["base","mantle","surface","overlay","text","subtext","muted","accent","accentAlt","good","warn","bad"]
findings = {}
def note(slug, cat, msg):
findings.setdefault(slug, []).append((cat, msg))
themes = sorted(Path(sys.argv[1] if len(sys.argv) > 1 else "themes").glob("*.json"))
for tf in themes:
t = json.loads(tf.read_text()); slug = tf.stem
c = t["colors"]; mode = t.get("mode", "dark"); dark = mode == "dark"
L = {r: oklch(c[r])[0] for r in ROLES}
C = {r: oklch(c[r])[1] for r in ROLES}
H = {r: oklch(c[r])[2] for r in ROLES}
# A. lightness architecture: bg stack monotonic away from base
stack = ["base", "surface", "overlay"]
diffs = [L[b] - L[a] for a, b in zip(stack, stack[1:])]
want = 1 if dark else -1
for (a, b), d in zip(zip(stack, stack[1:]), diffs):
if d * want < 0.005:
note(slug, "lightness", f"bg stack not raised: L({b})={L[b]:.3f} vs L({a})={L[a]:.3f} ({mode})")
# fg stack: muted < subtext < text in |L - L(base)|
dist = {r: abs(L[r] - L["base"]) for r in ("muted","subtext","text")}
if not (dist["muted"] <= dist["subtext"] + 0.005 and dist["subtext"] <= dist["text"] + 0.005):
note(slug, "lightness", f"fg hierarchy off: |dL| muted={dist['muted']:.2f} subtext={dist['subtext']:.2f} text={dist['text']:.2f}")
# B. contrast pairs (audit thresholds)
for f, b, mn, why in [
("text","base",4.5,"body text"), ("text","surface",4.5,"text on chips/menus"),
("subtext","base",3.0,"secondary text"), ("accent","base",3.0,"indicators"),
("accentAlt","base",3.0,"alt accent"), ("base","accent",3.0,"selected rows"),
("good","base",2.5,"status glyph"), ("warn","base",2.5,"status glyph"),
("bad","base",2.5,"status glyph"), ("muted","base",2.0,"dimmed text"),
]:
r = ratio(c[f], c[b])
if r < mn:
note(slug, "contrast", f"{f} on {b} = {r:.2f} (< {mn}, {why})")
# C. hue families for status roles + harmony
for role, lo, hi, fam in [("good",120,180,"green"),("warn",50,120,"yellow/orange"),("bad",0,45,"red")]:
h = H[role]
ok = hue_in(h, lo, hi) or (role == "bad" and hue_in(h, 330, 45))
if C[role] < 0.03:
note(slug, "hue", f"{role} is near-grey (C={C[role]:.3f}) — status color carries no hue")
elif not ok:
note(slug, "hue", f"{role} hue {h:.0f}° outside {fam} family")
dh = abs(H["accent"] - H["accentAlt"]); dh = min(dh, 360 - dh)
harm = "analogous" if dh < 60 else "triadic-ish" if dh < 150 else "complementary"
if 90 <= dh <= 150 and de(c["accent"], c["accentAlt"]) > 0.12:
note(slug, "harmony", f"accent/accentAlt Δhue={dh:.0f}° (square-clash zone; {harm})")
# D. CVD distinguishability of status pairs (small glyphs)
for M, name in [(PROTAN, "protan"), (DEUTAN, "deutan")]:
for r1, r2 in [("good","bad"),("good","warn"),("accent","bad")]:
d = de(cvd_hex(c[r1], M), cvd_hex(c[r2], M))
if d < 0.09:
note(slug, "cvd", f"{r1}/{r2} nearly identical under {name} (dE={d:.03f})")
# E. ANSI slot semantics (greeter/tty rely on them)
ansi = t.get("ansi", [])
if len(ansi) == 16:
for idx, lo, hi, fam in [(1,330,45,"red"),(2,100,180,"green"),(3,50,110,"yellow"),
(4,220,290,"blue"),(5,290,350,"magenta"),(6,160,230,"cyan")]:
for slot in (idx, idx+8):
Ls, Cs, Hs = oklch(ansi[slot])
if Cs >= 0.04 and not hue_in(Hs, lo, hi):
note(slug, "ansi", f"ansi[{slot}] hue {Hs:.0f}° not {fam}")
if oklch(ansi[0])[0] > oklch(ansi[15])[0]:
note(slug, "ansi", "ansi[0] lighter than ansi[15] (inverted slots)")
rt = ratio(ansi[7], ansi[0])
if rt < 3.0:
note(slug, "ansi", f"ansi[7] on ansi[0] = {rt:.2f} (tuigreet text=gray on container=black)")
print(f"audited {len(themes)} themes\n")
cats = {}
for slug in sorted(findings):
print(f"── {slug}")
for cat, msg in findings[slug]:
cats[cat] = cats.get(cat, 0) + 1
print(f" [{cat}] {msg}")
clean = [t.stem for t in themes if t.stem not in findings]
print(f"\nclean: {', '.join(clean) if clean else '(none)'}")
print("totals:", dict(sorted(cats.items())))