diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-19 10:19:59 -0400 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-19 10:19:59 -0400 |
| commit | 119968487f401fcb0d568ab4b6e16bd261d49e65 (patch) | |
| tree | d02fa553ecd58b71fb7f6e080a9e21af18c2ec1a /scripts/theme-studio | |
| parent | 8d07d04888fff226ff34674c0d0ef100e2fd012e (diff) | |
| download | dotemacs-119968487f401fcb0d568ab4b6e16bd261d49e65.tar.gz dotemacs-119968487f401fcb0d568ab4b6e16bd261d49e65.zip | |
refactor(theme-studio): share oklchOf and isPureEndpointHex from colormath
oklchOf and isPureEndpointHex were each defined identically in app-core.js and palette-generator-core.js, and hueOfHex inlined oklchOf's body a third time. I moved both helpers into colormath.js, which already owns the primitives they call, and had the two consumers import them. hueOfHex now calls oklchOf instead of re-deriving it.
Diffstat (limited to 'scripts/theme-studio')
| -rw-r--r-- | scripts/theme-studio/app-core.js | 4 | ||||
| -rw-r--r-- | scripts/theme-studio/colormath.js | 7 | ||||
| -rw-r--r-- | scripts/theme-studio/palette-generator-core.js | 6 | ||||
| -rw-r--r-- | scripts/theme-studio/theme-studio.html | 11 |
4 files changed, 15 insertions, 13 deletions
diff --git a/scripts/theme-studio/app-core.js b/scripts/theme-studio/app-core.js index 1f2ee9d91..9780a7820 100644 --- a/scripts/theme-studio/app-core.js +++ b/scripts/theme-studio/app-core.js @@ -9,7 +9,7 @@ // where normHex (app-util.js) and the colormath helpers are already present from // the bodies inlined above this one. import { normHex } from './app-util.js'; -import { oklch2hex, srgb2oklab, oklab2oklch, oklab2lrgb, lrgb2hex, inGamut, contrast } from './colormath.js'; +import { oklch2hex, srgb2oklab, oklab2lrgb, lrgb2hex, inGamut, contrast, oklchOf, isPureEndpointHex } from './colormath.js'; // Resolve a palette name (or a raw #hex) to a hex; null when the name is unknown. function nameToHex(n,palette){if(!n)return null;if(/^#/.test(n))return n;const p=palette.find(p=>p[1]===n);return p?p[0]:null;} @@ -204,9 +204,7 @@ function lMax(hue,chroma,fgSet,target){ // the editable truth; these pure functions group it, regenerate a ramp, and plan // assignment re-point across a regenerate. -function oklchOf(hex){return oklab2oklch(srgb2oklab(hex));} function isReservedGroundLikeName(name){return /^(bg|fg)(?:[-_+].+|\d.*)$/i.test(name||'');} -function isPureEndpointHex(hex){const h=(hex||'').toLowerCase();return h==='#ffffff'||h==='#000000';} function interpOklabHex(a,b,t,offset){ const lab={L:a.L+(b.L-a.L)*t,a:a.a+(b.a-a.a)*t,b:a.b+(b.b-a.b)*t}; const lrgb=oklab2lrgb(lab.L,lab.a,lab.b); diff --git a/scripts/theme-studio/colormath.js b/scripts/theme-studio/colormath.js index 2a7328e54..b57da9131 100644 --- a/scripts/theme-studio/colormath.js +++ b/scripts/theme-studio/colormath.js @@ -217,4 +217,9 @@ function reliefColors(bgHex) { return { hl: one(1.2, 0x8000), sh: one(0.6, 0x4000) }; } -export { srgb2oklab, oklab2oklch, oklch2oklab, oklch2hex, apca, deltaE, hex2rgb, lin, rl, contrast, rating, hsv2rgb, rgb2hsv, rgb2hex, oklab2lrgb, inGamut, lrgb2hex, planeCell, paletteWarnings, reliefColors }; +// 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 }; diff --git a/scripts/theme-studio/palette-generator-core.js b/scripts/theme-studio/palette-generator-core.js index 6bce0d59a..94ed38011 100644 --- a/scripts/theme-studio/palette-generator-core.js +++ b/scripts/theme-studio/palette-generator-core.js @@ -2,11 +2,9 @@ // from app-core.js, but owns candidate hue selection, naming, contrast filtering, // and conversion from preview columns to palette entries. import { normHex } from './app-util.js'; -import { oklch2hex, srgb2oklab, oklab2oklch, contrast, deltaE } from './colormath.js'; +import { oklch2hex, contrast, deltaE, oklchOf, isPureEndpointHex } from './colormath.js'; import { columnsFromPalette } from './app-core.js'; -function oklchOf(hex){return oklab2oklch(srgb2oklab(hex));} -function isPureEndpointHex(hex){const h=(hex||'').toLowerCase();return h==='#ffffff'||h==='#000000';} function generatedExistingNames(palette){ return new Set((palette||[]).map(p=>(p&&p[1]||'').toLowerCase()).filter(Boolean)); } @@ -27,7 +25,7 @@ function uniqueGeneratedName(base,used){ function hueOfHex(hex,fallback){ const h=typeof hex==='string'?normHex(hex):null; if(!h)return fallback; - const lch=oklab2oklch(srgb2oklab(h)); + const lch=oklchOf(h); return Number.isFinite(lch.H)?lch.H:fallback; } function generatorSourceHue(palette,ground,cfg){ diff --git a/scripts/theme-studio/theme-studio.html b/scripts/theme-studio/theme-studio.html index 4d2c51fc1..a3a5468cb 100644 --- a/scripts/theme-studio/theme-studio.html +++ b/scripts/theme-studio/theme-studio.html @@ -523,6 +523,11 @@ function reliefColors(bgHex) { }; 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';} // Pure package-model + dropdown logic, inlined verbatim from app-core.js. The // wrappers above (pname/seedPkgmap/ddList/pkgEffFg/pkgEffBg) delegate here. // Pure app logic — the package-face model and the dropdown option list — with no @@ -729,9 +734,7 @@ function lMax(hue,chroma,fgSet,target){ // the editable truth; these pure functions group it, regenerate a ramp, and plan // assignment re-point across a regenerate. -function oklchOf(hex){return oklab2oklch(srgb2oklab(hex));} function isReservedGroundLikeName(name){return /^(bg|fg)(?:[-_+].+|\d.*)$/i.test(name||'');} -function isPureEndpointHex(hex){const h=(hex||'').toLowerCase();return h==='#ffffff'||h==='#000000';} function interpOklabHex(a,b,t,offset){ const lab={L:a.L+(b.L-a.L)*t,a:a.a+(b.a-a.a)*t,b:a.b+(b.b-a.b)*t}; const lrgb=oklab2lrgb(lab.L,lab.a,lab.b); @@ -1069,8 +1072,6 @@ function contrastTitle(r){ // from app-core.js, but owns candidate hue selection, naming, contrast filtering, // and conversion from preview columns to palette entries. -function oklchOf(hex){return oklab2oklch(srgb2oklab(hex));} -function isPureEndpointHex(hex){const h=(hex||'').toLowerCase();return h==='#ffffff'||h==='#000000';} function generatedExistingNames(palette){ return new Set((palette||[]).map(p=>(p&&p[1]||'').toLowerCase()).filter(Boolean)); } @@ -1091,7 +1092,7 @@ function uniqueGeneratedName(base,used){ function hueOfHex(hex,fallback){ const h=typeof hex==='string'?normHex(hex):null; if(!h)return fallback; - const lch=oklab2oklch(srgb2oklab(h)); + const lch=oklchOf(h); return Number.isFinite(lch.H)?lch.H:fallback; } function generatorSourceHue(palette,ground,cfg){ |
