129 lines
5.0 KiB
Python
129 lines
5.0 KiB
Python
"""Verify the SHIPPED dark palette: parse tokens from src/index.html, re-measure WCAG AA,
|
|
and cross-check the values written in design/spec.md §10.9.1.
|
|
|
|
Запуск: python design/dark-palette/verify-shipped.py
|
|
Exit 0 — все пары проходят и spec совпадает с продом; exit 1 — расхождение.
|
|
|
|
Источник значений — сам прод (src/index.html), а не таблица в документации:
|
|
если кто-то поправит токен в HTML и забудет spec, скрипт это покажет.
|
|
"""
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
HTML = ROOT / "src" / "index.html"
|
|
SPEC = ROOT / "design" / "spec.md"
|
|
|
|
TEXT_MIN, UI_MIN = 4.5, 3.0
|
|
|
|
|
|
# ------------------------------------------------------------------ helpers
|
|
def rgb(h):
|
|
h = h.lstrip('#')
|
|
return tuple(int(h[i:i + 2], 16) for i in (0, 2, 4))
|
|
|
|
|
|
def _lin(c):
|
|
c = c / 255.0
|
|
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
|
|
|
|
|
|
def lum(h):
|
|
r, g, b = rgb(h)
|
|
return 0.2126 * _lin(r) + 0.7152 * _lin(g) + 0.0722 * _lin(b)
|
|
|
|
|
|
def ratio(fg, bg):
|
|
a, b = lum(fg), lum(bg)
|
|
hi, lo = max(a, b), min(a, b)
|
|
return (hi + 0.05) / (lo + 0.05)
|
|
|
|
|
|
def block(css, selector):
|
|
m = re.search(re.escape(selector) + r"\s*\{(.*?)\}", css, re.S)
|
|
if not m:
|
|
raise SystemExit(f"selector not found: {selector}")
|
|
out = {}
|
|
for name, value in re.findall(r"(--[a-z0-9-]+)\s*:\s*([^;]+);", m.group(1)):
|
|
out[name] = value.strip()
|
|
return out
|
|
|
|
|
|
# ------------------------------------------------------------------ parse
|
|
css = HTML.read_text(encoding="utf-8")
|
|
light = block(css, ":root")
|
|
dark = block(css, '[data-theme="dark"]')
|
|
|
|
PAIRS = [
|
|
("text", "--text-1", "--bg"), ("text", "--text-1", "--card"), ("text", "--text-1", "--input-bg"),
|
|
("text", "--text-2", "--bg"), ("text", "--text-2", "--card"), ("text", "--text-2", "--input-bg"),
|
|
("text", "--text-3", "--bg"), ("text", "--text-3", "--card"), ("text", "--text-3", "--input-bg"),
|
|
("text", "--brand-deep", "--bg"), ("text", "--brand-deep", "--card"), ("text", "--brand-deep", "--input-bg"),
|
|
("ui", "--line-strong", "--bg"), ("ui", "--line-strong", "--card"), ("ui", "--line-strong", "--input-bg"),
|
|
("text", "--danger", "--bg"), ("text", "--danger", "--card"),
|
|
("text", "--on-brand", "--brand"),
|
|
("text", "--on-deep", "--brand-deep"),
|
|
]
|
|
|
|
print(f"src/index.html :root({len(light)} tokens) [data-theme=dark]({len(dark)} tokens)")
|
|
fails = []
|
|
print(f"{'pair':52s} {'value':>8s} {'min':>4s} verdict")
|
|
for kind, fg, bg in PAIRS:
|
|
fg_v, bg_v = dark.get(fg), dark.get(bg)
|
|
if fg_v is None or bg_v is None:
|
|
fails.append(f"missing token: {fg if fg_v is None else bg}")
|
|
print(f"{fg} / {bg:12s} MISSING")
|
|
continue
|
|
r = ratio(fg_v, bg_v)
|
|
mn = TEXT_MIN if kind == "text" else UI_MIN
|
|
ok = r >= mn
|
|
if not ok:
|
|
fails.append(f"{fg} {fg_v} on {bg} {bg_v} = {r:.2f} < {mn}")
|
|
print(f"{(fg + ' ' + fg_v + ' on ' + bg + ' ' + bg_v):52s} {r:8.2f} {mn:4.1f} {'PASS' if ok else 'FAIL'}")
|
|
|
|
# derivable surfaces / decorations worth reporting
|
|
extra = [
|
|
("--bg / --card ratio (surface step)", ratio(dark["--bg"], dark["--card"]), 1.08),
|
|
("--card / --input-bg ratio", ratio(dark["--card"], dark["--input-bg"]), 1.08),
|
|
("--hairline / --bg (decoration)", ratio(dark["--hairline"], dark["--bg"]), 1.0),
|
|
("--brand-strong / --on-brand", ratio(dark["--on-brand"], dark["--brand-strong"]), TEXT_MIN),
|
|
("--brand-active / --on-brand", ratio(dark["--on-brand"], dark["--brand-active"]), TEXT_MIN),
|
|
]
|
|
print()
|
|
for label, val, mn in extra:
|
|
ok = val >= mn
|
|
if not ok:
|
|
fails.append(f"{label} = {val:.2f} < {mn}")
|
|
print(f"{label:52s} {val:8.3f} {mn:4.2f} {'PASS' if ok else 'FAIL'}")
|
|
|
|
# ------------------------------------------------- spec ↔ production cross-check
|
|
spec = SPEC.read_text(encoding="utf-8")
|
|
sec = re.search(r"#### 10\.9\.1 .*?(?=\n#### )", spec, re.S)
|
|
if not sec:
|
|
raise SystemExit("spec.md §10.9.1 section not found")
|
|
spec_rows = dict((m[0], m[1].upper()) for m in re.findall(
|
|
r"^\|\s*`(--[a-z0-9-]+)`\s*\|[^|]*\|\s*\*\*`(#[0-9A-Fa-f]{6}|rgba\([^)]*\))`\*\*", sec.group(0), re.M))
|
|
print(f"\nspec.md §10.9.1 tokens parsed: {len(spec_rows)}")
|
|
checked = 0
|
|
for tok, spec_val in spec_rows.items():
|
|
prod_val = dark.get(tok)
|
|
if prod_val is None:
|
|
fails.append(f"spec lists {tok} but prod dark block has none")
|
|
print(f" {tok:26s} spec {spec_val:24s} prod MISSING")
|
|
continue
|
|
checked += 1
|
|
same = prod_val.replace(' ', '').upper() == spec_val.replace(' ', '').upper()
|
|
if not same:
|
|
fails.append(f"spec/prod mismatch {tok}: spec {spec_val} prod {prod_val}")
|
|
print(f" {tok:26s} spec {spec_val:24s} prod {prod_val:24s} {'match' if same else 'MISMATCH'}")
|
|
|
|
print(f"\nchecked {checked} spec rows against prod")
|
|
if fails:
|
|
print("\nFAILURES:")
|
|
for f in fails:
|
|
print(" -", f)
|
|
sys.exit(1)
|
|
print("\nOK: all shipped dark pairs pass AA and spec.md §10.9.1 matches src/index.html")
|