@tenphi/glaze 0.0.0-snapshot.4e8eab7 → 0.0.0-snapshot.52e0dd6
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 +19 -1390
- package/dist/index.cjs +739 -563
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -8
- package/dist/index.d.mts +37 -8
- package/dist/index.mjs +739 -563
- package/dist/index.mjs.map +1 -1
- package/docs/api.md +1074 -0
- package/docs/methodology.md +336 -0
- package/docs/migration.md +237 -0
- package/package.json +3 -2
package/dist/index.mjs
CHANGED
|
@@ -558,6 +558,118 @@ function formatOklch(h, s, l) {
|
|
|
558
558
|
return `oklch(${fmt$1(L, 4)} ${fmt$1(C, 4)} ${fmt$1(hh, 2)})`;
|
|
559
559
|
}
|
|
560
560
|
|
|
561
|
+
//#endregion
|
|
562
|
+
//#region src/config.ts
|
|
563
|
+
/**
|
|
564
|
+
* Build a fresh defaults object. Called from module init and from
|
|
565
|
+
* `resetConfig()` so the two paths can't drift.
|
|
566
|
+
*/
|
|
567
|
+
function defaultConfig() {
|
|
568
|
+
return {
|
|
569
|
+
lightLightness: [10, 100],
|
|
570
|
+
darkLightness: [15, 95],
|
|
571
|
+
darkDesaturation: .1,
|
|
572
|
+
darkCurve: .5,
|
|
573
|
+
states: {
|
|
574
|
+
dark: "@dark",
|
|
575
|
+
highContrast: "@high-contrast"
|
|
576
|
+
},
|
|
577
|
+
modes: {
|
|
578
|
+
dark: true,
|
|
579
|
+
highContrast: false
|
|
580
|
+
},
|
|
581
|
+
autoFlip: true
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
let globalConfig = defaultConfig();
|
|
585
|
+
/**
|
|
586
|
+
* Monotonic counter incremented on every `configure()` / `resetConfig()`
|
|
587
|
+
* call. Theme / palette caches read this to invalidate stale resolve
|
|
588
|
+
* results when the config changes between exports.
|
|
589
|
+
*/
|
|
590
|
+
let configVersion = 0;
|
|
591
|
+
/** Live reference to the current config. Mutated by `configure()` / `resetConfig()`. */
|
|
592
|
+
function getConfig() {
|
|
593
|
+
return globalConfig;
|
|
594
|
+
}
|
|
595
|
+
function getConfigVersion() {
|
|
596
|
+
return configVersion;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Public-facing snapshot used by `glaze.getConfig()`. Returns a shallow
|
|
600
|
+
* copy so callers can't mutate the live config.
|
|
601
|
+
*/
|
|
602
|
+
function snapshotConfig() {
|
|
603
|
+
return { ...globalConfig };
|
|
604
|
+
}
|
|
605
|
+
function configure(config) {
|
|
606
|
+
configVersion++;
|
|
607
|
+
globalConfig = {
|
|
608
|
+
lightLightness: config.lightLightness ?? globalConfig.lightLightness,
|
|
609
|
+
darkLightness: config.darkLightness ?? globalConfig.darkLightness,
|
|
610
|
+
darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
|
|
611
|
+
darkCurve: config.darkCurve ?? globalConfig.darkCurve,
|
|
612
|
+
states: {
|
|
613
|
+
dark: config.states?.dark ?? globalConfig.states.dark,
|
|
614
|
+
highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
|
|
615
|
+
},
|
|
616
|
+
modes: {
|
|
617
|
+
dark: config.modes?.dark ?? globalConfig.modes.dark,
|
|
618
|
+
highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
|
|
619
|
+
},
|
|
620
|
+
shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning,
|
|
621
|
+
autoFlip: config.autoFlip ?? globalConfig.autoFlip
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
function resetConfig() {
|
|
625
|
+
configVersion++;
|
|
626
|
+
globalConfig = defaultConfig();
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
//#endregion
|
|
630
|
+
//#region src/hc-pair.ts
|
|
631
|
+
function pairNormal(p) {
|
|
632
|
+
return Array.isArray(p) ? p[0] : p;
|
|
633
|
+
}
|
|
634
|
+
function pairHC(p) {
|
|
635
|
+
return Array.isArray(p) ? p[1] : p;
|
|
636
|
+
}
|
|
637
|
+
function clamp(v, min, max) {
|
|
638
|
+
return Math.max(min, Math.min(max, v));
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Parse a value that can be absolute (number) or relative (signed string).
|
|
642
|
+
* Returns the numeric value and whether it's relative.
|
|
643
|
+
*/
|
|
644
|
+
function parseRelativeOrAbsolute(value) {
|
|
645
|
+
if (typeof value === "number") return {
|
|
646
|
+
value,
|
|
647
|
+
relative: false
|
|
648
|
+
};
|
|
649
|
+
return {
|
|
650
|
+
value: parseFloat(value),
|
|
651
|
+
relative: true
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Compute the effective hue for a color, given the theme seed hue
|
|
656
|
+
* and an optional per-color hue override.
|
|
657
|
+
*/
|
|
658
|
+
function resolveEffectiveHue(seedHue, defHue) {
|
|
659
|
+
if (defHue === void 0) return seedHue;
|
|
660
|
+
const parsed = parseRelativeOrAbsolute(defHue);
|
|
661
|
+
if (parsed.relative) return ((seedHue + parsed.value) % 360 + 360) % 360;
|
|
662
|
+
return (parsed.value % 360 + 360) % 360;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Check whether a lightness value represents an absolute root definition
|
|
666
|
+
* (i.e. a number, not a relative string).
|
|
667
|
+
*/
|
|
668
|
+
function isAbsoluteLightness(lightness) {
|
|
669
|
+
if (lightness === void 0) return false;
|
|
670
|
+
return typeof (Array.isArray(lightness) ? lightness[0] : lightness) === "number";
|
|
671
|
+
}
|
|
672
|
+
|
|
561
673
|
//#endregion
|
|
562
674
|
//#region src/contrast-solver.ts
|
|
563
675
|
/**
|
|
@@ -755,6 +867,25 @@ function findLightnessForContrast(options) {
|
|
|
755
867
|
branch: "preferred"
|
|
756
868
|
};
|
|
757
869
|
candidates.sort((a, b) => b.contrast - a.contrast);
|
|
870
|
+
if (candidates.length > 0 && options.flip) {
|
|
871
|
+
const best = candidates[0];
|
|
872
|
+
const center = (minL + maxL) / 2;
|
|
873
|
+
const flippedResult = findLightnessForContrast({
|
|
874
|
+
hue,
|
|
875
|
+
saturation,
|
|
876
|
+
preferredLightness: Math.max(minL, Math.min(maxL, 2 * center - best.lightness)),
|
|
877
|
+
baseLinearRgb,
|
|
878
|
+
contrast: contrastInput,
|
|
879
|
+
lightnessRange: [minL, maxL],
|
|
880
|
+
epsilon,
|
|
881
|
+
maxIterations
|
|
882
|
+
});
|
|
883
|
+
if (flippedResult.met) return {
|
|
884
|
+
...flippedResult,
|
|
885
|
+
branch: flippedResult.branch,
|
|
886
|
+
flipped: true
|
|
887
|
+
};
|
|
888
|
+
}
|
|
758
889
|
return candidates[0];
|
|
759
890
|
}
|
|
760
891
|
/**
|
|
@@ -827,7 +958,7 @@ function searchMixBranch(lo, hi, yBase, target, epsilon, maxIter, preferred, lum
|
|
|
827
958
|
* target against a base color, staying as close to `preferredValue` as possible.
|
|
828
959
|
*/
|
|
829
960
|
function findValueForMixContrast(options) {
|
|
830
|
-
const { preferredValue, baseLinearRgb, contrast: contrastInput, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
|
|
961
|
+
const { preferredValue, baseLinearRgb, targetLinearRgb, contrast: contrastInput, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
|
|
831
962
|
const target = resolveMinContrast(contrastInput);
|
|
832
963
|
const searchTarget = target * 1.01;
|
|
833
964
|
const yBase = gamutClampedLuminance(baseLinearRgb);
|
|
@@ -880,6 +1011,22 @@ function findValueForMixContrast(options) {
|
|
|
880
1011
|
met: false
|
|
881
1012
|
};
|
|
882
1013
|
candidates.sort((a, b) => b.contrast - a.contrast);
|
|
1014
|
+
if (candidates.length > 0 && options.flip) {
|
|
1015
|
+
const best = candidates[0];
|
|
1016
|
+
const flippedResult = findValueForMixContrast({
|
|
1017
|
+
preferredValue: Math.max(0, Math.min(1, 2 * .5 - best.lightness)),
|
|
1018
|
+
baseLinearRgb,
|
|
1019
|
+
targetLinearRgb,
|
|
1020
|
+
contrast: contrastInput,
|
|
1021
|
+
luminanceAtValue,
|
|
1022
|
+
epsilon,
|
|
1023
|
+
maxIterations
|
|
1024
|
+
});
|
|
1025
|
+
if (flippedResult.met) return {
|
|
1026
|
+
...flippedResult,
|
|
1027
|
+
flipped: true
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
883
1030
|
return {
|
|
884
1031
|
value: candidates[0].lightness,
|
|
885
1032
|
contrast: candidates[0].contrast,
|
|
@@ -888,116 +1035,15 @@ function findValueForMixContrast(options) {
|
|
|
888
1035
|
}
|
|
889
1036
|
|
|
890
1037
|
//#endregion
|
|
891
|
-
//#region src/
|
|
892
|
-
/**
|
|
893
|
-
* Glaze — OKHSL-based color theme generator.
|
|
894
|
-
*
|
|
895
|
-
* Generates robust light, dark, and high-contrast colors from a hue/saturation
|
|
896
|
-
* seed, preserving contrast for UI pairs via explicit dependencies.
|
|
897
|
-
*/
|
|
898
|
-
/** Internal name of the user-facing standalone color in the synthesized def map. */
|
|
899
|
-
const STANDALONE_VALUE = "value";
|
|
900
|
-
/** Internal name of the hidden static-anchor seed used for relative lightness / contrast. */
|
|
901
|
-
const STANDALONE_SEED = "seed";
|
|
902
|
-
/** Internal name of an externally-resolved `GlazeColorToken` injected as a base reference. */
|
|
903
|
-
const STANDALONE_BASE = "externalBase";
|
|
1038
|
+
//#region src/shadow.ts
|
|
904
1039
|
/**
|
|
905
|
-
*
|
|
906
|
-
* pass an explicit `scaling`. All windows are snapshotted from the
|
|
907
|
-
* current `globalConfig` so later `glaze.configure()` calls don't
|
|
908
|
-
* retroactively change the resolved variants of an already-created
|
|
909
|
-
* token (matches the documented "frozen at create time" semantics).
|
|
1040
|
+
* Shadow color computation.
|
|
910
1041
|
*
|
|
911
|
-
*
|
|
912
|
-
*
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
* structured inputs snapshot both windows from `globalConfig` verbatim
|
|
916
|
-
* so they behave like an ordinary theme color (auto-adapted on both
|
|
917
|
-
* sides).
|
|
918
|
-
*/
|
|
919
|
-
function defaultStandaloneScaling(isString) {
|
|
920
|
-
if (isString) {
|
|
921
|
-
const [darkLo] = globalConfig.darkLightness;
|
|
922
|
-
return {
|
|
923
|
-
lightLightness: false,
|
|
924
|
-
darkLightness: [darkLo, 100]
|
|
925
|
-
};
|
|
926
|
-
}
|
|
927
|
-
return {
|
|
928
|
-
lightLightness: globalConfig.lightLightness,
|
|
929
|
-
darkLightness: globalConfig.darkLightness
|
|
930
|
-
};
|
|
931
|
-
}
|
|
932
|
-
/** Reserved internal names that user-supplied `name` must not collide with. */
|
|
933
|
-
const RESERVED_STANDALONE_NAMES = new Set([
|
|
934
|
-
STANDALONE_VALUE,
|
|
935
|
-
STANDALONE_SEED,
|
|
936
|
-
STANDALONE_BASE
|
|
937
|
-
]);
|
|
938
|
-
/**
|
|
939
|
-
* Discriminate a `GlazeColorToken` from a raw `GlazeColorValue`.
|
|
940
|
-
* Used to widen `base?` so it accepts either a token reference or a
|
|
941
|
-
* raw value (auto-wrapped into `glaze.color(value)`).
|
|
942
|
-
*/
|
|
943
|
-
function isGlazeColorToken(candidate) {
|
|
944
|
-
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "resolve" in candidate && typeof candidate.resolve === "function";
|
|
945
|
-
}
|
|
946
|
-
let globalConfig = {
|
|
947
|
-
lightLightness: [10, 100],
|
|
948
|
-
darkLightness: [15, 95],
|
|
949
|
-
darkDesaturation: .1,
|
|
950
|
-
darkCurve: .5,
|
|
951
|
-
states: {
|
|
952
|
-
dark: "@dark",
|
|
953
|
-
highContrast: "@high-contrast"
|
|
954
|
-
},
|
|
955
|
-
modes: {
|
|
956
|
-
dark: true,
|
|
957
|
-
highContrast: false
|
|
958
|
-
}
|
|
959
|
-
};
|
|
960
|
-
function pairNormal(p) {
|
|
961
|
-
return Array.isArray(p) ? p[0] : p;
|
|
962
|
-
}
|
|
963
|
-
function pairHC(p) {
|
|
964
|
-
return Array.isArray(p) ? p[1] : p;
|
|
965
|
-
}
|
|
966
|
-
/**
|
|
967
|
-
* Dedupe contrast warnings within a single process. The cache survives
|
|
968
|
-
* the lifetime of a token because tokens memoize their resolution; the
|
|
969
|
-
* limit is a soft cap to keep noise bounded across long-lived sessions
|
|
970
|
-
* (e.g. dev servers with HMR re-resolving themes repeatedly).
|
|
971
|
-
*/
|
|
972
|
-
const CONTRAST_WARN_CACHE_LIMIT = 256;
|
|
973
|
-
const contrastWarnCache = /* @__PURE__ */ new Set();
|
|
974
|
-
function schemeLabel(isDark, isHighContrast) {
|
|
975
|
-
if (isDark && isHighContrast) return "darkContrast";
|
|
976
|
-
if (isDark) return "dark";
|
|
977
|
-
if (isHighContrast) return "lightContrast";
|
|
978
|
-
return "light";
|
|
979
|
-
}
|
|
980
|
-
function formatContrastTarget(input, ratio) {
|
|
981
|
-
return typeof input === "string" ? `"${input}" (${ratio.toFixed(2)})` : ratio.toFixed(2);
|
|
982
|
-
}
|
|
983
|
-
/**
|
|
984
|
-
* Slack factor below the requested target before we emit a warning.
|
|
985
|
-
* The contrast solver already overshoots by `OVERSHOOT` (currently 1%)
|
|
986
|
-
* to absorb rounding noise (`see findLightnessForContrast` in
|
|
987
|
-
* `contrast-solver.ts`), so an `actual` ratio within ~2x that overshoot
|
|
988
|
-
* is effectively a pass and not worth nagging the user about.
|
|
1042
|
+
* Owns the shadow / mix def predicates, default tuning constants, the
|
|
1043
|
+
* tuning merge, and the actual `computeShadow` math (hue blend,
|
|
1044
|
+
* saturation cap, lightness clamp, alpha curve). The resolver consumes
|
|
1045
|
+
* this module per scheme variant.
|
|
989
1046
|
*/
|
|
990
|
-
const CONTRAST_WARN_SLACK = .98;
|
|
991
|
-
function warnContrastUnmet(name, isDark, isHighContrast, target, actual) {
|
|
992
|
-
const targetRatio = resolveMinContrast(target);
|
|
993
|
-
if (actual >= targetRatio * CONTRAST_WARN_SLACK) return;
|
|
994
|
-
const scheme = schemeLabel(isDark, isHighContrast);
|
|
995
|
-
const key = `${name}|${scheme}|${targetRatio.toFixed(3)}|${actual.toFixed(2)}`;
|
|
996
|
-
if (contrastWarnCache.has(key)) return;
|
|
997
|
-
if (contrastWarnCache.size >= CONTRAST_WARN_CACHE_LIMIT) contrastWarnCache.clear();
|
|
998
|
-
contrastWarnCache.add(key);
|
|
999
|
-
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.`);
|
|
1000
|
-
}
|
|
1001
1047
|
function isShadowDef(def) {
|
|
1002
1048
|
return def.type === "shadow";
|
|
1003
1049
|
}
|
|
@@ -1014,11 +1060,12 @@ const DEFAULT_SHADOW_TUNING = {
|
|
|
1014
1060
|
bgHueBlend: .2
|
|
1015
1061
|
};
|
|
1016
1062
|
function resolveShadowTuning(perColor) {
|
|
1063
|
+
const globalTuning = getConfig().shadowTuning;
|
|
1017
1064
|
return {
|
|
1018
1065
|
...DEFAULT_SHADOW_TUNING,
|
|
1019
|
-
...
|
|
1066
|
+
...globalTuning,
|
|
1020
1067
|
...perColor,
|
|
1021
|
-
lightnessBounds: perColor?.lightnessBounds ??
|
|
1068
|
+
lightnessBounds: perColor?.lightnessBounds ?? globalTuning?.lightnessBounds ?? DEFAULT_SHADOW_TUNING.lightnessBounds
|
|
1022
1069
|
};
|
|
1023
1070
|
}
|
|
1024
1071
|
function circularLerp(a, b, t) {
|
|
@@ -1059,6 +1106,80 @@ function computeShadow(bg, fg, intensity, tuning) {
|
|
|
1059
1106
|
alpha
|
|
1060
1107
|
};
|
|
1061
1108
|
}
|
|
1109
|
+
|
|
1110
|
+
//#endregion
|
|
1111
|
+
//#region src/scheme-mapping.ts
|
|
1112
|
+
/**
|
|
1113
|
+
* Light / dark scheme lightness mappings.
|
|
1114
|
+
*
|
|
1115
|
+
* Owns the active lightness window selection (with per-call scaling
|
|
1116
|
+
* overrides and high-contrast handling), the Möbius curve used by the
|
|
1117
|
+
* `'auto'` dark adaptation, and the saturation-desaturation reducer
|
|
1118
|
+
* for dark mode.
|
|
1119
|
+
*/
|
|
1120
|
+
/**
|
|
1121
|
+
* Resolve the active lightness window for a scheme.
|
|
1122
|
+
* - HC variants always return `[0, 100]` (existing behavior, predates per-call overrides).
|
|
1123
|
+
* - Otherwise, per-call `scaling` (e.g. from `glaze.color()`'s third arg) wins;
|
|
1124
|
+
* `false` is interpreted as `[0, 100]` (no remap). Falls back to `globalConfig.*Lightness`.
|
|
1125
|
+
*/
|
|
1126
|
+
function lightnessWindow(isHighContrast, kind, scaling) {
|
|
1127
|
+
if (isHighContrast) return [0, 100];
|
|
1128
|
+
if (scaling) {
|
|
1129
|
+
const override = kind === "dark" ? scaling.darkLightness : scaling.lightLightness;
|
|
1130
|
+
if (override === false) return [0, 100];
|
|
1131
|
+
if (override !== void 0) return override;
|
|
1132
|
+
}
|
|
1133
|
+
const cfg = getConfig();
|
|
1134
|
+
return kind === "dark" ? cfg.darkLightness : cfg.lightLightness;
|
|
1135
|
+
}
|
|
1136
|
+
function mapLightnessLight(l, mode, isHighContrast, scaling) {
|
|
1137
|
+
if (mode === "static") return l;
|
|
1138
|
+
const [lo, hi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1139
|
+
return l * (hi - lo) / 100 + lo;
|
|
1140
|
+
}
|
|
1141
|
+
function mobiusCurve(t, beta) {
|
|
1142
|
+
if (beta >= 1) return t;
|
|
1143
|
+
return t / (t + beta * (1 - t));
|
|
1144
|
+
}
|
|
1145
|
+
function mapLightnessDark(l, mode, isHighContrast, scaling) {
|
|
1146
|
+
if (mode === "static") return l;
|
|
1147
|
+
const cfg = getConfig();
|
|
1148
|
+
const beta = isHighContrast ? pairHC(cfg.darkCurve) : pairNormal(cfg.darkCurve);
|
|
1149
|
+
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark", scaling);
|
|
1150
|
+
if (mode === "fixed") return l * (darkHi - darkLo) / 100 + darkLo;
|
|
1151
|
+
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1152
|
+
const t = (lightHi - (l * (lightHi - lightLo) / 100 + lightLo)) / (lightHi - lightLo);
|
|
1153
|
+
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
1154
|
+
}
|
|
1155
|
+
function lightMappedToDark(lightL, isHighContrast, scaling) {
|
|
1156
|
+
const cfg = getConfig();
|
|
1157
|
+
const beta = isHighContrast ? pairHC(cfg.darkCurve) : pairNormal(cfg.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);
|
|
1162
|
+
}
|
|
1163
|
+
function mapSaturationDark(s, mode) {
|
|
1164
|
+
if (mode === "static") return s;
|
|
1165
|
+
return s * (1 - getConfig().darkDesaturation);
|
|
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
|
+
}
|
|
1172
|
+
|
|
1173
|
+
//#endregion
|
|
1174
|
+
//#region src/validation.ts
|
|
1175
|
+
/**
|
|
1176
|
+
* Color graph validation and topological sort.
|
|
1177
|
+
*
|
|
1178
|
+
* `validateColorDefs` rejects bad references (missing / shadow-referencing /
|
|
1179
|
+
* base/contrast/lightness mismatches) and detects cycles before the
|
|
1180
|
+
* resolver runs. `topoSort` orders defs so each color is processed after
|
|
1181
|
+
* its base / bg / fg / target dependencies.
|
|
1182
|
+
*/
|
|
1062
1183
|
function validateColorDefs(defs, externalBases) {
|
|
1063
1184
|
const localNames = new Set(Object.keys(defs));
|
|
1064
1185
|
const allNames = new Set([...localNames, ...externalBases ? externalBases.keys() : []]);
|
|
@@ -1133,89 +1254,62 @@ function topoSort(defs) {
|
|
|
1133
1254
|
for (const name of Object.keys(defs)) visit(name);
|
|
1134
1255
|
return result;
|
|
1135
1256
|
}
|
|
1257
|
+
|
|
1258
|
+
//#endregion
|
|
1259
|
+
//#region src/warnings.ts
|
|
1136
1260
|
/**
|
|
1137
|
-
*
|
|
1138
|
-
*
|
|
1139
|
-
*
|
|
1140
|
-
*
|
|
1261
|
+
* Contrast-warning dispatcher.
|
|
1262
|
+
*
|
|
1263
|
+
* Tokens memoize their resolution, but a long-lived process (e.g. a dev
|
|
1264
|
+
* server with HMR) can re-resolve the same theme many times. The cache
|
|
1265
|
+
* here dedupes warnings within a session with a soft cap to keep noise
|
|
1266
|
+
* bounded.
|
|
1141
1267
|
*/
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
if (scaling) {
|
|
1145
|
-
const override = kind === "dark" ? scaling.darkLightness : scaling.lightLightness;
|
|
1146
|
-
if (override === false) return [0, 100];
|
|
1147
|
-
if (override !== void 0) return override;
|
|
1148
|
-
}
|
|
1149
|
-
return kind === "dark" ? globalConfig.darkLightness : globalConfig.lightLightness;
|
|
1150
|
-
}
|
|
1151
|
-
function mapLightnessLight(l, mode, isHighContrast, scaling) {
|
|
1152
|
-
if (mode === "static") return l;
|
|
1153
|
-
const [lo, hi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1154
|
-
return l * (hi - lo) / 100 + lo;
|
|
1155
|
-
}
|
|
1156
|
-
function mobiusCurve(t, beta) {
|
|
1157
|
-
if (beta >= 1) return t;
|
|
1158
|
-
return t / (t + beta * (1 - t));
|
|
1159
|
-
}
|
|
1160
|
-
function mapLightnessDark(l, mode, isHighContrast, scaling) {
|
|
1161
|
-
if (mode === "static") return l;
|
|
1162
|
-
const beta = isHighContrast ? pairHC(globalConfig.darkCurve) : pairNormal(globalConfig.darkCurve);
|
|
1163
|
-
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark", scaling);
|
|
1164
|
-
if (mode === "fixed") return l * (darkHi - darkLo) / 100 + darkLo;
|
|
1165
|
-
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1166
|
-
const t = (lightHi - (l * (lightHi - lightLo) / 100 + lightLo)) / (lightHi - lightLo);
|
|
1167
|
-
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
1168
|
-
}
|
|
1169
|
-
function lightMappedToDark(lightL, isHighContrast, scaling) {
|
|
1170
|
-
const beta = isHighContrast ? pairHC(globalConfig.darkCurve) : pairNormal(globalConfig.darkCurve);
|
|
1171
|
-
const [lightLo, lightHi] = lightnessWindow(isHighContrast, "light", scaling);
|
|
1172
|
-
const [darkLo, darkHi] = lightnessWindow(isHighContrast, "dark", scaling);
|
|
1173
|
-
const t = (lightHi - clamp(lightL, lightLo, lightHi)) / (lightHi - lightLo);
|
|
1174
|
-
return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
|
|
1175
|
-
}
|
|
1176
|
-
function mapSaturationDark(s, mode) {
|
|
1177
|
-
if (mode === "static") return s;
|
|
1178
|
-
return s * (1 - globalConfig.darkDesaturation);
|
|
1179
|
-
}
|
|
1180
|
-
function schemeLightnessRange(isDark, mode, isHighContrast, scaling) {
|
|
1181
|
-
if (mode === "static") return [0, 1];
|
|
1182
|
-
const [lo, hi] = lightnessWindow(isHighContrast, isDark ? "dark" : "light", scaling);
|
|
1183
|
-
return [lo / 100, hi / 100];
|
|
1184
|
-
}
|
|
1185
|
-
function clamp(v, min, max) {
|
|
1186
|
-
return Math.max(min, Math.min(max, v));
|
|
1187
|
-
}
|
|
1268
|
+
const CONTRAST_WARN_CACHE_LIMIT = 256;
|
|
1269
|
+
const contrastWarnCache = /* @__PURE__ */ new Set();
|
|
1188
1270
|
/**
|
|
1189
|
-
*
|
|
1190
|
-
*
|
|
1271
|
+
* Slack factor below the requested target before we emit a warning.
|
|
1272
|
+
* The contrast solver already overshoots by `OVERSHOOT` (currently 1%)
|
|
1273
|
+
* to absorb rounding noise (`see findLightnessForContrast` in
|
|
1274
|
+
* `contrast-solver.ts`), so an `actual` ratio within ~2x that overshoot
|
|
1275
|
+
* is effectively a pass and not worth nagging the user about.
|
|
1191
1276
|
*/
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
return
|
|
1198
|
-
value: parseFloat(value),
|
|
1199
|
-
relative: true
|
|
1200
|
-
};
|
|
1277
|
+
const CONTRAST_WARN_SLACK = .98;
|
|
1278
|
+
function schemeLabel(isDark, isHighContrast) {
|
|
1279
|
+
if (isDark && isHighContrast) return "darkContrast";
|
|
1280
|
+
if (isDark) return "dark";
|
|
1281
|
+
if (isHighContrast) return "lightContrast";
|
|
1282
|
+
return "light";
|
|
1201
1283
|
}
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
if (
|
|
1208
|
-
const
|
|
1209
|
-
|
|
1210
|
-
|
|
1284
|
+
function formatContrastTarget(input, ratio) {
|
|
1285
|
+
return typeof input === "string" ? `"${input}" (${ratio.toFixed(2)})` : ratio.toFixed(2);
|
|
1286
|
+
}
|
|
1287
|
+
function warnContrastUnmet(name, isDark, isHighContrast, target, actual) {
|
|
1288
|
+
const targetRatio = resolveMinContrast(target);
|
|
1289
|
+
if (actual >= targetRatio * CONTRAST_WARN_SLACK) return;
|
|
1290
|
+
const scheme = schemeLabel(isDark, isHighContrast);
|
|
1291
|
+
const key = `${name}|${scheme}|${targetRatio.toFixed(3)}|${actual.toFixed(2)}`;
|
|
1292
|
+
if (contrastWarnCache.has(key)) return;
|
|
1293
|
+
if (contrastWarnCache.size >= CONTRAST_WARN_CACHE_LIMIT) contrastWarnCache.clear();
|
|
1294
|
+
contrastWarnCache.add(key);
|
|
1295
|
+
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.`);
|
|
1211
1296
|
}
|
|
1297
|
+
|
|
1298
|
+
//#endregion
|
|
1299
|
+
//#region src/resolver.ts
|
|
1212
1300
|
/**
|
|
1213
|
-
*
|
|
1214
|
-
*
|
|
1301
|
+
* Color resolution engine.
|
|
1302
|
+
*
|
|
1303
|
+
* Runs the four-pass solver (light → light-HC → dark → dark-HC) that
|
|
1304
|
+
* turns a `ColorMap` into a fully resolved `ResolvedColor` per name.
|
|
1305
|
+
* Owns the per-scheme resolve helpers for regular, shadow, and mix
|
|
1306
|
+
* color defs.
|
|
1215
1307
|
*/
|
|
1216
|
-
function
|
|
1217
|
-
if (
|
|
1218
|
-
|
|
1308
|
+
function getSchemeVariant(color, isDark, isHighContrast) {
|
|
1309
|
+
if (isDark && isHighContrast) return color.darkContrast;
|
|
1310
|
+
if (isDark) return color.dark;
|
|
1311
|
+
if (isHighContrast) return color.lightContrast;
|
|
1312
|
+
return color.light;
|
|
1219
1313
|
}
|
|
1220
1314
|
function resolveRootColor(_name, def, _ctx, isHighContrast) {
|
|
1221
1315
|
const rawL = def.lightness;
|
|
@@ -1250,13 +1344,15 @@ function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effective
|
|
|
1250
1344
|
const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode) : satFactor * ctx.saturation / 100;
|
|
1251
1345
|
const baseLinearRgb = okhslToLinearSrgb(baseVariant.h, baseVariant.s, baseVariant.l);
|
|
1252
1346
|
const windowRange = schemeLightnessRange(isDark, mode, isHighContrast, ctx.scaling);
|
|
1347
|
+
const autoFlip = ctx.autoFlip ?? getConfig().autoFlip;
|
|
1253
1348
|
const result = findLightnessForContrast({
|
|
1254
1349
|
hue: effectiveHue,
|
|
1255
1350
|
saturation: effectiveSat,
|
|
1256
1351
|
preferredLightness: clamp(preferredL / 100, windowRange[0], windowRange[1]),
|
|
1257
1352
|
baseLinearRgb,
|
|
1258
1353
|
contrast: minCr,
|
|
1259
|
-
lightnessRange: [0, 1]
|
|
1354
|
+
lightnessRange: [0, 1],
|
|
1355
|
+
flip: autoFlip
|
|
1260
1356
|
});
|
|
1261
1357
|
if (!result.met) warnContrastUnmet(name, isDark, isHighContrast, minCr, result.contrast);
|
|
1262
1358
|
return {
|
|
@@ -1269,12 +1365,6 @@ function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effective
|
|
|
1269
1365
|
satFactor
|
|
1270
1366
|
};
|
|
1271
1367
|
}
|
|
1272
|
-
function getSchemeVariant(color, isDark, isHighContrast) {
|
|
1273
|
-
if (isDark && isHighContrast) return color.darkContrast;
|
|
1274
|
-
if (isDark) return color.dark;
|
|
1275
|
-
if (isHighContrast) return color.lightContrast;
|
|
1276
|
-
return color.light;
|
|
1277
|
-
}
|
|
1278
1368
|
function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
1279
1369
|
if (isShadowDef(def)) return resolveShadowForScheme(def, ctx, isDark, isHighContrast);
|
|
1280
1370
|
if (isMixDef(def)) return resolveMixForScheme(def, ctx, isDark, isHighContrast);
|
|
@@ -1378,12 +1468,14 @@ function resolveMixForScheme(def, ctx, isDark, isHighContrast) {
|
|
|
1378
1468
|
else luminanceAt = (v) => {
|
|
1379
1469
|
return gamutClampedLuminance(okhslToLinearSrgb(mixHue(baseVariant, targetVariant, v), baseVariant.s + (targetVariant.s - baseVariant.s) * v, baseVariant.l + (targetVariant.l - baseVariant.l) * v));
|
|
1380
1470
|
};
|
|
1471
|
+
const autoFlip = ctx.autoFlip ?? getConfig().autoFlip;
|
|
1381
1472
|
t = findValueForMixContrast({
|
|
1382
1473
|
preferredValue: t,
|
|
1383
1474
|
baseLinearRgb: baseLinear,
|
|
1384
1475
|
targetLinearRgb: targetLinear,
|
|
1385
1476
|
contrast: minCr,
|
|
1386
|
-
luminanceAtValue: luminanceAt
|
|
1477
|
+
luminanceAtValue: luminanceAt,
|
|
1478
|
+
flip: autoFlip
|
|
1387
1479
|
}).value;
|
|
1388
1480
|
}
|
|
1389
1481
|
if (blend === "transparent") return {
|
|
@@ -1400,26 +1492,27 @@ function resolveMixForScheme(def, ctx, isDark, isHighContrast) {
|
|
|
1400
1492
|
alpha: 1
|
|
1401
1493
|
};
|
|
1402
1494
|
}
|
|
1403
|
-
function
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
if (isShadowDef(def) || isMixDef(def)) return void 0;
|
|
1416
|
-
return def.mode ?? "auto";
|
|
1417
|
-
}
|
|
1418
|
-
const lightMap = /* @__PURE__ */ new Map();
|
|
1495
|
+
function defMode(def) {
|
|
1496
|
+
if (isShadowDef(def) || isMixDef(def)) return void 0;
|
|
1497
|
+
return def.mode ?? "auto";
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* Run a single resolve pass over all local names. Pass 1 lazily creates
|
|
1501
|
+
* each `ResolvedColor` (all four slots seeded with the just-resolved
|
|
1502
|
+
* variant) the first time it sees a name; later passes update the
|
|
1503
|
+
* `target` slot on the existing record.
|
|
1504
|
+
*/
|
|
1505
|
+
function runPass(order, defs, ctx, isDark, isHighContrast, target) {
|
|
1506
|
+
const out = /* @__PURE__ */ new Map();
|
|
1419
1507
|
for (const name of order) {
|
|
1420
|
-
const variant = resolveColorForScheme(name, defs[name], ctx,
|
|
1421
|
-
|
|
1422
|
-
ctx.resolved.
|
|
1508
|
+
const variant = resolveColorForScheme(name, defs[name], ctx, isDark, isHighContrast);
|
|
1509
|
+
out.set(name, variant);
|
|
1510
|
+
const existing = ctx.resolved.get(name);
|
|
1511
|
+
if (existing) ctx.resolved.set(name, {
|
|
1512
|
+
...existing,
|
|
1513
|
+
[target]: variant
|
|
1514
|
+
});
|
|
1515
|
+
else ctx.resolved.set(name, {
|
|
1423
1516
|
name,
|
|
1424
1517
|
light: variant,
|
|
1425
1518
|
dark: variant,
|
|
@@ -1428,49 +1521,42 @@ function resolveAllColors(hue, saturation, defs, scaling, externalBases) {
|
|
|
1428
1521
|
mode: defMode(defs[name])
|
|
1429
1522
|
});
|
|
1430
1523
|
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
lightHCMap.set(name, variant);
|
|
1439
|
-
ctx.resolved.set(name, {
|
|
1440
|
-
...ctx.resolved.get(name),
|
|
1441
|
-
lightContrast: variant
|
|
1442
|
-
});
|
|
1443
|
-
}
|
|
1444
|
-
const darkMap = /* @__PURE__ */ new Map();
|
|
1445
|
-
for (const name of order) ctx.resolved.set(name, {
|
|
1446
|
-
name,
|
|
1447
|
-
light: lightMap.get(name),
|
|
1448
|
-
dark: lightMap.get(name),
|
|
1449
|
-
lightContrast: lightHCMap.get(name),
|
|
1450
|
-
darkContrast: lightHCMap.get(name),
|
|
1451
|
-
mode: defMode(defs[name])
|
|
1452
|
-
});
|
|
1453
|
-
for (const name of order) {
|
|
1454
|
-
const variant = resolveColorForScheme(name, defs[name], ctx, true, false);
|
|
1455
|
-
darkMap.set(name, variant);
|
|
1456
|
-
ctx.resolved.set(name, {
|
|
1457
|
-
...ctx.resolved.get(name),
|
|
1458
|
-
dark: variant
|
|
1459
|
-
});
|
|
1460
|
-
}
|
|
1461
|
-
const darkHCMap = /* @__PURE__ */ new Map();
|
|
1462
|
-
for (const name of order) ctx.resolved.set(name, {
|
|
1463
|
-
...ctx.resolved.get(name),
|
|
1464
|
-
darkContrast: darkMap.get(name)
|
|
1465
|
-
});
|
|
1524
|
+
return out;
|
|
1525
|
+
}
|
|
1526
|
+
/**
|
|
1527
|
+
* Re-seed a single variant slot with a previously-resolved map so the
|
|
1528
|
+
* upcoming pass reads sensible fallbacks via `getSchemeVariant`.
|
|
1529
|
+
*/
|
|
1530
|
+
function seedField(order, ctx, field, source) {
|
|
1466
1531
|
for (const name of order) {
|
|
1467
|
-
const
|
|
1468
|
-
darkHCMap.set(name, variant);
|
|
1532
|
+
const existing = ctx.resolved.get(name);
|
|
1469
1533
|
ctx.resolved.set(name, {
|
|
1470
|
-
...
|
|
1471
|
-
|
|
1534
|
+
...existing,
|
|
1535
|
+
[field]: source.get(name)
|
|
1472
1536
|
});
|
|
1473
1537
|
}
|
|
1538
|
+
}
|
|
1539
|
+
function resolveAllColors(hue, saturation, defs, scaling, externalBases, overrideAutoFlip) {
|
|
1540
|
+
validateColorDefs(defs, externalBases);
|
|
1541
|
+
const order = topoSort(defs);
|
|
1542
|
+
const cfg = getConfig();
|
|
1543
|
+
const ctx = {
|
|
1544
|
+
hue,
|
|
1545
|
+
saturation,
|
|
1546
|
+
defs,
|
|
1547
|
+
resolved: /* @__PURE__ */ new Map(),
|
|
1548
|
+
scaling,
|
|
1549
|
+
autoFlip: overrideAutoFlip ?? cfg.autoFlip
|
|
1550
|
+
};
|
|
1551
|
+
if (externalBases) for (const [name, color] of externalBases) ctx.resolved.set(name, color);
|
|
1552
|
+
const lightMap = runPass(order, defs, ctx, false, false, "light");
|
|
1553
|
+
seedField(order, ctx, "lightContrast", lightMap);
|
|
1554
|
+
const lightHCMap = runPass(order, defs, ctx, false, true, "lightContrast");
|
|
1555
|
+
seedField(order, ctx, "dark", lightMap);
|
|
1556
|
+
seedField(order, ctx, "darkContrast", lightHCMap);
|
|
1557
|
+
const darkMap = runPass(order, defs, ctx, true, false, "dark");
|
|
1558
|
+
seedField(order, ctx, "darkContrast", darkMap);
|
|
1559
|
+
const darkHCMap = runPass(order, defs, ctx, true, true, "darkContrast");
|
|
1474
1560
|
const result = /* @__PURE__ */ new Map();
|
|
1475
1561
|
for (const name of order) result.set(name, {
|
|
1476
1562
|
name,
|
|
@@ -1482,6 +1568,19 @@ function resolveAllColors(hue, saturation, defs, scaling, externalBases) {
|
|
|
1482
1568
|
});
|
|
1483
1569
|
return result;
|
|
1484
1570
|
}
|
|
1571
|
+
|
|
1572
|
+
//#endregion
|
|
1573
|
+
//#region src/formatters.ts
|
|
1574
|
+
/**
|
|
1575
|
+
* Output formatting for resolved color maps.
|
|
1576
|
+
*
|
|
1577
|
+
* Owns the CSS-string formatter dispatch table (`okhsl` / `rgb` / `hsl` /
|
|
1578
|
+
* `oklch`) and the four token-map shapes Glaze emits:
|
|
1579
|
+
* - `buildTokenMap` — Tasty style-to-state bindings (`#name` keys, state aliases).
|
|
1580
|
+
* - `buildFlatTokenMap` — `{ light, dark, ... }` per-variant maps.
|
|
1581
|
+
* - `buildJsonMap` — `{ name: { light, dark, ... } }` per-color JSON.
|
|
1582
|
+
* - `buildCssMap` — CSS custom property declaration strings per variant.
|
|
1583
|
+
*/
|
|
1485
1584
|
const formatters = {
|
|
1486
1585
|
okhsl: formatOkhsl,
|
|
1487
1586
|
rgb: formatRgb,
|
|
@@ -1498,9 +1597,10 @@ function formatVariant(v, format = "okhsl") {
|
|
|
1498
1597
|
return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
|
|
1499
1598
|
}
|
|
1500
1599
|
function resolveModes(override) {
|
|
1600
|
+
const cfg = getConfig();
|
|
1501
1601
|
return {
|
|
1502
|
-
dark: override?.dark ??
|
|
1503
|
-
highContrast: override?.highContrast ??
|
|
1602
|
+
dark: override?.dark ?? cfg.modes.dark,
|
|
1603
|
+
highContrast: override?.highContrast ?? cfg.modes.highContrast
|
|
1504
1604
|
};
|
|
1505
1605
|
}
|
|
1506
1606
|
function buildTokenMap(resolved, prefix, states, modes, format = "okhsl") {
|
|
@@ -1561,206 +1661,74 @@ function buildCssMap(resolved, prefix, suffix, format) {
|
|
|
1561
1661
|
darkContrast: lines.darkContrast.join("\n")
|
|
1562
1662
|
};
|
|
1563
1663
|
}
|
|
1564
|
-
|
|
1565
|
-
|
|
1664
|
+
|
|
1665
|
+
//#endregion
|
|
1666
|
+
//#region src/color-token.ts
|
|
1667
|
+
/**
|
|
1668
|
+
* Standalone single-color tokens (`glaze.color()` / `glaze.colorFrom()`).
|
|
1669
|
+
*
|
|
1670
|
+
* Owns the value-shorthand parser (hex, `rgb()` / `hsl()` / `okhsl()` /
|
|
1671
|
+
* `oklch()`, OkhslColor object, [r, g, b] tuple), the structured-input
|
|
1672
|
+
* validator, the two factory paths (value vs structured), and the
|
|
1673
|
+
* JSON-safe export / rehydration round-trip.
|
|
1674
|
+
*
|
|
1675
|
+
* Standalone tokens snapshot the relevant `globalConfig` fields at
|
|
1676
|
+
* create time so later `configure()` calls do not retroactively change
|
|
1677
|
+
* exported tokens — the snapshot is captured eagerly in
|
|
1678
|
+
* `defaultStandaloneScaling()`. The token's resolved variants are then
|
|
1679
|
+
* memoized on first `.resolve()` / `.token()` / ... call.
|
|
1680
|
+
*/
|
|
1681
|
+
/** Internal name of the user-facing standalone color in the synthesized def map. */
|
|
1682
|
+
const STANDALONE_VALUE = "value";
|
|
1683
|
+
/** Internal name of the hidden static-anchor seed used for relative lightness / contrast. */
|
|
1684
|
+
const STANDALONE_SEED = "seed";
|
|
1685
|
+
/** Internal name of an externally-resolved `GlazeColorToken` injected as a base reference. */
|
|
1686
|
+
const STANDALONE_BASE = "externalBase";
|
|
1687
|
+
/** Reserved internal names that user-supplied `name` must not collide with. */
|
|
1688
|
+
const RESERVED_STANDALONE_NAMES = new Set([
|
|
1689
|
+
STANDALONE_VALUE,
|
|
1690
|
+
STANDALONE_SEED,
|
|
1691
|
+
STANDALONE_BASE
|
|
1692
|
+
]);
|
|
1693
|
+
/**
|
|
1694
|
+
* Build the create-time scaling snapshot used when the caller did not
|
|
1695
|
+
* pass an explicit `scaling`. All windows are snapshotted from the
|
|
1696
|
+
* current `globalConfig` so later `glaze.configure()` calls don't
|
|
1697
|
+
* retroactively change the resolved variants of an already-created
|
|
1698
|
+
* token (matches the documented "frozen at create time" semantics).
|
|
1699
|
+
*
|
|
1700
|
+
* String value-shorthand inputs preserve their light lightness exactly
|
|
1701
|
+
* (`lightLightness: false`) and use an extended dark window
|
|
1702
|
+
* `[globalConfig.darkLightness[0], 100]` so a totally-black input can
|
|
1703
|
+
* Möbius-invert to totally-white in dark mode. Object / tuple /
|
|
1704
|
+
* structured inputs snapshot both windows from `globalConfig` verbatim
|
|
1705
|
+
* so they behave like an ordinary theme color (auto-adapted on both
|
|
1706
|
+
* sides).
|
|
1707
|
+
*/
|
|
1708
|
+
function defaultStandaloneScaling(isString) {
|
|
1709
|
+
const cfg = getConfig();
|
|
1710
|
+
if (isString) {
|
|
1711
|
+
const [darkLo] = cfg.darkLightness;
|
|
1712
|
+
return {
|
|
1713
|
+
lightLightness: false,
|
|
1714
|
+
darkLightness: [darkLo, 100]
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1566
1717
|
return {
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
},
|
|
1570
|
-
get saturation() {
|
|
1571
|
-
return saturation;
|
|
1572
|
-
},
|
|
1573
|
-
colors(defs) {
|
|
1574
|
-
colorDefs = {
|
|
1575
|
-
...colorDefs,
|
|
1576
|
-
...defs
|
|
1577
|
-
};
|
|
1578
|
-
},
|
|
1579
|
-
color(name, def) {
|
|
1580
|
-
if (def === void 0) return colorDefs[name];
|
|
1581
|
-
colorDefs[name] = def;
|
|
1582
|
-
},
|
|
1583
|
-
remove(names) {
|
|
1584
|
-
const list = Array.isArray(names) ? names : [names];
|
|
1585
|
-
for (const name of list) delete colorDefs[name];
|
|
1586
|
-
},
|
|
1587
|
-
has(name) {
|
|
1588
|
-
return name in colorDefs;
|
|
1589
|
-
},
|
|
1590
|
-
list() {
|
|
1591
|
-
return Object.keys(colorDefs);
|
|
1592
|
-
},
|
|
1593
|
-
reset() {
|
|
1594
|
-
colorDefs = {};
|
|
1595
|
-
},
|
|
1596
|
-
export() {
|
|
1597
|
-
return {
|
|
1598
|
-
hue,
|
|
1599
|
-
saturation,
|
|
1600
|
-
colors: { ...colorDefs }
|
|
1601
|
-
};
|
|
1602
|
-
},
|
|
1603
|
-
extend(options) {
|
|
1604
|
-
const newHue = options.hue ?? hue;
|
|
1605
|
-
const newSat = options.saturation ?? saturation;
|
|
1606
|
-
const inheritedColors = {};
|
|
1607
|
-
for (const [name, def] of Object.entries(colorDefs)) if (def.inherit !== false) inheritedColors[name] = def;
|
|
1608
|
-
return createTheme(newHue, newSat, options.colors ? {
|
|
1609
|
-
...inheritedColors,
|
|
1610
|
-
...options.colors
|
|
1611
|
-
} : { ...inheritedColors });
|
|
1612
|
-
},
|
|
1613
|
-
resolve() {
|
|
1614
|
-
return resolveAllColors(hue, saturation, colorDefs);
|
|
1615
|
-
},
|
|
1616
|
-
tokens(options) {
|
|
1617
|
-
return buildFlatTokenMap(resolveAllColors(hue, saturation, colorDefs), "", resolveModes(options?.modes), options?.format);
|
|
1618
|
-
},
|
|
1619
|
-
tasty(options) {
|
|
1620
|
-
return buildTokenMap(resolveAllColors(hue, saturation, colorDefs), "", {
|
|
1621
|
-
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1622
|
-
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1623
|
-
}, resolveModes(options?.modes), options?.format);
|
|
1624
|
-
},
|
|
1625
|
-
json(options) {
|
|
1626
|
-
return buildJsonMap(resolveAllColors(hue, saturation, colorDefs), resolveModes(options?.modes), options?.format);
|
|
1627
|
-
},
|
|
1628
|
-
css(options) {
|
|
1629
|
-
return buildCssMap(resolveAllColors(hue, saturation, colorDefs), "", options?.suffix ?? "-color", options?.format ?? "rgb");
|
|
1630
|
-
}
|
|
1718
|
+
lightLightness: cfg.lightLightness,
|
|
1719
|
+
darkLightness: cfg.darkLightness
|
|
1631
1720
|
};
|
|
1632
1721
|
}
|
|
1633
|
-
function resolvePrefix(options, themeName, defaultPrefix = false) {
|
|
1634
|
-
const prefix = options?.prefix ?? defaultPrefix;
|
|
1635
|
-
if (prefix === true) return `${themeName}-`;
|
|
1636
|
-
if (typeof prefix === "object" && prefix !== null) return prefix[themeName] ?? `${themeName}-`;
|
|
1637
|
-
return "";
|
|
1638
|
-
}
|
|
1639
|
-
function validatePrimaryTheme(primary, themes) {
|
|
1640
|
-
if (primary !== void 0 && !(primary in themes)) {
|
|
1641
|
-
const available = Object.keys(themes).join(", ");
|
|
1642
|
-
throw new Error(`glaze: primary theme "${primary}" not found in palette. Available: ${available}.`);
|
|
1643
|
-
}
|
|
1644
|
-
}
|
|
1645
1722
|
/**
|
|
1646
|
-
*
|
|
1647
|
-
* `
|
|
1723
|
+
* Discriminate a `GlazeColorToken` from a raw `GlazeColorValue`.
|
|
1724
|
+
* Used to widen `base?` so it accepts either a token reference or a
|
|
1725
|
+
* raw value (auto-wrapped into `glaze.color(value)`).
|
|
1648
1726
|
*/
|
|
1649
|
-
function
|
|
1650
|
-
|
|
1651
|
-
return exportPrimary ?? palettePrimary;
|
|
1652
|
-
}
|
|
1653
|
-
/**
|
|
1654
|
-
* Filter a resolved color map, skipping keys already in `seen`.
|
|
1655
|
-
* Warns on collision and keeps the first-written value (first-write-wins).
|
|
1656
|
-
* Returns a new map containing only non-colliding entries.
|
|
1657
|
-
*/
|
|
1658
|
-
function filterCollisions(resolved, prefix, seen, themeName, isPrimary) {
|
|
1659
|
-
const filtered = /* @__PURE__ */ new Map();
|
|
1660
|
-
const label = isPrimary ? `${themeName} (primary)` : themeName;
|
|
1661
|
-
for (const [name, color] of resolved) {
|
|
1662
|
-
const key = `${prefix}${name}`;
|
|
1663
|
-
if (seen.has(key)) {
|
|
1664
|
-
console.warn(`glaze: token "${key}" from theme "${label}" collides with theme "${seen.get(key)}" — skipping.`);
|
|
1665
|
-
continue;
|
|
1666
|
-
}
|
|
1667
|
-
seen.set(key, label);
|
|
1668
|
-
filtered.set(name, color);
|
|
1669
|
-
}
|
|
1670
|
-
return filtered;
|
|
1727
|
+
function isGlazeColorToken(candidate) {
|
|
1728
|
+
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "resolve" in candidate && typeof candidate.resolve === "function";
|
|
1671
1729
|
}
|
|
1672
|
-
function
|
|
1673
|
-
|
|
1674
|
-
return {
|
|
1675
|
-
tokens(options) {
|
|
1676
|
-
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1677
|
-
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1678
|
-
const modes = resolveModes(options?.modes);
|
|
1679
|
-
const allTokens = {};
|
|
1680
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1681
|
-
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1682
|
-
const resolved = theme.resolve();
|
|
1683
|
-
const prefix = resolvePrefix(options, themeName, true);
|
|
1684
|
-
const tokens = buildFlatTokenMap(filterCollisions(resolved, prefix, seen, themeName), prefix, modes, options?.format);
|
|
1685
|
-
for (const variant of Object.keys(tokens)) {
|
|
1686
|
-
if (!allTokens[variant]) allTokens[variant] = {};
|
|
1687
|
-
Object.assign(allTokens[variant], tokens[variant]);
|
|
1688
|
-
}
|
|
1689
|
-
if (themeName === effectivePrimary) {
|
|
1690
|
-
const unprefixed = buildFlatTokenMap(filterCollisions(resolved, "", seen, themeName, true), "", modes, options?.format);
|
|
1691
|
-
for (const variant of Object.keys(unprefixed)) Object.assign(allTokens[variant], unprefixed[variant]);
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
return allTokens;
|
|
1695
|
-
},
|
|
1696
|
-
tasty(options) {
|
|
1697
|
-
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1698
|
-
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1699
|
-
const states = {
|
|
1700
|
-
dark: options?.states?.dark ?? globalConfig.states.dark,
|
|
1701
|
-
highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
|
|
1702
|
-
};
|
|
1703
|
-
const modes = resolveModes(options?.modes);
|
|
1704
|
-
const allTokens = {};
|
|
1705
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1706
|
-
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1707
|
-
const resolved = theme.resolve();
|
|
1708
|
-
const prefix = resolvePrefix(options, themeName, true);
|
|
1709
|
-
const tokens = buildTokenMap(filterCollisions(resolved, prefix, seen, themeName), prefix, states, modes, options?.format);
|
|
1710
|
-
Object.assign(allTokens, tokens);
|
|
1711
|
-
if (themeName === effectivePrimary) {
|
|
1712
|
-
const unprefixed = buildTokenMap(filterCollisions(resolved, "", seen, themeName, true), "", states, modes, options?.format);
|
|
1713
|
-
Object.assign(allTokens, unprefixed);
|
|
1714
|
-
}
|
|
1715
|
-
}
|
|
1716
|
-
return allTokens;
|
|
1717
|
-
},
|
|
1718
|
-
json(options) {
|
|
1719
|
-
const modes = resolveModes(options?.modes);
|
|
1720
|
-
const result = {};
|
|
1721
|
-
for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, options?.format);
|
|
1722
|
-
return result;
|
|
1723
|
-
},
|
|
1724
|
-
css(options) {
|
|
1725
|
-
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
1726
|
-
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
1727
|
-
const suffix = options?.suffix ?? "-color";
|
|
1728
|
-
const format = options?.format ?? "rgb";
|
|
1729
|
-
const allLines = {
|
|
1730
|
-
light: [],
|
|
1731
|
-
dark: [],
|
|
1732
|
-
lightContrast: [],
|
|
1733
|
-
darkContrast: []
|
|
1734
|
-
};
|
|
1735
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1736
|
-
for (const [themeName, theme] of Object.entries(themes)) {
|
|
1737
|
-
const resolved = theme.resolve();
|
|
1738
|
-
const prefix = resolvePrefix(options, themeName, true);
|
|
1739
|
-
const css = buildCssMap(filterCollisions(resolved, prefix, seen, themeName), prefix, suffix, format);
|
|
1740
|
-
for (const key of [
|
|
1741
|
-
"light",
|
|
1742
|
-
"dark",
|
|
1743
|
-
"lightContrast",
|
|
1744
|
-
"darkContrast"
|
|
1745
|
-
]) if (css[key]) allLines[key].push(css[key]);
|
|
1746
|
-
if (themeName === effectivePrimary) {
|
|
1747
|
-
const unprefixed = buildCssMap(filterCollisions(resolved, "", seen, themeName, true), "", suffix, format);
|
|
1748
|
-
for (const key of [
|
|
1749
|
-
"light",
|
|
1750
|
-
"dark",
|
|
1751
|
-
"lightContrast",
|
|
1752
|
-
"darkContrast"
|
|
1753
|
-
]) if (unprefixed[key]) allLines[key].push(unprefixed[key]);
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
return {
|
|
1757
|
-
light: allLines.light.join("\n"),
|
|
1758
|
-
dark: allLines.dark.join("\n"),
|
|
1759
|
-
lightContrast: allLines.lightContrast.join("\n"),
|
|
1760
|
-
darkContrast: allLines.darkContrast.join("\n")
|
|
1761
|
-
};
|
|
1762
|
-
}
|
|
1763
|
-
};
|
|
1730
|
+
function isStructuredColorInput(input) {
|
|
1731
|
+
return typeof input === "object" && input !== null && !Array.isArray(input) && "hue" in input && "lightness" in input;
|
|
1764
1732
|
}
|
|
1765
1733
|
/**
|
|
1766
1734
|
* Matches the CSS color functions Glaze itself emits (`rgb()`, `hsl()`,
|
|
@@ -1879,9 +1847,7 @@ function validateOkhslColor(value) {
|
|
|
1879
1847
|
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) throw new Error("glaze.color: OkhslColor h/s/l must be finite numbers.");
|
|
1880
1848
|
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)?");
|
|
1881
1849
|
}
|
|
1882
|
-
/**
|
|
1883
|
-
* Validate a user-supplied `[r, g, b]` tuple in 0-255.
|
|
1884
|
-
*/
|
|
1850
|
+
/** Validate a user-supplied `[r, g, b]` tuple in 0-255. */
|
|
1885
1851
|
function validateRgbTuple(value) {
|
|
1886
1852
|
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(", ")}]).`);
|
|
1887
1853
|
}
|
|
@@ -2000,17 +1966,20 @@ function buildStandaloneValueDefs(main, options) {
|
|
|
2000
1966
|
primary
|
|
2001
1967
|
};
|
|
2002
1968
|
}
|
|
2003
|
-
function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveScaling, baseToken, exportData) {
|
|
1969
|
+
function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveScaling, baseToken, exportData, autoFlip) {
|
|
2004
1970
|
let cached;
|
|
2005
1971
|
const resolveOnce = () => {
|
|
2006
1972
|
if (cached) return cached;
|
|
2007
|
-
cached = resolveAllColors(seedHue, seedSaturation, defs, effectiveScaling, baseToken ? new Map([[STANDALONE_BASE, baseToken.resolve()]]) : void 0);
|
|
1973
|
+
cached = resolveAllColors(seedHue, seedSaturation, defs, effectiveScaling, baseToken ? new Map([[STANDALONE_BASE, baseToken.resolve()]]) : void 0, autoFlip);
|
|
2008
1974
|
return cached;
|
|
2009
1975
|
};
|
|
2010
|
-
const resolveStates = (options) =>
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
1976
|
+
const resolveStates = (options) => {
|
|
1977
|
+
const cfg = getConfig();
|
|
1978
|
+
return {
|
|
1979
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
1980
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
1981
|
+
};
|
|
1982
|
+
};
|
|
2014
1983
|
const tokenLike = (options) => {
|
|
2015
1984
|
return buildTokenMap(resolveOnce(), "", resolveStates(options), resolveModes(options?.modes), options?.format)[`#${primary}`];
|
|
2016
1985
|
};
|
|
@@ -2040,39 +2009,7 @@ function resolveBaseToken(base) {
|
|
|
2040
2009
|
if (isGlazeColorToken(base)) return base;
|
|
2041
2010
|
return createColorTokenFromValue(base, void 0, void 0);
|
|
2042
2011
|
}
|
|
2043
|
-
|
|
2044
|
-
* Build a JSON-safe snapshot of `GlazeColorOverrides`. `base` is
|
|
2045
|
-
* recursively serialized when it was originally a token; raw values are
|
|
2046
|
-
* preserved as-is so `glaze.colorFrom(...)` round-trips them.
|
|
2047
|
-
*/
|
|
2048
|
-
function buildOverridesExport(options) {
|
|
2049
|
-
const out = {};
|
|
2050
|
-
if (options.hue !== void 0) out.hue = options.hue;
|
|
2051
|
-
if (options.saturation !== void 0) out.saturation = options.saturation;
|
|
2052
|
-
if (options.lightness !== void 0) out.lightness = options.lightness;
|
|
2053
|
-
if (options.saturationFactor !== void 0) out.saturationFactor = options.saturationFactor;
|
|
2054
|
-
if (options.mode !== void 0) out.mode = options.mode;
|
|
2055
|
-
if (options.contrast !== void 0) out.contrast = options.contrast;
|
|
2056
|
-
if (options.opacity !== void 0) out.opacity = options.opacity;
|
|
2057
|
-
if (options.name !== void 0) out.name = options.name;
|
|
2058
|
-
if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export() : options.base;
|
|
2059
|
-
return out;
|
|
2060
|
-
}
|
|
2061
|
-
function buildStructuredInputExport(input) {
|
|
2062
|
-
const out = {
|
|
2063
|
-
hue: input.hue,
|
|
2064
|
-
saturation: input.saturation,
|
|
2065
|
-
lightness: input.lightness
|
|
2066
|
-
};
|
|
2067
|
-
if (input.saturationFactor !== void 0) out.saturationFactor = input.saturationFactor;
|
|
2068
|
-
if (input.mode !== void 0) out.mode = input.mode;
|
|
2069
|
-
if (input.opacity !== void 0) out.opacity = input.opacity;
|
|
2070
|
-
if (input.contrast !== void 0) out.contrast = input.contrast;
|
|
2071
|
-
if (input.name !== void 0) out.name = input.name;
|
|
2072
|
-
if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export() : input.base;
|
|
2073
|
-
return out;
|
|
2074
|
-
}
|
|
2075
|
-
function createColorToken(input, scaling) {
|
|
2012
|
+
function createColorToken(input, scaling, overrideAutoFlip) {
|
|
2076
2013
|
validateStructuredInput(input);
|
|
2077
2014
|
const userName = input.name;
|
|
2078
2015
|
if (userName !== void 0) validateStandaloneName(userName);
|
|
@@ -2094,40 +2031,70 @@ function createColorToken(input, scaling) {
|
|
|
2094
2031
|
mode: "static"
|
|
2095
2032
|
};
|
|
2096
2033
|
const effectiveScaling = scaling ?? defaultStandaloneScaling(false);
|
|
2034
|
+
const autoFlip = overrideAutoFlip ?? getConfig().autoFlip;
|
|
2097
2035
|
const exportData = () => ({
|
|
2098
2036
|
form: "structured",
|
|
2099
2037
|
input: buildStructuredInputExport(input),
|
|
2100
|
-
scaling: effectiveScaling
|
|
2038
|
+
scaling: effectiveScaling,
|
|
2039
|
+
autoFlip
|
|
2101
2040
|
});
|
|
2102
|
-
return createColorTokenFromDefs(input.hue, input.saturation, defs, primary, effectiveScaling, baseToken, exportData);
|
|
2041
|
+
return createColorTokenFromDefs(input.hue, input.saturation, defs, primary, effectiveScaling, baseToken, exportData, autoFlip);
|
|
2103
2042
|
}
|
|
2104
|
-
function createColorTokenFromValue(value, options, scaling) {
|
|
2043
|
+
function createColorTokenFromValue(value, options, scaling, overrideAutoFlip) {
|
|
2105
2044
|
const inputIsString = typeof value === "string";
|
|
2106
2045
|
const main = extractOkhslFromValue(value);
|
|
2107
2046
|
const baseToken = resolveBaseToken(options?.base);
|
|
2108
2047
|
const { seedHue, seedSaturation, defs, primary } = buildStandaloneValueDefs(main, options);
|
|
2109
2048
|
const effectiveScaling = scaling ?? defaultStandaloneScaling(inputIsString);
|
|
2049
|
+
const autoFlip = overrideAutoFlip ?? getConfig().autoFlip;
|
|
2110
2050
|
const exportData = () => ({
|
|
2111
2051
|
form: "value",
|
|
2112
2052
|
input: value,
|
|
2113
2053
|
...options !== void 0 ? { overrides: buildOverridesExport(options) } : {},
|
|
2114
|
-
scaling: effectiveScaling
|
|
2054
|
+
scaling: effectiveScaling,
|
|
2055
|
+
autoFlip
|
|
2115
2056
|
});
|
|
2116
|
-
return createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveScaling, baseToken, exportData);
|
|
2057
|
+
return createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveScaling, baseToken, exportData, autoFlip);
|
|
2117
2058
|
}
|
|
2118
2059
|
/**
|
|
2119
|
-
*
|
|
2120
|
-
*
|
|
2060
|
+
* Build a JSON-safe snapshot of `GlazeColorOverrides`. `base` is
|
|
2061
|
+
* recursively serialized when it was originally a token; raw values are
|
|
2062
|
+
* preserved as-is so `glaze.colorFrom(...)` round-trips them.
|
|
2121
2063
|
*/
|
|
2122
|
-
function
|
|
2123
|
-
|
|
2124
|
-
if (
|
|
2125
|
-
if (
|
|
2126
|
-
if (
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2064
|
+
function buildOverridesExport(options) {
|
|
2065
|
+
const out = {};
|
|
2066
|
+
if (options.hue !== void 0) out.hue = options.hue;
|
|
2067
|
+
if (options.saturation !== void 0) out.saturation = options.saturation;
|
|
2068
|
+
if (options.lightness !== void 0) out.lightness = options.lightness;
|
|
2069
|
+
if (options.saturationFactor !== void 0) out.saturationFactor = options.saturationFactor;
|
|
2070
|
+
if (options.mode !== void 0) out.mode = options.mode;
|
|
2071
|
+
if (options.contrast !== void 0) out.contrast = options.contrast;
|
|
2072
|
+
if (options.opacity !== void 0) out.opacity = options.opacity;
|
|
2073
|
+
if (options.name !== void 0) out.name = options.name;
|
|
2074
|
+
if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export() : options.base;
|
|
2075
|
+
return out;
|
|
2076
|
+
}
|
|
2077
|
+
function buildStructuredInputExport(input) {
|
|
2078
|
+
const out = {
|
|
2079
|
+
hue: input.hue,
|
|
2080
|
+
saturation: input.saturation,
|
|
2081
|
+
lightness: input.lightness
|
|
2082
|
+
};
|
|
2083
|
+
if (input.saturationFactor !== void 0) out.saturationFactor = input.saturationFactor;
|
|
2084
|
+
if (input.mode !== void 0) out.mode = input.mode;
|
|
2085
|
+
if (input.opacity !== void 0) out.opacity = input.opacity;
|
|
2086
|
+
if (input.contrast !== void 0) out.contrast = input.contrast;
|
|
2087
|
+
if (input.name !== void 0) out.name = input.name;
|
|
2088
|
+
if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export() : input.base;
|
|
2089
|
+
return out;
|
|
2090
|
+
}
|
|
2091
|
+
/**
|
|
2092
|
+
* Discriminate a `GlazeColorTokenExport` from a raw `GlazeColorValue`.
|
|
2093
|
+
* `GlazeColorTokenExport` always has a `form` field set to either
|
|
2094
|
+
* `'value'` or `'structured'`; raw values never do.
|
|
2095
|
+
*/
|
|
2096
|
+
function isExportedToken(candidate) {
|
|
2097
|
+
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate) && "form" in candidate && (candidate.form === "value" || candidate.form === "structured");
|
|
2131
2098
|
}
|
|
2132
2099
|
function rehydrateOverrides(data) {
|
|
2133
2100
|
const out = {};
|
|
@@ -2157,13 +2124,264 @@ function rehydrateStructuredInput(data) {
|
|
|
2157
2124
|
return out;
|
|
2158
2125
|
}
|
|
2159
2126
|
/**
|
|
2160
|
-
*
|
|
2161
|
-
*
|
|
2162
|
-
* `'value'` or `'structured'`; raw values never do.
|
|
2127
|
+
* Rehydrate a token from its `.export()` snapshot. Recursively rebuilds
|
|
2128
|
+
* any base dependency. Inverse of `GlazeColorToken.export()`.
|
|
2163
2129
|
*/
|
|
2164
|
-
function
|
|
2165
|
-
|
|
2130
|
+
function colorFromExport(data) {
|
|
2131
|
+
if (data === null || typeof data !== "object") throw new Error(`glaze.colorFrom: expected an object from token.export(), got ${data === null ? "null" : typeof data}.`);
|
|
2132
|
+
if (data.form !== "value" && data.form !== "structured") throw new Error(`glaze.colorFrom: invalid "form" field — expected "value" or "structured" (got ${JSON.stringify(data.form)}).`);
|
|
2133
|
+
if (data.input === void 0) throw new Error(`glaze.colorFrom: missing "input" field — expected the original ${data.form === "value" ? "GlazeColorValue" : "GlazeColorInput"}.`);
|
|
2134
|
+
if (data.form === "value") {
|
|
2135
|
+
const value = data.input;
|
|
2136
|
+
const overrides = data.overrides ? rehydrateOverrides(data.overrides) : void 0;
|
|
2137
|
+
const cfg = getConfig();
|
|
2138
|
+
const effectiveAutoFlip = data.autoFlip ?? cfg.autoFlip;
|
|
2139
|
+
return createColorTokenFromValue(value, overrides, data.scaling, effectiveAutoFlip);
|
|
2140
|
+
}
|
|
2141
|
+
const input = rehydrateStructuredInput(data.input);
|
|
2142
|
+
const cfg = getConfig();
|
|
2143
|
+
const effectiveAutoFlip = data.autoFlip ?? cfg.autoFlip;
|
|
2144
|
+
return createColorToken(input, data.scaling, effectiveAutoFlip);
|
|
2166
2145
|
}
|
|
2146
|
+
|
|
2147
|
+
//#endregion
|
|
2148
|
+
//#region src/palette.ts
|
|
2149
|
+
/**
|
|
2150
|
+
* Palette factory.
|
|
2151
|
+
*
|
|
2152
|
+
* Composes multiple themes into a single token namespace with optional
|
|
2153
|
+
* theme-name prefixes and a "primary theme" that also surfaces an
|
|
2154
|
+
* unprefixed copy of its tokens. All four export methods (`tokens` /
|
|
2155
|
+
* `tasty` / `json` / `css`) share a `buildPaletteOutput` driver that
|
|
2156
|
+
* handles validation, per-theme iteration, prefix resolution, collision
|
|
2157
|
+
* filtering, and primary duplication.
|
|
2158
|
+
*/
|
|
2159
|
+
function resolvePrefix(options, themeName, defaultPrefix = false) {
|
|
2160
|
+
const prefix = options?.prefix ?? defaultPrefix;
|
|
2161
|
+
if (prefix === true) return `${themeName}-`;
|
|
2162
|
+
if (typeof prefix === "object" && prefix !== null) return prefix[themeName] ?? `${themeName}-`;
|
|
2163
|
+
return "";
|
|
2164
|
+
}
|
|
2165
|
+
function validatePrimaryTheme(primary, themes) {
|
|
2166
|
+
if (primary !== void 0 && !(primary in themes)) {
|
|
2167
|
+
const available = Object.keys(themes).join(", ");
|
|
2168
|
+
throw new Error(`glaze: primary theme "${primary}" not found in palette. Available: ${available}.`);
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
/**
|
|
2172
|
+
* Resolve the effective primary for an export call.
|
|
2173
|
+
* `false` disables, a string overrides, `undefined` inherits from palette.
|
|
2174
|
+
*/
|
|
2175
|
+
function resolveEffectivePrimary(exportPrimary, palettePrimary) {
|
|
2176
|
+
if (exportPrimary === false) return void 0;
|
|
2177
|
+
return exportPrimary ?? palettePrimary;
|
|
2178
|
+
}
|
|
2179
|
+
/**
|
|
2180
|
+
* Filter a resolved color map, skipping keys already in `seen`.
|
|
2181
|
+
* Warns on collision and keeps the first-written value (first-write-wins).
|
|
2182
|
+
* Returns a new map containing only non-colliding entries.
|
|
2183
|
+
*/
|
|
2184
|
+
function filterCollisions(resolved, prefix, seen, themeName, isPrimary) {
|
|
2185
|
+
const filtered = /* @__PURE__ */ new Map();
|
|
2186
|
+
const label = isPrimary ? `${themeName} (primary)` : themeName;
|
|
2187
|
+
for (const [name, color] of resolved) {
|
|
2188
|
+
const key = `${prefix}${name}`;
|
|
2189
|
+
if (seen.has(key)) {
|
|
2190
|
+
console.warn(`glaze: token "${key}" from theme "${label}" collides with theme "${seen.get(key)}" — skipping.`);
|
|
2191
|
+
continue;
|
|
2192
|
+
}
|
|
2193
|
+
seen.set(key, label);
|
|
2194
|
+
filtered.set(name, color);
|
|
2195
|
+
}
|
|
2196
|
+
return filtered;
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* Shared per-theme driver for `tokens` / `tasty` / `css`. `json` skips
|
|
2200
|
+
* this because it doesn't do collision filtering or primary duplication.
|
|
2201
|
+
*/
|
|
2202
|
+
function buildPaletteOutput(themes, paletteOptions, options, buildOne, merge, empty) {
|
|
2203
|
+
const effectivePrimary = resolveEffectivePrimary(options?.primary, paletteOptions?.primary);
|
|
2204
|
+
if (options?.primary !== void 0) validatePrimaryTheme(effectivePrimary, themes);
|
|
2205
|
+
const acc = empty();
|
|
2206
|
+
const seen = /* @__PURE__ */ new Map();
|
|
2207
|
+
for (const [themeName, theme] of Object.entries(themes)) {
|
|
2208
|
+
const resolved = theme.resolve();
|
|
2209
|
+
const prefix = resolvePrefix(options, themeName, true);
|
|
2210
|
+
merge(acc, buildOne(filterCollisions(resolved, prefix, seen, themeName), prefix));
|
|
2211
|
+
if (themeName === effectivePrimary) merge(acc, buildOne(filterCollisions(resolved, "", seen, themeName, true), ""));
|
|
2212
|
+
}
|
|
2213
|
+
return acc;
|
|
2214
|
+
}
|
|
2215
|
+
function createPalette(themes, paletteOptions) {
|
|
2216
|
+
validatePrimaryTheme(paletteOptions?.primary, themes);
|
|
2217
|
+
return {
|
|
2218
|
+
tokens(options) {
|
|
2219
|
+
const modes = resolveModes(options?.modes);
|
|
2220
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix) => buildFlatTokenMap(filtered, prefix, modes, options?.format), (acc, part) => {
|
|
2221
|
+
for (const variant of Object.keys(part)) {
|
|
2222
|
+
if (!acc[variant]) acc[variant] = {};
|
|
2223
|
+
Object.assign(acc[variant], part[variant]);
|
|
2224
|
+
}
|
|
2225
|
+
}, () => ({}));
|
|
2226
|
+
},
|
|
2227
|
+
tasty(options) {
|
|
2228
|
+
const cfg = getConfig();
|
|
2229
|
+
const states = {
|
|
2230
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
2231
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
2232
|
+
};
|
|
2233
|
+
const modes = resolveModes(options?.modes);
|
|
2234
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix) => buildTokenMap(filtered, prefix, states, modes, options?.format), (acc, part) => Object.assign(acc, part), () => ({}));
|
|
2235
|
+
},
|
|
2236
|
+
json(options) {
|
|
2237
|
+
const modes = resolveModes(options?.modes);
|
|
2238
|
+
const result = {};
|
|
2239
|
+
for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, options?.format);
|
|
2240
|
+
return result;
|
|
2241
|
+
},
|
|
2242
|
+
css(options) {
|
|
2243
|
+
const suffix = options?.suffix ?? "-color";
|
|
2244
|
+
const format = options?.format ?? "rgb";
|
|
2245
|
+
const lines = buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix) => buildCssMap(filtered, prefix, suffix, format), (acc, part) => {
|
|
2246
|
+
for (const key of [
|
|
2247
|
+
"light",
|
|
2248
|
+
"dark",
|
|
2249
|
+
"lightContrast",
|
|
2250
|
+
"darkContrast"
|
|
2251
|
+
]) if (part[key]) acc[key].push(part[key]);
|
|
2252
|
+
}, () => ({
|
|
2253
|
+
light: [],
|
|
2254
|
+
dark: [],
|
|
2255
|
+
lightContrast: [],
|
|
2256
|
+
darkContrast: []
|
|
2257
|
+
}));
|
|
2258
|
+
return {
|
|
2259
|
+
light: lines.light.join("\n"),
|
|
2260
|
+
dark: lines.dark.join("\n"),
|
|
2261
|
+
lightContrast: lines.lightContrast.join("\n"),
|
|
2262
|
+
darkContrast: lines.darkContrast.join("\n")
|
|
2263
|
+
};
|
|
2264
|
+
}
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
|
|
2268
|
+
//#endregion
|
|
2269
|
+
//#region src/theme.ts
|
|
2270
|
+
/**
|
|
2271
|
+
* Theme factory.
|
|
2272
|
+
*
|
|
2273
|
+
* Wraps a hue/saturation seed and a mutable `ColorMap`, and exposes
|
|
2274
|
+
* `tokens()` / `tasty()` / `json()` / `css()` / `resolve()` / `export()`
|
|
2275
|
+
* / `extend()`. Caches the last resolve result so successive exports
|
|
2276
|
+
* with the same defs and config don't re-run the four-pass resolver.
|
|
2277
|
+
*/
|
|
2278
|
+
function createTheme(hue, saturation, initialColors) {
|
|
2279
|
+
let colorDefs = initialColors ? { ...initialColors } : {};
|
|
2280
|
+
let cache = null;
|
|
2281
|
+
function resolveCached() {
|
|
2282
|
+
const version = getConfigVersion();
|
|
2283
|
+
if (cache && cache.version === version) return cache.map;
|
|
2284
|
+
const map = resolveAllColors(hue, saturation, colorDefs);
|
|
2285
|
+
cache = {
|
|
2286
|
+
map,
|
|
2287
|
+
version
|
|
2288
|
+
};
|
|
2289
|
+
return map;
|
|
2290
|
+
}
|
|
2291
|
+
function invalidate() {
|
|
2292
|
+
cache = null;
|
|
2293
|
+
}
|
|
2294
|
+
return {
|
|
2295
|
+
get hue() {
|
|
2296
|
+
return hue;
|
|
2297
|
+
},
|
|
2298
|
+
get saturation() {
|
|
2299
|
+
return saturation;
|
|
2300
|
+
},
|
|
2301
|
+
colors(defs) {
|
|
2302
|
+
colorDefs = {
|
|
2303
|
+
...colorDefs,
|
|
2304
|
+
...defs
|
|
2305
|
+
};
|
|
2306
|
+
invalidate();
|
|
2307
|
+
},
|
|
2308
|
+
color(name, def) {
|
|
2309
|
+
if (def === void 0) return colorDefs[name];
|
|
2310
|
+
colorDefs[name] = def;
|
|
2311
|
+
invalidate();
|
|
2312
|
+
},
|
|
2313
|
+
remove(names) {
|
|
2314
|
+
const list = Array.isArray(names) ? names : [names];
|
|
2315
|
+
for (const name of list) delete colorDefs[name];
|
|
2316
|
+
invalidate();
|
|
2317
|
+
},
|
|
2318
|
+
has(name) {
|
|
2319
|
+
return name in colorDefs;
|
|
2320
|
+
},
|
|
2321
|
+
list() {
|
|
2322
|
+
return Object.keys(colorDefs);
|
|
2323
|
+
},
|
|
2324
|
+
reset() {
|
|
2325
|
+
colorDefs = {};
|
|
2326
|
+
invalidate();
|
|
2327
|
+
},
|
|
2328
|
+
export() {
|
|
2329
|
+
return {
|
|
2330
|
+
hue,
|
|
2331
|
+
saturation,
|
|
2332
|
+
colors: { ...colorDefs }
|
|
2333
|
+
};
|
|
2334
|
+
},
|
|
2335
|
+
extend(options) {
|
|
2336
|
+
const newHue = options.hue ?? hue;
|
|
2337
|
+
const newSat = options.saturation ?? saturation;
|
|
2338
|
+
const inheritedColors = {};
|
|
2339
|
+
for (const [name, def] of Object.entries(colorDefs)) if (def.inherit !== false) inheritedColors[name] = def;
|
|
2340
|
+
return createTheme(newHue, newSat, options.colors ? {
|
|
2341
|
+
...inheritedColors,
|
|
2342
|
+
...options.colors
|
|
2343
|
+
} : { ...inheritedColors });
|
|
2344
|
+
},
|
|
2345
|
+
resolve() {
|
|
2346
|
+
return new Map(resolveCached());
|
|
2347
|
+
},
|
|
2348
|
+
tokens(options) {
|
|
2349
|
+
const modes = resolveModes(options?.modes);
|
|
2350
|
+
return buildFlatTokenMap(resolveCached(), "", modes, options?.format);
|
|
2351
|
+
},
|
|
2352
|
+
tasty(options) {
|
|
2353
|
+
const cfg = getConfig();
|
|
2354
|
+
const states = {
|
|
2355
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
2356
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
2357
|
+
};
|
|
2358
|
+
const modes = resolveModes(options?.modes);
|
|
2359
|
+
return buildTokenMap(resolveCached(), "", states, modes, options?.format);
|
|
2360
|
+
},
|
|
2361
|
+
json(options) {
|
|
2362
|
+
const modes = resolveModes(options?.modes);
|
|
2363
|
+
return buildJsonMap(resolveCached(), modes, options?.format);
|
|
2364
|
+
},
|
|
2365
|
+
css(options) {
|
|
2366
|
+
return buildCssMap(resolveCached(), "", options?.suffix ?? "-color", options?.format ?? "rgb");
|
|
2367
|
+
}
|
|
2368
|
+
};
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
//#endregion
|
|
2372
|
+
//#region src/glaze.ts
|
|
2373
|
+
/**
|
|
2374
|
+
* Glaze — OKHSL-based color theme generator.
|
|
2375
|
+
*
|
|
2376
|
+
* Public API entry. Wires `glaze()` and its attached static methods to
|
|
2377
|
+
* the focused modules in this folder:
|
|
2378
|
+
* - `theme.ts` — single-theme factory
|
|
2379
|
+
* - `palette.ts` — multi-theme composition
|
|
2380
|
+
* - `color-token.ts` — standalone single-color tokens (`glaze.color`)
|
|
2381
|
+
* - `shadow.ts` — standalone shadow factory (`glaze.shadow`)
|
|
2382
|
+
* - `formatters.ts` — variant → string (`glaze.format`)
|
|
2383
|
+
* - `config.ts` — global config singleton
|
|
2384
|
+
*/
|
|
2167
2385
|
/**
|
|
2168
2386
|
* Create a single-hue glaze theme.
|
|
2169
2387
|
*
|
|
@@ -2178,41 +2396,18 @@ function glaze(hueOrOptions, saturation) {
|
|
|
2178
2396
|
if (typeof hueOrOptions === "number") return createTheme(hueOrOptions, saturation ?? 100);
|
|
2179
2397
|
return createTheme(hueOrOptions.hue, hueOrOptions.saturation);
|
|
2180
2398
|
}
|
|
2181
|
-
/**
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
glaze.configure = function configure(config) {
|
|
2185
|
-
globalConfig = {
|
|
2186
|
-
lightLightness: config.lightLightness ?? globalConfig.lightLightness,
|
|
2187
|
-
darkLightness: config.darkLightness ?? globalConfig.darkLightness,
|
|
2188
|
-
darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
|
|
2189
|
-
darkCurve: config.darkCurve ?? globalConfig.darkCurve,
|
|
2190
|
-
states: {
|
|
2191
|
-
dark: config.states?.dark ?? globalConfig.states.dark,
|
|
2192
|
-
highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
|
|
2193
|
-
},
|
|
2194
|
-
modes: {
|
|
2195
|
-
dark: config.modes?.dark ?? globalConfig.modes.dark,
|
|
2196
|
-
highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
|
|
2197
|
-
},
|
|
2198
|
-
shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning
|
|
2199
|
-
};
|
|
2399
|
+
/** Configure global glaze settings. */
|
|
2400
|
+
glaze.configure = function configure$1(config) {
|
|
2401
|
+
configure(config);
|
|
2200
2402
|
};
|
|
2201
|
-
/**
|
|
2202
|
-
* Compose multiple themes into a palette.
|
|
2203
|
-
*/
|
|
2403
|
+
/** Compose multiple themes into a palette. */
|
|
2204
2404
|
glaze.palette = function palette(themes, options) {
|
|
2205
2405
|
return createPalette(themes, options);
|
|
2206
2406
|
};
|
|
2207
|
-
/**
|
|
2208
|
-
* Create a theme from a serialized export.
|
|
2209
|
-
*/
|
|
2407
|
+
/** Create a theme from a serialized export. */
|
|
2210
2408
|
glaze.from = function from(data) {
|
|
2211
2409
|
return createTheme(data.hue, data.saturation, data.colors);
|
|
2212
2410
|
};
|
|
2213
|
-
function isStructuredColorInput(input) {
|
|
2214
|
-
return typeof input === "object" && input !== null && !Array.isArray(input) && "hue" in input && "lightness" in input;
|
|
2215
|
-
}
|
|
2216
2411
|
/**
|
|
2217
2412
|
* Create a standalone single-color token.
|
|
2218
2413
|
*
|
|
@@ -2276,9 +2471,7 @@ glaze.shadow = function shadow(input) {
|
|
|
2276
2471
|
alpha: 1
|
|
2277
2472
|
} : void 0, input.intensity, tuning);
|
|
2278
2473
|
};
|
|
2279
|
-
/**
|
|
2280
|
-
* Format a resolved color variant as a CSS string.
|
|
2281
|
-
*/
|
|
2474
|
+
/** Format a resolved color variant as a CSS string. */
|
|
2282
2475
|
glaze.format = function format(variant, colorFormat) {
|
|
2283
2476
|
return formatVariant(variant, colorFormat);
|
|
2284
2477
|
};
|
|
@@ -2324,30 +2517,13 @@ glaze.fromRgb = function fromRgb(r, g, b) {
|
|
|
2324
2517
|
glaze.colorFrom = function colorFrom(data) {
|
|
2325
2518
|
return colorFromExport(data);
|
|
2326
2519
|
};
|
|
2327
|
-
/**
|
|
2328
|
-
* Get the current global configuration (for testing/debugging).
|
|
2329
|
-
*/
|
|
2520
|
+
/** Get the current global configuration (for testing/debugging). */
|
|
2330
2521
|
glaze.getConfig = function getConfig() {
|
|
2331
|
-
return
|
|
2522
|
+
return snapshotConfig();
|
|
2332
2523
|
};
|
|
2333
|
-
/**
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
glaze.resetConfig = function resetConfig() {
|
|
2337
|
-
globalConfig = {
|
|
2338
|
-
lightLightness: [10, 100],
|
|
2339
|
-
darkLightness: [15, 95],
|
|
2340
|
-
darkDesaturation: .1,
|
|
2341
|
-
darkCurve: .5,
|
|
2342
|
-
states: {
|
|
2343
|
-
dark: "@dark",
|
|
2344
|
-
highContrast: "@high-contrast"
|
|
2345
|
-
},
|
|
2346
|
-
modes: {
|
|
2347
|
-
dark: true,
|
|
2348
|
-
highContrast: false
|
|
2349
|
-
}
|
|
2350
|
-
};
|
|
2524
|
+
/** Reset global configuration to defaults. */
|
|
2525
|
+
glaze.resetConfig = function resetConfig$1() {
|
|
2526
|
+
resetConfig();
|
|
2351
2527
|
};
|
|
2352
2528
|
|
|
2353
2529
|
//#endregion
|