Files
Harvey_Team_Page/design/dark-palette/measure.py
T
2026-10-13 11:01:30 +03:00

193 lines
8.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""WCAG 2.1 contrast audit for the dark-theme palette candidates.
Harvey Monster Team landing · design-контракт §10.9.6 · канбан t_ca027aab (2026-10-12).
Self-contained (stdlib only): `python measure.py` печатает отчёт и перезаписывает
`contrast-2026-10-12.md` рядом с собой.
Метод: относительная яркость WCAG 2.1 (sRGB, порог 0.04045 → 12.92 / 1.055-гамма),
отношение (L_hi + 0.05) / (L_lo + 0.05). Пороги: текст ≥4.5:1, UI-границы ≥3:0.
"""
from pathlib import Path
# ---------------------------------------------------------------- color 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 hsl(h):
r, g, b = (v / 255 for v in rgb(h))
mx, mn = max(r, g, b), min(r, g, b)
l = (mx + mn) / 2
if mx == mn:
return 0.0, 0.0, l
d = mx - mn
s = d / (2 - mx - mn) if l > 0.5 else d / (mx + mn)
if mx == r:
hh = ((g - b) / d) % 6
elif mx == g:
hh = (b - r) / d + 2
else:
hh = (r - g) / d + 4
return hh * 60, s, l
def comp(fg_hex, alpha, bg_hex):
"""Плоское наложение rgba(fg,alpha) на непрозрачный фон → hex."""
f, b = fg_hex.lstrip('#'), bg_hex.lstrip('#')
out = []
for i in (0, 2, 4):
c_f, c_b = int(f[i:i + 2], 16), int(b[i:i + 2], 16)
out.append(f"{round(c_f * alpha + c_b * (1 - alpha)):02X}")
return "#" + "".join(out)
# --------------------------------------------------------------------- palettes
BRAND_FILL = "#00CC66"
ON_BRAND = "#04180C"
TEXT_MIN, UI_MIN = 4.5, 3.0
PALETTES = {
"A. coal (shipped before 2026-10-12)": dict(
bg="#12100C", card="#1C1913", field="#242018", hairline="#2E2A20",
line_strong="#8B8375", text_1="#F3EEE2", text_2="#C6BFAE", text_3="#9A9280",
brand_deep="#3FCB79", danger="#FF6B6B"),
"B. chocolate (SHIPPED)": dict(
bg="#181109", card="#251A0F", field="#2F2113", hairline="#3C2C1C",
line_strong="#A28B6E", text_1="#F8F2E7", text_2="#D5C4AC", text_3="#AA9679",
brand_deep="#4FD98A", danger="#FF7A6B"),
"C. warm gray (rejected)": dict(
bg="#141312", card="#1F1E1C", field="#272624", hairline="#312F2C",
line_strong="#8E8A83", text_1="#F2F0EC", text_2="#C6C2BA", text_3="#9B978F",
brand_deep="#46D18A", danger="#FF7B7B"),
"D. mixed (rejected)": dict(
bg="#16120E", card="#221E19", field="#2B2620", hairline="#352F28",
line_strong="#9A8C7B", text_1="#F5F0E7", text_2="#CBC2B3", text_3="#A29A8B",
brand_deep="#4AD488", danger="#FF7669"),
}
CHARACTER = {
"A. coal (shipped before 2026-10-12)": "тёплый уголь, слабая хрома",
"B. chocolate (SHIPPED)": "какао / шоколад, заметная тёплая хрома",
"C. warm gray (rejected)": "нейтральный тёплый серый (хрома ~5 %)",
"D. mixed (rejected)": "какао-фон + нейтральные карточки",
}
PAIRS = [
("text-1 / bg", lambda p: ratio(p['text_1'], p['bg']), TEXT_MIN),
("text-1 / card", lambda p: ratio(p['text_1'], p['card']), TEXT_MIN),
("text-1 / input", lambda p: ratio(p['text_1'], p['field']), TEXT_MIN),
("text-2 / bg", lambda p: ratio(p['text_2'], p['bg']), TEXT_MIN),
("text-2 / card", lambda p: ratio(p['text_2'], p['card']), TEXT_MIN),
("text-2 / input", lambda p: ratio(p['text_2'], p['field']), TEXT_MIN),
("text-3 / bg", lambda p: ratio(p['text_3'], p['bg']), TEXT_MIN),
("text-3 / card", lambda p: ratio(p['text_3'], p['card']), TEXT_MIN),
("text-3 / input", lambda p: ratio(p['text_3'], p['field']), TEXT_MIN),
("brand-deep / bg", lambda p: ratio(p['brand_deep'], p['bg']), TEXT_MIN),
("brand-deep / card", lambda p: ratio(p['brand_deep'], p['card']), TEXT_MIN),
("brand-deep / input", lambda p: ratio(p['brand_deep'], p['field']), TEXT_MIN),
("line-strong / bg (UI)", lambda p: ratio(p['line_strong'], p['bg']), UI_MIN),
("line-strong / card (UI)", lambda p: ratio(p['line_strong'], p['card']), UI_MIN),
("line-strong / input (UI)", lambda p: ratio(p['line_strong'], p['field']), UI_MIN),
("danger / bg", lambda p: ratio(p['danger'], p['bg']), TEXT_MIN),
("danger / card", lambda p: ratio(p['danger'], p['card']), TEXT_MIN),
("on-brand / brand fill", lambda p: ratio(ON_BRAND, BRAND_FILL), TEXT_MIN),
("bg / card (шаг поверхностей)", lambda p: ratio(p['bg'], p['card']), 1.08),
("card / input (шаг поверхностей)", lambda p: ratio(p['card'], p['field']), 1.08),
]
L = []
def w(s=""):
L.append(s)
print(s)
def main():
w("# Тёмная палитра — замеры контраста (WCAG 2.1 AA)")
w()
w("Проект: `G:\\Project\\Haevey_Monster_Team_Page` · канбан `t_ca027aab` · дата: 2026-10-12")
w("Скрипт: `design/dark-palette/measure.py` (stdlib, пересчёт из этой же палитры)")
w()
w("## Кандидаты")
w()
w("| Вариант | bg | card | input | hairline | line-strong | text-1 | text-2 | text-3 | brand-deep | danger |")
w("|---|---|---|---|---|---|---|---|---|---|---|")
for name, p in PALETTES.items():
w("| " + name + " | " + " | ".join("`" + p[k] + "`" for k in (
"bg", "card", "field", "hairline", "line_strong", "text_1", "text_2",
"text_3", "brand_deep", "danger")) + " |")
w()
w("## Характер поверхностей (HSL: bg / card / input)")
w()
w("| Вариант | оттенок, ° | насыщенность, % | светлота, % | чтение |")
w("|---|---|---|---|---|")
for name, p in PALETTES.items():
hs = [hsl(p[k]) for k in ("bg", "card", "field")]
w("| " + name + " | " + " / ".join(f"{h[0]:.0f}" for h in hs) + " | "
+ " / ".join(f"{h[1] * 100:.0f}" for h in hs) + " | "
+ " / ".join(f"{h[2] * 100:.1f}" for h in hs) + " | " + CHARACTER[name] + " |")
w()
w("## Замеры пар (текст ≥4.5:1, UI ≥3:1)")
w()
w("| Пара | Порог | " + " | ".join(PALETTES) + " |")
w("|---|---|" + "---|" * len(PALETTES))
for label, fn, mn in PAIRS:
w(f"| {label} | {mn} | " + " | ".join(f"{fn(p):.2f}" for p in PALETTES.values()) + " |")
w()
w("## Композиты для принятого варианта (B. chocolate)")
w()
p = PALETTES["B. chocolate (SHIPPED)"]
w("| Композит | Поверхность | hex | текст на нём | отношение |")
w("|---|---|---|---|---|")
for sn in ("bg", "card", "field"):
surf = p[sn]
chip = comp(BRAND_FILL, 0.16, surf)
w(f"| brand-soft .16 | {sn} | `{chip}` | brand-deep `{p['brand_deep']}` | {ratio(p['brand_deep'], chip):.2f} |")
gh = comp(p['brand_deep'], 0.12, surf)
w(f"| ghost-hover .12 | {sn} | `{gh}` | text-2 `{p['text_2']}` | {ratio(p['text_2'], gh):.2f} |")
nav = comp(p['bg'], 0.94, surf)
w(f"| navbar .94 | {sn} | `{nav}` | text-1 `{p['text_1']}` | {ratio(p['text_1'], nav):.2f} |")
w(f"| disabled primary | — | `#3A3228` | `#8B8172` | {ratio('#8B8172', '#3A3228'):.2f} |")
w(f"| green-on-green (запрещено) | — | `{BRAND_FILL}` | brand-deep `{p['brand_deep']}` | {ratio(p['brand_deep'], BRAND_FILL):.2f} |")
fails = [(name, label, fn(pp), mn) for name, pp in PALETTES.items() for label, fn, mn in PAIRS if fn(pp) < mn]
w()
w("## Итог")
w()
w(f"- Пар проверено: {len(PAIRS) * len(PALETTES)}; провалов: {len(fails)}.")
for name, label, val, mn in fails:
w(f" - FAIL: {name} · {label} = {val:.2f} (порог {mn})")
w("- Принят вариант **B «шоколад»**: метафора «песок ↔ какао», лучшие `brand-deep` и `line-strong`,"
" наибольший шаг поверхностей среди тёплых кандидатов; все пары проходят AA.")
out = Path(__file__).with_name("contrast-2026-10-12.md")
out.write_text("\n".join(L) + "\n", encoding="utf-8")
print("\nwritten:", out)
if __name__ == "__main__":
main()