88 lines
5.0 KiB
JavaScript
88 lines
5.0 KiB
JavaScript
'use strict'
|
||
// 对比度回归守卫(M5-T5.4)
|
||
//
|
||
// 为什么需要它:暗色主题的对比度不是「写死一个值」就完了 —— derive() 用 mix + clampAgainst
|
||
// 现算,任何一次调色(把 panel 提亮、把 layer 压深、把文字权重从 .30 改成 .34)都可能让
|
||
// 某对「文字 × 表面」掉到阈值以下,而**肉眼在编辑器里看不出来**(唯一能看出来的用户会截图说
|
||
// 「这个字看不清」,但我们拿不到他的主题参数)。
|
||
//
|
||
// 阈值取 theme-tokens.js 的 RATIO(单一源,禁止在这里再写 4.5 / 7 这种魔数):
|
||
// text 7.0(AAA 正文)/ text2 4.5(AA 正文)/ dim 3.0(AA 大字下限)/ canvas 6.0(节点标题,预留 opacity .8)/ onBrand 4.5
|
||
// 计划里写的「text ≥4.5、tertiary ≥3.0、on-brand ≥4.5」是**下限**,实现取更严的 7.0/3.0/4.5。
|
||
//
|
||
// 亮暗两套主题都跑;DEFAULTS 之外的配色(用户在设置里改过的)不在本守卫范围 ——
|
||
// 那条路由 clampText 在运行时兜底。
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const sandbox = {}
|
||
new Function('window', fs.readFileSync(path.join(root, 'src', 'theme-tokens.js'), 'utf8'))(sandbox)
|
||
const T = sandbox.IWBThemeTokens
|
||
assert.ok(T && typeof T.derive === 'function', 'theme-tokens.js 没暴露 IWBThemeTokens.derive')
|
||
|
||
// 承载文字的整套表面。canvas 单独处理:画布上只有节点标题,它用 canvasText(阈值 6,
|
||
// 且要预留 opacity .8 的余量)—— 界面文字本来就不该直接压在画布底上,混进通用集合会误报。
|
||
const SURFACES = ['bg', 'panel', 'layer', 'layer2', 'nodeBg', 'field', 'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover', 'hover', 'dockBg', 'chrome', 'inset']
|
||
|
||
const rows = []
|
||
function check (theme, fgName, fg, surfaces, min, label) {
|
||
for (const s of surfaces) {
|
||
const bg = theme[s]
|
||
if (!bg || !/^#[0-9a-f]{6}$/i.test(bg)) continue
|
||
const r = T.contrast(fg, bg)
|
||
rows.push({ name: fgName + ' × ' + s, ratio: r, min, label, ok: r + 1e-9 >= min })
|
||
}
|
||
}
|
||
|
||
for (const dark of [false, true]) {
|
||
const t = T.derive(T.DEFAULTS, dark)
|
||
const label = dark ? '暗色' : '亮色'
|
||
const present = SURFACES.filter((s) => t[s])
|
||
check(t, 'text', t.text, present, T.RATIO.text, label)
|
||
check(t, 'text2', t.text2, present, T.RATIO.text2, label)
|
||
check(t, 'textDim', t.textDim, present, T.RATIO.dim, label)
|
||
check(t, 'brandText', t.brandText, present, T.RATIO.text2, label)
|
||
check(t, 'canvasText', t.canvasText, ['canvasBg'], T.RATIO.canvas, label)
|
||
check(t, 'nodeText2', t.nodeText2, ['nodeBg', 'layer', 'panel'], T.RATIO.text2, label)
|
||
// M6 补漏:状态色(ok/err/warn)在 .iwb-status-ok / .iwb-ratio.ok / 插件标签里是**直接当文字色**
|
||
// 用的,M5 的 114 对只覆盖了 text/text2/dim/brandText,把它们漏了 —— 于是亮色 --iwb-ok
|
||
// 长期是 #22c55e(对浅页面 2.13)也一路绿灯。状态文字同样按 AA 正文要求 ≥4.5。
|
||
const STATUS_SURFACES = ['bg', 'panel', 'layer', 'layer2']
|
||
for (const k of ['ok', 'err', 'warn']) check(t, k, t[k], STATUS_SURFACES, T.RATIO.text2, label)
|
||
// 品牌底上的字:onBrand 是按对比度自动选的黑/白,选错了就会「主按钮白字压浅蓝」
|
||
rows.push({
|
||
name: 'onBrand × brand',
|
||
ratio: T.contrast(t.onBrand, t.brand),
|
||
min: T.RATIO.onBrand,
|
||
label,
|
||
ok: T.contrast(t.onBrand, t.brand) + 1e-9 >= T.RATIO.onBrand
|
||
})
|
||
}
|
||
|
||
const bad = rows.filter((r) => !r.ok)
|
||
if (bad.length) {
|
||
console.log('对比度不达标(阈值来自 theme-tokens.js 的 RATIO):')
|
||
for (const r of bad) console.log(' ✗ ' + r.label + ' ' + r.name.padEnd(26) + r.ratio.toFixed(2) + ' < ' + r.min)
|
||
console.log('\n最低的三对(含达标的):')
|
||
for (const r of rows.slice().sort((a, b) => a.ratio - b.ratio).slice(0, 3)) {
|
||
console.log(' · ' + r.label + ' ' + r.name.padEnd(26) + r.ratio.toFixed(2) + '(下限 ' + r.min + ')')
|
||
}
|
||
}
|
||
assert.deepStrictEqual(bad.map((r) => r.label + ' ' + r.name + ' ' + r.ratio.toFixed(2) + '<' + r.min), [],
|
||
'文字与表面的对比度掉到阈值以下 —— 调色时改坏了,改回去或用 clampAgainst 重新夹紧')
|
||
|
||
// 外观 v2 的 seeds 是 { neutral, accent },derive 必须同样吃这套字段名;
|
||
// 否则设置里的强调色/中性基调会静默回退默认灰(2026-11-26 实测)。
|
||
{
|
||
const v2 = T.derive({ neutral: '#0f5bf7', accent: '#0f5bf7', depth: 22 }, false)
|
||
assert.strictEqual(v2.brand, '#0f5bf7', 'v2 seeds.accent 必须驱动 --iwb-brand')
|
||
const legacy = T.derive({ seedNeutral: '#0f5bf7', seedAccent: '#0f5bf7', depth: 22 }, false)
|
||
assert.strictEqual(legacy.brand, '#0f5bf7', '旧字段 seedAccent 仍需兼容')
|
||
}
|
||
|
||
const minRow = rows.slice().sort((a, b) => a.ratio - b.ratio)[0]
|
||
console.log('token-contrast: ' + rows.length + ' 对(亮/暗 × 文字 × 表面)全部达标;最紧的一对是 '
|
||
+ minRow.label + ' ' + minRow.name + ' = ' + minRow.ratio.toFixed(2) + '(下限 ' + minRow.min + ')')
|