@tenphi/glaze 0.0.0-snapshot.07e5d83 → 0.0.0-snapshot.0821a67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +424 -47
- package/dist/index.cjs +790 -94
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +340 -30
- package/dist/index.d.mts +340 -30
- package/dist/index.mjs +788 -95
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -346,6 +346,11 @@ function gamutClampedLuminance(linearRgb) {
|
|
|
346
346
|
const linearSrgbToOklab = (rgb) => {
|
|
347
347
|
return transform(cbrt3(transform(rgb, linear_sRGB_to_LMS_M)), LMS_to_OKLab_M);
|
|
348
348
|
};
|
|
349
|
+
/**
|
|
350
|
+
* Convert OKLab to OKHSL.
|
|
351
|
+
* Input: [L, a, b] where L: 0–1, a/b: roughly -0.5 to 0.5.
|
|
352
|
+
* Returns [h, s, l] where h: 0–360, s: 0–1, l: 0–1.
|
|
353
|
+
*/
|
|
349
354
|
const oklabToOkhsl = (lab) => {
|
|
350
355
|
const L = lab[0];
|
|
351
356
|
const a = lab[1];
|
|
@@ -393,32 +398,108 @@ function srgbToOkhsl(rgb) {
|
|
|
393
398
|
]));
|
|
394
399
|
}
|
|
395
400
|
/**
|
|
401
|
+
* Convert CSS HSL (sRGB-based) to gamma-encoded sRGB [r, g, b] in 0–1 range.
|
|
402
|
+
* h: 0–360, s: 0–1, l: 0–1.
|
|
403
|
+
*
|
|
404
|
+
* Note: CSS HSL is not the same as OKHSL — it's HSL in the sRGB color space.
|
|
405
|
+
* Use this when parsing `hsl(...)` strings before passing to `srgbToOkhsl`.
|
|
406
|
+
*/
|
|
407
|
+
function hslToSrgb(h, s, l) {
|
|
408
|
+
const hh = (h % 360 + 360) % 360 / 360;
|
|
409
|
+
const ss = clampVal(s, 0, 1);
|
|
410
|
+
const ll = clampVal(l, 0, 1);
|
|
411
|
+
if (ss === 0) return [
|
|
412
|
+
ll,
|
|
413
|
+
ll,
|
|
414
|
+
ll
|
|
415
|
+
];
|
|
416
|
+
const q = ll < .5 ? ll * (1 + ss) : ll + ss - ll * ss;
|
|
417
|
+
const p = 2 * ll - q;
|
|
418
|
+
const hueToChannel = (t) => {
|
|
419
|
+
let tt = t;
|
|
420
|
+
if (tt < 0) tt += 1;
|
|
421
|
+
if (tt > 1) tt -= 1;
|
|
422
|
+
if (tt < 1 / 6) return p + (q - p) * 6 * tt;
|
|
423
|
+
if (tt < 1 / 2) return q;
|
|
424
|
+
if (tt < 2 / 3) return p + (q - p) * (2 / 3 - tt) * 6;
|
|
425
|
+
return p;
|
|
426
|
+
};
|
|
427
|
+
return [
|
|
428
|
+
hueToChannel(hh + 1 / 3),
|
|
429
|
+
hueToChannel(hh),
|
|
430
|
+
hueToChannel(hh - 1 / 3)
|
|
431
|
+
];
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
396
434
|
* Parse a hex color string (#rgb or #rrggbb) to sRGB [r, g, b] in 0–1 range.
|
|
397
435
|
* Returns null if the string is not a valid hex color.
|
|
436
|
+
*
|
|
437
|
+
* For 8-digit hex (`#rrggbbaa`) and 4-digit hex (`#rgba`) with alpha,
|
|
438
|
+
* use {@link parseHexAlpha}.
|
|
398
439
|
*/
|
|
399
440
|
function parseHex(hex) {
|
|
441
|
+
const result = parseHexAlpha(hex);
|
|
442
|
+
if (!result || result.alpha !== void 0) return null;
|
|
443
|
+
return result.rgb;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Parse a hex color string (#rgb, #rrggbb, #rgba, or #rrggbbaa) to
|
|
447
|
+
* sRGB [r, g, b] in 0–1 range plus an optional alpha (0–1).
|
|
448
|
+
* Returns null if the string is not a valid hex color.
|
|
449
|
+
*/
|
|
450
|
+
function parseHexAlpha(hex) {
|
|
400
451
|
const h = hex.startsWith("#") ? hex.slice(1) : hex;
|
|
401
452
|
if (h.length === 3) {
|
|
402
453
|
const r = parseInt(h[0] + h[0], 16);
|
|
403
454
|
const g = parseInt(h[1] + h[1], 16);
|
|
404
455
|
const b = parseInt(h[2] + h[2], 16);
|
|
405
456
|
if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
|
|
406
|
-
return [
|
|
457
|
+
return { rgb: [
|
|
407
458
|
r / 255,
|
|
408
459
|
g / 255,
|
|
409
460
|
b / 255
|
|
410
|
-
];
|
|
461
|
+
] };
|
|
462
|
+
}
|
|
463
|
+
if (h.length === 4) {
|
|
464
|
+
const r = parseInt(h[0] + h[0], 16);
|
|
465
|
+
const g = parseInt(h[1] + h[1], 16);
|
|
466
|
+
const b = parseInt(h[2] + h[2], 16);
|
|
467
|
+
const a = parseInt(h[3] + h[3], 16);
|
|
468
|
+
if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
|
|
469
|
+
return {
|
|
470
|
+
rgb: [
|
|
471
|
+
r / 255,
|
|
472
|
+
g / 255,
|
|
473
|
+
b / 255
|
|
474
|
+
],
|
|
475
|
+
alpha: a / 255
|
|
476
|
+
};
|
|
411
477
|
}
|
|
412
478
|
if (h.length === 6) {
|
|
413
479
|
const r = parseInt(h.slice(0, 2), 16);
|
|
414
480
|
const g = parseInt(h.slice(2, 4), 16);
|
|
415
481
|
const b = parseInt(h.slice(4, 6), 16);
|
|
416
482
|
if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
|
|
417
|
-
return [
|
|
483
|
+
return { rgb: [
|
|
418
484
|
r / 255,
|
|
419
485
|
g / 255,
|
|
420
486
|
b / 255
|
|
421
|
-
];
|
|
487
|
+
] };
|
|
488
|
+
}
|
|
489
|
+
if (h.length === 8) {
|
|
490
|
+
const r = parseInt(h.slice(0, 2), 16);
|
|
491
|
+
const g = parseInt(h.slice(2, 4), 16);
|
|
492
|
+
const b = parseInt(h.slice(4, 6), 16);
|
|
493
|
+
const a = parseInt(h.slice(6, 8), 16);
|
|
494
|
+
if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
|
|
495
|
+
return {
|
|
496
|
+
rgb: [
|
|
497
|
+
r / 255,
|
|
498
|
+
g / 255,
|
|
499
|
+
b / 255
|
|
500
|
+
],
|
|
501
|
+
alpha: a / 255
|
|
502
|
+
};
|
|
422
503
|
}
|
|
423
504
|
return null;
|
|
424
505
|
}
|
|
@@ -433,12 +514,13 @@ function formatOkhsl(h, s, l) {
|
|
|
433
514
|
return `okhsl(${fmt$1(h, 2)} ${fmt$1(s, 2)}% ${fmt$1(l, 2)}%)`;
|
|
434
515
|
}
|
|
435
516
|
/**
|
|
436
|
-
* Format OKHSL values as a CSS `rgb(R G B)` string
|
|
517
|
+
* Format OKHSL values as a CSS `rgb(R G B)` string.
|
|
518
|
+
* Uses 2 decimal places to avoid 8-bit quantization contrast loss.
|
|
437
519
|
* h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
|
|
438
520
|
*/
|
|
439
521
|
function formatRgb(h, s, l) {
|
|
440
522
|
const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
|
|
441
|
-
return `rgb(${
|
|
523
|
+
return `rgb(${parseFloat((r * 255).toFixed(2))} ${parseFloat((g * 255).toFixed(2))} ${parseFloat((b * 255).toFixed(2))})`;
|
|
442
524
|
}
|
|
443
525
|
/**
|
|
444
526
|
* Format OKHSL values as a CSS `hsl(H S% L%)` string.
|
|
@@ -469,7 +551,7 @@ function formatOklch(h, s, l) {
|
|
|
469
551
|
const C = Math.sqrt(a * a + b * b);
|
|
470
552
|
let hh = Math.atan2(b, a) * (180 / Math.PI);
|
|
471
553
|
hh = constrainAngle(hh);
|
|
472
|
-
return `oklch(${fmt$1(L, 4)} ${fmt$1(C, 4)} ${fmt$1(hh,
|
|
554
|
+
return `oklch(${fmt$1(L, 4)} ${fmt$1(C, 4)} ${fmt$1(hh, 2)})`;
|
|
473
555
|
}
|
|
474
556
|
|
|
475
557
|
//#endregion
|
|
@@ -619,7 +701,7 @@ function coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter) {
|
|
|
619
701
|
function findLightnessForContrast(options) {
|
|
620
702
|
const { hue, saturation, preferredLightness, baseLinearRgb, contrast: contrastInput, lightnessRange = [0, 1], epsilon = 1e-4, maxIterations = 14 } = options;
|
|
621
703
|
const target = resolveMinContrast(contrastInput);
|
|
622
|
-
const searchTarget = target * 1.
|
|
704
|
+
const searchTarget = target * 1.01;
|
|
623
705
|
const yBase = gamutClampedLuminance(baseLinearRgb);
|
|
624
706
|
const crPref = contrastRatioFromLuminance(cachedLuminance(hue, saturation, preferredLightness), yBase);
|
|
625
707
|
if (crPref >= searchTarget) return {
|
|
@@ -743,7 +825,7 @@ function searchMixBranch(lo, hi, yBase, target, epsilon, maxIter, preferred, lum
|
|
|
743
825
|
function findValueForMixContrast(options) {
|
|
744
826
|
const { preferredValue, baseLinearRgb, contrast: contrastInput, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
|
|
745
827
|
const target = resolveMinContrast(contrastInput);
|
|
746
|
-
const searchTarget = target * 1.
|
|
828
|
+
const searchTarget = target * 1.01;
|
|
747
829
|
const yBase = gamutClampedLuminance(baseLinearRgb);
|
|
748
830
|
const crPref = contrastRatioFromLuminance(luminanceAtValue(preferredValue), yBase);
|
|
749
831
|
if (crPref >= searchTarget) return {
|
|
@@ -809,10 +891,50 @@ function findValueForMixContrast(options) {
|
|
|
809
891
|
* Generates robust light, dark, and high-contrast colors from a hue/saturation
|
|
810
892
|
* seed, preserving contrast for UI pairs via explicit dependencies.
|
|
811
893
|
*/
|
|
894
|
+
/** Internal name of the user-facing standalone color in the synthesized def map. */
|
|
895
|
+
const STANDALONE_VALUE = "value";
|
|
896
|
+
/** Internal name of the hidden static-anchor seed used for relative lightness / contrast. */
|
|
897
|
+
const STANDALONE_SEED = "seed";
|
|
898
|
+
/** Internal name of an externally-resolved `GlazeColorToken` injected as a base reference. */
|
|
899
|
+
const STANDALONE_BASE = "externalBase";
|
|
900
|
+
/**
|
|
901
|
+
* Build the create-time scaling snapshot used when the caller did not
|
|
902
|
+
* pass an explicit `scaling`. All windows are snapshotted from the
|
|
903
|
+
* current `globalConfig` so later `glaze.configure()` calls don't
|
|
904
|
+
* retroactively change the resolved variants of an already-created
|
|
905
|
+
* token (matches the documented "frozen at create time" semantics).
|
|
906
|
+
*
|
|
907
|
+
* String value-shorthand inputs use an extended dark window
|
|
908
|
+
* `[globalConfig.darkLightness[0], 100]` so a totally-black input can
|
|
909
|
+
* Möbius-invert to totally-white in dark mode; object / tuple /
|
|
910
|
+
* structured inputs use `globalConfig.darkLightness` verbatim.
|
|
911
|
+
*/
|
|
912
|
+
function defaultStandaloneScaling(extendDark) {
|
|
913
|
+
const [lo, hi] = globalConfig.darkLightness;
|
|
914
|
+
return {
|
|
915
|
+
lightLightness: false,
|
|
916
|
+
darkLightness: extendDark ? [lo, 100] : [lo, hi]
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
/** Reserved internal names that user-supplied `name` must not collide with. */
|
|
920
|
+
const RESERVED_STANDALONE_NAMES = new Set([
|
|
921
|
+
STANDALONE_VALUE,
|
|
922
|
+
STANDALONE_SEED,
|
|
923
|
+
STANDALONE_BASE
|
|
924
|
+
]);
|
|
925
|
+
/**
|
|
926
|
+
* Discriminate a `GlazeColorToken` from a raw `GlazeColorValue`.
|
|
927
|
+
* Used to widen `base?` so it accepts either a token reference or a
|
|
928
|
+
* raw value (auto-wrapped into `glaze.color(value)`).
|
|
929
|
+
*/
|
|
930
|
+
function isGlazeColorToken(candidate) {
|
|
931
|
+
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "resolve" in candidate && typeof candidate.resolve === "function";
|
|
932
|
+
}
|
|
812
933
|
let globalConfig = {
|
|
813
934
|
lightLightness: [10, 100],
|
|
814
935
|
darkLightness: [15, 95],
|
|
815
936
|
darkDesaturation: .1,
|
|
937
|
+
darkCurve: .5,
|
|
816
938
|
states: {
|
|
817
939
|
dark: "@dark",
|
|
818
940
|
highContrast: "@high-contrast"
|
|
@@ -828,6 +950,41 @@ function pairNormal(p) {
|
|
|
828
950
|
function pairHC(p) {
|
|
829
951
|
return Array.isArray(p) ? p[1] : p;
|
|
830
952
|
}
|
|
953
|
+
/**
|
|
954
|
+
* Dedupe contrast warnings within a single process. The cache survives
|
|
955
|
+
* the lifetime of a token because tokens memoize their resolution; the
|
|
956
|
+
* limit is a soft cap to keep noise bounded across long-lived sessions
|
|
957
|
+
* (e.g. dev servers with HMR re-resolving themes repeatedly).
|
|
958
|
+
*/
|
|
959
|
+
const CONTRAST_WARN_CACHE_LIMIT = 256;
|
|
960
|
+
const contrastWarnCache = /* @__PURE__ */ new Set();
|
|
961
|
+
function schemeLabel(isDark, isHighContrast) {
|
|
962
|
+
if (isDark && isHighContrast) return "darkContrast";
|
|
963
|
+
if (isDark) return "dark";
|
|
964
|
+
if (isHighContrast) return "lightContrast";
|
|
965
|
+
return "light";
|
|
966
|
+
}
|
|
967
|
+
function formatContrastTarget(input, ratio) {
|
|
968
|
+
return typeof input === "string" ? `"${input}" (${ratio.toFixed(2)})` : ratio.toFixed(2);
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Slack factor below the requested target before we emit a warning.
|
|
972
|
+
* The contrast solver already overshoots by `OVERSHOOT` (currently 1%)
|
|
973
|
+
* to absorb rounding noise (`see findLightnessForContrast` in
|
|
974
|
+
* `contrast-solver.ts`), so an `actual` ratio within ~2x that overshoot
|
|
975
|
+
* is effectively a pass and not worth nagging the user about.
|
|
976
|
+
*/
|
|
977
|
+
const CONTRAST_WARN_SLACK = .98;
|
|
978
|
+
function warnContrastUnmet(name, isDark, isHighContrast, target, actual) {
|
|
979
|
+
const targetRatio = resolveMinContrast(target);
|
|
980
|
+
if (actual >= targetRatio * CONTRAST_WARN_SLACK) return;
|
|
981
|
+
const scheme = schemeLabel(isDark, isHighContrast);
|
|
982
|
+
const key = `${name}|${scheme}|${targetRatio.toFixed(3)}|${actual.toFixed(2)}`;
|
|
983
|
+
if (contrastWarnCache.has(key)) return;
|
|
984
|
+
if (contrastWarnCache.size >= CONTRAST_WARN_CACHE_LIMIT) contrastWarnCache.clear();
|
|
985
|
+
contrastWarnCache.add(key);
|
|
986
|
+
console.warn(`glaze: color "${name}" cannot meet contrast ${formatContrastTarget(target, targetRatio)} in ${scheme} scheme (got ${actual.toFixed(2)}). Try widening the lightness window, lowering the contrast target, or picking a base color further from this color's lightness.`);
|
|
987
|
+
}
|
|
831
988
|
function isShadowDef(def) {
|
|
832
989
|
return def.type === "shadow";
|
|
833
990
|
}
|
|
@@ -889,36 +1046,38 @@ function computeShadow(bg, fg, intensity, tuning) {
|
|
|
889
1046
|
alpha
|
|
890
1047
|
};
|
|
891
1048
|
}
|
|
892
|
-
function validateColorDefs(defs) {
|
|
893
|
-
const
|
|
1049
|
+
function validateColorDefs(defs, externalBases) {
|
|
1050
|
+
const localNames = new Set(Object.keys(defs));
|
|
1051
|
+
const allNames = new Set([...localNames, ...externalBases ? externalBases.keys() : []]);
|
|
894
1052
|
for (const [name, def] of Object.entries(defs)) {
|
|
895
1053
|
if (isShadowDef(def)) {
|
|
896
|
-
if (!
|
|
897
|
-
if (isShadowDef(defs[def.bg])) throw new Error(`glaze: shadow "${name}" bg "${def.bg}" references another shadow color.`);
|
|
1054
|
+
if (!allNames.has(def.bg)) throw new Error(`glaze: shadow "${name}" references non-existent bg "${def.bg}".`);
|
|
1055
|
+
if (localNames.has(def.bg) && isShadowDef(defs[def.bg])) throw new Error(`glaze: shadow "${name}" bg "${def.bg}" references another shadow color.`);
|
|
898
1056
|
if (def.fg !== void 0) {
|
|
899
|
-
if (!
|
|
900
|
-
if (isShadowDef(defs[def.fg])) throw new Error(`glaze: shadow "${name}" fg "${def.fg}" references another shadow color.`);
|
|
1057
|
+
if (!allNames.has(def.fg)) throw new Error(`glaze: shadow "${name}" references non-existent fg "${def.fg}".`);
|
|
1058
|
+
if (localNames.has(def.fg) && isShadowDef(defs[def.fg])) throw new Error(`glaze: shadow "${name}" fg "${def.fg}" references another shadow color.`);
|
|
901
1059
|
}
|
|
902
1060
|
continue;
|
|
903
1061
|
}
|
|
904
1062
|
if (isMixDef(def)) {
|
|
905
|
-
if (!
|
|
906
|
-
if (!
|
|
907
|
-
if (isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
|
|
908
|
-
if (isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
|
|
1063
|
+
if (!allNames.has(def.base)) throw new Error(`glaze: mix "${name}" references non-existent base "${def.base}".`);
|
|
1064
|
+
if (!allNames.has(def.target)) throw new Error(`glaze: mix "${name}" references non-existent target "${def.target}".`);
|
|
1065
|
+
if (localNames.has(def.base) && isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
|
|
1066
|
+
if (localNames.has(def.target) && isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
|
|
909
1067
|
continue;
|
|
910
1068
|
}
|
|
911
1069
|
const regDef = def;
|
|
912
1070
|
if (regDef.contrast !== void 0 && !regDef.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
|
|
913
1071
|
if (regDef.lightness !== void 0 && !isAbsoluteLightness(regDef.lightness) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "lightness" without "base".`);
|
|
914
|
-
if (regDef.base && !
|
|
915
|
-
if (regDef.base && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
|
|
1072
|
+
if (regDef.base && !allNames.has(regDef.base)) throw new Error(`glaze: color "${name}" references non-existent base "${regDef.base}".`);
|
|
1073
|
+
if (regDef.base && localNames.has(regDef.base) && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
|
|
916
1074
|
if (!isAbsoluteLightness(regDef.lightness) && regDef.base === void 0) throw new Error(`glaze: color "${name}" must have either absolute "lightness" (root) or "base" (dependent).`);
|
|
917
1075
|
if (regDef.contrast !== void 0 && regDef.opacity !== void 0) console.warn(`glaze: color "${name}" has both "contrast" and "opacity". Opacity makes perceived lightness unpredictable.`);
|
|
918
1076
|
}
|
|
919
1077
|
const visited = /* @__PURE__ */ new Set();
|
|
920
1078
|
const inStack = /* @__PURE__ */ new Set();
|
|
921
1079
|
function dfs(name) {
|
|
1080
|
+
if (!localNames.has(name)) return;
|
|
922
1081
|
if (inStack.has(name)) throw new Error(`glaze: circular base reference detected involving "${name}".`);
|
|
923
1082
|
if (visited.has(name)) return;
|
|
924
1083
|
inStack.add(name);
|
|
@@ -936,7 +1095,7 @@ function validateColorDefs(defs) {
|
|
|
936
1095
|
inStack.delete(name);
|
|
937
1096
|
visited.add(name);
|
|
938
1097
|
}
|
|
939
|
-
for (const name of
|
|
1098
|
+
for (const name of localNames) dfs(name);
|
|
940
1099
|
}
|
|
941
1100
|
function topoSort(defs) {
|
|
942
1101
|
const result = [];
|
|
@@ -945,6 +1104,7 @@ function topoSort(defs) {
|
|
|
945
1104
|
if (visited.has(name)) return;
|
|
946
1105
|
visited.add(name);
|
|
947
1106
|
const def = defs[name];
|
|
1107
|
+
if (def === void 0) return;
|
|
948
1108
|
if (isShadowDef(def)) {
|
|
949
1109
|
visit(def.bg);
|
|
950
1110
|
if (def.fg) visit(def.fg);
|
|
@@ -960,21 +1120,55 @@ function topoSort(defs) {
|
|
|
960
1120
|
for (const name of Object.keys(defs)) visit(name);
|
|
961
1121
|
return result;
|
|
962
1122
|
}
|
|
963
|
-
|
|
1123
|
+
/**
|
|
1124
|
+
* Resolve the active lightness window for a scheme.
|
|
1125
|
+
* - HC variants always return `[0, 100]` (existing behavior, predates per-call overrides).
|
|
1126
|
+
* - Otherwise, per-call `scaling` (e.g. from `glaze.color()`'s third arg) wins;
|
|
1127
|
+
* `false` is interpreted as `[0, 100]` (no remap). Falls back to `globalConfig.*Lightness`.
|
|
1128
|
+
*/
|
|
1129
|
+
function lightnessWindow(isHighContrast, kind, scaling) {
|
|
1130
|
+
if (isHighContrast) return [0, 100];
|
|
1131
|
+
if (scaling) {
|
|
1132
|
+
const override = kind === "dark" ? scaling.darkLightness : scaling.lightLightness;
|
|
1133
|
+
if (override === false) return [0, 100];
|
|
1134
|
+
if (override !== void 0) return override;
|
|
1135
|
+
}
|
|
1136
|
+
return kind === "dark" ? globalConfig.darkLightness : globalConfig.lightLightness;
|
|
1137
|
+
}
|
|
1138
|
+
function mapLightnessLight(l, mode, isHighContrast, scaling) {
|
|
964
1139
|
if (mode === "static") return l;
|
|
965
|
-
const [lo, hi] =
|
|
1140
|
+
const [lo, hi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
966
1141
|
return l * (hi - lo) / 100 + lo;
|
|
967
1142
|
}
|
|
968
|
-
function
|
|
1143
|
+
function mobiusCurve(t, beta) {
|
|
1144
|
+
if (beta >= 1) return t;
|
|
1145
|
+
return t / (t + beta * (1 - t));
|
|
1146
|
+
}
|
|
1147
|
+
function mapLightnessDark(l, mode, isHighContrast, scaling) {
|
|
969
1148
|
if (mode === "static") return l;
|
|
970
|
-
const
|
|
971
|
-
|
|
972
|
-
|
|
1149
|
+
const beta = isHighContrast ? pairHC(globalConfig.darkCurve) : pairNormal(globalConfig.darkCurve);
|
|
1150
|
+
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark", scaling);
|
|
1151
|
+
if (mode === "fixed") return l * (darkHi - darkLo) / 100 + darkLo;
|
|
1152
|
+
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1153
|
+
const t = (lightHi - (l * (lightHi - lightLo) / 100 + lightLo)) / (lightHi - lightLo);
|
|
1154
|
+
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
1155
|
+
}
|
|
1156
|
+
function lightMappedToDark(lightL, isHighContrast, scaling) {
|
|
1157
|
+
const beta = isHighContrast ? pairHC(globalConfig.darkCurve) : pairNormal(globalConfig.darkCurve);
|
|
1158
|
+
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1159
|
+
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark", scaling);
|
|
1160
|
+
const t = (lightHi - clamp(lightL, lightLo, lightHi)) / (lightHi - lightLo);
|
|
1161
|
+
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
973
1162
|
}
|
|
974
1163
|
function mapSaturationDark(s, mode) {
|
|
975
1164
|
if (mode === "static") return s;
|
|
976
1165
|
return s * (1 - globalConfig.darkDesaturation);
|
|
977
1166
|
}
|
|
1167
|
+
function schemeLightnessRange(isDark, mode, isHighContrast, scaling) {
|
|
1168
|
+
if (mode === "static") return [0, 1];
|
|
1169
|
+
const [lo, hi] = lightnessWindow(isHighContrast, isDark ? "dark" : "light", scaling);
|
|
1170
|
+
return [lo / 100, hi / 100];
|
|
1171
|
+
}
|
|
978
1172
|
function clamp(v, min, max) {
|
|
979
1173
|
return Math.max(min, Math.min(max, v));
|
|
980
1174
|
}
|
|
@@ -1031,25 +1225,29 @@ function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effective
|
|
|
1031
1225
|
else {
|
|
1032
1226
|
const parsed = parseRelativeOrAbsolute(isHighContrast ? pairHC(rawLightness) : pairNormal(rawLightness));
|
|
1033
1227
|
if (parsed.relative) {
|
|
1034
|
-
|
|
1035
|
-
if (isDark && mode === "auto")
|
|
1036
|
-
preferredL = clamp(baseL + delta, 0, 100);
|
|
1037
|
-
} else if (isDark) preferredL = mapLightnessDark(parsed.value, mode);
|
|
1038
|
-
else preferredL =
|
|
1228
|
+
const delta = parsed.value;
|
|
1229
|
+
if (isDark && mode === "auto") preferredL = lightMappedToDark(clamp(getSchemeVariant(baseResolved, false, isHighContrast).l * 100 + delta, 0, 100), isHighContrast, ctx.scaling);
|
|
1230
|
+
else preferredL = clamp(baseL + delta, 0, 100);
|
|
1231
|
+
} else if (isDark) preferredL = mapLightnessDark(parsed.value, mode, isHighContrast, ctx.scaling);
|
|
1232
|
+
else preferredL = mapLightnessLight(parsed.value, mode, isHighContrast, ctx.scaling);
|
|
1039
1233
|
}
|
|
1040
1234
|
const rawContrast = def.contrast;
|
|
1041
1235
|
if (rawContrast !== void 0) {
|
|
1042
1236
|
const minCr = isHighContrast ? pairHC(rawContrast) : pairNormal(rawContrast);
|
|
1043
1237
|
const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode) : satFactor * ctx.saturation / 100;
|
|
1044
1238
|
const baseLinearRgb = okhslToLinearSrgb(baseVariant.h, baseVariant.s, baseVariant.l);
|
|
1239
|
+
const windowRange = schemeLightnessRange(isDark, mode, isHighContrast, ctx.scaling);
|
|
1240
|
+
const result = findLightnessForContrast({
|
|
1241
|
+
hue: effectiveHue,
|
|
1242
|
+
saturation: effectiveSat,
|
|
1243
|
+
preferredLightness: clamp(preferredL / 100, windowRange[0], windowRange[1]),
|
|
1244
|
+
baseLinearRgb,
|
|
1245
|
+
contrast: minCr,
|
|
1246
|
+
lightnessRange: [0, 1]
|
|
1247
|
+
});
|
|
1248
|
+
if (!result.met) warnContrastUnmet(name, isDark, isHighContrast, minCr, result.contrast);
|
|
1045
1249
|
return {
|
|
1046
|
-
l:
|
|
1047
|
-
hue: effectiveHue,
|
|
1048
|
-
saturation: effectiveSat,
|
|
1049
|
-
preferredLightness: preferredL / 100,
|
|
1050
|
-
baseLinearRgb,
|
|
1051
|
-
contrast: minCr
|
|
1052
|
-
}).lightness * 100,
|
|
1250
|
+
l: result.lightness * 100,
|
|
1053
1251
|
satFactor
|
|
1054
1252
|
};
|
|
1055
1253
|
}
|
|
@@ -1085,13 +1283,13 @@ function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
|
1085
1283
|
let finalL;
|
|
1086
1284
|
let finalSat;
|
|
1087
1285
|
if (isDark && isRoot) {
|
|
1088
|
-
finalL = mapLightnessDark(lightL, mode);
|
|
1286
|
+
finalL = mapLightnessDark(lightL, mode, isHighContrast, ctx.scaling);
|
|
1089
1287
|
finalSat = mapSaturationDark(satFactor * ctx.saturation / 100, mode);
|
|
1090
1288
|
} else if (isDark && !isRoot) {
|
|
1091
1289
|
finalL = lightL;
|
|
1092
1290
|
finalSat = mapSaturationDark(satFactor * ctx.saturation / 100, mode);
|
|
1093
1291
|
} else if (isRoot) {
|
|
1094
|
-
finalL = mapLightnessLight(lightL, mode);
|
|
1292
|
+
finalL = mapLightnessLight(lightL, mode, isHighContrast, ctx.scaling);
|
|
1095
1293
|
finalSat = satFactor * ctx.saturation / 100;
|
|
1096
1294
|
} else {
|
|
1097
1295
|
finalL = lightL;
|
|
@@ -1189,15 +1387,17 @@ function resolveMixForScheme(def, ctx, isDark, isHighContrast) {
|
|
|
1189
1387
|
alpha: 1
|
|
1190
1388
|
};
|
|
1191
1389
|
}
|
|
1192
|
-
function resolveAllColors(hue, saturation, defs) {
|
|
1193
|
-
validateColorDefs(defs);
|
|
1390
|
+
function resolveAllColors(hue, saturation, defs, scaling, externalBases) {
|
|
1391
|
+
validateColorDefs(defs, externalBases);
|
|
1194
1392
|
const order = topoSort(defs);
|
|
1195
1393
|
const ctx = {
|
|
1196
1394
|
hue,
|
|
1197
1395
|
saturation,
|
|
1198
1396
|
defs,
|
|
1199
|
-
resolved: /* @__PURE__ */ new Map()
|
|
1397
|
+
resolved: /* @__PURE__ */ new Map(),
|
|
1398
|
+
scaling
|
|
1200
1399
|
};
|
|
1400
|
+
if (externalBases) for (const [name, color] of externalBases) ctx.resolved.set(name, color);
|
|
1201
1401
|
function defMode(def) {
|
|
1202
1402
|
if (isShadowDef(def) || isMixDef(def)) return void 0;
|
|
1203
1403
|
return def.mode ?? "auto";
|
|
@@ -1388,10 +1588,14 @@ function createTheme(hue, saturation, initialColors) {
|
|
|
1388
1588
|
};
|
|
1389
1589
|
},
|
|
1390
1590
|
extend(options) {
|
|
1391
|
-
|
|
1392
|
-
|
|
1591
|
+
const newHue = options.hue ?? hue;
|
|
1592
|
+
const newSat = options.saturation ?? saturation;
|
|
1593
|
+
const inheritedColors = {};
|
|
1594
|
+
for (const [name, def] of Object.entries(colorDefs)) if (def.inherit !== false) inheritedColors[name] = def;
|
|
1595
|
+
return createTheme(newHue, newSat, options.colors ? {
|
|
1596
|
+
...inheritedColors,
|
|
1393
1597
|
...options.colors
|
|
1394
|
-
} : { ...
|
|
1598
|
+
} : { ...inheritedColors });
|
|
1395
1599
|
},
|
|
1396
1600
|
resolve() {
|
|
1397
1601
|
return resolveAllColors(hue, saturation, colorDefs);
|
|
@@ -1413,35 +1617,88 @@ function createTheme(hue, saturation, initialColors) {
|
|
|
1413
1617
|
}
|
|
1414
1618
|
};
|
|
1415
1619
|
}
|
|
1416
|
-
function resolvePrefix(options, themeName) {
|
|
1417
|
-
|
|
1418
|
-
if (
|
|
1620
|
+
function resolvePrefix(options, themeName, defaultPrefix = false) {
|
|
1621
|
+
const prefix = options?.prefix ?? defaultPrefix;
|
|
1622
|
+
if (prefix === true) return `${themeName}-`;
|
|
1623
|
+
if (typeof prefix === "object" && prefix !== null) return prefix[themeName] ?? `${themeName}-`;
|
|
1419
1624
|
return "";
|
|
1420
1625
|
}
|
|
1421
|
-
function
|
|
1626
|
+
function validatePrimaryTheme(primary, themes) {
|
|
1627
|
+
if (primary !== void 0 && !(primary in themes)) {
|
|
1628
|
+
const available = Object.keys(themes).join(", ");
|
|
1629
|
+
throw new Error(`glaze: primary theme "${primary}" not found in palette. Available: ${available}.`);
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
/**
|
|
1633
|
+
* Resolve the effective primary for an export call.
|
|
1634
|
+
* `false` disables, a string overrides, `undefined` inherits from palette.
|
|
1635
|
+
*/
|
|
1636
|
+
function resolveEffectivePrimary(exportPrimary, palettePrimary) {
|
|
1637
|
+
if (exportPrimary === false) return void 0;
|
|
1638
|
+
return exportPrimary ?? palettePrimary;
|
|
1639
|
+
}
|
|
1640
|
+
/**
|
|
1641
|
+
* Filter a resolved color map, skipping keys already in `seen`.
|
|
1642
|
+
* Warns on collision and keeps the first-written value (first-write-wins).
|
|
1643
|
+
* Returns a new map containing only non-colliding entries.
|
|
1644
|
+
*/
|
|
1645
|
+
function filterCollisions(resolved, prefix, seen, themeName, isPrimary) {
|
|
1646
|
+
const filtered = /* @__PURE__ */ new Map();
|
|
1647
|
+
const label = isPrimary ? `${themeName} (primary)` : themeName;
|
|
1648
|
+
for (const [name, color] of resolved) {
|
|
1649
|
+
const key = `${prefix}${name}`;
|
|
1650
|
+
if (seen.has(key)) {
|
|
1651
|
+
console.warn(`glaze: token "${key}" from theme "${label}" collides with theme "${seen.get(key)}" — skipping.`);
|
|
1652
|
+
continue;
|
|
1653
|
+
}
|
|
1654
|
+
seen.set(key, label);
|
|
1655
|
+
filtered.set(name, color);
|
|
1656
|
+
}
|
|
1657
|
+
return filtered;
|
|
1658
|
+
}
|
|
1659
|
+
function createPalette(themes, paletteOptions) {
|
|
1660
|
+
validatePrimaryTheme(paletteOptions?.primary, themes);
|
|
1422
1661
|
return {
|
|
1423
1662
|
tokens(options) {
|
|
1663
|
+
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1664
|
+
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1424
1665
|
const modes = resolveModes(options?.modes);
|
|
1425
1666
|
const allTokens = {};
|
|
1667
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1426
1668
|
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1427
|
-
const
|
|
1669
|
+
const resolved = theme.resolve();
|
|
1670
|
+
const prefix = resolvePrefix(options, themeName, true);
|
|
1671
|
+
const tokens = buildFlatTokenMap(filterCollisions(resolved, prefix, seen, themeName), prefix, modes, options?.format);
|
|
1428
1672
|
for (const variant of Object.keys(tokens)) {
|
|
1429
1673
|
if (!allTokens[variant]) allTokens[variant] = {};
|
|
1430
1674
|
Object.assign(allTokens[variant], tokens[variant]);
|
|
1431
1675
|
}
|
|
1676
|
+
if (themeName === effectivePrimary) {
|
|
1677
|
+
const unprefixed = buildFlatTokenMap(filterCollisions(resolved, "", seen, themeName, true), "", modes, options?.format);
|
|
1678
|
+
for (const variant of Object.keys(unprefixed)) Object.assign(allTokens[variant], unprefixed[variant]);
|
|
1679
|
+
}
|
|
1432
1680
|
}
|
|
1433
1681
|
return allTokens;
|
|
1434
1682
|
},
|
|
1435
1683
|
tasty(options) {
|
|
1684
|
+
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1685
|
+
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1436
1686
|
const states = {
|
|
1437
1687
|
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1438
1688
|
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1439
1689
|
};
|
|
1440
1690
|
const modes = resolveModes(options?.modes);
|
|
1441
1691
|
const allTokens = {};
|
|
1692
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1442
1693
|
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1443
|
-
const
|
|
1694
|
+
const resolved = theme.resolve();
|
|
1695
|
+
const prefix = resolvePrefix(options, themeName, true);
|
|
1696
|
+
const tokens = buildTokenMap(filterCollisions(resolved, prefix, seen, themeName), prefix, states, modes, options?.format);
|
|
1444
1697
|
Object.assign(allTokens, tokens);
|
|
1698
|
+
if (themeName === effectivePrimary) {
|
|
1699
|
+
const unprefixed = buildTokenMap(filterCollisions(resolved, "", seen, themeName, true), "", states, modes, options?.format);
|
|
1700
|
+
Object.assign(allTokens, unprefixed);
|
|
1701
|
+
}
|
|
1445
1702
|
}
|
|
1446
1703
|
return allTokens;
|
|
1447
1704
|
},
|
|
@@ -1452,6 +1709,8 @@ function createPalette(themes) {
|
|
|
1452
1709
|
return result;
|
|
1453
1710
|
},
|
|
1454
1711
|
css(options) {
|
|
1712
|
+
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1713
|
+
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1455
1714
|
const suffix = options?.suffix ?? "-color";
|
|
1456
1715
|
const format = options?.format ?? "rgb";
|
|
1457
1716
|
const allLines = {
|
|
@@ -1460,14 +1719,26 @@ function createPalette(themes) {
|
|
|
1460
1719
|
lightContrast: [],
|
|
1461
1720
|
darkContrast: []
|
|
1462
1721
|
};
|
|
1722
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1463
1723
|
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1464
|
-
const
|
|
1724
|
+
const resolved = theme.resolve();
|
|
1725
|
+
const prefix = resolvePrefix(options, themeName, true);
|
|
1726
|
+
const css = buildCssMap(filterCollisions(resolved, prefix, seen, themeName), prefix, suffix, format);
|
|
1465
1727
|
for (const key of [
|
|
1466
1728
|
"light",
|
|
1467
1729
|
"dark",
|
|
1468
1730
|
"lightContrast",
|
|
1469
1731
|
"darkContrast"
|
|
1470
1732
|
]) if (css[key]) allLines[key].push(css[key]);
|
|
1733
|
+
if (themeName === effectivePrimary) {
|
|
1734
|
+
const unprefixed = buildCssMap(filterCollisions(resolved, "", seen, themeName, true), "", suffix, format);
|
|
1735
|
+
for (const key of [
|
|
1736
|
+
"light",
|
|
1737
|
+
"dark",
|
|
1738
|
+
"lightContrast",
|
|
1739
|
+
"darkContrast"
|
|
1740
|
+
]) if (unprefixed[key]) allLines[key].push(unprefixed[key]);
|
|
1741
|
+
}
|
|
1471
1742
|
}
|
|
1472
1743
|
return {
|
|
1473
1744
|
light: allLines.light.join("\n"),
|
|
@@ -1478,32 +1749,406 @@ function createPalette(themes) {
|
|
|
1478
1749
|
}
|
|
1479
1750
|
};
|
|
1480
1751
|
}
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1752
|
+
/**
|
|
1753
|
+
* Matches the CSS color functions Glaze itself emits (`rgb()`, `hsl()`,
|
|
1754
|
+
* `okhsl()`, `oklch()`) plus their legacy alpha aliases (`rgba()`, `hsla()`).
|
|
1755
|
+
*
|
|
1756
|
+
* Only bare numeric components are supported. Named colors (`red`),
|
|
1757
|
+
* relative-color syntax (`from <color> ...`), and angle units other
|
|
1758
|
+
* than bare degrees (`deg` is the only suffix tolerated by `parseFloat`)
|
|
1759
|
+
* are out of scope.
|
|
1760
|
+
*/
|
|
1761
|
+
const COLOR_FN_RE = /^(rgba?|hsla?|okhsl|oklch)\(\s*([^)]*)\s*\)$/i;
|
|
1762
|
+
function parseNumberOrPercent(raw, percentScale) {
|
|
1763
|
+
if (raw.endsWith("%")) return parseFloat(raw) / 100 * percentScale;
|
|
1764
|
+
return parseFloat(raw);
|
|
1765
|
+
}
|
|
1766
|
+
/**
|
|
1767
|
+
* Split the body of a CSS color function into its components and detect
|
|
1768
|
+
* whether an alpha channel was present.
|
|
1769
|
+
*
|
|
1770
|
+
* Handles both modern slash syntax (`R G B / A` or `R, G, B / A`) and
|
|
1771
|
+
* legacy comma syntax (`R, G, B, A`). The alpha value itself is discarded
|
|
1772
|
+
* by the caller — standalone Glaze colors have no opacity field.
|
|
1773
|
+
*/
|
|
1774
|
+
function splitColorBody(body) {
|
|
1775
|
+
const slashIdx = body.indexOf("/");
|
|
1776
|
+
if (slashIdx !== -1) return {
|
|
1777
|
+
components: body.slice(0, slashIdx).trim().split(/[\s,]+/).filter(Boolean),
|
|
1778
|
+
hadAlpha: body.slice(slashIdx + 1).trim().length > 0
|
|
1779
|
+
};
|
|
1780
|
+
const components = body.split(/[\s,]+/).filter(Boolean);
|
|
1781
|
+
if (components.length === 4) {
|
|
1782
|
+
components.pop();
|
|
1783
|
+
return {
|
|
1784
|
+
components,
|
|
1785
|
+
hadAlpha: true
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1788
|
+
return {
|
|
1789
|
+
components,
|
|
1790
|
+
hadAlpha: false
|
|
1791
|
+
};
|
|
1792
|
+
}
|
|
1793
|
+
function warnDroppedAlpha(input) {
|
|
1794
|
+
console.warn(`glaze: alpha component dropped from "${input}" (standalone color has no opacity field).`);
|
|
1795
|
+
}
|
|
1796
|
+
function parseColorString(input) {
|
|
1797
|
+
if (input.startsWith("#")) {
|
|
1798
|
+
const parsed = parseHexAlpha(input);
|
|
1799
|
+
if (!parsed) throw new Error(`glaze: invalid hex color "${input}".`);
|
|
1800
|
+
if (parsed.alpha !== void 0) warnDroppedAlpha(input);
|
|
1801
|
+
const [h, s, l] = srgbToOkhsl(parsed.rgb);
|
|
1802
|
+
return {
|
|
1803
|
+
h,
|
|
1804
|
+
s,
|
|
1805
|
+
l
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1808
|
+
const m = input.match(COLOR_FN_RE);
|
|
1809
|
+
if (!m) throw new Error(`glaze: unsupported color string "${input}".`);
|
|
1810
|
+
const fn = m[1].toLowerCase();
|
|
1811
|
+
const { components, hadAlpha } = splitColorBody(m[2].trim());
|
|
1812
|
+
if (hadAlpha) warnDroppedAlpha(input);
|
|
1813
|
+
if (components.length !== 3) throw new Error(`glaze: expected 3 components in "${input}".`);
|
|
1814
|
+
switch (fn) {
|
|
1815
|
+
case "rgb":
|
|
1816
|
+
case "rgba": {
|
|
1817
|
+
const [h, s, l] = srgbToOkhsl([
|
|
1818
|
+
parseNumberOrPercent(components[0], 255) / 255,
|
|
1819
|
+
parseNumberOrPercent(components[1], 255) / 255,
|
|
1820
|
+
parseNumberOrPercent(components[2], 255) / 255
|
|
1821
|
+
]);
|
|
1822
|
+
return {
|
|
1823
|
+
h,
|
|
1824
|
+
s,
|
|
1825
|
+
l
|
|
1826
|
+
};
|
|
1827
|
+
}
|
|
1828
|
+
case "hsl":
|
|
1829
|
+
case "hsla": {
|
|
1830
|
+
const [oh, os, ol] = srgbToOkhsl(hslToSrgb(parseFloat(components[0]), parseNumberOrPercent(components[1], 1), parseNumberOrPercent(components[2], 1)));
|
|
1831
|
+
return {
|
|
1832
|
+
h: oh,
|
|
1833
|
+
s: os,
|
|
1834
|
+
l: ol
|
|
1835
|
+
};
|
|
1836
|
+
}
|
|
1837
|
+
case "okhsl": return {
|
|
1838
|
+
h: parseFloat(components[0]),
|
|
1839
|
+
s: parseNumberOrPercent(components[1], 1),
|
|
1840
|
+
l: parseNumberOrPercent(components[2], 1)
|
|
1841
|
+
};
|
|
1842
|
+
case "oklch": {
|
|
1843
|
+
const L = parseNumberOrPercent(components[0], 1);
|
|
1844
|
+
const C = parseNumberOrPercent(components[1], .4);
|
|
1845
|
+
const hRad = parseFloat(components[2]) * Math.PI / 180;
|
|
1846
|
+
const [h, s, l] = oklabToOkhsl([
|
|
1847
|
+
L,
|
|
1848
|
+
C * Math.cos(hRad),
|
|
1849
|
+
C * Math.sin(hRad)
|
|
1850
|
+
]);
|
|
1851
|
+
return {
|
|
1852
|
+
h,
|
|
1853
|
+
s,
|
|
1854
|
+
l
|
|
1855
|
+
};
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
throw new Error(`glaze: unsupported color function "${fn}".`);
|
|
1859
|
+
}
|
|
1860
|
+
/**
|
|
1861
|
+
* Validate a user-supplied `OkhslColor`. Catches the common 0-100 vs 0-1
|
|
1862
|
+
* confusion (the structured form uses 0-100, OKHSL objects use 0-1).
|
|
1863
|
+
*/
|
|
1864
|
+
function validateOkhslColor(value) {
|
|
1865
|
+
const { h, s, l } = value;
|
|
1866
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) throw new Error("glaze.color: OkhslColor h/s/l must be finite numbers.");
|
|
1867
|
+
if (s > 1.5 || l > 1.5) throw new Error("glaze.color: OkhslColor s/l must be in 0–1 range. Did you mean the structured form { hue, saturation, lightness } (which uses 0–100)?");
|
|
1868
|
+
}
|
|
1869
|
+
/**
|
|
1870
|
+
* Validate a user-supplied `[r, g, b]` tuple in 0-255.
|
|
1871
|
+
*/
|
|
1872
|
+
function validateRgbTuple(value) {
|
|
1873
|
+
for (const n of value) if (!Number.isFinite(n) || n < 0 || n > 255) throw new Error(`glaze.color: RGB tuple components must be finite numbers in 0–255 (got [${value.join(", ")}]).`);
|
|
1874
|
+
}
|
|
1875
|
+
/**
|
|
1876
|
+
* Validate a user-supplied `opacity` override on `glaze.color()`.
|
|
1877
|
+
* Must be a finite number in `0..=1`.
|
|
1878
|
+
*/
|
|
1879
|
+
function validateStandaloneOpacity(value) {
|
|
1880
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error(`glaze.color: opacity must be a finite number in 0–1 (got ${value}).`);
|
|
1881
|
+
}
|
|
1882
|
+
/**
|
|
1883
|
+
* Validate a structured `GlazeColorInput`. Range-checks the `hue` /
|
|
1884
|
+
* `saturation` / `lightness` numerics (and any HC-pair second value)
|
|
1885
|
+
* before the resolver sees them so out-of-range or non-finite inputs
|
|
1886
|
+
* fail with a helpful, top-level error rather than producing a
|
|
1887
|
+
* NaN-laden token. `opacity` is checked here too so all input
|
|
1888
|
+
* validation lives in one place.
|
|
1889
|
+
*/
|
|
1890
|
+
function validateStructuredInput(input) {
|
|
1891
|
+
if (!Number.isFinite(input.hue)) throw new Error(`glaze.color: structured hue must be a finite number (got ${input.hue}).`);
|
|
1892
|
+
if (!Number.isFinite(input.saturation) || input.saturation < 0 || input.saturation > 100) throw new Error(`glaze.color: structured saturation must be a finite number in 0–100 (got ${input.saturation}).`);
|
|
1893
|
+
const checkLightness = (value, label) => {
|
|
1894
|
+
if (!Number.isFinite(value) || value < 0 || value > 100) throw new Error(`glaze.color: structured ${label} must be a finite number in 0–100 (got ${value}).`);
|
|
1895
|
+
};
|
|
1896
|
+
if (Array.isArray(input.lightness)) {
|
|
1897
|
+
checkLightness(input.lightness[0], "lightness[normal]");
|
|
1898
|
+
checkLightness(input.lightness[1], "lightness[hc]");
|
|
1899
|
+
} else checkLightness(input.lightness, "lightness");
|
|
1900
|
+
if (input.saturationFactor !== void 0) {
|
|
1901
|
+
if (!Number.isFinite(input.saturationFactor) || input.saturationFactor < 0 || input.saturationFactor > 1) throw new Error(`glaze.color: structured saturationFactor must be a finite number in 0–1 (got ${input.saturationFactor}).`);
|
|
1902
|
+
}
|
|
1903
|
+
if (input.opacity !== void 0) validateStandaloneOpacity(input.opacity);
|
|
1904
|
+
}
|
|
1905
|
+
/**
|
|
1906
|
+
* Validate a user-supplied `name` override. Rejects empty / whitespace-only
|
|
1907
|
+
* strings and names colliding with `glaze`'s reserved internal sentinels.
|
|
1908
|
+
*/
|
|
1909
|
+
function validateStandaloneName(name) {
|
|
1910
|
+
if (typeof name !== "string" || name.trim() === "") throw new Error("glaze.color: name must be a non-empty string. Omit `name` if you do not want to set a debug label.");
|
|
1911
|
+
if (RESERVED_STANDALONE_NAMES.has(name)) {
|
|
1912
|
+
const reserved = [...RESERVED_STANDALONE_NAMES].map((n) => `"${n}"`).join(", ");
|
|
1913
|
+
throw new Error(`glaze.color: name "${name}" is reserved (used internally). Reserved names are: ${reserved}. Pick a different name.`);
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
/**
|
|
1917
|
+
* Extract an OKHSL color from any `GlazeColorValue` form. Also used by
|
|
1918
|
+
* `glaze.shadow()` so all shadow inputs (hex, color functions, OKHSL,
|
|
1919
|
+
* RGB tuple) go through one parser.
|
|
1920
|
+
*/
|
|
1921
|
+
function extractOkhslFromValue(value) {
|
|
1922
|
+
if (typeof value === "string") return parseColorString(value);
|
|
1923
|
+
if (Array.isArray(value)) {
|
|
1924
|
+
const tuple = value;
|
|
1925
|
+
validateRgbTuple(tuple);
|
|
1926
|
+
const [r, g, b] = tuple;
|
|
1927
|
+
const [h, s, l] = srgbToOkhsl([
|
|
1928
|
+
r / 255,
|
|
1929
|
+
g / 255,
|
|
1930
|
+
b / 255
|
|
1931
|
+
]);
|
|
1932
|
+
return {
|
|
1933
|
+
h,
|
|
1934
|
+
s,
|
|
1935
|
+
l
|
|
1936
|
+
};
|
|
1937
|
+
}
|
|
1938
|
+
validateOkhslColor(value);
|
|
1939
|
+
return value;
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Build the `ColorMap` for a value-shorthand `glaze.color()` call.
|
|
1943
|
+
*
|
|
1944
|
+
* The user-facing color (`STANDALONE_VALUE`) defaults to `mode: 'auto'`
|
|
1945
|
+
* for string inputs (Möbius-inverted dark variant — pairs with the
|
|
1946
|
+
* extended dark window so a totally-black input renders as totally-white
|
|
1947
|
+
* in dark mode) and `mode: 'fixed'` for `OkhslColor` / RGB-tuple inputs
|
|
1948
|
+
* (linear, no inversion).
|
|
1949
|
+
*
|
|
1950
|
+
* When the user requests `contrast` or relative `lightness`, a hidden
|
|
1951
|
+
* `STANDALONE_SEED` def is synthesized at `mode: 'static'`. That keeps
|
|
1952
|
+
* the seed pinned to the literal user-provided color across all four
|
|
1953
|
+
* variants, so the contrast solver always anchors against it.
|
|
1954
|
+
*/
|
|
1955
|
+
function buildStandaloneValueDefs(main, options, inputIsString) {
|
|
1956
|
+
const seedHue = typeof options?.hue === "number" ? options.hue : main.h;
|
|
1957
|
+
const seedSaturation = options?.saturation ?? main.s * 100;
|
|
1958
|
+
const relativeHue = typeof options?.hue === "string" ? options.hue : void 0;
|
|
1959
|
+
const lightnessOption = options?.lightness;
|
|
1960
|
+
const hasExternalBase = options?.base !== void 0;
|
|
1961
|
+
const needsSeedAnchor = !hasExternalBase && (options?.contrast !== void 0 || lightnessOption !== void 0 && !isAbsoluteLightness(lightnessOption));
|
|
1962
|
+
if (options?.opacity !== void 0) validateStandaloneOpacity(options.opacity);
|
|
1963
|
+
const userName = options?.name;
|
|
1964
|
+
if (userName !== void 0) validateStandaloneName(userName);
|
|
1965
|
+
const primary = userName ?? STANDALONE_VALUE;
|
|
1966
|
+
const valueDef = {
|
|
1967
|
+
hue: relativeHue,
|
|
1968
|
+
saturation: options?.saturationFactor,
|
|
1969
|
+
lightness: lightnessOption ?? main.l * 100,
|
|
1970
|
+
contrast: options?.contrast,
|
|
1971
|
+
mode: options?.mode ?? (inputIsString ? "auto" : "fixed"),
|
|
1972
|
+
opacity: options?.opacity,
|
|
1973
|
+
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
1974
|
+
};
|
|
1975
|
+
const defs = { [primary]: valueDef };
|
|
1976
|
+
if (needsSeedAnchor) defs[STANDALONE_SEED] = {
|
|
1977
|
+
hue: main.h,
|
|
1978
|
+
saturation: 1,
|
|
1979
|
+
lightness: main.l * 100,
|
|
1980
|
+
mode: "static"
|
|
1981
|
+
};
|
|
1982
|
+
return {
|
|
1983
|
+
seedHue,
|
|
1984
|
+
seedSaturation,
|
|
1985
|
+
defs,
|
|
1986
|
+
primary
|
|
1987
|
+
};
|
|
1988
|
+
}
|
|
1989
|
+
function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveScaling, baseToken, exportData) {
|
|
1990
|
+
let cached;
|
|
1991
|
+
const resolveOnce = () => {
|
|
1992
|
+
if (cached) return cached;
|
|
1993
|
+
cached = resolveAllColors(seedHue, seedSaturation, defs, effectiveScaling, baseToken ? new Map([[STANDALONE_BASE, baseToken.resolve()]]) : void 0);
|
|
1994
|
+
return cached;
|
|
1995
|
+
};
|
|
1996
|
+
const resolveStates = (options) => ({
|
|
1997
|
+
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1998
|
+
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1999
|
+
});
|
|
2000
|
+
const tokenLike = (options) => {
|
|
2001
|
+
return buildTokenMap(resolveOnce(), "", resolveStates(options), resolveModes(options?.modes), options?.format)[`#${primary}`];
|
|
2002
|
+
};
|
|
1487
2003
|
return {
|
|
1488
2004
|
resolve() {
|
|
1489
|
-
return
|
|
2005
|
+
return resolveOnce().get(primary);
|
|
1490
2006
|
},
|
|
1491
|
-
token
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
}, resolveModes(options?.modes), options?.format)["#__color__"];
|
|
2007
|
+
token: tokenLike,
|
|
2008
|
+
tasty: tokenLike,
|
|
2009
|
+
json(options) {
|
|
2010
|
+
return buildJsonMap(resolveOnce(), resolveModes(options?.modes), options?.format)[primary];
|
|
1496
2011
|
},
|
|
1497
|
-
|
|
1498
|
-
return
|
|
1499
|
-
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1500
|
-
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1501
|
-
}, resolveModes(options?.modes), options?.format)["#__color__"];
|
|
2012
|
+
css(options) {
|
|
2013
|
+
return buildCssMap(new Map([[options.name, resolveOnce().get(primary)]]), "", options.suffix ?? "-color", options.format ?? "rgb");
|
|
1502
2014
|
},
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
2015
|
+
export: exportData
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Resolve `base` (which may be a token reference or a raw color value)
|
|
2020
|
+
* into a `GlazeColorToken`. Raw values are auto-wrapped via
|
|
2021
|
+
* `glaze.color(value)` so they pick up the same auto-invert defaults as
|
|
2022
|
+
* an explicit wrap. Returns `undefined` when no base is provided.
|
|
2023
|
+
*/
|
|
2024
|
+
function resolveBaseToken(base) {
|
|
2025
|
+
if (base === void 0) return void 0;
|
|
2026
|
+
if (isGlazeColorToken(base)) return base;
|
|
2027
|
+
return createColorTokenFromValue(base, void 0, void 0);
|
|
2028
|
+
}
|
|
2029
|
+
/**
|
|
2030
|
+
* Build a JSON-safe snapshot of `GlazeColorOverrides`. `base` is
|
|
2031
|
+
* recursively serialized when it was originally a token; raw values are
|
|
2032
|
+
* preserved as-is so `glaze.colorFrom(...)` round-trips them.
|
|
2033
|
+
*/
|
|
2034
|
+
function buildOverridesExport(options) {
|
|
2035
|
+
const out = {};
|
|
2036
|
+
if (options.hue !== void 0) out.hue = options.hue;
|
|
2037
|
+
if (options.saturation !== void 0) out.saturation = options.saturation;
|
|
2038
|
+
if (options.lightness !== void 0) out.lightness = options.lightness;
|
|
2039
|
+
if (options.saturationFactor !== void 0) out.saturationFactor = options.saturationFactor;
|
|
2040
|
+
if (options.mode !== void 0) out.mode = options.mode;
|
|
2041
|
+
if (options.contrast !== void 0) out.contrast = options.contrast;
|
|
2042
|
+
if (options.opacity !== void 0) out.opacity = options.opacity;
|
|
2043
|
+
if (options.name !== void 0) out.name = options.name;
|
|
2044
|
+
if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export() : options.base;
|
|
2045
|
+
return out;
|
|
2046
|
+
}
|
|
2047
|
+
function buildStructuredInputExport(input) {
|
|
2048
|
+
const out = {
|
|
2049
|
+
hue: input.hue,
|
|
2050
|
+
saturation: input.saturation,
|
|
2051
|
+
lightness: input.lightness
|
|
2052
|
+
};
|
|
2053
|
+
if (input.saturationFactor !== void 0) out.saturationFactor = input.saturationFactor;
|
|
2054
|
+
if (input.mode !== void 0) out.mode = input.mode;
|
|
2055
|
+
if (input.opacity !== void 0) out.opacity = input.opacity;
|
|
2056
|
+
if (input.contrast !== void 0) out.contrast = input.contrast;
|
|
2057
|
+
if (input.name !== void 0) out.name = input.name;
|
|
2058
|
+
if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export() : input.base;
|
|
2059
|
+
return out;
|
|
2060
|
+
}
|
|
2061
|
+
function createColorToken(input, scaling) {
|
|
2062
|
+
validateStructuredInput(input);
|
|
2063
|
+
const userName = input.name;
|
|
2064
|
+
if (userName !== void 0) validateStandaloneName(userName);
|
|
2065
|
+
const primary = userName ?? STANDALONE_VALUE;
|
|
2066
|
+
const baseToken = resolveBaseToken(input.base);
|
|
2067
|
+
const hasExternalBase = baseToken !== void 0;
|
|
2068
|
+
const needsSeedAnchor = !hasExternalBase && input.contrast !== void 0;
|
|
2069
|
+
const defs = { [primary]: {
|
|
2070
|
+
lightness: input.lightness,
|
|
2071
|
+
saturation: input.saturationFactor,
|
|
2072
|
+
mode: input.mode ?? "fixed",
|
|
2073
|
+
contrast: input.contrast,
|
|
2074
|
+
opacity: input.opacity,
|
|
2075
|
+
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
2076
|
+
} };
|
|
2077
|
+
if (needsSeedAnchor) defs[STANDALONE_SEED] = {
|
|
2078
|
+
lightness: pairNormal(input.lightness),
|
|
2079
|
+
saturation: 1,
|
|
2080
|
+
mode: "static"
|
|
1506
2081
|
};
|
|
2082
|
+
const effectiveScaling = scaling ?? defaultStandaloneScaling(false);
|
|
2083
|
+
const exportData = () => ({
|
|
2084
|
+
form: "structured",
|
|
2085
|
+
input: buildStructuredInputExport(input),
|
|
2086
|
+
scaling: effectiveScaling
|
|
2087
|
+
});
|
|
2088
|
+
return createColorTokenFromDefs(input.hue, input.saturation, defs, primary, effectiveScaling, baseToken, exportData);
|
|
2089
|
+
}
|
|
2090
|
+
function createColorTokenFromValue(value, options, scaling) {
|
|
2091
|
+
const inputIsString = typeof value === "string";
|
|
2092
|
+
const main = extractOkhslFromValue(value);
|
|
2093
|
+
const baseToken = resolveBaseToken(options?.base);
|
|
2094
|
+
const { seedHue, seedSaturation, defs, primary } = buildStandaloneValueDefs(main, options, inputIsString);
|
|
2095
|
+
const effectiveScaling = scaling ?? defaultStandaloneScaling(inputIsString);
|
|
2096
|
+
const exportData = () => ({
|
|
2097
|
+
form: "value",
|
|
2098
|
+
input: value,
|
|
2099
|
+
...options !== void 0 ? { overrides: buildOverridesExport(options) } : {},
|
|
2100
|
+
scaling: effectiveScaling
|
|
2101
|
+
});
|
|
2102
|
+
return createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveScaling, baseToken, exportData);
|
|
2103
|
+
}
|
|
2104
|
+
/**
|
|
2105
|
+
* Rehydrate a token from its `.export()` snapshot. Recursively rebuilds
|
|
2106
|
+
* any base dependency. Inverse of `GlazeColorToken.export()`.
|
|
2107
|
+
*/
|
|
2108
|
+
function colorFromExport(data) {
|
|
2109
|
+
if (data === null || typeof data !== "object") throw new Error(`glaze.colorFrom: expected an object from token.export(), got ${data === null ? "null" : typeof data}.`);
|
|
2110
|
+
if (data.form !== "value" && data.form !== "structured") throw new Error(`glaze.colorFrom: invalid "form" field — expected "value" or "structured" (got ${JSON.stringify(data.form)}).`);
|
|
2111
|
+
if (data.input === void 0) throw new Error(`glaze.colorFrom: missing "input" field — expected the original ${data.form === "value" ? "GlazeColorValue" : "GlazeColorInput"}.`);
|
|
2112
|
+
if (data.form === "value") {
|
|
2113
|
+
const value = data.input;
|
|
2114
|
+
return createColorTokenFromValue(value, data.overrides ? rehydrateOverrides(data.overrides) : void 0, data.scaling);
|
|
2115
|
+
}
|
|
2116
|
+
return createColorToken(rehydrateStructuredInput(data.input), data.scaling);
|
|
2117
|
+
}
|
|
2118
|
+
function rehydrateOverrides(data) {
|
|
2119
|
+
const out = {};
|
|
2120
|
+
if (data.hue !== void 0) out.hue = data.hue;
|
|
2121
|
+
if (data.saturation !== void 0) out.saturation = data.saturation;
|
|
2122
|
+
if (data.lightness !== void 0) out.lightness = data.lightness;
|
|
2123
|
+
if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
|
|
2124
|
+
if (data.mode !== void 0) out.mode = data.mode;
|
|
2125
|
+
if (data.contrast !== void 0) out.contrast = data.contrast;
|
|
2126
|
+
if (data.opacity !== void 0) out.opacity = data.opacity;
|
|
2127
|
+
if (data.name !== void 0) out.name = data.name;
|
|
2128
|
+
if (data.base !== void 0) out.base = isExportedToken(data.base) ? colorFromExport(data.base) : data.base;
|
|
2129
|
+
return out;
|
|
2130
|
+
}
|
|
2131
|
+
function rehydrateStructuredInput(data) {
|
|
2132
|
+
const out = {
|
|
2133
|
+
hue: data.hue,
|
|
2134
|
+
saturation: data.saturation,
|
|
2135
|
+
lightness: data.lightness
|
|
2136
|
+
};
|
|
2137
|
+
if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
|
|
2138
|
+
if (data.mode !== void 0) out.mode = data.mode;
|
|
2139
|
+
if (data.opacity !== void 0) out.opacity = data.opacity;
|
|
2140
|
+
if (data.contrast !== void 0) out.contrast = data.contrast;
|
|
2141
|
+
if (data.name !== void 0) out.name = data.name;
|
|
2142
|
+
if (data.base !== void 0) out.base = isExportedToken(data.base) ? colorFromExport(data.base) : data.base;
|
|
2143
|
+
return out;
|
|
2144
|
+
}
|
|
2145
|
+
/**
|
|
2146
|
+
* Discriminate a `GlazeColorTokenExport` from a raw `GlazeColorValue`.
|
|
2147
|
+
* `GlazeColorTokenExport` always has a `form` field set to either
|
|
2148
|
+
* `'value'` or `'structured'`; raw values never do.
|
|
2149
|
+
*/
|
|
2150
|
+
function isExportedToken(candidate) {
|
|
2151
|
+
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "form" in candidate && (candidate.form === "value" || candidate.form === "structured");
|
|
1507
2152
|
}
|
|
1508
2153
|
/**
|
|
1509
2154
|
* Create a single-hue glaze theme.
|
|
@@ -1527,6 +2172,7 @@ glaze.configure = function configure(config) {
|
|
|
1527
2172
|
lightLightness: config.lightLightness ?? globalConfig.lightLightness,
|
|
1528
2173
|
darkLightness: config.darkLightness ?? globalConfig.darkLightness,
|
|
1529
2174
|
darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
|
|
2175
|
+
darkCurve: config.darkCurve ?? globalConfig.darkCurve,
|
|
1530
2176
|
states: {
|
|
1531
2177
|
dark: config.states?.dark ?? globalConfig.states.dark,
|
|
1532
2178
|
highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
|
|
@@ -1541,8 +2187,8 @@ glaze.configure = function configure(config) {
|
|
|
1541
2187
|
/**
|
|
1542
2188
|
* Compose multiple themes into a palette.
|
|
1543
2189
|
*/
|
|
1544
|
-
glaze.palette = function palette(themes) {
|
|
1545
|
-
return createPalette(themes);
|
|
2190
|
+
glaze.palette = function palette(themes, options) {
|
|
2191
|
+
return createPalette(themes, options);
|
|
1546
2192
|
};
|
|
1547
2193
|
/**
|
|
1548
2194
|
* Create a theme from a serialized export.
|
|
@@ -1550,18 +2196,57 @@ glaze.palette = function palette(themes) {
|
|
|
1550
2196
|
glaze.from = function from(data) {
|
|
1551
2197
|
return createTheme(data.hue, data.saturation, data.colors);
|
|
1552
2198
|
};
|
|
2199
|
+
function isStructuredColorInput(input) {
|
|
2200
|
+
return typeof input === "object" && input !== null && !Array.isArray(input) && "hue" in input && "lightness" in input;
|
|
2201
|
+
}
|
|
1553
2202
|
/**
|
|
1554
2203
|
* Create a standalone single-color token.
|
|
2204
|
+
*
|
|
2205
|
+
* Two overloads:
|
|
2206
|
+
* - `glaze.color(input, scaling?)` — structured form:
|
|
2207
|
+
* `{ hue, saturation, lightness, ... }` plus an optional per-call
|
|
2208
|
+
* lightness-window override.
|
|
2209
|
+
* - `glaze.color(value, overrides?, scaling?)` — value-shorthand: a hex
|
|
2210
|
+
* string (3/6/8 digits), one of the CSS color functions Glaze itself
|
|
2211
|
+
* emits (`rgb()`, `hsl()`, `okhsl()`, `oklch()`), an `OkhslColor`
|
|
2212
|
+
* object `{ h, s, l }` (0–1 ranges), or an `[r, g, b]` (0–255) tuple.
|
|
2213
|
+
*
|
|
2214
|
+
* Defaults vary by input form:
|
|
2215
|
+
* - String value-shorthand: `mode: 'auto'` with snapshotted scaling
|
|
2216
|
+
* `{ lightLightness: false, darkLightness: [globalConfig.darkLightness[0], 100] }`.
|
|
2217
|
+
* Light preserves the input exactly; dark Möbius-inverts up to 100, so
|
|
2218
|
+
* `glaze.color('#000')` renders as `#fff` in dark mode (and
|
|
2219
|
+
* `glaze.color('#fff')` falls to the dark `lo` floor).
|
|
2220
|
+
* - `OkhslColor` object / RGB-tuple value-shorthand: `mode: 'fixed'`
|
|
2221
|
+
* with `scaling: { lightLightness: false }` — light preserves the
|
|
2222
|
+
* input; dark linearly maps into `globalConfig.darkLightness`.
|
|
2223
|
+
* - Structured form (`{ hue, saturation, lightness, ... }`):
|
|
2224
|
+
* `mode: 'fixed'`; both windows come from `globalConfig`.
|
|
2225
|
+
*
|
|
2226
|
+
* Relative `lightness: '+N'` and `contrast: <ratio>` are anchored to
|
|
2227
|
+
* the literal seed (the value passed in) by default, pinned at
|
|
2228
|
+
* `mode: 'static'` across all four variants. Pass `overrides.base` (a
|
|
2229
|
+
* `GlazeColorToken`) to anchor `contrast` and relative `lightness`
|
|
2230
|
+
* against another color's resolved variant per scheme instead. Relative
|
|
2231
|
+
* `hue: '+N'` always anchors to the seed.
|
|
2232
|
+
*
|
|
2233
|
+
* Alpha components in `rgba()` / `hsla()` / slash-alpha syntax and
|
|
2234
|
+
* 8-digit hex are parsed but dropped with a `console.warn`.
|
|
1555
2235
|
*/
|
|
1556
|
-
glaze.color = function color(input) {
|
|
1557
|
-
return createColorToken(input);
|
|
2236
|
+
glaze.color = function color(input, arg2, arg3) {
|
|
2237
|
+
if (isStructuredColorInput(input)) return createColorToken(input, arg2);
|
|
2238
|
+
return createColorTokenFromValue(input, arg2, arg3);
|
|
1558
2239
|
};
|
|
1559
2240
|
/**
|
|
1560
2241
|
* Compute a shadow color from a bg/fg pair and intensity.
|
|
2242
|
+
*
|
|
2243
|
+
* Both `bg` and `fg` accept any `GlazeColorValue` form: hex (`#rgb` /
|
|
2244
|
+
* `#rrggbb` / `#rrggbbaa`), `rgb()` / `hsl()` / `okhsl()` / `oklch()`
|
|
2245
|
+
* strings, `OkhslColor` objects, or `[r, g, b]` (0–255) tuples.
|
|
1561
2246
|
*/
|
|
1562
2247
|
glaze.shadow = function shadow(input) {
|
|
1563
|
-
const bg =
|
|
1564
|
-
const fg = input.fg ?
|
|
2248
|
+
const bg = extractOkhslFromValue(input.bg);
|
|
2249
|
+
const fg = input.fg ? extractOkhslFromValue(input.fg) : void 0;
|
|
1565
2250
|
const tuning = resolveShadowTuning(input.tuning);
|
|
1566
2251
|
return computeShadow({
|
|
1567
2252
|
...bg,
|
|
@@ -1577,19 +2262,6 @@ glaze.shadow = function shadow(input) {
|
|
|
1577
2262
|
glaze.format = function format(variant, colorFormat) {
|
|
1578
2263
|
return formatVariant(variant, colorFormat);
|
|
1579
2264
|
};
|
|
1580
|
-
function parseOkhslInput(input) {
|
|
1581
|
-
if (typeof input === "string") {
|
|
1582
|
-
const rgb = parseHex(input);
|
|
1583
|
-
if (!rgb) throw new Error(`glaze: invalid hex color "${input}".`);
|
|
1584
|
-
const [h, s, l] = srgbToOkhsl(rgb);
|
|
1585
|
-
return {
|
|
1586
|
-
h,
|
|
1587
|
-
s,
|
|
1588
|
-
l
|
|
1589
|
-
};
|
|
1590
|
-
}
|
|
1591
|
-
return input;
|
|
1592
|
-
}
|
|
1593
2265
|
/**
|
|
1594
2266
|
* Create a theme from a hex color string.
|
|
1595
2267
|
* Extracts hue and saturation from the color.
|
|
@@ -1613,6 +2285,26 @@ glaze.fromRgb = function fromRgb(r, g, b) {
|
|
|
1613
2285
|
return createTheme(h, s * 100);
|
|
1614
2286
|
};
|
|
1615
2287
|
/**
|
|
2288
|
+
* Rehydrate a `glaze.color()` token from a `.export()` snapshot.
|
|
2289
|
+
*
|
|
2290
|
+
* The snapshot is a plain JSON-safe object containing the original
|
|
2291
|
+
* input value, overrides (with any `base` token recursively serialized),
|
|
2292
|
+
* and the captured scaling. The reconstructed token is identical in
|
|
2293
|
+
* behavior to the original at the time of export.
|
|
2294
|
+
*
|
|
2295
|
+
* @example
|
|
2296
|
+
* ```ts
|
|
2297
|
+
* const text = glaze.color('#1a1a1a', { contrast: 'AA' });
|
|
2298
|
+
* const data = text.export(); // JSON-safe
|
|
2299
|
+
* localStorage.setItem('text', JSON.stringify(data));
|
|
2300
|
+
* // ...later...
|
|
2301
|
+
* const restored = glaze.colorFrom(JSON.parse(localStorage.getItem('text')!));
|
|
2302
|
+
* ```
|
|
2303
|
+
*/
|
|
2304
|
+
glaze.colorFrom = function colorFrom(data) {
|
|
2305
|
+
return colorFromExport(data);
|
|
2306
|
+
};
|
|
2307
|
+
/**
|
|
1616
2308
|
* Get the current global configuration (for testing/debugging).
|
|
1617
2309
|
*/
|
|
1618
2310
|
glaze.getConfig = function getConfig() {
|
|
@@ -1626,6 +2318,7 @@ glaze.resetConfig = function resetConfig() {
|
|
|
1626
2318
|
lightLightness: [10, 100],
|
|
1627
2319
|
darkLightness: [15, 95],
|
|
1628
2320
|
darkDesaturation: .1,
|
|
2321
|
+
darkCurve: .5,
|
|
1629
2322
|
states: {
|
|
1630
2323
|
dark: "@dark",
|
|
1631
2324
|
highContrast: "@high-contrast"
|
|
@@ -1647,10 +2340,13 @@ exports.formatOklch = formatOklch;
|
|
|
1647
2340
|
exports.formatRgb = formatRgb;
|
|
1648
2341
|
exports.gamutClampedLuminance = gamutClampedLuminance;
|
|
1649
2342
|
exports.glaze = glaze;
|
|
2343
|
+
exports.hslToSrgb = hslToSrgb;
|
|
1650
2344
|
exports.okhslToLinearSrgb = okhslToLinearSrgb;
|
|
1651
2345
|
exports.okhslToOklab = okhslToOklab;
|
|
1652
2346
|
exports.okhslToSrgb = okhslToSrgb;
|
|
2347
|
+
exports.oklabToOkhsl = oklabToOkhsl;
|
|
1653
2348
|
exports.parseHex = parseHex;
|
|
2349
|
+
exports.parseHexAlpha = parseHexAlpha;
|
|
1654
2350
|
exports.relativeLuminanceFromLinearRgb = relativeLuminanceFromLinearRgb;
|
|
1655
2351
|
exports.resolveMinContrast = resolveMinContrast;
|
|
1656
2352
|
exports.srgbToOkhsl = srgbToOkhsl;
|