150 lines
5.6 KiB
JavaScript
150 lines
5.6 KiB
JavaScript
/* theme-tokens.js —— 配色自动适配(v3.25.81,P1)
|
||
*
|
||
* 用户裁定:只定 4 个「种子」,其余颜色由函数派生:
|
||
* ① 中性基调 seedNeutral(一个中灰)
|
||
* ② 画布深浅 depth(0~60:画布相对 UI 底色的明暗差)
|
||
* ③ 强调色 seedAccent(按钮/端口/选中框)
|
||
* ④ 模式(亮 / 暗,跟随系统在调用方决定)
|
||
*
|
||
* 派生规则:
|
||
* · 亮色:往白里 mix(越靠表面越白),画布往灰里 mix
|
||
* · 暗色:往黑里 mix(同一套语义名,方向相反)
|
||
* · 文本对表面做 4.5:1 对比度夹紧(不够就自动加深/提亮)
|
||
* 产出沿用现有 --iwb-* 变量名,CSS 不需要重写。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
|
||
function normHex (v) {
|
||
const s = String(v || '').trim()
|
||
if (!/^#[0-9a-fA-F]{3,8}$/.test(s)) return ''
|
||
if (s.length === 4) return ('#' + s[1] + s[1] + s[2] + s[2] + s[3] + s[3]).toLowerCase()
|
||
return s.slice(0, 7).toLowerCase()
|
||
}
|
||
function hexToRgb (h) {
|
||
const s = normHex(h) || '#808080'
|
||
return { r: parseInt(s.slice(1, 3), 16), g: parseInt(s.slice(3, 5), 16), b: parseInt(s.slice(5, 7), 16) }
|
||
}
|
||
function toHex (c) {
|
||
const p = (n) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, '0')
|
||
return '#' + p(c.r) + p(c.g) + p(c.b)
|
||
}
|
||
// t=0 → a,t=1 → b
|
||
function mix (a, b, t) {
|
||
const A = hexToRgb(a)
|
||
const B = hexToRgb(b)
|
||
const k = Math.max(0, Math.min(1, Number(t) || 0))
|
||
return toHex({ r: A.r + (B.r - A.r) * k, g: A.g + (B.g - A.g) * k, b: A.b + (B.b - A.b) * k })
|
||
}
|
||
function lum (h) {
|
||
const c = hexToRgb(h)
|
||
const f = (v) => { const x = v / 255; return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4) }
|
||
return 0.2126 * f(c.r) + 0.7152 * f(c.g) + 0.0722 * f(c.b)
|
||
}
|
||
function contrast (a, b) {
|
||
const l1 = Math.max(lum(a), lum(b))
|
||
const l2 = Math.min(lum(a), lum(b))
|
||
return (l1 + 0.05) / (l2 + 0.05)
|
||
}
|
||
// 文本对表面做对比度夹紧:不够就朝「远离表面」的方向继续 mix
|
||
function clampText (fg, bg, minRatio) {
|
||
const min = Number(minRatio) || 4.5
|
||
let out = fg
|
||
const away = lum(bg) > 0.5 ? '#000000' : '#ffffff'
|
||
for (let i = 0; i < 12; i++) {
|
||
if (contrast(out, bg) >= min) break
|
||
out = mix(out, away, 0.12)
|
||
}
|
||
return out
|
||
}
|
||
|
||
const DEFAULTS = { seedNeutral: '#808080', seedAccent: '#6b7280', depth: 22 }
|
||
|
||
function derive (cfg, dark) {
|
||
const c = cfg || {}
|
||
const N = normHex(c.seedNeutral) || DEFAULTS.seedNeutral
|
||
const A = normHex(c.seedAccent) || DEFAULTS.seedAccent
|
||
const depth = Math.max(0, Math.min(60, Number(c.depth) === undefined ? DEFAULTS.depth : Number(c.depth)))
|
||
const d = !!dark
|
||
const t = d ? '#000000' : '#ffffff'
|
||
|
||
// 表面阶:亮色往白、暗色往黑
|
||
const surface0 = mix(N, t, d ? 0.86 : 0.94)
|
||
const surface1 = mix(N, t, d ? 0.78 : 0.99)
|
||
const surface2 = mix(N, t, d ? 0.70 : 0.90)
|
||
const surface3 = mix(N, t, d ? 0.64 : 0.84)
|
||
// 画布:亮色更深、暗色略亮(与 UI 拉开层次)
|
||
const canvas = d ? mix(surface0, '#ffffff', depth / 100 * 0.6) : mix(surface0, '#000000', depth / 100 * 0.9)
|
||
const border = mix(N, t, d ? 0.52 : 0.52)
|
||
const border2 = mix(N, t, d ? 0.40 : 0.34)
|
||
const layer = mix(N, t, d ? 0.66 : 0.92)
|
||
const nodeBg = mix(N, t, d ? 0.72 : 0.88)
|
||
const textBase = mix(N, t, d ? 0.06 : 0.06)
|
||
const text = clampText(textBase, surface0, 4.5)
|
||
const text2 = clampText(mix(N, t, d ? 0.30 : 0.34), surface0, 3.2)
|
||
const brand = A
|
||
const brandHover = mix(A, d ? '#ffffff' : '#000000', 0.14)
|
||
const edgeHot = d ? mix(A, '#ffffff', 0.25) : A
|
||
const gridDot = mix(N, t, d ? 0.36 : 0.30)
|
||
|
||
// 语义色:色相固定,明度随主题调整(暗色提亮一档,避免发闷)
|
||
const err = d ? mix('#ef4444', '#ffffff', 0.18) : '#ef4444'
|
||
const ok = d ? mix('#22c55e', '#ffffff', 0.18) : '#22c55e'
|
||
const warn = d ? mix('#d97706', '#ffffff', 0.22) : '#d97706'
|
||
// 品牌色上的文字:按对比度自动选黑/白
|
||
const onBrand = contrast('#ffffff', brand) >= 4.5 ? '#ffffff' : '#1c2333'
|
||
|
||
return {
|
||
err, ok, warn, onBrand,
|
||
bg: surface0,
|
||
panel: surface1,
|
||
layer,
|
||
layer2: d ? surface1 : '#ffffff',
|
||
nodeBg,
|
||
canvasBg: canvas,
|
||
chrome: mix(surface0, canvas, 0.4),
|
||
homeBg: mix(surface0, canvas, 0.35),
|
||
dockBg: mix(surface3, canvas, 0.45),
|
||
dockHeadBg: mix(mix(surface3, canvas, 0.45), '#000000', 0.06),
|
||
border,
|
||
border2,
|
||
text,
|
||
text2,
|
||
brand,
|
||
brandHover,
|
||
edge: border2,
|
||
edgeHot,
|
||
gridDot,
|
||
}
|
||
}
|
||
|
||
function applyTo (root, tokens) {
|
||
if (!root || !root.style) return
|
||
const s = (k, v) => { if (v) root.style.setProperty(k, v) }
|
||
s('--iwb-bg', tokens.bg)
|
||
s('--iwb-canvas-bg', tokens.canvasBg)
|
||
s('--iwb-chrome', tokens.chrome)
|
||
s('--iwb-home-bg', tokens.homeBg)
|
||
s('--iwb-dock-bg', tokens.dockBg)
|
||
s('--iwb-dock-head-bg', tokens.dockHeadBg)
|
||
s('--iwb-panel', tokens.panel)
|
||
s('--iwb-layer', tokens.layer)
|
||
s('--iwb-layer2', tokens.layer2)
|
||
s('--iwb-node-bg', tokens.nodeBg)
|
||
s('--iwb-border', tokens.border)
|
||
s('--iwb-border2', tokens.border2)
|
||
s('--iwb-text', tokens.text)
|
||
s('--iwb-text2', tokens.text2)
|
||
s('--iwb-brand', tokens.brand)
|
||
s('--iwb-edge', tokens.edge)
|
||
s('--iwb-edge-hot', tokens.edgeHot)
|
||
s('--iwb-grid-dot', tokens.gridDot)
|
||
s('--iwb-err', tokens.err)
|
||
s('--iwb-ok', tokens.ok)
|
||
s('--iwb-warn', tokens.warn)
|
||
s('--iwb-on-brand', tokens.onBrand)
|
||
}
|
||
|
||
global.IWBThemeTokens = { DEFAULTS, derive, applyTo, mix, contrast, clampText }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|