Files
novalon-website/docs/acceptance/2026-10-22-gitea-branding/contrast-probe.py
T
zhangxiang becb9407bd feat(gitea): 升 28.2.0 并把品牌装饰层纳管为可重跑的仓库真源
自建 Gitea 此前是出厂外观(茶杯标 + 「Gitea: Git with a cup of tea」标题),而品牌「纯字标 + 朱砂条」已在官网落地;同时现网 1.27.2
  落后两个安全版本 —— 1.27.3 的 fix(git): restrict hook permissions 打的正是本实例 8 月被入侵所用的 packObjectsHook
  向量。装饰基准取自升级后从容器抽出的真实模板,不凭记忆手写。

- 升级 28.2.0:pg_dump 后还原到一次性探针库对账(116 表 / 20 仓库 / 8 用户全等),停服 8s;28.0.0 的 git egress BREAKING
  按裁定收为 EGRESS_MODE=strict(实测 app.ini 无旧 ALLOWED_* 键、mirror 仓库 0 个 ⇒ 空白名单不伤业务)。

- 装饰层真源入库 infra/cicd/gitea-custom/(结构即实测 CustomPath=/data/gitea):页头/OG/默认头像换简标与刻版、novalon-
  brand.css 覆盖 --color-primary 全色阶、custom/header.tmpl 外链注入、signin_inner.tmpl
  登录页加字标锁定(220×103)。

- 色阶由脚本生成并自带夹具:先对上游 14 档对账(light/dark 两档 ramp 命名互为反向,手敲必错),再喂上游蓝做反向自证,双双通过才写盘。

- 深档按钮改「黑字朱底」:Gitea 用同一令牌既当按钮底色又当链接文字色,AA 区间为空(按钮白字 L≤0.1833 vs 链接 on #1e1f20 L≥0.2362)⇒ #E13C58
  × #0D0D0D 实测 4.62:1;深档链接 3.92:1 作为已登记让步保留(上游蓝同位 4.14,本就不达标)。

- deploy/verify 带预检、备份、内容 sha256 比对、负对照与 0/1/2/3 退出码分档,核心判红自动回滚(首轮就靠它把「标题未生效」与「装饰未生效」区分开)。

- 纠正两处生产口径失真:仓库那份「加固版」compose 从未上线(真实文件纳管为 infra/cicd/gitea/docker-compose.live.yml);生产
  REQUIRE_SIGNIN_VIEW 实为 false,且 compose 的 DISABLE_REGISTRATION=false 会经 environment-to-ini
  在重建时静默打开注册 —— 已对齐为 true。遗留的匿名浏览面与 SSH_PORT 错配只登记不擅动。
2026-10-22 18:55:55 +08:00

135 lines
5.7 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.
#!/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())