mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-28 07:27:03 +08:00
移动端 ACCENTS 有四组点缀色却没有落地入口。桌面把它做成可切换的一维: data-accent 与 data-theme 正交,都只是根节点属性,组件与令牌零改动;默认色 不写属性——它就是 :root/深色块本身,与 theme 的 "" 缺省同一姿势。Chromium 109 不给 color-mix()/相对颜色,颜色只能预先算好落成静态块。 配色规则是**整套令牌绕色相轴旋转**,不是给每个色手挑一组值:主题色 = 默认绿 那 14 个令牌在 OKLCH 里转到目标色相,OKLab 的 L 与 C 不动。这么定有四个理由: 默认绿逐字节不变(旋转 0° 是恒等,有断言钉着);L 不变则四色感知轻重一致; 设计师手调的 accSelT/linkH/accSelDim/userBg 自动跟着走,不用逐色重调;加第五 个色只要给一个色相。选 OKLab 而非 HSL——HSL 的 L 不是感知亮度,同 L 的蓝看着 比绿沉得多,"只换色相"会变成"顺便改了轻重"。 色相没照抄移动端(蓝 245°/紫 307°/橙 55°,原 260/291/65):移动端的蓝与紫在 OKLab 里只差 31°,靠各自明度不同才勉强分得开,统一到同一 L/C 后会糊成两块 蓝紫;重排后最小间隔 62°,且三个色相都拿到满彩度(原橙 65° 会被 sRGB 色域 压掉 9%)。对比度只升不降:白字压在主题色上,绿 2.74:1(样板)、蓝 2.90、 橙 3.10、紫 3.15——移动端原橙 #f29a35 只有 2.2:1,比默认绿还差一档,照抄 就是把坑搬过来。比值由脚本实测后写进每个 CSS 块的注释:WCAG 相对亮度与 OKLab 的 L 是两套加权,等 L 不等于等对比度,不能凭规则想当然。 gen_accent_tokens.py 从 mobile/src/theme.tsx 解析 ACCENTS 拿色名,并断言 styles.css 的 --acc 与移动端 fill 一致——把 #1f8a5b 那次漂移的复发路径钉死。 产出 styles.css 生成块与 ui/src/gen/accents.ts,--check 进 CI。单测 13 例, 写出来当场抓到两个真问题:等 L 旋转在某些色相会被色域压彩度(改成"亮度是硬 约束、彩度只能不超过原值",另给调色板断言满彩度);蓝紫只差 31°(加 55° 下限 拦住)。另有 --onAcc: #fff 是 3 位简写,按 6 位解析会读成一个蓝色,标注的对比度 整片静默算错。 顺带全量扫了 ui/ 里绕过令牌的颜色,四处: - index.html 的首帧防闪底色写死成**浅色**一档,是功能性 bug: <script type="module"> 是 defer 的,applyStoredTheme() 赶不上第一次绘制, 深色用户启动必闪一帧浅色——正是这条规则本该防住的事。补深色一档 + 一段 同步内联脚本,先于首帧按 mc.theme 落 data-theme - App.tsx 引擎崩溃横幅底 rgba(248,113,113,.1) 是 Tailwind red-400,不在本 调色板里、深色下也不跟随,而同一条横幅的描边用的是 var(--err) → var(--errBg) - 二维码衬底 #fff、窗口关闭键 hover #e81123/#fff → --qrBg / --capClose / --capCloseTx。三者归为"平台/功能约定色":两主题同值、不跟主题色走,收进 令牌只为过"颜色一律经令牌"这条红线,**为什么不跟随**写在注释里 - check_theme_tokens.py 增 check_boot_background:index.html 那两个值必须与 --bg 逐字对齐,且必须有不带属性的 <script>。反向验证过——拿旧版 index.html 跑,两条都报 扫描后剩两处字面量,都是结构上走不了 var() 的,各留了注释:index.html 的首帧 底色(样式表还没加载,已被检查器盯住)、cloudterm.tsx 里 getComputedStyle 读 不到令牌时的兜底值。 验证:gen_accent_tokens.py --check / check_theme_tokens.py OK;scripts 单测 45 passed(新增 17 例);tsc --noEmit 无输出;vitest 27 files / 203 passed; vite build OK。**未做视觉验证**——四个色板 × 深浅共 8 种组合需人工过一眼。
114 lines
4.8 KiB
Python
114 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
||
"""Fail when the dark theme drifts from the light token set.
|
||
|
||
深色主题的设计是"只填令牌值、组件零改动"(ui/src/styles.css 头部注释):
|
||
`:root` 定义全部令牌,`[data-theme="dark"]` 逐个覆盖同名令牌。这条不变量破了
|
||
不会有编译错误——深色下某个颜色悄悄回落成浅色值,而开发几乎总在浅色下写
|
||
代码,肉眼永远看不见。"加新令牌只改 :root"是最容易漏的一步。
|
||
|
||
放在 scripts/ 而不是 vitest:检查对象是 CSS 源文本这一静态文件契约,与
|
||
check_command_contract.py / check_bundle_configs.py 同类;而 vitest 默认把
|
||
CSS 导入 stub 成空串(`?raw` 拿不到内容),浏览器工程也不该为读文件引
|
||
@types/node。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import pathlib
|
||
import re
|
||
import sys
|
||
|
||
|
||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||
STYLES = ROOT / "ui/src/styles.css"
|
||
INDEX_HTML = ROOT / "ui/index.html"
|
||
LIGHT_SELECTOR = ":root"
|
||
DARK_SELECTOR = '[data-theme="dark"]'
|
||
# 深色块曾经只有一行 TODO 注释(切换是 no-op)。给个下限,防它被清空后
|
||
# "集合一致"因为两边都空而假成立。
|
||
MIN_TOKENS = 50
|
||
|
||
|
||
def tokens_of(css: str, selector: str) -> list[str]:
|
||
"""按出现顺序取某选择器块内声明的令牌名。自定义属性块不嵌套,取到首个
|
||
`}` 即整块。"""
|
||
block = re.search(re.escape(selector) + r"\s*\{([^}]*)\}", css)
|
||
if not block:
|
||
raise ValueError(f"{STYLES.name} 中找不到选择器块:{selector}")
|
||
return re.findall(r"^\s*(--[\w-]+)\s*:", block.group(1), re.M)
|
||
|
||
|
||
def check(styles: pathlib.Path = STYLES) -> list[str]:
|
||
css = styles.read_text(encoding="utf-8")
|
||
light = tokens_of(css, LIGHT_SELECTOR)
|
||
dark = tokens_of(css, DARK_SELECTOR)
|
||
errors: list[str] = []
|
||
|
||
if missing := sorted(set(light) - set(dark)):
|
||
errors.append(
|
||
f"深色块缺令牌(深色下会回落成浅色值): {', '.join(missing)}"
|
||
)
|
||
if extra := sorted(set(dark) - set(light)):
|
||
errors.append(f"深色块多出 :root 没有的令牌: {', '.join(extra)}")
|
||
for name, seq in (("’:root’", light), ("深色块", dark)):
|
||
if dupes := sorted({t for t in seq if seq.count(t) > 1}):
|
||
errors.append(f"{name} 重复声明同一令牌: {', '.join(dupes)}")
|
||
if len(dark) < MIN_TOKENS:
|
||
errors.append(
|
||
f"深色块只有 {len(dark)} 个令牌(下限 {MIN_TOKENS}):"
|
||
f"主题切换很可能又退化成 no-op"
|
||
)
|
||
errors += check_boot_background(css)
|
||
return errors
|
||
|
||
|
||
def value_of(css: str, selector: str, token: str) -> str | None:
|
||
block = re.search(re.escape(selector) + r"\s*\{([^}]*)\}", css)
|
||
if not block:
|
||
return None
|
||
found = re.search(rf"^\s*{re.escape(token)}\s*:\s*([^;]+);", block.group(1), re.M)
|
||
return found.group(1).split("/*")[0].strip() if found else None
|
||
|
||
|
||
def check_boot_background(css: str, index_html: pathlib.Path = INDEX_HTML) -> list[str]:
|
||
"""index.html 的首帧防闪底色必须与 --bg 逐字对齐。
|
||
|
||
那两个值是**唯一**不能走 var() 的颜色:styles.css 由 main.tsx 引入,首帧时
|
||
还没到。代价是它们会随 --bg 漂——漂了不报错,只是深色启动闪一帧浅色(这条
|
||
规则原本就是为防它而写的),没人会注意到。曾经它只写死了浅色一档。"""
|
||
html = index_html.read_text(encoding="utf-8")
|
||
errors: list[str] = []
|
||
for selector, token, rule in (
|
||
(LIGHT_SELECTOR, "--bg", r"^\s*html, body \{ background: (#[0-9a-fA-F]{3,6}); \}"),
|
||
(DARK_SELECTOR, "--bg", r'^\s*html\[data-theme="dark"\][^{]*\{ background: (#[0-9a-fA-F]{3,6}); \}'),
|
||
):
|
||
want = value_of(css, selector, token)
|
||
found = re.search(rule, html, re.M)
|
||
if not found:
|
||
errors.append(f"index.html 缺首帧防闪底色规则(对应 {selector} 的 {token})")
|
||
elif want and found.group(1).lower() != want.lower():
|
||
errors.append(
|
||
f"index.html 的首帧底色 {found.group(1)} 与 {selector} 的 {token} "
|
||
f"({want})不一致:深色启动会闪一帧另一个颜色"
|
||
)
|
||
# 属性得在首帧之前落上,否则上面两档规则都命不中。判据就是"有一个不带任何
|
||
# 属性的 <script>":带 type="module" 的是 defer,轮到它时首帧已经画完了。
|
||
if "<script>" not in html or "mc.theme" not in html:
|
||
errors.append("index.html 缺同步内联脚本(按 mc.theme 在首帧前落 data-theme)")
|
||
return errors
|
||
|
||
|
||
def main() -> int:
|
||
errors = check()
|
||
if errors:
|
||
print("主题令牌契约破裂:", file=sys.stderr)
|
||
for error in errors:
|
||
print(f"- {error}", file=sys.stderr)
|
||
return 1
|
||
print("主题令牌契约 OK")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|