Files
image-workbench-desktop/tests/theme-plugin-tokens-test.js

38 lines
1.8 KiB
JavaScript
Raw Permalink 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.
// 插件通用灰由主题派生(v3.26.20):亮/暗都要有值、暗色必须更暗、且保持中性灰(不带蓝调)。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const src = fs.readFileSync(path.join(__dirname, '..', 'src', 'theme-tokens.js'), 'utf8')
const sandbox = {}
new Function('window', src)(sandbox)
const T = sandbox.IWBThemeTokens
assert.ok(T && typeof T.derive === 'function', 'theme-tokens 应导出 derive')
const light = T.derive(T.DEFAULTS, false)
const dark = T.derive(T.DEFAULTS, true)
const KEYS = ['pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover']
function rgb (hex) {
const s = String(hex).trim()
assert.ok(/^#[0-9a-f]{6}$/i.test(s), '应为 6 位 hex,实际 ' + s)
return { r: parseInt(s.slice(1, 3), 16), g: parseInt(s.slice(3, 5), 16), b: parseInt(s.slice(5, 7), 16) }
}
const lum = (hex) => { const c = rgb(hex); return 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b }
for (const k of KEYS) {
assert.ok(light[k], '亮色缺少 ' + k)
assert.ok(dark[k], '暗色缺少 ' + k)
assert.ok(lum(dark[k]) < lum(light[k]), k + ' 暗色应比亮色更暗:' + dark[k] + ' vs ' + light[k])
// 中性灰:通道极差 <= 3(防止又混进蓝调 e3e7ea)
const c = rgb(light[k])
const spread = Math.max(c.r, c.g, c.b) - Math.min(c.r, c.g, c.b)
assert.ok(spread <= 3, k + ' 亮色应为中性灰(通道差 ' + spread + '),实际 ' + light[k])
}
// 卡片必须比内嵌块更亮(层次:卡片 > 面板 > 内嵌)
assert.ok(lum(light.pluginCard) >= lum(light.pluginSurface), '卡片不应比面板更暗')
assert.ok(lum(light.pluginSurface) >= lum(light.pluginInset), '面板不应比内嵌块更暗')
console.log('theme plugin tokens checks passed(亮/暗派生 / 暗色更暗 / 中性灰 / 层次正确)')