diff options
Diffstat (limited to 'scripts/theme-studio/colormath.js')
| -rw-r--r-- | scripts/theme-studio/colormath.js | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/scripts/theme-studio/colormath.js b/scripts/theme-studio/colormath.js index 367b6abec..b57da9131 100644 --- a/scripts/theme-studio/colormath.js +++ b/scripts/theme-studio/colormath.js @@ -167,4 +167,59 @@ function rgb2hex(r, g, b) { return '#' + [r, g, b].map(x => Math.max(0, Math.min(255, x)).toString(16).padStart(2, '0')).join(''); } -export { srgb2oklab, oklab2oklch, oklch2oklab, oklch2hex, apca, deltaE, hex2rgb, lin, rl, contrast, rating, hsv2rgb, rgb2hsv, rgb2hex }; +// One Chroma×Lightness plane cell at a fixed hue: the sRGB color if the (L,C,H) +// is reachable, else flagged out of gamut. Forward-only (one conversion + a +// range check) — the binary-search clamp is reserved for committing a color. +function planeCell(L, C, H) { + const lab = oklch2oklab(L, C, H), lrgb = oklab2lrgb(lab.L, lab.a, lab.b); + return inGamut(lrgb) ? { inGamut: true, hex: lrgb2hex(lrgb) } : { inGamut: false, hex: null }; +} + +// Pairwise palette analysis. palette is [[hex, name], ...]. Returns the pairs +// closer than threshold (OKLab ΔE), closest-first and capped, the overflow count +// beyond the cap, and each color's nearest-neighbor distance for its chip title. +function paletteWarnings(palette, threshold = 0.02, cap = 5) { + const n = palette.length, nearest = new Array(n).fill(Infinity), pairs = []; + for (let i = 0; i < n; i++) for (let j = i + 1; j < n; j++) { + const d = deltaE(palette[i][0], palette[j][0]); + if (d < nearest[i]) nearest[i] = d; + if (d < nearest[j]) nearest[j] = d; + if (d < threshold) pairs.push({ i, j, aName: palette[i][1], bName: palette[j][1], dE: d }); + } + pairs.sort((a, b) => a.dE - b.dE); + return { warnings: pairs.slice(0, cap), overflow: Math.max(0, pairs.length - cap), nearest }; +} + +// --- 3D-box relief colors, matching Emacs's renderer --------------------- +// Port of x_alloc_lighter_color (Emacs 30 xterm.c): highlight = bg x 1.2 +// (delta 0x8000), shadow = bg x 0.6 (delta 0x4000), both in 16-bit channel +// space. Backgrounds dimmer than 48000/65535 (by Emacs's 2R+3G+B/6 weighting) +// get an additive boost of delta*dimness*factor/2, because scaling alone +// barely moves a dark color. When the result still equals the background +// (pure black shadow, pure white highlight), Emacs retries with bg+delta. +function reliefColors(bgHex) { + const rgb = hex2rgb(bgHex); + if (rgb.some((c) => Number.isNaN(c))) return { hl: null, sh: null }; + const ch16 = rgb.map((c) => c * 257); + const one = (factor, delta) => { + let nw = ch16.map((c) => Math.min(0xffff, factor * c)); + const bright = (2 * ch16[0] + 3 * ch16[1] + ch16[2]) / 6; + if (bright < 48000) { + const md = delta * (1 - bright / 48000) * factor / 2; + nw = factor < 1 + ? nw.map((v) => Math.max(0, v - md)) + : nw.map((v) => Math.min(0xffff, v + md)); + } + if (nw.every((v, i) => Math.round(v) === ch16[i])) + nw = ch16.map((c) => Math.min(0xffff, c + delta)); + return '#' + nw.map((v) => Math.round(v / 257).toString(16).padStart(2, '0')).join(''); + }; + return { hl: one(1.2, 0x8000), sh: one(0.6, 0x4000) }; +} + +// OKLCH of a hex, and the pure black/white endpoint test. Shared by app-core +// and palette-generator-core (both previously kept their own identical copies). +function oklchOf(hex){return oklab2oklch(srgb2oklab(hex));} +function isPureEndpointHex(hex){const h=(hex||'').toLowerCase();return h==='#ffffff'||h==='#000000';} + +export { srgb2oklab, oklab2oklch, oklch2oklab, oklch2hex, apca, deltaE, hex2rgb, lin, rl, contrast, rating, hsv2rgb, rgb2hsv, rgb2hex, oklab2lrgb, inGamut, lrgb2hex, planeCell, paletteWarnings, reliefColors, oklchOf, isPureEndpointHex }; |
