#!/usr/bin/env python3 """Gitea 品牌色对比度探针(2026-10-22 · gitea-branding) 背景:品牌手册 §3 钉了两档朱砂 —— 纸底 #C41E3A / 深底 #E13C58,并留了一句警示 「#E13C58 对白只有 4.44」。Gitea 侧要同时喂浅色与深色两套主题,按钮文字是 --color-primary-contrast(实测两档都是 #ffffff),链接文字直接用 --color-primary。 ⇒ 不能照抄手册色值就收工,必须按 Gitea 实测底色量一遍。 口径:WCAG 2.1 相对亮度与对比度公式(与 scripts/accessibility 一致)。 判据:正文/链接 4.5:1(AA);按钮文字同为 4.5:1;非文本 UI(描边/焦点环/朱砂条)3:1。 仪器自证:已知好/坏的两对必须先验证公式本身 —— 判据不敏感时后面的表全是废纸。 用 /usr/bin/python3 运行(本机只有它有 Pillow;本脚本其实不需要,但口径统一)。 """ import re import sys UP = "infra/cicd/gitea-custom/upstream/public/assets/css" LIGHT = f"{UP}/theme-gitea-light.CcrYmZOI.css" DARK = f"{UP}/theme-gitea-dark.n17NpZ-u.css" def srgb_lin(c): c = c / 255.0 return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4 def lum(hexstr): h = hexstr.lstrip("#") if len(h) == 3: h = "".join(ch * 2 for ch in h) r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4)) return 0.2126 * srgb_lin(r) + 0.7152 * srgb_lin(g) + 0.0722 * srgb_lin(b) 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) def token(path, name): s = open(path, encoding="utf-8").read() m = re.search(r"--%s:\s*(#[0-9a-fA-F]{3,8})" % re.escape(name), s) if not m: raise SystemExit("FATAL: %s 里找不到 --%s(口径变了,别接着算)" % (path, name)) return m.group(1) def selftest_instrument(): """仪器自证:公式必须把已知的好/坏分开。""" checks = [ (ratio("#000000", "#ffffff") >= 20.9, "黑对白应≈21:1"), (ratio("#ffffff", "#ffffff") < 1.01, "白对白应≈1:1(若判合格说明仪器坏)"), (ratio("#777777", "#ffffff") < 4.6, "中灰对白应刚掉出 4.5"), (abs(ratio("#C41E3A", "#ffffff") - 5.84) < 0.05, "#C41E3A 对白实测≈5.84"), ] bad = [msg for ok, msg in checks if not ok] if bad: print("FATAL 仪器自证失败:", "; ".join(bad)) sys.exit(2) print("仪器自证 4/4 通过(含一对必须判失败的负对照)\n") def tune(fg, other, need, toward): """把 fg 朝 toward 插值,返回首个满足「与 other 的对比度 ≥ need」的色与偏移量。 方向不能只朝背景插值 —— 按钮文字是白色时,把朱砂往白里提只会更糟,必须往黑走。 所以调用方两个方向都试,取位移小的那个。 """ def split(x): x = x.lstrip("#") return [int(x[i:i + 2], 16) for i in (0, 2, 4)] f, t = split(fg), split(toward) for step in range(0, 101): k = step / 100.0 c = "#%02X%02X%02X" % tuple(round(f[i] + (t[i] - f[i]) * k) for i in range(3)) if ratio(c, other) >= need: return c, step return None, None def main(): selftest_instrument() l_body, l_text, l_primary = token(LIGHT, "color-body"), token(LIGHT, "color-text"), token(LIGHT, "color-primary") d_body, d_text, d_primary = token(DARK, "color-body"), token(DARK, "color-text"), token(DARK, "color-primary") print("Gitea 实测底色(从 28.2.0 内嵌主题 CSS 读出,非推断)") print(" 浅色 --color-body=%s --color-text=%s 上游 --color-primary=%s" % (l_body, l_text, l_primary)) print(" 深色 --color-body=%s --color-text=%s 上游 --color-primary=%s\n" % (d_body, d_text, d_primary)) WHITE, INK = "#ffffff", "#26221E" PAPER, SEAL_DEEP = "#C41E3A", "#E13C58" rows = [ ("上游蓝 按钮文字(白)", l_primary, WHITE, 4.5), ("上游蓝 链接 on 浅底", l_primary, l_body, 4.5), ("上游蓝 按钮文字(白)·深", d_primary, WHITE, 4.5), ("上游蓝 链接 on 深底", d_primary, d_body, 4.5), ("纸底朱砂 #C41E3A 按钮文字(白)", PAPER, WHITE, 4.5), ("纸底朱砂 链接 on 浅底", PAPER, l_body, 4.5), ("纸底朱砂 焦点环 on 浅底", PAPER, l_body, 3.0), ("深底朱砂 #E13C58 按钮文字(白)", SEAL_DEEP, WHITE, 4.5), ("深底朱砂 链接 on 深底", SEAL_DEEP, d_body, 4.5), ("深底朱砂 焦点环/描边 on 深底", SEAL_DEEP, d_body, 3.0), ] print("%-38s %-22s %8s %s" % ("场景", "组合", "实测", "AA 判据")) reds = [] for name, fg, bg, need in rows: r = ratio(fg, bg) ok = "过" if r >= need else "红" if r < need: reds.append((name, fg, bg, need, r)) print("%-38s %-22s %7.2f:1 ≥%.1f %s" % (name, "%s/%s" % (fg, bg), r, need, ok)) print("\n需要裁决的红项(不自动改色,先给候选):") for name, fg, bg, need, r in reds: opts = [] for toward in ("#FFFFFF", "#000000"): cand, step = tune(fg, bg, need, toward) if cand: opts.append((step, cand, toward)) print(" ✗ %s 实测 %.2f(差 %.2f)" % (name, r, need - r)) for step, cand, toward in sorted(opts)[:2]: print(" %s 移 %.0f%% → %s,实测 %.2f" % ("提亮" if toward == "#FFFFFF" else "压暗", step, cand, ratio(cand, bg))) print("\n结论口径:上游蓝在深底做链接文字本身只有 %.2f:1," "「比上游更差」不是可接受的辩护,但「与上游同档」也不是通过。" % ratio(d_primary, d_body)) return 1 if reds else 0 if __name__ == "__main__": sys.exit(main())