Files
image-workbench-desktop/tests/theme-static-snapshot-test.js
T
qiqi d2b1a15878
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 39s
feat(tokens): 色彩模型重建 D1–D9 —— 暗色从「中灰底+深字」改为真正的深色
这是设计 token 计划里唯一一批用户能看出变化的改动(M2/3.26.44)。

## 派生模型(src/theme-tokens.js)
- 对比度目标收成单一常量 RATIO:text 7(AAA) / text2 4.5(AA) / dim 3 / canvas 6 / onBrand 4.5
- 新增 clampAgainst(fg, surfaces, min):文字对 13 个承载表面取最差者夹紧。
  旧实现只对 bg 夹紧,于是 --iwb-text 在暗色 layer(3.94)/nodeBg(4.38)/pluginCard(3.94) 全不达标,
  而它在 style.css 里出现 200+ 次
- 新增 canvasText(画布上的节点标题,抵消 opacity .8)/ brandText(品牌色的文字版)/ menuBg(浮层底)

## 九条缺陷
- D1 三写入者::root 与 .theme-dark 改为 derive 的静态快照;chrome/homeBg/dockBg/dockHeadBg
  统一成一套 color-mix 公式(此前 CSS 用 chrome、derive 用 surface3、apply 用 layer,三套)
- D2 同名两套值:.theme-dark 从 #757575/#a5a5a5/#111 改为近黑 + 浅字
- D3/D4/D5 对比度:暗色 text 3.94→7.11(最低),text2 3.34→4.68,textDim 2.25→3.14
- D6 layer 语义:layer 固定为「内嵌」,两套主题都比 panel 暗;hover 5 处→--iwb-hover、
  浮层 3 处→--iwb-bg-menu(暗色 #3c3c3c)
- D7 品牌色:恢复 #6b7280;另拆 --iwb-brand-text(34 处文字/图标引用切换)
- D8 焦点环:新增 --iwb-focus-ring,统一 3 处各自发明 + :where() 低权重兜底
- D9 暗色硬编码:24 条删 8 组、改 3 组,剩 16 条是节点预设底色的暗色对位

## 计划外发现的三处层次反转
- nodeBg 暗色 #242424 比画布 #313131 还暗(卡片凹进去)→ 改为相对 canvas 派生
- 改完 nodeBg 与 border 撞成 #3d3d3d(边框消失)→ 暗色 border 系数 .52→.60
- 首页 homeBg 暗色 #1d1d1d 比 panel 还亮(卡片混完等于背景而隐身)→ 卡片改用浮起层

## 守卫
- tests/theme-static-snapshot-test.js:CSS 静态快照必须等于 derive() 输出(58 色值 × 亮暗)
  它在本次改动里当场抓到两处漏同步,已注册进 test:unit
- token-audit.js 修自身缺陷:对比表硬编码旧的 #757575(脚本成了过时信息源)→ 实时解析;
  textDim 被按 4.5 标记 → 按 RATIO 分级

12 张视觉基线已重建(暗色明显变亮变清晰);verify:ci 全绿。
2026-11-23 13:56:40 +08:00

112 lines
5.0 KiB
JavaScript
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.
// CSS 静态 token 必须等于 IWBThemeTokens.derive() 的输出(v3.26.44,M2/D2 守卫)
//
// 背景:style.css 的 :root / .theme-dark 是设计 token 的「静态快照」,用途是顶住 JS 执行前
// 那一帧。历史上这两段是**另一套独立设计**(暗色是中灰底 #757575 + 深字 #111),与 derive
// 的「近黑底 + 浅字」同名不同值 —— 结果切主题像换了个产品,而且首屏会闪一下再跳变。
//
// 这个测试把「快照 == 派生」钉成硬约束:改派生公式忘了同步 CSS,这里就红。
// 结构层(chrome/home/dock/edge/hr/plugin 别名)刻意用 color-mix 表达关系式,跳过。
const fs = require('fs')
const path = require('path')
const ROOT = path.resolve(__dirname, '..')
const css = fs.readFileSync(path.join(ROOT, 'style.css'), 'utf8')
// 载入 theme-tokens(它是 UMD,挂到传入的对象上)
const sandbox = {}
new Function('window', fs.readFileSync(path.join(ROOT, 'src/theme-tokens.js'), 'utf8'))(sandbox)
const T = sandbox.IWBThemeTokens
assert(T && typeof T.derive === 'function', 'theme-tokens 未正确导出 derive')
function assert (cond, msg) {
if (!cond) { console.error('✗ ' + msg); process.exit(1) }
}
// 取出某个选择器块里的 `--var: value;` 声明
function block (selector) {
const i = css.indexOf(selector + ' {')
assert(i >= 0, '找不到选择器块: ' + selector)
const start = css.indexOf('{', i)
let depth = 0
for (let j = start; j < css.length; j++) {
if (css[j] === '{') depth++
else if (css[j] === '}') {
depth--
if (depth === 0) return css.slice(start + 1, j)
}
}
assert(false, '选择器块未闭合: ' + selector)
}
function decls (body) {
const out = {}
// 去掉注释,避免注释里的示例声明被当成真声明
const clean = body.replace(/\/\*[\s\S]*?\*\//g, '')
const re = /--(?:iwb-)?([a-z0-9-]+)\s*:\s*([^;]+);/g
let m
while ((m = re.exec(clean))) out[m[1]] = m[2].trim()
return out
}
const rootVars = decls(block(':root'))
const darkVars = decls(block('.theme-dark'))
// token 键(derive 返回的驼峰)→ CSS 变量名(kebab)
const KEYS = [
'bg', 'canvasBg', 'panel', 'layer', 'layer2', 'nodeBg', 'border', 'border2',
'text', 'text2', 'textDim', 'canvasText', 'nodeText', 'nodeText2', 'tint',
'surface', 'hover', 'menuBg', 'brand', 'brandText', 'gridDot',
'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover',
'err', 'ok', 'warn', 'onBrand',
]
// derive 键 → CSS 变量名(去掉 --iwb- 前缀)。canvasText 的变量名是特例(--iwb-color-text-on-canvas)
const VAR_ALIAS = { canvasText: 'color-text-on-canvas', menuBg: 'bg-menu' }
const varOf = (k) => VAR_ALIAS[k] || k.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())
let checked = 0
const bad = []
function checkOne (mode, dark, vars, table) {
const tokens = T.derive(T.DEFAULTS, dark)
for (const k of KEYS) {
const name = '--iwb-' + varOf(k)
const want = tokens[k]
const got = vars[varOf(k)]
if (!want) { bad.push(`${mode}: derive 未返回 ${k}`); continue }
if (!got) { bad.push(`${mode}: CSS 缺少 ${name}(期望 ${want})`); continue }
// 只比对字面量色值;color-mix/var() 表达式归结构层,跳过
if (!/^#[0-9a-f]{6}$/i.test(got)) continue
checked++
if (got.toLowerCase() !== String(want).toLowerCase()) {
bad.push(`${mode}: ${name} = ${got},derive 给 ${want}`)
}
}
}
checkOne(':root', false, rootVars)
checkOne('.theme-dark', true, darkVars)
// 结构层:CSS 用 color-mix 表达,必须与 derive 用同一组系数
assert(/^color-mix\(in srgb, var\(--iwb-bg\) 60%, var\(--iwb-canvas-bg\) 40%\)$/.test(rootVars.chrome || ''),
'chrome 公式应为 bg 60% + canvas 40%(derive 用 mix(bg, canvas, 0.4))')
assert(/^color-mix\(in srgb, var\(--iwb-chrome\) 55%, var\(--iwb-canvas-bg\) 45%\)$/.test(rootVars['dock-bg'] || ''),
'dockBg 公式应为 chrome 55% + canvas 45%(derive 用 mix(chrome, canvas, 0.45);曾有三套写法)')
assert(/^color-mix\(in srgb, var\(--iwb-panel\) 55%, var\(--iwb-dock-bg\)\)$/.test(rootVars['dock-head-bg'] || ''),
'dockHeadBg 公式应为 panel 55% + dockBg 45%')
// 暗色块不应再重复定义结构层(否则又会分叉出第二套公式)
for (const k of ['chrome', 'home-bg', 'dock-bg', 'dock-head-bg']) {
assert(!darkVars[k], `暗色块不应重新定义 --iwb-${k}(应让 :root 的 color-mix 在 html 上重新求值)`)
}
if (bad.length) {
console.error('✗ CSS 静态快照与 derive() 不一致:\n ' + bad.join('\n '))
console.error('\n修法:改 style.css 里对应的字面量,或(若刚改了派生公式)用')
console.error(' node -e "const s={};new Function(\'window\',require(\'fs\').readFileSync(\'src/theme-tokens.js\',\'utf8\'))(s);console.log(s.IWBThemeTokens.derive(s.IWBThemeTokens.DEFAULTS,true))"')
console.error('重新生成快照。')
process.exit(1)
}
console.log(`theme static snapshot checks passed(${checked} 个色值与 derive 一致,亮/暗各一套)`)