@tenphi/glaze 0.0.0-snapshot.8f16949 → 0.0.0-snapshot.94c4efb
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 +86 -31
- package/dist/index.cjs +1684 -735
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +435 -58
- package/dist/index.d.mts +435 -58
- package/dist/index.mjs +1674 -736
- package/dist/index.mjs.map +1 -1
- package/docs/api.md +927 -424
- package/docs/methodology.md +176 -53
- package/docs/migration.md +83 -117
- package/docs/okhst.md +148 -204
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -590,6 +590,24 @@ function formatOkhsl(h, s, l, pastel = false) {
|
|
|
590
590
|
return `okhsl(${fmt$1(h, 2)} ${fmt$1(outS, 2)}% ${fmt$1(l, 2)}%)`;
|
|
591
591
|
}
|
|
592
592
|
/**
|
|
593
|
+
* Format OKHST values as a CSS `okhst(H S% T%)` string.
|
|
594
|
+
* h: 0–360, s: 0–100, t: 0–100 (percentage scale for s and t).
|
|
595
|
+
*
|
|
596
|
+
* Pastel recompute matches `formatOkhsl`: convert via OKLab so external
|
|
597
|
+
* parsers that only understand non-pastel OKHST render identically.
|
|
598
|
+
*/
|
|
599
|
+
function formatOkhst(h, s, t, pastel = false) {
|
|
600
|
+
let outS = s;
|
|
601
|
+
if (pastel) {
|
|
602
|
+
const REF_EPS = .05;
|
|
603
|
+
const den = Math.log(1 + REF_EPS) - Math.log(REF_EPS);
|
|
604
|
+
const y = Math.exp(t / 100 * den + Math.log(REF_EPS)) - REF_EPS;
|
|
605
|
+
const l = toe(Math.cbrt(Math.max(0, y)));
|
|
606
|
+
outS = oklabToOkhsl(okhslToOklab(h, s / 100, l, true), false)[1] * 100;
|
|
607
|
+
}
|
|
608
|
+
return `okhst(${fmt$1(h, 2)} ${fmt$1(outS, 2)}% ${fmt$1(t, 2)}%)`;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
593
611
|
* Format OKHSL values as a CSS `rgb(R G B)` string.
|
|
594
612
|
* Uses 2 decimal places to avoid 8-bit quantization contrast loss.
|
|
595
613
|
* h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
|
|
@@ -657,6 +675,8 @@ function okhslToOklch(h, s, l, pastel = false) {
|
|
|
657
675
|
/**
|
|
658
676
|
* Build a fresh defaults object. Called from module init and from
|
|
659
677
|
* `resetConfig()` so the two paths can't drift.
|
|
678
|
+
*
|
|
679
|
+
* `pastel: false` is a fixed instance default — not globally configurable.
|
|
660
680
|
*/
|
|
661
681
|
function defaultConfig() {
|
|
662
682
|
return {
|
|
@@ -672,8 +692,8 @@ function defaultConfig() {
|
|
|
672
692
|
},
|
|
673
693
|
darkDesaturation: .1,
|
|
674
694
|
states: {
|
|
675
|
-
dark: "@dark",
|
|
676
|
-
highContrast: "@
|
|
695
|
+
dark: "@media(prefers-color-scheme: dark)",
|
|
696
|
+
highContrast: "@media(prefers-contrast: more)"
|
|
677
697
|
},
|
|
678
698
|
modes: {
|
|
679
699
|
dark: true,
|
|
@@ -681,9 +701,21 @@ function defaultConfig() {
|
|
|
681
701
|
},
|
|
682
702
|
autoFlip: true,
|
|
683
703
|
pastel: false,
|
|
684
|
-
inferRole: true
|
|
704
|
+
inferRole: true,
|
|
705
|
+
contrastLevel: "auto"
|
|
685
706
|
};
|
|
686
707
|
}
|
|
708
|
+
/**
|
|
709
|
+
* Validate an authored `contrastLevel`. Numbers clamp to 0–100 so a slider
|
|
710
|
+
* can't produce an out-of-range level; a non-finite number throws rather than
|
|
711
|
+
* silently reading as `'auto'`, which would turn a `NaN` slider value into an
|
|
712
|
+
* invisible no-op.
|
|
713
|
+
*/
|
|
714
|
+
function normalizeContrastLevel(value) {
|
|
715
|
+
if (value === void 0 || value === "auto") return "auto";
|
|
716
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new Error(`glaze: contrastLevel must be a finite number 0–100 or 'auto' (got ${String(value)}).`);
|
|
717
|
+
return Math.max(0, Math.min(100, value));
|
|
718
|
+
}
|
|
687
719
|
let globalConfig = defaultConfig();
|
|
688
720
|
/**
|
|
689
721
|
* Monotonic counter incremented on every `configure()` / `resetConfig()`
|
|
@@ -721,8 +753,9 @@ function configure(config) {
|
|
|
721
753
|
},
|
|
722
754
|
shadowTuning: config.shadowTuning ?? globalConfig.shadowTuning,
|
|
723
755
|
autoFlip: config.autoFlip ?? globalConfig.autoFlip,
|
|
724
|
-
pastel:
|
|
725
|
-
inferRole: config.inferRole ?? globalConfig.inferRole
|
|
756
|
+
pastel: false,
|
|
757
|
+
inferRole: config.inferRole ?? globalConfig.inferRole,
|
|
758
|
+
contrastLevel: normalizeContrastLevel(config.contrastLevel ?? globalConfig.contrastLevel)
|
|
726
759
|
};
|
|
727
760
|
}
|
|
728
761
|
function resetConfig() {
|
|
@@ -734,9 +767,15 @@ function resetConfig() {
|
|
|
734
767
|
* Only fields present in `override` are replaced; others fall through
|
|
735
768
|
* from `base`. `false` for tone windows passes through as-is
|
|
736
769
|
* (treated as the full range by `activeWindow()` in okhst.ts).
|
|
770
|
+
*
|
|
771
|
+
* `pastel` is instance-only: `override.pastel ?? false`, never inherited
|
|
772
|
+
* from the global base.
|
|
737
773
|
*/
|
|
738
774
|
function mergeConfig(base, override) {
|
|
739
|
-
if (!override) return base
|
|
775
|
+
if (!override) return base.pastel === false ? base : {
|
|
776
|
+
...base,
|
|
777
|
+
pastel: false
|
|
778
|
+
};
|
|
740
779
|
return {
|
|
741
780
|
lightTone: override.lightTone !== void 0 ? override.lightTone : base.lightTone,
|
|
742
781
|
darkTone: override.darkTone !== void 0 ? override.darkTone : base.darkTone,
|
|
@@ -745,9 +784,101 @@ function mergeConfig(base, override) {
|
|
|
745
784
|
modes: base.modes,
|
|
746
785
|
shadowTuning: override.shadowTuning ?? base.shadowTuning,
|
|
747
786
|
autoFlip: override.autoFlip ?? base.autoFlip,
|
|
748
|
-
pastel: override.pastel ??
|
|
749
|
-
inferRole: override.inferRole ?? base.inferRole
|
|
787
|
+
pastel: override.pastel ?? false,
|
|
788
|
+
inferRole: override.inferRole ?? base.inferRole,
|
|
789
|
+
contrastLevel: normalizeContrastLevel(override.contrastLevel ?? base.contrastLevel)
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Freeze `getConfig() ∪ instanceLocal ∪ exportArg` as a plain, resolve-relevant
|
|
794
|
+
* override for authoring export, so restored snapshots pin these fields. Later
|
|
795
|
+
* wins; nested objects (`shadowTuning`) replace wholesale. The final
|
|
796
|
+
* `structuredClone` detaches tone-window / shadow objects that `mergeConfig`
|
|
797
|
+
* may still share with the live global config.
|
|
798
|
+
*/
|
|
799
|
+
function freezeConfigForExport(instanceLocal, exportArg) {
|
|
800
|
+
const effective = mergeConfig(getConfig(), {
|
|
801
|
+
...instanceLocal,
|
|
802
|
+
...exportArg
|
|
803
|
+
});
|
|
804
|
+
const out = {
|
|
805
|
+
lightTone: effective.lightTone,
|
|
806
|
+
darkTone: effective.darkTone,
|
|
807
|
+
darkDesaturation: effective.darkDesaturation,
|
|
808
|
+
autoFlip: effective.autoFlip,
|
|
809
|
+
pastel: effective.pastel,
|
|
810
|
+
inferRole: effective.inferRole
|
|
750
811
|
};
|
|
812
|
+
if (effective.shadowTuning !== void 0) out.shadowTuning = effective.shadowTuning;
|
|
813
|
+
const authoredLevel = exportArg?.contrastLevel ?? instanceLocal?.contrastLevel;
|
|
814
|
+
if (authoredLevel !== void 0) out.contrastLevel = normalizeContrastLevel(authoredLevel);
|
|
815
|
+
return structuredClone(out);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
//#endregion
|
|
819
|
+
//#region src/types.ts
|
|
820
|
+
/**
|
|
821
|
+
* Current authoring-export schema version. Bump when the export shape
|
|
822
|
+
* changes in a non-compatible way. Written on every `.export()` snapshot.
|
|
823
|
+
*/
|
|
824
|
+
const GLAZE_EXPORT_VERSION = 1;
|
|
825
|
+
|
|
826
|
+
//#endregion
|
|
827
|
+
//#region src/serialize.ts
|
|
828
|
+
/**
|
|
829
|
+
* Authoring-export helpers: schema version, type guards, and restore
|
|
830
|
+
* validation shared by `themeFrom` / `colorFrom` / `paletteFrom`.
|
|
831
|
+
*/
|
|
832
|
+
function isPlainObject(data) {
|
|
833
|
+
return typeof data === "object" && data !== null && !Array.isArray(data);
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Reject unknown / invalid schema versions. Missing `version` is allowed
|
|
837
|
+
* (legacy snapshots). Present versions must be an integer in
|
|
838
|
+
* `1..=GLAZE_EXPORT_VERSION`.
|
|
839
|
+
*/
|
|
840
|
+
function assertExportVersion(data, factory) {
|
|
841
|
+
if (data.version === void 0) return;
|
|
842
|
+
if (typeof data.version !== "number" || !Number.isInteger(data.version) || data.version < 1) throw new Error(`${factory}: invalid "version" field — expected an integer >= 1 (got ${JSON.stringify(data.version)}).`);
|
|
843
|
+
if (data.version > GLAZE_EXPORT_VERSION) throw new Error(`${factory}: unsupported export version ${data.version} (this library supports version ${GLAZE_EXPORT_VERSION}). Upgrade @tenphi/glaze to load this snapshot.`);
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* When `kind` is present, it must match the factory. Missing `kind` is
|
|
847
|
+
* allowed for legacy snapshots.
|
|
848
|
+
*/
|
|
849
|
+
function assertExportKind(data, expected, factory) {
|
|
850
|
+
if (data.kind === void 0) return;
|
|
851
|
+
if (data.kind !== expected) throw new Error(`${factory}: expected kind "${expected}", got ${JSON.stringify(data.kind)}.`);
|
|
852
|
+
}
|
|
853
|
+
function hasThemeShape(data) {
|
|
854
|
+
return typeof data.hue === "number" && typeof data.saturation === "number" && !("form" in data) && !("themes" in data);
|
|
855
|
+
}
|
|
856
|
+
function hasColorTokenShape(data) {
|
|
857
|
+
return data.form === "value" || data.form === "structured";
|
|
858
|
+
}
|
|
859
|
+
function hasPaletteShape(data) {
|
|
860
|
+
return isPlainObject(data.themes) && !("form" in data) && typeof data.hue !== "number";
|
|
861
|
+
}
|
|
862
|
+
/** Type guard for theme authoring snapshots (prefers `kind`, falls back to shape). */
|
|
863
|
+
function isThemeExport(data) {
|
|
864
|
+
if (!isPlainObject(data)) return false;
|
|
865
|
+
if (data.kind === "theme") return hasThemeShape(data);
|
|
866
|
+
if (data.kind !== void 0) return false;
|
|
867
|
+
return hasThemeShape(data);
|
|
868
|
+
}
|
|
869
|
+
/** Type guard for color-token authoring snapshots. */
|
|
870
|
+
function isColorTokenExport(data) {
|
|
871
|
+
if (!isPlainObject(data)) return false;
|
|
872
|
+
if (data.kind === "color") return hasColorTokenShape(data);
|
|
873
|
+
if (data.kind !== void 0) return false;
|
|
874
|
+
return hasColorTokenShape(data);
|
|
875
|
+
}
|
|
876
|
+
/** Type guard for palette authoring snapshots. */
|
|
877
|
+
function isPaletteExport(data) {
|
|
878
|
+
if (!isPlainObject(data)) return false;
|
|
879
|
+
if (data.kind === "palette") return hasPaletteShape(data);
|
|
880
|
+
if (data.kind !== void 0) return false;
|
|
881
|
+
return hasPaletteShape(data);
|
|
751
882
|
}
|
|
752
883
|
|
|
753
884
|
//#endregion
|
|
@@ -761,6 +892,43 @@ function pairHC(p) {
|
|
|
761
892
|
function clamp(v, min, max) {
|
|
762
893
|
return Math.max(min, Math.min(max, v));
|
|
763
894
|
}
|
|
895
|
+
function lerp(a, b, f) {
|
|
896
|
+
return a + (b - a) * f;
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Blend fraction at/above which an un-interpolable HC pair switches from its
|
|
900
|
+
* normal entry to its high-contrast one (level 50).
|
|
901
|
+
*/
|
|
902
|
+
const PAIR_SWITCH = .5;
|
|
903
|
+
/** Blend fraction (0–1) for an authored 0–100 contrast level. */
|
|
904
|
+
function levelFraction(level) {
|
|
905
|
+
return clamp(level, 0, 100) / 100;
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Manual-contrast blend fraction (0–1) for a resolved config, or `undefined`
|
|
909
|
+
* in `'auto'` mode (the two-tier normal + high-contrast model).
|
|
910
|
+
*
|
|
911
|
+
* Note `contrastLevel: 0` yields `0`, not `undefined`: "pinned to normal
|
|
912
|
+
* contrast with no high-contrast tier" is a distinct state from `'auto'`.
|
|
913
|
+
*/
|
|
914
|
+
function contrastFraction(config) {
|
|
915
|
+
const level = config.contrastLevel;
|
|
916
|
+
if (typeof level !== "number" || !Number.isFinite(level)) return void 0;
|
|
917
|
+
return levelFraction(level);
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* A numeric HC pair at a blend fraction (shadow `intensity`, mix `value`).
|
|
921
|
+
*
|
|
922
|
+
* The endpoints return the authored entry *by identity* rather than through
|
|
923
|
+
* float arithmetic — `a + (b - a) * 1` is not bit-exactly `b` in IEEE 754, and
|
|
924
|
+
* levels 0 / 100 must reproduce the classic output exactly.
|
|
925
|
+
*/
|
|
926
|
+
function numberAt(p, f) {
|
|
927
|
+
if (!Array.isArray(p)) return p;
|
|
928
|
+
if (f <= 0) return p[0];
|
|
929
|
+
if (f >= 1) return p[1];
|
|
930
|
+
return lerp(p[0], p[1], f);
|
|
931
|
+
}
|
|
764
932
|
/** Whether a tone value is an extreme keyword (`'max'` / `'min'`). */
|
|
765
933
|
function isExtremeTone(value) {
|
|
766
934
|
return value === "max" || value === "min";
|
|
@@ -805,6 +973,34 @@ function parseToneValue(value) {
|
|
|
805
973
|
};
|
|
806
974
|
}
|
|
807
975
|
/**
|
|
976
|
+
* Parse an authored tone pair at a manual-contrast blend fraction, returning
|
|
977
|
+
* the same normalized shape as {@link parseToneValue}.
|
|
978
|
+
*
|
|
979
|
+
* Same-kind numeric pairs interpolate their magnitude — absolute tones
|
|
980
|
+
* (`[30, 20]` → 25 at level 50) and relative deltas (`['+10', '+20']` → +15).
|
|
981
|
+
* Everything else is un-interpolable and switches at {@link PAIR_SWITCH}:
|
|
982
|
+
* extremes carry no magnitude, and a mixed-kind pair would change *which*
|
|
983
|
+
* resolver branch runs mid-ramp (absolute remap vs. extreme-against-base vs.
|
|
984
|
+
* base-anchored delta), putting a discontinuity at level 100 — exactly where
|
|
985
|
+
* the output must stay bit-exact.
|
|
986
|
+
*
|
|
987
|
+
* Returns a parsed struct rather than a `ToneValue` so a blended relative
|
|
988
|
+
* delta never round-trips through a `'+15.5'` string, and so `kind` — which
|
|
989
|
+
* selects the resolver branch — cannot drift.
|
|
990
|
+
*/
|
|
991
|
+
function parseToneValueAt(p, f) {
|
|
992
|
+
if (!Array.isArray(p)) return parseToneValue(p);
|
|
993
|
+
if (f <= 0) return parseToneValue(p[0]);
|
|
994
|
+
if (f >= 1) return parseToneValue(p[1]);
|
|
995
|
+
const normal = parseToneValue(p[0]);
|
|
996
|
+
const hc = parseToneValue(p[1]);
|
|
997
|
+
if (normal.kind === hc.kind && normal.kind !== "extreme") return {
|
|
998
|
+
kind: normal.kind,
|
|
999
|
+
value: lerp(normal.value, hc.value, f)
|
|
1000
|
+
};
|
|
1001
|
+
return f < PAIR_SWITCH ? normal : hc;
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
808
1004
|
* Compute the effective hue for a color, given the theme seed hue
|
|
809
1005
|
* and an optional per-color hue override.
|
|
810
1006
|
*/
|
|
@@ -840,7 +1036,9 @@ function isAbsoluteTone(tone) {
|
|
|
840
1036
|
* - the resolved-variant edge adapter (`variantToOkhsl`),
|
|
841
1037
|
* - the per-scheme tone mapping that replaced the Möbius dark curve
|
|
842
1038
|
* (`mapToneForScheme`), the dark desaturation reducer, and the solver's scheme
|
|
843
|
-
* tone range
|
|
1039
|
+
* tone range,
|
|
1040
|
+
* - the scheme tone window, including the high-contrast bypass and its
|
|
1041
|
+
* continuous generalization under a manual contrast level.
|
|
844
1042
|
*
|
|
845
1043
|
* See `docs/okhst.md` for the full specification and the calibrated
|
|
846
1044
|
* default constants.
|
|
@@ -939,7 +1137,12 @@ function normalizeToneWindow(win) {
|
|
|
939
1137
|
/**
|
|
940
1138
|
* Resolve the active tone window for a scheme as OKHSL-lightness endpoints.
|
|
941
1139
|
* - HC variants always return the full range `[0, 100]` with the mode eps.
|
|
942
|
-
* -
|
|
1140
|
+
* - Under a manual `contrastLevel`, each endpoint is interpolated toward that
|
|
1141
|
+
* full range: light `10 → 0` / `100 → 100`, dark `15 → 0` / `95 → 100`. The
|
|
1142
|
+
* endpoints are exact — level 0 returns the configured window and level 100
|
|
1143
|
+
* the same literal the HC branch returns.
|
|
1144
|
+
* - `false` (= "no clamping") is treated as `[0, 100]` with the reference eps,
|
|
1145
|
+
* and is therefore level-invariant.
|
|
943
1146
|
*/
|
|
944
1147
|
function activeWindow(isHighContrast, kind, config) {
|
|
945
1148
|
const win = normalizeToneWindow(kind === "dark" ? config.darkTone : config.lightTone);
|
|
@@ -948,7 +1151,18 @@ function activeWindow(isHighContrast, kind, config) {
|
|
|
948
1151
|
hi: 100,
|
|
949
1152
|
eps: win.eps
|
|
950
1153
|
};
|
|
951
|
-
|
|
1154
|
+
const f = contrastFraction(config);
|
|
1155
|
+
if (f === void 0 || f <= 0) return win;
|
|
1156
|
+
if (f >= 1) return {
|
|
1157
|
+
lo: 0,
|
|
1158
|
+
hi: 100,
|
|
1159
|
+
eps: win.eps
|
|
1160
|
+
};
|
|
1161
|
+
return {
|
|
1162
|
+
lo: lerp(win.lo, 0, f),
|
|
1163
|
+
hi: lerp(win.hi, 100, f),
|
|
1164
|
+
eps: win.eps
|
|
1165
|
+
};
|
|
952
1166
|
}
|
|
953
1167
|
/**
|
|
954
1168
|
* Remap an authored tone (0–100) into a scheme window and return the final
|
|
@@ -972,6 +1186,9 @@ function remapToneToLightness(authorTone, win) {
|
|
|
972
1186
|
* The window remap uses the mode's render eps to land a final OKHSL
|
|
973
1187
|
* lightness; that lightness is then re-expressed as canonical tone so
|
|
974
1188
|
* relative offsets and contrast stay comparable across schemes.
|
|
1189
|
+
*
|
|
1190
|
+
* A manual `contrastLevel` widens the window continuously (see
|
|
1191
|
+
* `activeWindow`); inversion is unaffected.
|
|
975
1192
|
*/
|
|
976
1193
|
function mapToneForScheme(authorTone, mode, isDark, isHighContrast, config) {
|
|
977
1194
|
if (mode === "static") return clamp(authorTone, 0, 100);
|
|
@@ -986,7 +1203,8 @@ function mapSaturationDark(s, mode, config) {
|
|
|
986
1203
|
/**
|
|
987
1204
|
* Tone search range (0–1) for the contrast solver in a given scheme.
|
|
988
1205
|
* `static` searches the full range; otherwise the scheme window's tone
|
|
989
|
-
* endpoints (HC bypasses to full range
|
|
1206
|
+
* endpoints (HC bypasses to full range, a manual `contrastLevel` widens
|
|
1207
|
+
* toward it — see `activeWindow`).
|
|
990
1208
|
*/
|
|
991
1209
|
function schemeToneRange(isDark, mode, isHighContrast, config) {
|
|
992
1210
|
if (mode === "static") return [0, 1];
|
|
@@ -995,124 +1213,468 @@ function schemeToneRange(isDark, mode, isHighContrast, config) {
|
|
|
995
1213
|
}
|
|
996
1214
|
|
|
997
1215
|
//#endregion
|
|
998
|
-
//#region src/
|
|
1216
|
+
//#region src/color-value.ts
|
|
999
1217
|
/**
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
* Finds the tone closest to a preferred tone that satisfies a contrast
|
|
1003
|
-
* floor (WCAG 2 ratio or APCA Lc) against a base color. Because tone is
|
|
1004
|
-
* contrast-uniform, the WCAG branch gets a closed-form seed and the search
|
|
1005
|
-
* converges quickly.
|
|
1218
|
+
* Parsing and validation for `GlazeColorValue` — the literal color forms.
|
|
1006
1219
|
*
|
|
1007
|
-
*
|
|
1008
|
-
*
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
*
|
|
1012
|
-
* relative luminance for `wcag`, APCA screen luminance (`Ys`) for `apca`.
|
|
1220
|
+
* Hex, the CSS color functions Glaze emits, and the four value-object shapes all
|
|
1221
|
+
* land here and come out as OKHSL. Split out of `color-token.ts` so both
|
|
1222
|
+
* consumers can reach it without an import cycle: standalone `glaze.color()`
|
|
1223
|
+
* tokens build on it, and the resolver needs it for a theme color's `from`. It
|
|
1224
|
+
* depends on nothing but the color math, which is what keeps that true.
|
|
1013
1225
|
*/
|
|
1014
|
-
function metricLuminance(metric, linearRgb) {
|
|
1015
|
-
return metric === "apca" ? apcaLuminanceFromLinearRgb(linearRgb) : gamutClampedLuminance(linearRgb);
|
|
1016
|
-
}
|
|
1017
|
-
const APCA_PRESETS = {
|
|
1018
|
-
preferred: 90,
|
|
1019
|
-
body: 75,
|
|
1020
|
-
content: 60,
|
|
1021
|
-
large: 45,
|
|
1022
|
-
"non-text": 30,
|
|
1023
|
-
min: 15
|
|
1024
|
-
};
|
|
1025
1226
|
/**
|
|
1026
|
-
*
|
|
1027
|
-
*
|
|
1227
|
+
* Matches the CSS color functions Glaze itself emits (`rgb()`, `hsl()`,
|
|
1228
|
+
* `okhsl()`, `oklch()`) plus their legacy alpha aliases (`rgba()`, `hsla()`).
|
|
1229
|
+
*
|
|
1230
|
+
* Only bare numeric components are supported. Named colors (`red`),
|
|
1231
|
+
* relative-color syntax (`from <color> ...`), and angle units other
|
|
1232
|
+
* than bare degrees (`deg` is the only suffix tolerated by `parseFloat`)
|
|
1233
|
+
* are out of scope.
|
|
1028
1234
|
*/
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
return
|
|
1032
|
-
|
|
1033
|
-
const CONTRAST_PRESETS = {
|
|
1034
|
-
AA: 4.5,
|
|
1035
|
-
AAA: 7,
|
|
1036
|
-
"AA-large": 3,
|
|
1037
|
-
"AAA-large": 4.5
|
|
1038
|
-
};
|
|
1039
|
-
function resolveMinContrast(value) {
|
|
1040
|
-
if (typeof value === "number") return Math.max(1, value);
|
|
1041
|
-
return CONTRAST_PRESETS[value];
|
|
1042
|
-
}
|
|
1043
|
-
function pickPair(p, isHighContrast) {
|
|
1044
|
-
return Array.isArray(p) ? isHighContrast ? p[1] : p[0] : p;
|
|
1235
|
+
const COLOR_FN_RE = /^(rgba?|hsla?|okhsl|okhst|oklch)\(\s*([^)]*)\s*\)$/i;
|
|
1236
|
+
function parseNumberOrPercent(raw, percentScale) {
|
|
1237
|
+
if (raw.endsWith("%")) return parseFloat(raw) / 100 * percentScale;
|
|
1238
|
+
return parseFloat(raw);
|
|
1045
1239
|
}
|
|
1046
1240
|
/**
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
1050
|
-
*
|
|
1241
|
+
* Split the body of a CSS color function into its components and detect
|
|
1242
|
+
* whether an alpha channel was present.
|
|
1243
|
+
*
|
|
1244
|
+
* Handles both modern slash syntax (`R G B / A` or `R, G, B / A`) and
|
|
1245
|
+
* legacy comma syntax (`R, G, B, A`). The alpha value itself is discarded
|
|
1246
|
+
* by the caller — standalone Glaze colors have no opacity field.
|
|
1051
1247
|
*/
|
|
1052
|
-
function
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
if ("apca" in spec) return {
|
|
1058
|
-
metric: "apca",
|
|
1059
|
-
target: resolveApcaTarget(pickPair(spec.apca, isHighContrast)),
|
|
1060
|
-
polarity: polarity ?? "fg"
|
|
1248
|
+
function splitColorBody(body) {
|
|
1249
|
+
const slashIdx = body.indexOf("/");
|
|
1250
|
+
if (slashIdx !== -1) return {
|
|
1251
|
+
components: body.slice(0, slashIdx).trim().split(/[\s,]+/).filter(Boolean),
|
|
1252
|
+
hadAlpha: body.slice(slashIdx + 1).trim().length > 0
|
|
1061
1253
|
};
|
|
1254
|
+
const components = body.split(/[\s,]+/).filter(Boolean);
|
|
1255
|
+
if (components.length === 4) {
|
|
1256
|
+
components.pop();
|
|
1257
|
+
return {
|
|
1258
|
+
components,
|
|
1259
|
+
hadAlpha: true
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1062
1262
|
return {
|
|
1063
|
-
|
|
1064
|
-
|
|
1263
|
+
components,
|
|
1264
|
+
hadAlpha: false
|
|
1065
1265
|
};
|
|
1066
1266
|
}
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
normBG: .56,
|
|
1070
|
-
normTXT: .57,
|
|
1071
|
-
revTXT: .62,
|
|
1072
|
-
revBG: .65
|
|
1073
|
-
};
|
|
1074
|
-
const APCA_BLACK_THRESH = .022;
|
|
1075
|
-
const APCA_BLACK_CLIP = 1.414;
|
|
1076
|
-
const APCA_DELTA_Y_MIN = 5e-4;
|
|
1077
|
-
const APCA_SCALE = 1.14;
|
|
1078
|
-
const APCA_LO_OFFSET = .027;
|
|
1079
|
-
function apcaSoftClamp(y) {
|
|
1080
|
-
const yc = Math.max(0, y);
|
|
1081
|
-
if (yc >= APCA_BLACK_THRESH) return yc;
|
|
1082
|
-
return yc + Math.pow(APCA_BLACK_THRESH - yc, APCA_BLACK_CLIP);
|
|
1267
|
+
function warnDroppedAlpha(input) {
|
|
1268
|
+
console.warn(`glaze: alpha component dropped from "${input}" (standalone color has no opacity field).`);
|
|
1083
1269
|
}
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
return sapc < .1 ? 0 : (sapc - APCA_LO_OFFSET) * 100;
|
|
1270
|
+
function parseColorString(input) {
|
|
1271
|
+
if (input.startsWith("#")) {
|
|
1272
|
+
const parsed = parseHexAlpha(input);
|
|
1273
|
+
if (!parsed) throw new Error(`glaze: invalid hex color "${input}".`);
|
|
1274
|
+
if (parsed.alpha !== void 0) warnDroppedAlpha(input);
|
|
1275
|
+
const [h, s, l] = srgbToOkhsl(parsed.rgb);
|
|
1276
|
+
return {
|
|
1277
|
+
h,
|
|
1278
|
+
s,
|
|
1279
|
+
l
|
|
1280
|
+
};
|
|
1096
1281
|
}
|
|
1097
|
-
|
|
1098
|
-
|
|
1282
|
+
const m = input.match(COLOR_FN_RE);
|
|
1283
|
+
if (!m) throw new Error(`glaze: unsupported color string "${input}".`);
|
|
1284
|
+
const fn = m[1].toLowerCase();
|
|
1285
|
+
const { components, hadAlpha } = splitColorBody(m[2].trim());
|
|
1286
|
+
if (hadAlpha) warnDroppedAlpha(input);
|
|
1287
|
+
if (components.length !== 3) throw new Error(`glaze: expected 3 components in "${input}".`);
|
|
1288
|
+
switch (fn) {
|
|
1289
|
+
case "rgb":
|
|
1290
|
+
case "rgba": {
|
|
1291
|
+
const [h, s, l] = srgbToOkhsl([
|
|
1292
|
+
parseNumberOrPercent(components[0], 255) / 255,
|
|
1293
|
+
parseNumberOrPercent(components[1], 255) / 255,
|
|
1294
|
+
parseNumberOrPercent(components[2], 255) / 255
|
|
1295
|
+
]);
|
|
1296
|
+
return {
|
|
1297
|
+
h,
|
|
1298
|
+
s,
|
|
1299
|
+
l
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
case "hsl":
|
|
1303
|
+
case "hsla": {
|
|
1304
|
+
const [oh, os, ol] = srgbToOkhsl(hslToSrgb(parseFloat(components[0]), parseNumberOrPercent(components[1], 1), parseNumberOrPercent(components[2], 1)));
|
|
1305
|
+
return {
|
|
1306
|
+
h: oh,
|
|
1307
|
+
s: os,
|
|
1308
|
+
l: ol
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
case "okhsl": return {
|
|
1312
|
+
h: parseFloat(components[0]),
|
|
1313
|
+
s: parseNumberOrPercent(components[1], 1),
|
|
1314
|
+
l: parseNumberOrPercent(components[2], 1)
|
|
1315
|
+
};
|
|
1316
|
+
case "okhst": return okhstToOkhsl({
|
|
1317
|
+
h: parseFloat(components[0]),
|
|
1318
|
+
s: parseNumberOrPercent(components[1], 1),
|
|
1319
|
+
t: parseNumberOrPercent(components[2], 1)
|
|
1320
|
+
});
|
|
1321
|
+
case "oklch": {
|
|
1322
|
+
const L = parseNumberOrPercent(components[0], 1);
|
|
1323
|
+
const C = parseNumberOrPercent(components[1], .4);
|
|
1324
|
+
const hRad = parseFloat(components[2]) * Math.PI / 180;
|
|
1325
|
+
const [h, s, l] = oklabToOkhsl([
|
|
1326
|
+
L,
|
|
1327
|
+
C * Math.cos(hRad),
|
|
1328
|
+
C * Math.sin(hRad)
|
|
1329
|
+
]);
|
|
1330
|
+
return {
|
|
1331
|
+
h,
|
|
1332
|
+
s,
|
|
1333
|
+
l
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
throw new Error(`glaze: unsupported color function "${fn}".`);
|
|
1099
1338
|
}
|
|
1100
|
-
const CACHE_SIZE = 512;
|
|
1101
|
-
const luminanceCache = /* @__PURE__ */ new Map();
|
|
1102
|
-
const cacheOrder = [];
|
|
1103
1339
|
/**
|
|
1104
|
-
*
|
|
1105
|
-
* the
|
|
1106
|
-
* WCAG and APCA derive different luminances from the same color.
|
|
1340
|
+
* Validate a user-supplied `OkhslColor`. Catches the common 0-100 vs 0-1
|
|
1341
|
+
* confusion (the structured form uses 0-100, OKHSL objects use 0-1).
|
|
1107
1342
|
*/
|
|
1108
|
-
function
|
|
1109
|
-
const
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1343
|
+
function validateOkhslColor(value) {
|
|
1344
|
+
const { h, s, l } = value;
|
|
1345
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) throw new Error("glaze.color: OkhslColor h/s/l must be finite numbers.");
|
|
1346
|
+
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, tone } (which uses 0–100)?");
|
|
1347
|
+
}
|
|
1348
|
+
/** Validate a user-supplied `{ r, g, b }` object in 0–255. */
|
|
1349
|
+
function validateRgbColor(value) {
|
|
1350
|
+
for (const key of [
|
|
1351
|
+
"r",
|
|
1352
|
+
"g",
|
|
1353
|
+
"b"
|
|
1354
|
+
]) {
|
|
1355
|
+
const n = value[key];
|
|
1356
|
+
if (!Number.isFinite(n) || n < 0 || n > 255) throw new Error(`glaze.color: RgbColor ${key} must be a finite number in 0–255 (got ${n}).`);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
/** Validate a user-supplied `{ l, c, h }` OKLCh object. */
|
|
1360
|
+
function validateOklchColor(value) {
|
|
1361
|
+
const { l, c, h } = value;
|
|
1362
|
+
if (!Number.isFinite(l) || !Number.isFinite(c) || !Number.isFinite(h)) throw new Error("glaze.color: OklchColor l/c/h must be finite numbers.");
|
|
1363
|
+
if (l > 1.5 || c > 1.5) throw new Error("glaze.color: OklchColor l/c must be in 0–1 range (matching oklch() strings).");
|
|
1364
|
+
}
|
|
1365
|
+
function oklchComponentsToOkhsl(l, c, hDeg) {
|
|
1366
|
+
const hRad = hDeg * Math.PI / 180;
|
|
1367
|
+
const [h, s, outL] = oklabToOkhsl([
|
|
1368
|
+
l,
|
|
1369
|
+
c * Math.cos(hRad),
|
|
1370
|
+
c * Math.sin(hRad)
|
|
1371
|
+
]);
|
|
1372
|
+
return {
|
|
1373
|
+
h,
|
|
1374
|
+
s,
|
|
1375
|
+
l: outL
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
function isRgbColorObject(value) {
|
|
1379
|
+
return "r" in value && "g" in value && "b" in value;
|
|
1380
|
+
}
|
|
1381
|
+
function isOklchColorObject(value) {
|
|
1382
|
+
return "c" in value && "l" in value && "h" in value;
|
|
1383
|
+
}
|
|
1384
|
+
function isOkhstColorObject(value) {
|
|
1385
|
+
return "t" in value && "h" in value && "s" in value;
|
|
1386
|
+
}
|
|
1387
|
+
/** Validate a user-supplied `{ h, s, t }` OKHST object (s/t in 0–1). */
|
|
1388
|
+
function validateOkhstColor(value) {
|
|
1389
|
+
const { h, s, t } = value;
|
|
1390
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(t)) throw new Error("glaze.color: OkhstColor h/s/t must be finite numbers.");
|
|
1391
|
+
if (s > 1.5 || t > 1.5) throw new Error("glaze.color: OkhstColor s/t must be in 0–1 range. Did you mean the structured form { hue, saturation, tone } (which uses 0–100)?");
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Extract an OKHSL color from any `GlazeColorValue` form. Also used by
|
|
1395
|
+
* `glaze.shadow()` so all shadow inputs (hex, color functions, OKHSL,
|
|
1396
|
+
* literal objects) go through one parser.
|
|
1397
|
+
*/
|
|
1398
|
+
function extractOkhslFromValue(value) {
|
|
1399
|
+
if (typeof value === "string") return parseColorString(value);
|
|
1400
|
+
if (Array.isArray(value)) throw new Error("glaze.color: RGB tuple [r, g, b] is no longer supported — use { r, g, b } instead.");
|
|
1401
|
+
if (isRgbColorObject(value)) {
|
|
1402
|
+
validateRgbColor(value);
|
|
1403
|
+
const [h, s, l] = srgbToOkhsl([
|
|
1404
|
+
value.r / 255,
|
|
1405
|
+
value.g / 255,
|
|
1406
|
+
value.b / 255
|
|
1407
|
+
]);
|
|
1408
|
+
return {
|
|
1409
|
+
h,
|
|
1410
|
+
s,
|
|
1411
|
+
l
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
if (isOklchColorObject(value)) {
|
|
1415
|
+
validateOklchColor(value);
|
|
1416
|
+
return oklchComponentsToOkhsl(value.l, value.c, value.h);
|
|
1417
|
+
}
|
|
1418
|
+
if (isOkhstColorObject(value)) {
|
|
1419
|
+
validateOkhstColor(value);
|
|
1420
|
+
return okhstToOkhsl(value);
|
|
1421
|
+
}
|
|
1422
|
+
validateOkhslColor(value);
|
|
1423
|
+
return value;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
//#endregion
|
|
1427
|
+
//#region src/format-guard.ts
|
|
1428
|
+
const NON_NATIVE_FORMATS = new Set(["okhsl", "okhst"]);
|
|
1429
|
+
/**
|
|
1430
|
+
* Throw when a non-native Glaze color space is requested for an export that
|
|
1431
|
+
* emits raw CSS or non-Tasty token maps.
|
|
1432
|
+
*/
|
|
1433
|
+
function assertNativeFormat(format, method) {
|
|
1434
|
+
if (format !== void 0 && NON_NATIVE_FORMATS.has(format)) throw new Error(`glaze: ${format} output is only supported by tasty() (not a native CSS color space). Use tasty({ format: '${format}' }) or pick a native format (oklch|hsl|rgb) for ${method}().`);
|
|
1435
|
+
}
|
|
1436
|
+
const SCHEME_FIELDS = [
|
|
1437
|
+
{
|
|
1438
|
+
field: "light",
|
|
1439
|
+
modes: () => true
|
|
1440
|
+
},
|
|
1441
|
+
{
|
|
1442
|
+
field: "dark",
|
|
1443
|
+
modes: (m) => m.dark
|
|
1444
|
+
},
|
|
1445
|
+
{
|
|
1446
|
+
field: "lightContrast",
|
|
1447
|
+
modes: (m) => m.highContrast
|
|
1448
|
+
},
|
|
1449
|
+
{
|
|
1450
|
+
field: "darkContrast",
|
|
1451
|
+
modes: (m) => m.dark && m.highContrast
|
|
1452
|
+
}
|
|
1453
|
+
];
|
|
1454
|
+
/**
|
|
1455
|
+
* Throw when `splitHue` is enabled but any exported color is not pastel.
|
|
1456
|
+
* Hue rotation is only clip-free when chroma is bounded by the hue-independent
|
|
1457
|
+
* safe chroma (`computeSafeChromaOKLCH`).
|
|
1458
|
+
*/
|
|
1459
|
+
function assertAllPastel(resolved, modes) {
|
|
1460
|
+
const nonPastel = [];
|
|
1461
|
+
for (const [name, color] of resolved) for (const { field, modes: active } of SCHEME_FIELDS) {
|
|
1462
|
+
if (!active(modes)) continue;
|
|
1463
|
+
if (color[field].pastel !== true) {
|
|
1464
|
+
if (!nonPastel.includes(name)) nonPastel.push(name);
|
|
1465
|
+
break;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
if (nonPastel.length === 0) return;
|
|
1469
|
+
throw new Error(`glaze: splitHue requires every color to be pastel (hue rotation is only clip-free when chroma is bounded by the hue-independent safe chroma). Non-pastel: ${nonPastel.join(", ")}. Set pastel: true (per-theme, per-token, or per-color) or drop splitHue.`);
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
//#endregion
|
|
1473
|
+
//#region src/contrast-solver.ts
|
|
1474
|
+
/**
|
|
1475
|
+
* Contrast solver — operates in OKHST tone.
|
|
1476
|
+
*
|
|
1477
|
+
* Finds the tone closest to a preferred tone that satisfies a contrast
|
|
1478
|
+
* floor (WCAG 2 ratio or APCA Lc) against a base color. Because tone is
|
|
1479
|
+
* contrast-uniform, the WCAG branch gets a closed-form seed and the search
|
|
1480
|
+
* converges quickly.
|
|
1481
|
+
*
|
|
1482
|
+
* Public API: `findToneForContrast`, `findValueForMixContrast`,
|
|
1483
|
+
* `resolveMinContrast`, `resolveContrastForMode`, `resolveContrastForLevel`,
|
|
1484
|
+
* `apcaContrast`.
|
|
1485
|
+
*/
|
|
1486
|
+
/**
|
|
1487
|
+
* Luminance of a linear-sRGB color in the basis the metric expects: WCAG
|
|
1488
|
+
* relative luminance for `wcag`, APCA screen luminance (`Ys`) for `apca`.
|
|
1489
|
+
*/
|
|
1490
|
+
function metricLuminance(metric, linearRgb) {
|
|
1491
|
+
return metric === "apca" ? apcaLuminanceFromLinearRgb(linearRgb) : gamutClampedLuminance(linearRgb);
|
|
1492
|
+
}
|
|
1493
|
+
const APCA_PRESETS = {
|
|
1494
|
+
preferred: 90,
|
|
1495
|
+
body: 75,
|
|
1496
|
+
content: 60,
|
|
1497
|
+
large: 45,
|
|
1498
|
+
"non-text": 30,
|
|
1499
|
+
min: 15
|
|
1500
|
+
};
|
|
1501
|
+
/**
|
|
1502
|
+
* APCA-W3 "Enhanced Level" delta added to a bare APCA target in high-contrast
|
|
1503
|
+
* mode when no explicit HC value is provided (analogous to WCAG AAA over AA).
|
|
1504
|
+
* Only applied when neither the outer `contrast` pair nor the inner `apca`
|
|
1505
|
+
* pair carries an explicit HC entry.
|
|
1506
|
+
*/
|
|
1507
|
+
const APCA_HC_ENHANCEMENT = 15;
|
|
1508
|
+
/** Upper bound for an APCA Lc target after HC enhancement. */
|
|
1509
|
+
const APCA_MAX_LC = 106;
|
|
1510
|
+
/**
|
|
1511
|
+
* Resolve an APCA target — a raw Lc number (kept as-is) or an `ApcaPreset`
|
|
1512
|
+
* keyword mapped to its Lc value. The magnitude is forced non-negative.
|
|
1513
|
+
*/
|
|
1514
|
+
function resolveApcaTarget(value) {
|
|
1515
|
+
if (typeof value === "number") return Math.abs(value);
|
|
1516
|
+
return APCA_PRESETS[value];
|
|
1517
|
+
}
|
|
1518
|
+
const CONTRAST_PRESETS = {
|
|
1519
|
+
AA: 4.5,
|
|
1520
|
+
AAA: 7,
|
|
1521
|
+
"AA-large": 3,
|
|
1522
|
+
"AAA-large": 4.5
|
|
1523
|
+
};
|
|
1524
|
+
/**
|
|
1525
|
+
* WCAG high-contrast auto-promotion (analog of APCA's Enhanced Level). A bare
|
|
1526
|
+
* AA / AA-large preset is promoted to its spec-defined "Enhanced" successor
|
|
1527
|
+
* (SC 1.4.3 → SC 1.4.6) in high-contrast mode. AAA / AAA-large are already
|
|
1528
|
+
* the top WCAG tier and are left unchanged. Bare numeric targets have no
|
|
1529
|
+
* defined successor tier and are also left unchanged. An explicit HC value
|
|
1530
|
+
* (outer or inner pair) always overrides.
|
|
1531
|
+
*/
|
|
1532
|
+
const WCAG_HC_PROMOTION = {
|
|
1533
|
+
AA: "AAA",
|
|
1534
|
+
"AA-large": "AAA-large"
|
|
1535
|
+
};
|
|
1536
|
+
function resolveMinContrast(value) {
|
|
1537
|
+
if (typeof value === "number") return Math.max(1, value);
|
|
1538
|
+
return CONTRAST_PRESETS[value];
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* Resolve a WCAG target (number or preset) for a mode, applying the
|
|
1542
|
+
* high-contrast auto-promotion when `explicitHC` is false and the value is an
|
|
1543
|
+
* AA-family preset. Bare numbers and AAA-family presets pass through.
|
|
1544
|
+
*/
|
|
1545
|
+
function resolveWcagTarget(value, isHighContrast, explicitHC) {
|
|
1546
|
+
if (typeof value === "number") return resolveMinContrast(value);
|
|
1547
|
+
if (isHighContrast && !explicitHC) {
|
|
1548
|
+
const promoted = WCAG_HC_PROMOTION[value];
|
|
1549
|
+
if (promoted !== void 0) return resolveMinContrast(promoted);
|
|
1550
|
+
}
|
|
1551
|
+
return resolveMinContrast(value);
|
|
1552
|
+
}
|
|
1553
|
+
function pickPair(p, isHighContrast) {
|
|
1554
|
+
return Array.isArray(p) ? isHighContrast ? p[1] : p[0] : p;
|
|
1555
|
+
}
|
|
1556
|
+
/**
|
|
1557
|
+
* The metric a single `ContrastSpec` measures in. Bare numbers and WCAG
|
|
1558
|
+
* presets are `'wcag'`; only an `{ apca }` object is `'apca'`.
|
|
1559
|
+
*/
|
|
1560
|
+
function contrastMetricOf(spec) {
|
|
1561
|
+
return typeof spec === "object" && "apca" in spec ? "apca" : "wcag";
|
|
1562
|
+
}
|
|
1563
|
+
/**
|
|
1564
|
+
* Resolve a `ContrastSpec` (already selected from any outer HC pair) for a
|
|
1565
|
+
* given mode into `{ metric, target }`. Handles the inner metric HC pair and
|
|
1566
|
+
* preset resolution. `polarity` is passed through to the result for the APCA
|
|
1567
|
+
* branch (it controls argument order in the solver); WCAG ignores it.
|
|
1568
|
+
*
|
|
1569
|
+
* `outerExplicitHC` indicates whether the caller selected this `spec` from an
|
|
1570
|
+
* explicit high-contrast entry of the outer `contrast` pair. Together with the
|
|
1571
|
+
* inner metric pair, it decides whether the HC auto-enhancement fires:
|
|
1572
|
+
* - APCA: +15 Lc "Enhanced Level" boost when neither level is explicit.
|
|
1573
|
+
* - WCAG: AA → AAA / AA-large → AAA-large promotion (SC 1.4.3 → 1.4.6) when
|
|
1574
|
+
* neither level is explicit. AAA-family presets and bare numbers are left
|
|
1575
|
+
* unchanged (AAA is the top WCAG tier).
|
|
1576
|
+
* Defaults to `false` (correct for direct callers, which pass a single
|
|
1577
|
+
* selected spec rather than an outer pair).
|
|
1578
|
+
*/
|
|
1579
|
+
function resolveContrastForMode(spec, isHighContrast, polarity, outerExplicitHC) {
|
|
1580
|
+
if (typeof spec === "number" || typeof spec === "string") return {
|
|
1581
|
+
metric: "wcag",
|
|
1582
|
+
target: resolveWcagTarget(spec, isHighContrast, !!outerExplicitHC)
|
|
1583
|
+
};
|
|
1584
|
+
if ("apca" in spec) {
|
|
1585
|
+
const baseTarget = resolveApcaTarget(pickPair(spec.apca, isHighContrast));
|
|
1586
|
+
const innerExplicitHC = Array.isArray(spec.apca);
|
|
1587
|
+
return {
|
|
1588
|
+
metric: "apca",
|
|
1589
|
+
target: isHighContrast && !outerExplicitHC && !innerExplicitHC ? Math.min(baseTarget + APCA_HC_ENHANCEMENT, APCA_MAX_LC) : baseTarget,
|
|
1590
|
+
polarity: polarity ?? "fg"
|
|
1591
|
+
};
|
|
1592
|
+
}
|
|
1593
|
+
const innerExplicitHC = Array.isArray(spec.wcag);
|
|
1594
|
+
return {
|
|
1595
|
+
metric: "wcag",
|
|
1596
|
+
target: resolveWcagTarget(pickPair(spec.wcag, isHighContrast), isHighContrast, !!outerExplicitHC || innerExplicitHC)
|
|
1597
|
+
};
|
|
1598
|
+
}
|
|
1599
|
+
/**
|
|
1600
|
+
* Resolve a `contrast` spec — including its outer `[normal, highContrast]`
|
|
1601
|
+
* pair — at a manual contrast `level` (0–100), by resolving both ends and
|
|
1602
|
+
* interpolating the numeric target.
|
|
1603
|
+
*
|
|
1604
|
+
* Level 0 / 100 delegate verbatim to {@link resolveContrastForMode}, so the
|
|
1605
|
+
* endpoints are bit-identical to classic normal / high-contrast resolution.
|
|
1606
|
+
* Resolving each end fully first also means every rule that governs the
|
|
1607
|
+
* escalation still applies at both ends for free: preset mapping, the
|
|
1608
|
+
* `APCA_MAX_LC` clamp, and the explicit-HC suppression that cancels the
|
|
1609
|
+
* `AA → AAA` promotion and the `+15 Lc` enhancement.
|
|
1610
|
+
*
|
|
1611
|
+
* Throws when the two ends measure in different metrics — a WCAG ratio and an
|
|
1612
|
+
* APCA Lc live on different scales, so there is no target between them. Such a
|
|
1613
|
+
* pair is rejected at authoring time by `validateColorDefs`; this is the guard
|
|
1614
|
+
* for direct callers.
|
|
1615
|
+
*/
|
|
1616
|
+
function resolveContrastForLevel(spec, level, polarity) {
|
|
1617
|
+
const outerExplicitHC = Array.isArray(spec);
|
|
1618
|
+
const f = levelFraction(level);
|
|
1619
|
+
const normal = resolveContrastForMode(pairNormal(spec), false, polarity, outerExplicitHC);
|
|
1620
|
+
if (f <= 0) return normal;
|
|
1621
|
+
const hc = resolveContrastForMode(pairHC(spec), true, polarity, outerExplicitHC);
|
|
1622
|
+
if (f >= 1) return hc;
|
|
1623
|
+
if (normal.metric !== hc.metric) throw new Error(`glaze: a "contrast" pair cannot switch metric between its normal (${normal.metric}) and high-contrast (${hc.metric}) entry — the two targets are on different scales. Use one metric for both entries.`);
|
|
1624
|
+
return {
|
|
1625
|
+
...normal,
|
|
1626
|
+
target: lerp(normal.target, hc.target, f)
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
const APCA_EXPONENTS = {
|
|
1630
|
+
mainTRC: 2.4,
|
|
1631
|
+
normBG: .56,
|
|
1632
|
+
normTXT: .57,
|
|
1633
|
+
revTXT: .62,
|
|
1634
|
+
revBG: .65
|
|
1635
|
+
};
|
|
1636
|
+
const APCA_BLACK_THRESH = .022;
|
|
1637
|
+
const APCA_BLACK_CLIP = 1.414;
|
|
1638
|
+
const APCA_DELTA_Y_MIN = 5e-4;
|
|
1639
|
+
const APCA_SCALE = 1.14;
|
|
1640
|
+
const APCA_LO_OFFSET = .027;
|
|
1641
|
+
function apcaSoftClamp(y) {
|
|
1642
|
+
const yc = Math.max(0, y);
|
|
1643
|
+
if (yc >= APCA_BLACK_THRESH) return yc;
|
|
1644
|
+
return yc + Math.pow(APCA_BLACK_THRESH - yc, APCA_BLACK_CLIP);
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* APCA lightness contrast (Lc), signed: positive for dark text on light bg,
|
|
1648
|
+
* negative for light text on dark bg. Inputs are screen luminances (0–1).
|
|
1649
|
+
*/
|
|
1650
|
+
function apcaContrast(yText, yBg) {
|
|
1651
|
+
const txt = apcaSoftClamp(yText);
|
|
1652
|
+
const bg = apcaSoftClamp(yBg);
|
|
1653
|
+
if (Math.abs(bg - txt) < APCA_DELTA_Y_MIN) return 0;
|
|
1654
|
+
let sapc;
|
|
1655
|
+
if (bg > txt) {
|
|
1656
|
+
sapc = (Math.pow(bg, APCA_EXPONENTS.normBG) - Math.pow(txt, APCA_EXPONENTS.normTXT)) * APCA_SCALE;
|
|
1657
|
+
return sapc < .1 ? 0 : (sapc - APCA_LO_OFFSET) * 100;
|
|
1658
|
+
}
|
|
1659
|
+
sapc = (Math.pow(bg, APCA_EXPONENTS.revBG) - Math.pow(txt, APCA_EXPONENTS.revTXT)) * APCA_SCALE;
|
|
1660
|
+
return sapc > -.1 ? 0 : (sapc + APCA_LO_OFFSET) * 100;
|
|
1661
|
+
}
|
|
1662
|
+
const CACHE_SIZE = 512;
|
|
1663
|
+
const luminanceCache = /* @__PURE__ */ new Map();
|
|
1664
|
+
const cacheOrder = [];
|
|
1665
|
+
/**
|
|
1666
|
+
* Luminance of an OKHST color `(h, s, t)` with t in 0–1 (reference eps), in
|
|
1667
|
+
* the metric's luminance basis. The metric is part of the cache key because
|
|
1668
|
+
* WCAG and APCA derive different luminances from the same color.
|
|
1669
|
+
*/
|
|
1670
|
+
function cachedLuminance(metric, h, s, t, pastel) {
|
|
1671
|
+
const tRounded = Math.round(t * 1e4) / 1e4;
|
|
1672
|
+
const key = `${metric}|${h}|${s}|${tRounded}|${pastel}`;
|
|
1673
|
+
const cached = luminanceCache.get(key);
|
|
1674
|
+
if (cached !== void 0) return cached;
|
|
1675
|
+
const y = metricLuminance(metric, okhslToLinearSrgb(h, s, fromTone(tRounded * 100, REF_EPS), pastel));
|
|
1676
|
+
if (luminanceCache.size >= CACHE_SIZE) {
|
|
1677
|
+
const evict = cacheOrder.shift();
|
|
1116
1678
|
luminanceCache.delete(evict);
|
|
1117
1679
|
}
|
|
1118
1680
|
luminanceCache.set(key, y);
|
|
@@ -1204,7 +1766,7 @@ function wcagToneSeed(yBase, target, darker) {
|
|
|
1204
1766
|
return Math.max(0, Math.min(1, toneFromY(yClamped, REF_EPS) / 100));
|
|
1205
1767
|
}
|
|
1206
1768
|
function solveNearestContrast(opts) {
|
|
1207
|
-
const { lum, yBase, metric, target, searchTarget, lo, hi, searchAnchor, distanceAnchor, epsilon, maxIterations, flip, initialIsLower, polarity } = opts;
|
|
1769
|
+
const { lum, yBase, metric, target, searchTarget, lo, hi, searchAnchor, distanceAnchor, epsilon, maxIterations, flip, preferInitial, initialIsLower, polarity } = opts;
|
|
1208
1770
|
const runBranch = (lower) => lower ? searchBranch(lum, lo, searchAnchor, yBase, metric, searchTarget, epsilon, maxIterations, searchAnchor, polarity) : searchBranch(lum, searchAnchor, hi, yBase, metric, searchTarget, epsilon, maxIterations, searchAnchor, polarity);
|
|
1209
1771
|
const initialResult = runBranch(initialIsLower);
|
|
1210
1772
|
initialResult.met = initialResult.contrast >= target;
|
|
@@ -1215,14 +1777,20 @@ function solveNearestContrast(opts) {
|
|
|
1215
1777
|
if (flip) {
|
|
1216
1778
|
const oppositeResult = (initialIsLower ? distanceAnchor < hi : distanceAnchor > lo) ? runBranch(!initialIsLower) : null;
|
|
1217
1779
|
if (oppositeResult) oppositeResult.met = oppositeResult.contrast >= target;
|
|
1218
|
-
if (initialResult.met && oppositeResult?.met)
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1780
|
+
if (initialResult.met && oppositeResult?.met) {
|
|
1781
|
+
if (preferInitial) return {
|
|
1782
|
+
...initialResult,
|
|
1783
|
+
lower: initialIsLower
|
|
1784
|
+
};
|
|
1785
|
+
return Math.abs(initialResult.pos - distanceAnchor) <= Math.abs(oppositeResult.pos - distanceAnchor) ? {
|
|
1786
|
+
...initialResult,
|
|
1787
|
+
lower: initialIsLower
|
|
1788
|
+
} : {
|
|
1789
|
+
...oppositeResult,
|
|
1790
|
+
lower: !initialIsLower,
|
|
1791
|
+
flipped: true
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1226
1794
|
if (initialResult.met) return {
|
|
1227
1795
|
...initialResult,
|
|
1228
1796
|
lower: initialIsLower
|
|
@@ -1285,6 +1853,7 @@ function findToneForContrast(options) {
|
|
|
1285
1853
|
epsilon,
|
|
1286
1854
|
maxIterations,
|
|
1287
1855
|
flip: options.flip ?? false,
|
|
1856
|
+
preferInitial: options.preferInitial ?? false,
|
|
1288
1857
|
initialIsLower: initialIsDarker,
|
|
1289
1858
|
polarity
|
|
1290
1859
|
});
|
|
@@ -1335,6 +1904,7 @@ function findValueForMixContrast(options) {
|
|
|
1335
1904
|
epsilon,
|
|
1336
1905
|
maxIterations,
|
|
1337
1906
|
flip: options.flip ?? false,
|
|
1907
|
+
preferInitial: false,
|
|
1338
1908
|
initialIsLower,
|
|
1339
1909
|
polarity
|
|
1340
1910
|
});
|
|
@@ -1536,6 +2106,32 @@ function computeShadow(bg, fg, intensity, tuning) {
|
|
|
1536
2106
|
* resolver runs. `topoSort` orders defs so each color is processed after
|
|
1537
2107
|
* its base / bg / fg / target dependencies.
|
|
1538
2108
|
*/
|
|
2109
|
+
/**
|
|
2110
|
+
* Does this color sit at an absolute tone — either authored, or carried by a
|
|
2111
|
+
* `from` value? That is the test for "root" (placed on its own) as opposed to
|
|
2112
|
+
* "dependent" (placed relative to a base), and it is what lets a bare
|
|
2113
|
+
* `{ from: '#2F5BFF' }` stand as a complete color definition.
|
|
2114
|
+
*/
|
|
2115
|
+
function hasAbsoluteTone(def) {
|
|
2116
|
+
if (def.tone === void 0) return def.from !== void 0;
|
|
2117
|
+
return isAbsoluteTone(def.tone);
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Reject a `contrast` pair whose two entries measure in different metrics.
|
|
2121
|
+
*
|
|
2122
|
+
* A WCAG ratio and an APCA Lc are different scales, so `[4.5, { apca: 75 }]`
|
|
2123
|
+
* asks one color to be judged two incompatible ways. There is no target between
|
|
2124
|
+
* them for a manual `contrastLevel` to resolve, and even in `'auto'` mode the
|
|
2125
|
+
* two variants become incomparable. Pick one metric and pair its values
|
|
2126
|
+
* instead: `{ wcag: [4.5, 7] }` or `{ apca: [60, 90] }`.
|
|
2127
|
+
*/
|
|
2128
|
+
function assertConsistentContrastMetric(name, contrast) {
|
|
2129
|
+
if (contrast === void 0 || !Array.isArray(contrast)) return;
|
|
2130
|
+
const normal = contrastMetricOf(pairNormal(contrast));
|
|
2131
|
+
const hc = contrastMetricOf(pairHC(contrast));
|
|
2132
|
+
if (normal === hc) return;
|
|
2133
|
+
throw new Error(`glaze: color "${name}" has a "contrast" pair that switches metric (${normal} → ${hc}). A WCAG ratio and an APCA Lc are different scales; use one metric for both entries, e.g. { ${normal}: [normal, highContrast] }.`);
|
|
2134
|
+
}
|
|
1539
2135
|
function validateColorDefs(defs, externalBases) {
|
|
1540
2136
|
const localNames = new Set(Object.keys(defs));
|
|
1541
2137
|
const allNames = new Set([...localNames, ...externalBases ? externalBases.keys() : []]);
|
|
@@ -1554,14 +2150,21 @@ function validateColorDefs(defs, externalBases) {
|
|
|
1554
2150
|
if (!allNames.has(def.target)) throw new Error(`glaze: mix "${name}" references non-existent target "${def.target}".`);
|
|
1555
2151
|
if (localNames.has(def.base) && isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
|
|
1556
2152
|
if (localNames.has(def.target) && isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
|
|
2153
|
+
assertConsistentContrastMetric(name, def.contrast);
|
|
1557
2154
|
continue;
|
|
1558
2155
|
}
|
|
1559
2156
|
const regDef = def;
|
|
2157
|
+
assertConsistentContrastMetric(name, regDef.contrast);
|
|
2158
|
+
if (regDef.from !== void 0) try {
|
|
2159
|
+
extractOkhslFromValue(regDef.from);
|
|
2160
|
+
} catch (error) {
|
|
2161
|
+
throw new Error(`glaze: color "${name}" has an invalid "from" value. ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
2162
|
+
}
|
|
1560
2163
|
if (regDef.contrast !== void 0 && !regDef.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
|
|
1561
2164
|
if (regDef.tone !== void 0 && !isAbsoluteTone(regDef.tone) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "tone" without "base".`);
|
|
1562
2165
|
if (regDef.base && !allNames.has(regDef.base)) throw new Error(`glaze: color "${name}" references non-existent base "${regDef.base}".`);
|
|
1563
2166
|
if (regDef.base && localNames.has(regDef.base) && isShadowDef(defs[regDef.base])) throw new Error(`glaze: color "${name}" base "${regDef.base}" references a shadow color.`);
|
|
1564
|
-
if (!
|
|
2167
|
+
if (!hasAbsoluteTone(regDef) && regDef.base === void 0) throw new Error(`glaze: color "${name}" must have either absolute "tone" (root), "from" (a literal color), or "base" (dependent).`);
|
|
1565
2168
|
if (regDef.contrast !== void 0 && regDef.opacity !== void 0) console.warn(`glaze: color "${name}" has both "contrast" and "opacity". Opacity makes perceived tone unpredictable.`);
|
|
1566
2169
|
}
|
|
1567
2170
|
const visited = /* @__PURE__ */ new Set();
|
|
@@ -1673,7 +2276,10 @@ function warnContrastDrift(name, isDark, isHighContrast, contrast, yColor, yBase
|
|
|
1673
2276
|
* Color resolution engine.
|
|
1674
2277
|
*
|
|
1675
2278
|
* Runs the four-pass solver (light → light-HC → dark → dark-HC) that
|
|
1676
|
-
* turns a `ColorMap` into a fully resolved `ResolvedColor` per name
|
|
2279
|
+
* turns a `ColorMap` into a fully resolved `ResolvedColor` per name — or,
|
|
2280
|
+
* under a manual `contrastLevel`, a two-pass solver (light → dark) whose
|
|
2281
|
+
* authored HC pairs, tone window, and contrast targets are interpolated at
|
|
2282
|
+
* that level and whose high-contrast slots mirror the normal ones.
|
|
1677
2283
|
* Owns the per-scheme resolve helpers for regular, shadow, and mix
|
|
1678
2284
|
* color defs.
|
|
1679
2285
|
*
|
|
@@ -1762,109 +2368,212 @@ function resolveRoleInMap(name, def, defs, inferRole, roles) {
|
|
|
1762
2368
|
function resolveRole(name, def, ctx) {
|
|
1763
2369
|
return resolveRoleInMap(name, def, ctx.defs, ctx.config.inferRole, ctx.roles);
|
|
1764
2370
|
}
|
|
1765
|
-
function resolveContrastSpec(spec, isHighContrast, polarity) {
|
|
1766
|
-
|
|
2371
|
+
function resolveContrastSpec(spec, isHighContrast, config, polarity) {
|
|
2372
|
+
if (!isHighContrast && contrastFraction(config) !== void 0) return resolveContrastForLevel(spec, config.contrastLevel, polarity);
|
|
2373
|
+
const outerExplicitHC = Array.isArray(spec);
|
|
2374
|
+
return resolveContrastForMode(isHighContrast ? pairHC(spec) : pairNormal(spec), isHighContrast, polarity, outerExplicitHC);
|
|
2375
|
+
}
|
|
2376
|
+
/**
|
|
2377
|
+
* The authored tone for this pass: the pair's own entry in `'auto'` mode, or
|
|
2378
|
+
* the manual-level interpolation. Returns the parsed struct so the `kind` —
|
|
2379
|
+
* which selects the branch in `resolveDependentColor` — is fixed here once.
|
|
2380
|
+
*/
|
|
2381
|
+
function passTone(tone, isHighContrast, config) {
|
|
2382
|
+
const f = contrastFraction(config);
|
|
2383
|
+
return f === void 0 || isHighContrast ? parseToneValue(isHighContrast ? pairHC(tone) : pairNormal(tone)) : parseToneValueAt(tone, f);
|
|
2384
|
+
}
|
|
2385
|
+
/** The authored numeric pair for this pass (shadow `intensity`, mix `value`). */
|
|
2386
|
+
function passNumber(p, isHighContrast, config) {
|
|
2387
|
+
const f = contrastFraction(config);
|
|
2388
|
+
if (f === void 0 || isHighContrast) return isHighContrast ? pairHC(p) : pairNormal(p);
|
|
2389
|
+
return numberAt(p, f);
|
|
1767
2390
|
}
|
|
1768
2391
|
/**
|
|
1769
2392
|
* Apply the relative-tone delta against a base, honoring `flip`.
|
|
1770
2393
|
*
|
|
1771
|
-
* When `flip` is on and `base + delta` falls outside `[0, 100]`,
|
|
1772
|
-
* delta to the other side of the base
|
|
1773
|
-
*
|
|
2394
|
+
* When `flip` is on and `base + delta` falls outside `[0, 100]`, try mirroring
|
|
2395
|
+
* the delta to the other side of the base. If the mirrored target is also out
|
|
2396
|
+
* of range, keep the original delta so the caller clamps on the authored side.
|
|
2397
|
+
* When off, the caller clamps as usual.
|
|
1774
2398
|
*/
|
|
1775
2399
|
function applyToneFlip(delta, baseTone, flip) {
|
|
1776
2400
|
if (!flip) return delta;
|
|
1777
2401
|
const target = baseTone + delta;
|
|
1778
2402
|
if (target >= 0 && target <= 100) return delta;
|
|
1779
|
-
|
|
2403
|
+
const mirrored = baseTone - delta;
|
|
2404
|
+
if (mirrored >= 0 && mirrored <= 100) return -delta;
|
|
2405
|
+
return delta;
|
|
1780
2406
|
}
|
|
1781
|
-
|
|
1782
|
-
|
|
2407
|
+
/**
|
|
2408
|
+
* Dark position for an extreme tone (`'max'` / `'min'`) anchored to a base.
|
|
2409
|
+
*
|
|
2410
|
+
* A windowed dark remap compresses the base→extreme span, so the pair loses
|
|
2411
|
+
* contrast in dark. Instead, measure the tone shift the light scheme applied
|
|
2412
|
+
* between the base and the extreme, then replay it against the base's dark
|
|
2413
|
+
* tone: `auto` mirrors the sign (both ends invert), `fixed` keeps it. The
|
|
2414
|
+
* result is clamped to `[0, 100]` only — crossing the dark tone window is
|
|
2415
|
+
* intended, since the author asked for the extreme.
|
|
2416
|
+
*/
|
|
2417
|
+
function extremeDarkTone(authorTone, mode, isHighContrast, baseResolved, config) {
|
|
2418
|
+
const extremeLightTone = mapToneForScheme(authorTone, mode, false, isHighContrast, config);
|
|
2419
|
+
const lightBase = getSchemeVariant(baseResolved, false, isHighContrast);
|
|
2420
|
+
const darkBase = getSchemeVariant(baseResolved, true, isHighContrast);
|
|
2421
|
+
const shift = extremeLightTone - lightBase.t * 100;
|
|
2422
|
+
return clamp(darkBase.t * 100 + (mode === "auto" ? -shift : shift), 0, 100);
|
|
2423
|
+
}
|
|
2424
|
+
/**
|
|
2425
|
+
* Hue, absolute saturation (0–1) and tone (0–100) read off a color def's `from`.
|
|
2426
|
+
*
|
|
2427
|
+
* Deliberately not memoized. Keying a cache on the def object looks free — a
|
|
2428
|
+
* resolve reads this a handful of times per color — but defs are stored by
|
|
2429
|
+
* reference, so mutating one in place and re-setting it would invalidate the
|
|
2430
|
+
* theme's own cache while a def-keyed cache quietly served the old color. The
|
|
2431
|
+
* parse is a few microseconds against a resolve that is already cached; that is
|
|
2432
|
+
* not a trade worth a staleness bug.
|
|
2433
|
+
*/
|
|
2434
|
+
function fromSeed(def) {
|
|
2435
|
+
if (def.from === void 0) return void 0;
|
|
2436
|
+
const { h, s, l } = extractOkhslFromValue(def.from);
|
|
1783
2437
|
return {
|
|
1784
|
-
|
|
1785
|
-
|
|
2438
|
+
hue: h,
|
|
2439
|
+
saturation: s,
|
|
2440
|
+
tone: toTone(l)
|
|
1786
2441
|
};
|
|
1787
2442
|
}
|
|
1788
|
-
|
|
1789
|
-
|
|
2443
|
+
/**
|
|
2444
|
+
* The config this color resolves against.
|
|
2445
|
+
*
|
|
2446
|
+
* A `from` color carries a literal value, so its light variant has to survive
|
|
2447
|
+
* the light tone window intact — hence a local `lightTone: false`, the same
|
|
2448
|
+
* default the value-shorthand form of `glaze.color()` applies. Only the light
|
|
2449
|
+
* window is dropped: dark and high contrast keep theirs, because that is where
|
|
2450
|
+
* the color is expected to adapt.
|
|
2451
|
+
*/
|
|
2452
|
+
function configForColor(def, config) {
|
|
2453
|
+
if (def.from === void 0) return config;
|
|
2454
|
+
return {
|
|
2455
|
+
...config,
|
|
2456
|
+
lightTone: false
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
function resolveRootColor(def, isHighContrast, config) {
|
|
2460
|
+
if (def.tone === void 0) return clamp(fromSeed(def).tone, 0, 100);
|
|
2461
|
+
return clamp(passTone(def.tone, isHighContrast, config).value, 0, 100);
|
|
2462
|
+
}
|
|
2463
|
+
/**
|
|
2464
|
+
* Effective hue and saturation for one color in one scheme.
|
|
2465
|
+
*
|
|
2466
|
+
* Dark schemes read the dark seed pair and the color's `darkHue` /
|
|
2467
|
+
* `darkSaturation`, each falling back to its light counterpart. An explicit
|
|
2468
|
+
* dark saturation — on the seed or the def — is taken literally; the global
|
|
2469
|
+
* `darkDesaturation` reduction applies only when neither is authored.
|
|
2470
|
+
* `mode: 'static'` pins one value across every scheme, so it skips all of it.
|
|
2471
|
+
*
|
|
2472
|
+
* Shared by `resolveColorForScheme` (the emitted variant) and
|
|
2473
|
+
* `resolveDependentColor` (the contrast solver's inputs) so the solver always
|
|
2474
|
+
* measures the color it is actually going to produce.
|
|
2475
|
+
*/
|
|
2476
|
+
function resolveChannels(def, ctx, isDark) {
|
|
2477
|
+
const mode = def.mode ?? "auto";
|
|
2478
|
+
const satFactor = clamp(def.saturation ?? 1, 0, 1);
|
|
2479
|
+
const seed = fromSeed(def);
|
|
2480
|
+
const fromHue = seed !== void 0 && def.hue === void 0 ? seed.hue : void 0;
|
|
2481
|
+
const fromSaturation = seed !== void 0 && def.saturation === void 0 ? seed.saturation : void 0;
|
|
2482
|
+
if (!isDark || mode === "static") return {
|
|
2483
|
+
hue: fromHue ?? resolveEffectiveHue(ctx.hue, def.hue),
|
|
2484
|
+
saturation: fromSaturation ?? clamp(satFactor * ctx.saturation / 100, 0, 1)
|
|
2485
|
+
};
|
|
2486
|
+
const darkSeedSaturation = ctx.darkSaturation ?? ctx.saturation;
|
|
2487
|
+
const darkFactor = clamp(def.darkSaturation ?? satFactor, 0, 1);
|
|
2488
|
+
const fromDark = fromSaturation !== void 0 && def.darkSaturation === void 0;
|
|
2489
|
+
const raw = fromDark ? fromSaturation : darkFactor * darkSeedSaturation / 100;
|
|
2490
|
+
const explicitDark = fromDark ? false : def.darkSaturation !== void 0 || ctx.darkSaturation !== void 0;
|
|
2491
|
+
return {
|
|
2492
|
+
hue: fromHue !== void 0 && def.darkHue === void 0 ? fromHue : resolveEffectiveHue(ctx.darkHue ?? ctx.hue, def.darkHue ?? def.hue),
|
|
2493
|
+
saturation: clamp(explicitDark ? raw : mapSaturationDark(raw, mode, ctx.config), 0, 1)
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
function resolveDependentColor(name, def, ctx, isHighContrast, isDark, channels, polarity, effectivePastel) {
|
|
2497
|
+
const baseName = def.base;
|
|
1790
2498
|
const baseResolved = ctx.resolved.get(baseName);
|
|
1791
2499
|
if (!baseResolved) throw new Error(`glaze: base "${baseName}" not yet resolved for "${name}".`);
|
|
1792
2500
|
const mode = def.mode ?? "auto";
|
|
1793
|
-
const
|
|
1794
|
-
const flip = def.flip ?? ctx.config.autoFlip;
|
|
2501
|
+
const flip = def.autoFlip ?? ctx.config.autoFlip;
|
|
1795
2502
|
const pastel = effectivePastel;
|
|
2503
|
+
const config = configForColor(def, ctx.config);
|
|
1796
2504
|
const baseVariant = getSchemeVariant(baseResolved, isDark, isHighContrast);
|
|
1797
2505
|
const baseTone = baseVariant.t * 100;
|
|
1798
2506
|
let preferredTone;
|
|
1799
|
-
|
|
2507
|
+
let isExtreme = false;
|
|
2508
|
+
const seed = fromSeed(def);
|
|
2509
|
+
const rawTone = def.tone ?? (seed !== void 0 ? seed.tone : void 0);
|
|
1800
2510
|
if (rawTone === void 0) preferredTone = baseTone;
|
|
1801
2511
|
else {
|
|
1802
|
-
const parsed =
|
|
2512
|
+
const parsed = passTone(rawTone, isHighContrast, config);
|
|
1803
2513
|
if (parsed.kind === "relative") if (isDark && mode === "auto") {
|
|
1804
2514
|
const baseLightTone = getSchemeVariant(baseResolved, false, isHighContrast).t * 100;
|
|
1805
|
-
preferredTone = mapToneForScheme(clamp(baseLightTone + applyToneFlip(parsed.value, baseLightTone, flip), 0, 100), "auto", true, isHighContrast,
|
|
2515
|
+
preferredTone = mapToneForScheme(clamp(baseLightTone + applyToneFlip(parsed.value, baseLightTone, flip), 0, 100), "auto", true, isHighContrast, config);
|
|
1806
2516
|
} else preferredTone = clamp(baseTone + applyToneFlip(parsed.value, baseTone, flip), 0, 100);
|
|
1807
|
-
else
|
|
2517
|
+
else {
|
|
2518
|
+
isExtreme = parsed.kind === "extreme";
|
|
2519
|
+
if (isExtreme && isDark && mode !== "static") preferredTone = extremeDarkTone(parsed.value, mode, isHighContrast, baseResolved, config);
|
|
2520
|
+
else preferredTone = mapToneForScheme(parsed.value, mode, isDark, isHighContrast, config);
|
|
2521
|
+
}
|
|
1808
2522
|
}
|
|
1809
2523
|
const rawContrast = def.contrast;
|
|
1810
2524
|
if (rawContrast !== void 0) {
|
|
1811
|
-
const resolvedContrast = resolveContrastSpec(rawContrast, isHighContrast, polarity);
|
|
1812
|
-
const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode, ctx.config) : satFactor * ctx.saturation / 100;
|
|
2525
|
+
const resolvedContrast = resolveContrastSpec(rawContrast, isHighContrast, config, polarity);
|
|
1813
2526
|
const baseOkhsl = toOkhslVariant(baseVariant);
|
|
1814
2527
|
const baseLinearRgb = okhslToLinearSrgb(baseOkhsl.h, baseOkhsl.s, baseOkhsl.l, baseVariant.pastel ?? ctx.config.pastel);
|
|
1815
|
-
const
|
|
2528
|
+
const preferredRange = isExtreme ? [0, 1] : schemeToneRange(isDark, mode, isHighContrast, config);
|
|
1816
2529
|
let initialDirection;
|
|
1817
2530
|
if (preferredTone < baseTone) initialDirection = "darker";
|
|
1818
2531
|
else if (preferredTone > baseTone) initialDirection = "lighter";
|
|
1819
|
-
const
|
|
1820
|
-
hue:
|
|
1821
|
-
saturation:
|
|
1822
|
-
preferredTone: clamp(preferredTone / 100,
|
|
2532
|
+
const solve = {
|
|
2533
|
+
hue: channels.hue,
|
|
2534
|
+
saturation: channels.saturation,
|
|
2535
|
+
preferredTone: clamp(preferredTone / 100, preferredRange[0], preferredRange[1]),
|
|
1823
2536
|
baseLinearRgb,
|
|
1824
|
-
contrast: resolvedContrast,
|
|
1825
2537
|
toneRange: [0, 1],
|
|
1826
|
-
initialDirection,
|
|
1827
2538
|
flip,
|
|
1828
2539
|
pastel
|
|
2540
|
+
};
|
|
2541
|
+
const level = contrastFraction(ctx.config);
|
|
2542
|
+
let preferInitial = false;
|
|
2543
|
+
if (level !== void 0 && level > 0 && level < 1) {
|
|
2544
|
+
initialDirection = findToneForContrast({
|
|
2545
|
+
...solve,
|
|
2546
|
+
contrast: resolveContrastForLevel(rawContrast, level < PAIR_SWITCH ? 0 : 100, polarity),
|
|
2547
|
+
initialDirection
|
|
2548
|
+
}).tone * 100 < baseTone ? "darker" : "lighter";
|
|
2549
|
+
preferInitial = true;
|
|
2550
|
+
}
|
|
2551
|
+
const result = findToneForContrast({
|
|
2552
|
+
...solve,
|
|
2553
|
+
contrast: resolvedContrast,
|
|
2554
|
+
initialDirection,
|
|
2555
|
+
preferInitial
|
|
1829
2556
|
});
|
|
1830
2557
|
if (!result.met) warnContrastUnmet(name, isDark, isHighContrast, resolvedContrast, result.contrast);
|
|
1831
|
-
return
|
|
1832
|
-
tone: result.tone * 100,
|
|
1833
|
-
satFactor
|
|
1834
|
-
};
|
|
2558
|
+
return result.tone * 100;
|
|
1835
2559
|
}
|
|
1836
|
-
return
|
|
1837
|
-
tone: clamp(preferredTone, 0, 100),
|
|
1838
|
-
satFactor
|
|
1839
|
-
};
|
|
2560
|
+
return clamp(preferredTone, 0, 100);
|
|
1840
2561
|
}
|
|
1841
2562
|
function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
1842
2563
|
if (isShadowDef(def)) return resolveShadowForScheme(def, ctx, isDark, isHighContrast);
|
|
1843
2564
|
if (isMixDef(def)) return resolveMixForScheme(name, def, ctx, isDark, isHighContrast);
|
|
1844
2565
|
const regDef = def;
|
|
1845
2566
|
const mode = regDef.mode ?? "auto";
|
|
1846
|
-
const isRoot =
|
|
1847
|
-
const
|
|
2567
|
+
const isRoot = hasAbsoluteTone(regDef) && !regDef.base;
|
|
2568
|
+
const channels = resolveChannels(regDef, ctx, isDark);
|
|
1848
2569
|
const polarity = roleToPolarity(resolveRole(name, def, ctx));
|
|
1849
2570
|
const pastel = regDef.pastel ?? ctx.config.pastel;
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
if (isRoot) {
|
|
1853
|
-
const root = resolveRootColor(regDef, isHighContrast);
|
|
1854
|
-
finalTone = mapToneForScheme(root.authorTone, mode, isDark, isHighContrast, ctx.config);
|
|
1855
|
-
satFactor = root.satFactor;
|
|
1856
|
-
} else {
|
|
1857
|
-
const dep = resolveDependentColor(name, regDef, ctx, isHighContrast, isDark, effectiveHue, polarity, pastel);
|
|
1858
|
-
finalTone = dep.tone;
|
|
1859
|
-
satFactor = dep.satFactor;
|
|
1860
|
-
}
|
|
1861
|
-
const baseSat = satFactor * ctx.saturation / 100;
|
|
1862
|
-
const finalSat = isDark ? mapSaturationDark(baseSat, mode, ctx.config) : baseSat;
|
|
1863
|
-
const toneFraction = clamp(finalTone / 100, 0, 1);
|
|
2571
|
+
const config = configForColor(regDef, ctx.config);
|
|
2572
|
+
const finalTone = isRoot ? mapToneForScheme(resolveRootColor(regDef, isHighContrast, config), mode, isDark, isHighContrast, config) : resolveDependentColor(name, regDef, ctx, isHighContrast, isDark, channels, polarity, pastel);
|
|
1864
2573
|
return {
|
|
1865
|
-
h:
|
|
1866
|
-
s:
|
|
1867
|
-
t:
|
|
2574
|
+
h: channels.hue,
|
|
2575
|
+
s: channels.saturation,
|
|
2576
|
+
t: clamp(finalTone / 100, 0, 1),
|
|
1868
2577
|
alpha: regDef.opacity ?? 1,
|
|
1869
2578
|
pastel
|
|
1870
2579
|
};
|
|
@@ -1873,7 +2582,7 @@ function resolveShadowForScheme(def, ctx, isDark, isHighContrast) {
|
|
|
1873
2582
|
const bgVariant = toOkhslVariant(getSchemeVariant(ctx.resolved.get(def.bg), isDark, isHighContrast));
|
|
1874
2583
|
let fgVariant;
|
|
1875
2584
|
if (def.fg) fgVariant = toOkhslVariant(getSchemeVariant(ctx.resolved.get(def.fg), isDark, isHighContrast));
|
|
1876
|
-
const intensity =
|
|
2585
|
+
const intensity = passNumber(def.intensity, isHighContrast, ctx.config);
|
|
1877
2586
|
const tuning = resolveShadowTuning(def.tuning, ctx.config.shadowTuning);
|
|
1878
2587
|
return {
|
|
1879
2588
|
...toToneVariant(computeShadow(bgVariant, fgVariant, intensity, tuning)),
|
|
@@ -1922,7 +2631,7 @@ function resolveMixForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
|
1922
2631
|
const targetResolved = ctx.resolved.get(def.target);
|
|
1923
2632
|
const baseVariant = toOkhslVariant(getSchemeVariant(baseResolved, isDark, isHighContrast));
|
|
1924
2633
|
const targetVariant = toOkhslVariant(getSchemeVariant(targetResolved, isDark, isHighContrast));
|
|
1925
|
-
let t = clamp(
|
|
2634
|
+
let t = clamp(passNumber(def.value, isHighContrast, ctx.config), 0, 100) / 100;
|
|
1926
2635
|
const blend = def.blend ?? "opaque";
|
|
1927
2636
|
const space = def.space ?? "okhsl";
|
|
1928
2637
|
const polarity = roleToPolarity(resolveRole(name, def, ctx));
|
|
@@ -1930,7 +2639,7 @@ function resolveMixForScheme(name, def, ctx, isDark, isHighContrast) {
|
|
|
1930
2639
|
const baseLinear = okhslVariantToLinearRgb(baseVariant, baseVariant.pastel ?? ctx.config.pastel);
|
|
1931
2640
|
const targetLinear = okhslVariantToLinearRgb(targetVariant, targetVariant.pastel ?? ctx.config.pastel);
|
|
1932
2641
|
if (def.contrast !== void 0) {
|
|
1933
|
-
const resolvedContrast = resolveContrastSpec(def.contrast, isHighContrast, polarity);
|
|
2642
|
+
const resolvedContrast = resolveContrastSpec(def.contrast, isHighContrast, ctx.config, polarity);
|
|
1934
2643
|
const metric = resolvedContrast.metric;
|
|
1935
2644
|
let luminanceAt;
|
|
1936
2645
|
if (blend === "transparent" || space === "srgb") luminanceAt = (v) => metricLuminance(metric, linearSrgbLerp(baseLinear, targetLinear, v));
|
|
@@ -2014,9 +2723,14 @@ function seedField(order, ctx, field, source) {
|
|
|
2014
2723
|
}
|
|
2015
2724
|
}
|
|
2016
2725
|
/**
|
|
2017
|
-
* After the
|
|
2726
|
+
* After the passes, surface chromatic contrast drift (§10): a color
|
|
2018
2727
|
* resolved with a `base` + `contrast` may land slightly under the contrast
|
|
2019
2728
|
* its tone implies because chromatic luminance drifts from the gray tone.
|
|
2729
|
+
*
|
|
2730
|
+
* Under a manual `contrastLevel` only the two emitted variants are checked
|
|
2731
|
+
* (the high-contrast slots are mirrors), and the spec is resolved at the level
|
|
2732
|
+
* so the check measures the output against the target it was actually solved
|
|
2733
|
+
* for.
|
|
2020
2734
|
*/
|
|
2021
2735
|
function verifyContrastDrift(order, defs, result, config) {
|
|
2022
2736
|
const roles = /* @__PURE__ */ new Map();
|
|
@@ -2029,7 +2743,7 @@ function verifyContrastDrift(order, defs, result, config) {
|
|
|
2029
2743
|
const base = result.get(regDef.base);
|
|
2030
2744
|
if (!color || !base) continue;
|
|
2031
2745
|
const polarity = roleToPolarity(resolveRoleInMap(name, def, defs, config.inferRole, roles));
|
|
2032
|
-
|
|
2746
|
+
const schemes = [
|
|
2033
2747
|
{
|
|
2034
2748
|
isDark: false,
|
|
2035
2749
|
isHighContrast: false,
|
|
@@ -2050,8 +2764,11 @@ function verifyContrastDrift(order, defs, result, config) {
|
|
|
2050
2764
|
isHighContrast: true,
|
|
2051
2765
|
field: "darkContrast"
|
|
2052
2766
|
}
|
|
2053
|
-
]
|
|
2054
|
-
|
|
2767
|
+
];
|
|
2768
|
+
const manual = contrastFraction(config) !== void 0;
|
|
2769
|
+
for (const s of schemes) {
|
|
2770
|
+
if (manual && s.isHighContrast) continue;
|
|
2771
|
+
const spec = resolveContrastSpec(regDef.contrast, s.isHighContrast, config, polarity);
|
|
2055
2772
|
const cVariant = color[s.field];
|
|
2056
2773
|
const bVariant = base[s.field];
|
|
2057
2774
|
const cOkhsl = toOkhslVariant(cVariant);
|
|
@@ -2064,26 +2781,32 @@ function verifyContrastDrift(order, defs, result, config) {
|
|
|
2064
2781
|
}
|
|
2065
2782
|
}
|
|
2066
2783
|
}
|
|
2067
|
-
function resolveAllColors(
|
|
2784
|
+
function resolveAllColors(seed, defs, config, externalBases) {
|
|
2068
2785
|
validateColorDefs(defs, externalBases);
|
|
2069
2786
|
const order = topoSort(defs);
|
|
2070
2787
|
const ctx = {
|
|
2071
|
-
|
|
2072
|
-
saturation,
|
|
2788
|
+
...seed,
|
|
2073
2789
|
defs,
|
|
2074
2790
|
resolved: /* @__PURE__ */ new Map(),
|
|
2075
2791
|
config,
|
|
2076
2792
|
roles: /* @__PURE__ */ new Map()
|
|
2077
2793
|
};
|
|
2078
2794
|
if (externalBases) for (const [name, color] of externalBases) ctx.resolved.set(name, color);
|
|
2795
|
+
const manual = contrastFraction(config) !== void 0;
|
|
2079
2796
|
const lightMap = runPass(order, defs, ctx, false, false, "light");
|
|
2080
|
-
|
|
2081
|
-
|
|
2797
|
+
let lightHCMap = lightMap;
|
|
2798
|
+
if (!manual) {
|
|
2799
|
+
seedField(order, ctx, "lightContrast", lightMap);
|
|
2800
|
+
lightHCMap = runPass(order, defs, ctx, false, true, "lightContrast");
|
|
2801
|
+
}
|
|
2082
2802
|
seedField(order, ctx, "dark", lightMap);
|
|
2083
2803
|
seedField(order, ctx, "darkContrast", lightHCMap);
|
|
2084
2804
|
const darkMap = runPass(order, defs, ctx, true, false, "dark");
|
|
2085
|
-
|
|
2086
|
-
|
|
2805
|
+
let darkHCMap = darkMap;
|
|
2806
|
+
if (!manual) {
|
|
2807
|
+
seedField(order, ctx, "darkContrast", darkMap);
|
|
2808
|
+
darkHCMap = runPass(order, defs, ctx, true, true, "darkContrast");
|
|
2809
|
+
}
|
|
2087
2810
|
const result = /* @__PURE__ */ new Map();
|
|
2088
2811
|
for (const name of order) result.set(name, {
|
|
2089
2812
|
name,
|
|
@@ -2097,6 +2820,164 @@ function resolveAllColors(hue, saturation, defs, config, externalBases) {
|
|
|
2097
2820
|
return result;
|
|
2098
2821
|
}
|
|
2099
2822
|
|
|
2823
|
+
//#endregion
|
|
2824
|
+
//#region src/channels.ts
|
|
2825
|
+
/**
|
|
2826
|
+
* Hue channel planning for `splitHue` exports.
|
|
2827
|
+
*
|
|
2828
|
+
* Builds per-color hue var references and the `--*-hue` declarations backing
|
|
2829
|
+
* them for oklch CSS / Tasty output when every color is pastel.
|
|
2830
|
+
*
|
|
2831
|
+
* Hue is scheme-independent unless the theme or a color authors a dark hue,
|
|
2832
|
+
* so declarations are collected per scheme (`'light'` / `'dark'`) and the dark
|
|
2833
|
+
* set is emitted only when it actually differs — see `buildHueDeclarations`.
|
|
2834
|
+
* A color that authors a hue in *either* scheme gets its own var in both, so
|
|
2835
|
+
* the shared `var()` reference stays valid.
|
|
2836
|
+
*/
|
|
2837
|
+
const ACHROMATIC_EPSILON = 1e-6;
|
|
2838
|
+
function cssProp(prefix, name, suffix) {
|
|
2839
|
+
return `--${prefix}${name}${suffix}`;
|
|
2840
|
+
}
|
|
2841
|
+
function isAchromatic(v) {
|
|
2842
|
+
return v.s <= ACHROMATIC_EPSILON;
|
|
2843
|
+
}
|
|
2844
|
+
/** Seed hue backing `--{baseName}-hue` in a given scheme. */
|
|
2845
|
+
function schemeSeedHue(ctx, scheme) {
|
|
2846
|
+
return scheme === "dark" ? ctx.darkSeedHue ?? ctx.seedHue : ctx.seedHue;
|
|
2847
|
+
}
|
|
2848
|
+
function themeHuePlan(name, def, variant, ctx, scheme) {
|
|
2849
|
+
if (def === void 0 || isShadowDef(def) || isMixDef(def) || isAchromatic(variant)) return {
|
|
2850
|
+
hueVar: "",
|
|
2851
|
+
inline: true,
|
|
2852
|
+
declarations: []
|
|
2853
|
+
};
|
|
2854
|
+
const regDef = def;
|
|
2855
|
+
const baseHueVar = `var(--${ctx.baseName}-hue)`;
|
|
2856
|
+
const prop = cssProp(ctx.prefix, name, "-hue");
|
|
2857
|
+
if ((regDef.mode ?? "auto") === "static" && schemeSeedHue(ctx, "dark") !== ctx.seedHue) return {
|
|
2858
|
+
hueVar: `var(${prop})`,
|
|
2859
|
+
inline: false,
|
|
2860
|
+
declarations: [{
|
|
2861
|
+
prop,
|
|
2862
|
+
value: String(variant.h)
|
|
2863
|
+
}]
|
|
2864
|
+
};
|
|
2865
|
+
if (regDef.hue === void 0 && regDef.darkHue === void 0 && regDef.from === void 0) return {
|
|
2866
|
+
hueVar: baseHueVar,
|
|
2867
|
+
inline: false,
|
|
2868
|
+
declarations: []
|
|
2869
|
+
};
|
|
2870
|
+
const authored = scheme === "dark" ? regDef.darkHue ?? regDef.hue : regDef.hue;
|
|
2871
|
+
if (authored === void 0) {
|
|
2872
|
+
if (regDef.from !== void 0) return {
|
|
2873
|
+
hueVar: `var(${prop})`,
|
|
2874
|
+
inline: false,
|
|
2875
|
+
declarations: [{
|
|
2876
|
+
prop,
|
|
2877
|
+
value: String(variant.h)
|
|
2878
|
+
}]
|
|
2879
|
+
};
|
|
2880
|
+
return {
|
|
2881
|
+
hueVar: `var(${prop})`,
|
|
2882
|
+
inline: false,
|
|
2883
|
+
declarations: [{
|
|
2884
|
+
prop,
|
|
2885
|
+
value: baseHueVar
|
|
2886
|
+
}]
|
|
2887
|
+
};
|
|
2888
|
+
}
|
|
2889
|
+
const parsed = parseRelativeOrAbsolute(authored);
|
|
2890
|
+
if (parsed.relative) {
|
|
2891
|
+
const sign = parsed.value >= 0 ? "+" : "-";
|
|
2892
|
+
const magnitude = Math.abs(parsed.value);
|
|
2893
|
+
const value = `calc(var(--${ctx.baseName}-hue) ${sign} ${magnitude})`;
|
|
2894
|
+
return {
|
|
2895
|
+
hueVar: `var(${prop})`,
|
|
2896
|
+
inline: false,
|
|
2897
|
+
declarations: [{
|
|
2898
|
+
prop,
|
|
2899
|
+
value
|
|
2900
|
+
}]
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
const absHue = (parsed.value % 360 + 360) % 360;
|
|
2904
|
+
return {
|
|
2905
|
+
hueVar: `var(${prop})`,
|
|
2906
|
+
inline: false,
|
|
2907
|
+
declarations: [{
|
|
2908
|
+
prop,
|
|
2909
|
+
value: String(absHue)
|
|
2910
|
+
}]
|
|
2911
|
+
};
|
|
2912
|
+
}
|
|
2913
|
+
function standaloneHuePlan(name, variant, ctx, scheme) {
|
|
2914
|
+
if (isAchromatic(variant)) return {
|
|
2915
|
+
hueVar: "",
|
|
2916
|
+
inline: true,
|
|
2917
|
+
declarations: []
|
|
2918
|
+
};
|
|
2919
|
+
const hue = scheme === "dark" ? variant.h : ctx.resolvedHue ?? variant.h;
|
|
2920
|
+
const prop = cssProp(ctx.prefix, name, "-hue");
|
|
2921
|
+
return {
|
|
2922
|
+
hueVar: `var(${prop})`,
|
|
2923
|
+
inline: false,
|
|
2924
|
+
declarations: [{
|
|
2925
|
+
prop,
|
|
2926
|
+
value: String(hue)
|
|
2927
|
+
}]
|
|
2928
|
+
};
|
|
2929
|
+
}
|
|
2930
|
+
function buildHuePlan(name, def, variant, ctx, scheme = "light") {
|
|
2931
|
+
if (ctx.mode === "standalone") return standaloneHuePlan(name, variant, ctx, scheme);
|
|
2932
|
+
return themeHuePlan(name, def, variant, ctx, scheme);
|
|
2933
|
+
}
|
|
2934
|
+
/** Collect unique hue declarations across all colors (theme + per-color). */
|
|
2935
|
+
function collectHueDeclarations(resolved, ctx, scheme) {
|
|
2936
|
+
if (ctx.emitDeclarations === false) return [];
|
|
2937
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2938
|
+
const out = [];
|
|
2939
|
+
const push = (decl) => {
|
|
2940
|
+
if (seen.has(decl.prop)) return;
|
|
2941
|
+
seen.add(decl.prop);
|
|
2942
|
+
out.push(decl);
|
|
2943
|
+
};
|
|
2944
|
+
if (ctx.mode === "theme") push({
|
|
2945
|
+
prop: `--${ctx.baseName}-hue`,
|
|
2946
|
+
value: String(schemeSeedHue(ctx, scheme))
|
|
2947
|
+
});
|
|
2948
|
+
for (const [name, color] of resolved) {
|
|
2949
|
+
const def = ctx.defs[name];
|
|
2950
|
+
const plan = buildHuePlan(name, def, scheme === "dark" ? color.dark : color.light, ctx, scheme);
|
|
2951
|
+
for (const decl of plan.declarations) push(decl);
|
|
2952
|
+
}
|
|
2953
|
+
return out;
|
|
2954
|
+
}
|
|
2955
|
+
/**
|
|
2956
|
+
* Hue declarations for both schemes. `dark` is empty when it resolves to the
|
|
2957
|
+
* same hues as light — the common case, with no `darkHue` anywhere.
|
|
2958
|
+
*
|
|
2959
|
+
* When they differ, the **whole** dark set is returned rather than only the
|
|
2960
|
+
* entries whose text changed. A relative declaration reads
|
|
2961
|
+
* `calc(var(--theme-hue) + N)` in both schemes even though its resolved value
|
|
2962
|
+
* moves with the seed, and custom properties substitute `var()` at
|
|
2963
|
+
* computed-value time on the declaring element — so re-declaring
|
|
2964
|
+
* `--{baseName}-hue` alone would leave inherited per-color vars still holding
|
|
2965
|
+
* the light-substituted value.
|
|
2966
|
+
*/
|
|
2967
|
+
function buildHueDeclarations(resolved, ctx) {
|
|
2968
|
+
const light = collectHueDeclarations(resolved, ctx, "light");
|
|
2969
|
+
const dark = collectHueDeclarations(resolved, ctx, "dark");
|
|
2970
|
+
return {
|
|
2971
|
+
light,
|
|
2972
|
+
dark: dark.length === light.length && dark.every((decl, i) => decl.prop === light[i].prop && decl.value === light[i].value) ? [] : dark
|
|
2973
|
+
};
|
|
2974
|
+
}
|
|
2975
|
+
function buildHuePlans(resolved, ctx) {
|
|
2976
|
+
const plans = /* @__PURE__ */ new Map();
|
|
2977
|
+
for (const [name, color] of resolved) plans.set(name, buildHuePlan(name, ctx.defs[name], color.light, ctx));
|
|
2978
|
+
return plans;
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2100
2981
|
//#endregion
|
|
2101
2982
|
//#region src/formatters.ts
|
|
2102
2983
|
/**
|
|
@@ -2121,34 +3002,90 @@ const formatters = {
|
|
|
2121
3002
|
function fmt(value, decimals) {
|
|
2122
3003
|
return parseFloat(value.toFixed(decimals)).toString();
|
|
2123
3004
|
}
|
|
2124
|
-
function formatVariant(v, format = "
|
|
3005
|
+
function formatVariant(v, format = "oklch", pastel = false) {
|
|
3006
|
+
const effectivePastel = v.pastel ?? pastel;
|
|
3007
|
+
let base;
|
|
3008
|
+
if (format === "okhst") base = formatOkhst(v.h, v.s * 100, v.t * 100, effectivePastel);
|
|
3009
|
+
else {
|
|
3010
|
+
const { l } = variantToOkhsl(v);
|
|
3011
|
+
base = formatters[format](v.h, v.s * 100, l * 100, effectivePastel);
|
|
3012
|
+
}
|
|
3013
|
+
if (v.alpha >= 1) return base;
|
|
3014
|
+
const closing = base.lastIndexOf(")");
|
|
3015
|
+
return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
|
|
3016
|
+
}
|
|
3017
|
+
/**
|
|
3018
|
+
* Format a resolved variant as `oklch(L C <hueVar>)`, splicing a CSS hue var
|
|
3019
|
+
* for `splitHue` exports. Falls back to inline when the plan is inline.
|
|
3020
|
+
*/
|
|
3021
|
+
function formatVariantHue(v, plan, pastel = false) {
|
|
2125
3022
|
const effectivePastel = v.pastel ?? pastel;
|
|
2126
3023
|
const { l } = variantToOkhsl(v);
|
|
2127
|
-
const
|
|
3024
|
+
const [L, C] = okhslToOklch(v.h, v.s, l, effectivePastel);
|
|
3025
|
+
let base;
|
|
3026
|
+
if (plan.inline) if (v.s <= 1e-6) base = `oklch(${fmt(L, 4)} 0 0)`;
|
|
3027
|
+
else base = formatOklch(v.h, v.s * 100, l * 100, effectivePastel);
|
|
3028
|
+
else base = `oklch(${fmt(L, 4)} ${fmt(C, 4)} ${plan.hueVar})`;
|
|
2128
3029
|
if (v.alpha >= 1) return base;
|
|
2129
3030
|
const closing = base.lastIndexOf(")");
|
|
2130
3031
|
return `${base.slice(0, closing)} / ${fmt(v.alpha, 4)})`;
|
|
2131
3032
|
}
|
|
3033
|
+
function formatColorValue(v, format, pastel, huePlan) {
|
|
3034
|
+
if (format === "oklch" && huePlan !== void 0) return formatVariantHue(v, huePlan, pastel);
|
|
3035
|
+
return formatVariant(v, format, pastel);
|
|
3036
|
+
}
|
|
3037
|
+
/**
|
|
3038
|
+
* Resolve the effective output modes for an export.
|
|
3039
|
+
*
|
|
3040
|
+
* A global manual `contrastLevel` turns off high-contrast output outright: the
|
|
3041
|
+
* level *is* the contrast preference, so a separate high-contrast tier has no
|
|
3042
|
+
* meaning alongside it. `modes.highContrast` therefore reads as "emit a separate
|
|
3043
|
+
* high-contrast set **when** contrast is automatic" — it goes inert under a
|
|
3044
|
+
* manual level rather than fighting it, and silently, since switching a
|
|
3045
|
+
* preference from auto to manual is normal use and not a mistake to report.
|
|
3046
|
+
*
|
|
3047
|
+
* A level set on a single theme or token does not change which modes are
|
|
3048
|
+
* emitted: sibling themes in a palette may still have a real high-contrast
|
|
3049
|
+
* tier, and the manual one correctly reports its own resolved values there —
|
|
3050
|
+
* the alternative would drop its colors from that tier entirely.
|
|
3051
|
+
*/
|
|
2132
3052
|
function resolveModes(override) {
|
|
2133
3053
|
const cfg = getConfig();
|
|
2134
3054
|
return {
|
|
2135
3055
|
dark: override?.dark ?? cfg.modes.dark,
|
|
2136
|
-
highContrast: override?.highContrast ?? cfg.modes.highContrast
|
|
3056
|
+
highContrast: contrastFraction(cfg) !== void 0 ? false : override?.highContrast ?? cfg.modes.highContrast
|
|
2137
3057
|
};
|
|
2138
3058
|
}
|
|
2139
|
-
function buildTokenMap(resolved, prefix, states, modes, format = "
|
|
3059
|
+
function buildTokenMap(resolved, prefix, states, modes, format = "oklch", pastel = false, channelCtx) {
|
|
2140
3060
|
const tokens = {};
|
|
3061
|
+
const huePlans = channelCtx !== void 0 && format === "oklch" ? buildHuePlans(resolved, channelCtx) : void 0;
|
|
3062
|
+
if (huePlans !== void 0 && channelCtx !== void 0) {
|
|
3063
|
+
const hueDecls = buildHueDeclarations(resolved, channelCtx);
|
|
3064
|
+
for (const decl of hueDecls.light) tokens[`$${decl.prop.slice(2)}`] = { "": decl.value };
|
|
3065
|
+
if (modes.dark) for (const decl of hueDecls.dark) {
|
|
3066
|
+
const key = `$${decl.prop.slice(2)}`;
|
|
3067
|
+
tokens[key] = {
|
|
3068
|
+
...tokens[key],
|
|
3069
|
+
[states.dark]: decl.value
|
|
3070
|
+
};
|
|
3071
|
+
}
|
|
3072
|
+
for (const [name, color] of resolved) tokens[`#${prefix}${name}`] = buildTokenEntry(color, states, modes, format, pastel, huePlans.get(name));
|
|
3073
|
+
return tokens;
|
|
3074
|
+
}
|
|
2141
3075
|
for (const [name, color] of resolved) {
|
|
2142
3076
|
const key = `#${prefix}${name}`;
|
|
2143
|
-
|
|
2144
|
-
if (modes.dark) entry[states.dark] = formatVariant(color.dark, format, pastel);
|
|
2145
|
-
if (modes.highContrast) entry[states.highContrast] = formatVariant(color.lightContrast, format, pastel);
|
|
2146
|
-
if (modes.dark && modes.highContrast) entry[`${states.dark} & ${states.highContrast}`] = formatVariant(color.darkContrast, format, pastel);
|
|
2147
|
-
tokens[key] = entry;
|
|
3077
|
+
tokens[key] = buildTokenEntry(color, states, modes, format, pastel);
|
|
2148
3078
|
}
|
|
2149
3079
|
return tokens;
|
|
2150
3080
|
}
|
|
2151
|
-
function
|
|
3081
|
+
function buildTokenEntry(color, states, modes, format, pastel, huePlan) {
|
|
3082
|
+
const entry = { "": formatColorValue(color.light, format, pastel, huePlan) };
|
|
3083
|
+
if (modes.dark) entry[states.dark] = formatColorValue(color.dark, format, pastel, huePlan);
|
|
3084
|
+
if (modes.highContrast) entry[states.highContrast] = formatColorValue(color.lightContrast, format, pastel, huePlan);
|
|
3085
|
+
if (modes.dark && modes.highContrast) entry[`${states.dark} & ${states.highContrast}`] = formatColorValue(color.darkContrast, format, pastel, huePlan);
|
|
3086
|
+
return entry;
|
|
3087
|
+
}
|
|
3088
|
+
function buildFlatTokenMap(resolved, prefix, modes, format = "oklch", pastel = false) {
|
|
2152
3089
|
const result = { light: {} };
|
|
2153
3090
|
if (modes.dark) result.dark = {};
|
|
2154
3091
|
if (modes.highContrast) result.lightContrast = {};
|
|
@@ -2162,7 +3099,7 @@ function buildFlatTokenMap(resolved, prefix, modes, format = "okhsl", pastel = f
|
|
|
2162
3099
|
}
|
|
2163
3100
|
return result;
|
|
2164
3101
|
}
|
|
2165
|
-
function buildJsonMap(resolved, modes, format = "
|
|
3102
|
+
function buildJsonMap(resolved, modes, format = "oklch", pastel = false) {
|
|
2166
3103
|
const result = {};
|
|
2167
3104
|
for (const [name, color] of resolved) {
|
|
2168
3105
|
const entry = { light: formatVariant(color.light, format, pastel) };
|
|
@@ -2173,19 +3110,29 @@ function buildJsonMap(resolved, modes, format = "okhsl", pastel = false) {
|
|
|
2173
3110
|
}
|
|
2174
3111
|
return result;
|
|
2175
3112
|
}
|
|
2176
|
-
function buildCssMap(resolved, prefix, suffix, format, pastel = false) {
|
|
3113
|
+
function buildCssMap(resolved, prefix, suffix, format, pastel = false, channelCtx) {
|
|
2177
3114
|
const lines = {
|
|
2178
3115
|
light: [],
|
|
2179
3116
|
dark: [],
|
|
2180
3117
|
lightContrast: [],
|
|
2181
3118
|
darkContrast: []
|
|
2182
3119
|
};
|
|
3120
|
+
const huePlans = channelCtx !== void 0 && format === "oklch" ? buildHuePlans(resolved, channelCtx) : void 0;
|
|
3121
|
+
if (huePlans !== void 0 && channelCtx !== void 0) {
|
|
3122
|
+
const hueDecls = buildHueDeclarations(resolved, channelCtx);
|
|
3123
|
+
for (const decl of hueDecls.light) lines.light.push(`${decl.prop}: ${decl.value};`);
|
|
3124
|
+
for (const decl of hueDecls.dark) {
|
|
3125
|
+
lines.dark.push(`${decl.prop}: ${decl.value};`);
|
|
3126
|
+
lines.darkContrast.push(`${decl.prop}: ${decl.value};`);
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
2183
3129
|
for (const [name, color] of resolved) {
|
|
2184
3130
|
const prop = `--${prefix}${name}${suffix}`;
|
|
2185
|
-
|
|
2186
|
-
lines.
|
|
2187
|
-
lines.
|
|
2188
|
-
lines.
|
|
3131
|
+
const plan = huePlans?.get(name);
|
|
3132
|
+
lines.light.push(`${prop}: ${formatColorValue(color.light, format, pastel, plan)};`);
|
|
3133
|
+
lines.dark.push(`${prop}: ${formatColorValue(color.dark, format, pastel, plan)};`);
|
|
3134
|
+
lines.lightContrast.push(`${prop}: ${formatColorValue(color.lightContrast, format, pastel, plan)};`);
|
|
3135
|
+
lines.darkContrast.push(`${prop}: ${formatColorValue(color.darkContrast, format, pastel, plan)};`);
|
|
2189
3136
|
}
|
|
2190
3137
|
return {
|
|
2191
3138
|
light: lines.light.join("\n"),
|
|
@@ -2392,12 +3339,10 @@ function buildTailwindMap(resolved, themePrefix, cssPrefix, modes, format, darkS
|
|
|
2392
3339
|
* `{ l, c, h }`), the structured-input validator, the two factory paths
|
|
2393
3340
|
* (value vs structured), and the JSON-safe export / rehydration round-trip.
|
|
2394
3341
|
*
|
|
2395
|
-
*
|
|
2396
|
-
*
|
|
2397
|
-
*
|
|
2398
|
-
* `
|
|
2399
|
-
* The token's resolved variants are then memoized on first
|
|
2400
|
-
* `.resolve()` / `.token()` / ... call.
|
|
3342
|
+
* Tokens store a sparse local config override only. Resolve merges the
|
|
3343
|
+
* live global config with that local override (invalidated on
|
|
3344
|
+
* `configure()`). Authoring `.export(override?)` freezes
|
|
3345
|
+
* `getConfig() ∪ local ∪ override` at call time.
|
|
2401
3346
|
*/
|
|
2402
3347
|
/** Internal name of the user-facing standalone color in the synthesized def map. */
|
|
2403
3348
|
const STANDALONE_VALUE = "value";
|
|
@@ -2412,342 +3357,159 @@ const RESERVED_STANDALONE_NAMES = new Set([
|
|
|
2412
3357
|
STANDALONE_BASE
|
|
2413
3358
|
]);
|
|
2414
3359
|
/**
|
|
2415
|
-
*
|
|
2416
|
-
*
|
|
2417
|
-
* Light window defaults to `false` (preserve input tone exactly).
|
|
2418
|
-
* All other fields snapshot from global at create time. User override
|
|
2419
|
-
* fields win over all defaults.
|
|
3360
|
+
* Value-form local override: `lightTone` defaults to `false` (preserve
|
|
3361
|
+
* input tone). User override fields win.
|
|
2420
3362
|
*/
|
|
2421
|
-
function
|
|
2422
|
-
const cfg = getConfig();
|
|
3363
|
+
function sparseValueFormLocal(userOverride) {
|
|
2423
3364
|
return {
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
darkDesaturation: userOverride?.darkDesaturation ?? cfg.darkDesaturation,
|
|
2427
|
-
autoFlip: userOverride?.autoFlip ?? cfg.autoFlip,
|
|
2428
|
-
shadowTuning: userOverride?.shadowTuning ?? cfg.shadowTuning
|
|
3365
|
+
...userOverride,
|
|
3366
|
+
lightTone: userOverride?.lightTone !== void 0 ? userOverride.lightTone : false
|
|
2429
3367
|
};
|
|
2430
3368
|
}
|
|
2431
3369
|
/**
|
|
2432
|
-
*
|
|
2433
|
-
*
|
|
2434
|
-
* Both light and dark windows snapshot from global at create time.
|
|
2435
|
-
* User override fields win.
|
|
3370
|
+
* Validate a user-supplied `opacity` override on `glaze.color()`.
|
|
3371
|
+
* Must be a finite number in `0..=1`.
|
|
2436
3372
|
*/
|
|
2437
|
-
function
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
3373
|
+
function validateStandaloneOpacity(value) {
|
|
3374
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error(`glaze.color: opacity must be a finite number in 0–1 (got ${value}).`);
|
|
3375
|
+
}
|
|
3376
|
+
/**
|
|
3377
|
+
* Validate a structured `GlazeColorInput`. Range-checks the `hue` /
|
|
3378
|
+
* `saturation` / `tone` numerics (and any HC-pair second value)
|
|
3379
|
+
* before the resolver sees them so out-of-range or non-finite inputs
|
|
3380
|
+
* fail with a helpful, top-level error rather than producing a
|
|
3381
|
+
* NaN-laden token. `opacity` is checked here too so all input
|
|
3382
|
+
* validation lives in one place.
|
|
3383
|
+
*/
|
|
3384
|
+
function validateStructuredInput(input) {
|
|
3385
|
+
if (!Number.isFinite(input.hue)) throw new Error(`glaze.color: structured hue must be a finite number (got ${input.hue}).`);
|
|
3386
|
+
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}).`);
|
|
3387
|
+
const checkTone = (value, label) => {
|
|
3388
|
+
if (value === "max" || value === "min") return;
|
|
3389
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 100) throw new Error(`glaze.color: structured ${label} must be a finite number in 0–100 or 'max'/'min' (got ${String(value)}).`);
|
|
2445
3390
|
};
|
|
3391
|
+
if (Array.isArray(input.tone)) {
|
|
3392
|
+
checkTone(input.tone[0], "tone[normal]");
|
|
3393
|
+
checkTone(input.tone[1], "tone[hc]");
|
|
3394
|
+
} else checkTone(input.tone, "tone");
|
|
3395
|
+
if (input.saturationFactor !== void 0) {
|
|
3396
|
+
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}).`);
|
|
3397
|
+
}
|
|
3398
|
+
if (typeof input.darkHue === "number" && !Number.isFinite(input.darkHue)) throw new Error(`glaze.color: structured darkHue must be a finite number (got ${input.darkHue}).`);
|
|
3399
|
+
if (input.darkSaturation !== void 0) {
|
|
3400
|
+
if (!Number.isFinite(input.darkSaturation) || input.darkSaturation < 0 || input.darkSaturation > 100) throw new Error(`glaze.color: structured darkSaturation must be a finite number in 0–100 (got ${input.darkSaturation}).`);
|
|
3401
|
+
}
|
|
3402
|
+
if (input.darkSaturationFactor !== void 0) {
|
|
3403
|
+
if (!Number.isFinite(input.darkSaturationFactor) || input.darkSaturationFactor < 0 || input.darkSaturationFactor > 1) throw new Error(`glaze.color: structured darkSaturationFactor must be a finite number in 0–1 (got ${input.darkSaturationFactor}).`);
|
|
3404
|
+
}
|
|
3405
|
+
if (input.opacity !== void 0) validateStandaloneOpacity(input.opacity);
|
|
2446
3406
|
}
|
|
2447
3407
|
/**
|
|
2448
|
-
*
|
|
2449
|
-
*
|
|
2450
|
-
* fields are present; the snapshot fields win.
|
|
3408
|
+
* Validate a user-supplied `name` override. Rejects empty / whitespace-only
|
|
3409
|
+
* strings and names colliding with `glaze`'s reserved internal sentinels.
|
|
2451
3410
|
*/
|
|
2452
|
-
function
|
|
2453
|
-
|
|
3411
|
+
function validateStandaloneName(name) {
|
|
3412
|
+
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.");
|
|
3413
|
+
if (RESERVED_STANDALONE_NAMES.has(name)) {
|
|
3414
|
+
const reserved = [...RESERVED_STANDALONE_NAMES].map((n) => `"${n}"`).join(", ");
|
|
3415
|
+
throw new Error(`glaze.color: name "${name}" is reserved (used internally). Reserved names are: ${reserved}. Pick a different name.`);
|
|
3416
|
+
}
|
|
2454
3417
|
}
|
|
2455
3418
|
/**
|
|
2456
|
-
*
|
|
2457
|
-
* `okhsl()`, `oklch()`) plus their legacy alpha aliases (`rgba()`, `hsla()`).
|
|
3419
|
+
* Resolve `'max'` / `'min'` to their literal author tones (100 / 0).
|
|
2458
3420
|
*
|
|
2459
|
-
*
|
|
2460
|
-
*
|
|
2461
|
-
*
|
|
2462
|
-
*
|
|
3421
|
+
* The hidden `STANDALONE_SEED` anchor exists only so `contrast` has something
|
|
3422
|
+
* to measure against — it is not an authored base. Pinning the extreme keeps
|
|
3423
|
+
* the token on the plain scheme mapping instead of the base-anchored dark
|
|
3424
|
+
* replay the resolver applies to real `base` references.
|
|
2463
3425
|
*/
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
return parseFloat(raw);
|
|
3426
|
+
function pinExtremeTone(tone) {
|
|
3427
|
+
const pin = (value) => value === "max" ? 100 : value === "min" ? 0 : value;
|
|
3428
|
+
return Array.isArray(tone) ? [pin(tone[0]), pin(tone[1])] : pin(tone);
|
|
2468
3429
|
}
|
|
2469
3430
|
/**
|
|
2470
|
-
*
|
|
2471
|
-
* whether an alpha channel was present.
|
|
3431
|
+
* Build the `ColorMap` for a value-shorthand `glaze.color()` call.
|
|
2472
3432
|
*
|
|
2473
|
-
*
|
|
2474
|
-
*
|
|
2475
|
-
*
|
|
3433
|
+
* The user-facing color (`STANDALONE_VALUE`) defaults to `mode: 'auto'`
|
|
3434
|
+
* across every value-shorthand form.
|
|
3435
|
+
*
|
|
3436
|
+
* An absolute `hue` override becomes the seed while a relative one becomes a
|
|
3437
|
+
* def field; `darkHue` always lands on the def in either form, so an absolute
|
|
3438
|
+
* `darkHue` wins outright instead of being re-offset by a relative `hue`.
|
|
3439
|
+
*
|
|
3440
|
+
* When the user requests `contrast` or relative `tone`, a hidden
|
|
3441
|
+
* `STANDALONE_SEED` def is synthesized at `mode: 'static'`. That keeps
|
|
3442
|
+
* the seed pinned to the literal user-provided color across all four
|
|
3443
|
+
* variants, so the contrast solver always anchors against it.
|
|
2476
3444
|
*/
|
|
2477
|
-
function
|
|
2478
|
-
const
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
3445
|
+
function buildStandaloneValueDefs(main, options) {
|
|
3446
|
+
const seedHue = typeof options?.hue === "number" ? options.hue : main.h;
|
|
3447
|
+
const seedSaturation = options?.saturation ?? main.s * 100;
|
|
3448
|
+
const relativeHue = typeof options?.hue === "string" ? options.hue : void 0;
|
|
3449
|
+
const toneOption = options?.tone;
|
|
3450
|
+
const hasExternalBase = options?.base !== void 0;
|
|
3451
|
+
const needsSeedAnchor = !hasExternalBase && (options?.contrast !== void 0 || toneOption !== void 0 && !isAbsoluteTone(toneOption));
|
|
3452
|
+
if (options?.opacity !== void 0) validateStandaloneOpacity(options.opacity);
|
|
3453
|
+
const userName = options?.name;
|
|
3454
|
+
if (userName !== void 0) validateStandaloneName(userName);
|
|
3455
|
+
const primary = userName ?? STANDALONE_VALUE;
|
|
3456
|
+
const seedTone = toTone(main.l);
|
|
3457
|
+
const primaryTone = toneOption ?? seedTone;
|
|
3458
|
+
const valueDef = {
|
|
3459
|
+
hue: relativeHue,
|
|
3460
|
+
saturation: options?.saturationFactor,
|
|
3461
|
+
darkHue: options?.darkHue,
|
|
3462
|
+
darkSaturation: options?.darkSaturationFactor,
|
|
3463
|
+
tone: needsSeedAnchor ? pinExtremeTone(primaryTone) : primaryTone,
|
|
3464
|
+
contrast: options?.contrast,
|
|
3465
|
+
mode: options?.mode ?? "auto",
|
|
3466
|
+
autoFlip: options?.autoFlip,
|
|
3467
|
+
opacity: options?.opacity,
|
|
3468
|
+
pastel: options?.pastel,
|
|
3469
|
+
role: options?.role,
|
|
3470
|
+
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
3471
|
+
};
|
|
3472
|
+
const defs = { [primary]: valueDef };
|
|
3473
|
+
if (needsSeedAnchor) defs[STANDALONE_SEED] = {
|
|
3474
|
+
hue: main.h,
|
|
3475
|
+
saturation: 1,
|
|
3476
|
+
tone: seedTone,
|
|
3477
|
+
mode: "static"
|
|
2482
3478
|
};
|
|
2483
|
-
const components = body.split(/[\s,]+/).filter(Boolean);
|
|
2484
|
-
if (components.length === 4) {
|
|
2485
|
-
components.pop();
|
|
2486
|
-
return {
|
|
2487
|
-
components,
|
|
2488
|
-
hadAlpha: true
|
|
2489
|
-
};
|
|
2490
|
-
}
|
|
2491
3479
|
return {
|
|
2492
|
-
|
|
2493
|
-
|
|
3480
|
+
seed: {
|
|
3481
|
+
hue: seedHue,
|
|
3482
|
+
saturation: seedSaturation,
|
|
3483
|
+
darkSaturation: options?.darkSaturation
|
|
3484
|
+
},
|
|
3485
|
+
defs,
|
|
3486
|
+
primary
|
|
2494
3487
|
};
|
|
2495
3488
|
}
|
|
2496
|
-
function
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
const
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
h,
|
|
2507
|
-
s,
|
|
2508
|
-
l
|
|
3489
|
+
function createColorTokenFromDefs(seed, defs, primary, configOverride, baseToken, buildExport) {
|
|
3490
|
+
let cache = null;
|
|
3491
|
+
function getEffectiveConfig() {
|
|
3492
|
+
const version = getConfigVersion();
|
|
3493
|
+
if (cache && cache.version === version) return cache.effectiveConfig;
|
|
3494
|
+
const effectiveConfig = mergeConfig(getConfig(), configOverride);
|
|
3495
|
+
cache = {
|
|
3496
|
+
map: null,
|
|
3497
|
+
version,
|
|
3498
|
+
effectiveConfig
|
|
2509
3499
|
};
|
|
3500
|
+
return effectiveConfig;
|
|
2510
3501
|
}
|
|
2511
|
-
const m = input.match(COLOR_FN_RE);
|
|
2512
|
-
if (!m) throw new Error(`glaze: unsupported color string "${input}".`);
|
|
2513
|
-
const fn = m[1].toLowerCase();
|
|
2514
|
-
const { components, hadAlpha } = splitColorBody(m[2].trim());
|
|
2515
|
-
if (hadAlpha) warnDroppedAlpha(input);
|
|
2516
|
-
if (components.length !== 3) throw new Error(`glaze: expected 3 components in "${input}".`);
|
|
2517
|
-
switch (fn) {
|
|
2518
|
-
case "rgb":
|
|
2519
|
-
case "rgba": {
|
|
2520
|
-
const [h, s, l] = srgbToOkhsl([
|
|
2521
|
-
parseNumberOrPercent(components[0], 255) / 255,
|
|
2522
|
-
parseNumberOrPercent(components[1], 255) / 255,
|
|
2523
|
-
parseNumberOrPercent(components[2], 255) / 255
|
|
2524
|
-
]);
|
|
2525
|
-
return {
|
|
2526
|
-
h,
|
|
2527
|
-
s,
|
|
2528
|
-
l
|
|
2529
|
-
};
|
|
2530
|
-
}
|
|
2531
|
-
case "hsl":
|
|
2532
|
-
case "hsla": {
|
|
2533
|
-
const [oh, os, ol] = srgbToOkhsl(hslToSrgb(parseFloat(components[0]), parseNumberOrPercent(components[1], 1), parseNumberOrPercent(components[2], 1)));
|
|
2534
|
-
return {
|
|
2535
|
-
h: oh,
|
|
2536
|
-
s: os,
|
|
2537
|
-
l: ol
|
|
2538
|
-
};
|
|
2539
|
-
}
|
|
2540
|
-
case "okhsl": return {
|
|
2541
|
-
h: parseFloat(components[0]),
|
|
2542
|
-
s: parseNumberOrPercent(components[1], 1),
|
|
2543
|
-
l: parseNumberOrPercent(components[2], 1)
|
|
2544
|
-
};
|
|
2545
|
-
case "okhst": return okhstToOkhsl({
|
|
2546
|
-
h: parseFloat(components[0]),
|
|
2547
|
-
s: parseNumberOrPercent(components[1], 1),
|
|
2548
|
-
t: parseNumberOrPercent(components[2], 1)
|
|
2549
|
-
});
|
|
2550
|
-
case "oklch": {
|
|
2551
|
-
const L = parseNumberOrPercent(components[0], 1);
|
|
2552
|
-
const C = parseNumberOrPercent(components[1], .4);
|
|
2553
|
-
const hRad = parseFloat(components[2]) * Math.PI / 180;
|
|
2554
|
-
const [h, s, l] = oklabToOkhsl([
|
|
2555
|
-
L,
|
|
2556
|
-
C * Math.cos(hRad),
|
|
2557
|
-
C * Math.sin(hRad)
|
|
2558
|
-
]);
|
|
2559
|
-
return {
|
|
2560
|
-
h,
|
|
2561
|
-
s,
|
|
2562
|
-
l
|
|
2563
|
-
};
|
|
2564
|
-
}
|
|
2565
|
-
}
|
|
2566
|
-
throw new Error(`glaze: unsupported color function "${fn}".`);
|
|
2567
|
-
}
|
|
2568
|
-
/**
|
|
2569
|
-
* Validate a user-supplied `OkhslColor`. Catches the common 0-100 vs 0-1
|
|
2570
|
-
* confusion (the structured form uses 0-100, OKHSL objects use 0-1).
|
|
2571
|
-
*/
|
|
2572
|
-
function validateOkhslColor(value) {
|
|
2573
|
-
const { h, s, l } = value;
|
|
2574
|
-
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) throw new Error("glaze.color: OkhslColor h/s/l must be finite numbers.");
|
|
2575
|
-
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, tone } (which uses 0–100)?");
|
|
2576
|
-
}
|
|
2577
|
-
/** Validate a user-supplied `{ r, g, b }` object in 0–255. */
|
|
2578
|
-
function validateRgbColor(value) {
|
|
2579
|
-
for (const key of [
|
|
2580
|
-
"r",
|
|
2581
|
-
"g",
|
|
2582
|
-
"b"
|
|
2583
|
-
]) {
|
|
2584
|
-
const n = value[key];
|
|
2585
|
-
if (!Number.isFinite(n) || n < 0 || n > 255) throw new Error(`glaze.color: RgbColor ${key} must be a finite number in 0–255 (got ${n}).`);
|
|
2586
|
-
}
|
|
2587
|
-
}
|
|
2588
|
-
/** Validate a user-supplied `{ l, c, h }` OKLCh object. */
|
|
2589
|
-
function validateOklchColor(value) {
|
|
2590
|
-
const { l, c, h } = value;
|
|
2591
|
-
if (!Number.isFinite(l) || !Number.isFinite(c) || !Number.isFinite(h)) throw new Error("glaze.color: OklchColor l/c/h must be finite numbers.");
|
|
2592
|
-
if (l > 1.5 || c > 1.5) throw new Error("glaze.color: OklchColor l/c must be in 0–1 range (matching oklch() strings).");
|
|
2593
|
-
}
|
|
2594
|
-
function oklchComponentsToOkhsl(l, c, hDeg) {
|
|
2595
|
-
const hRad = hDeg * Math.PI / 180;
|
|
2596
|
-
const [h, s, outL] = oklabToOkhsl([
|
|
2597
|
-
l,
|
|
2598
|
-
c * Math.cos(hRad),
|
|
2599
|
-
c * Math.sin(hRad)
|
|
2600
|
-
]);
|
|
2601
|
-
return {
|
|
2602
|
-
h,
|
|
2603
|
-
s,
|
|
2604
|
-
l: outL
|
|
2605
|
-
};
|
|
2606
|
-
}
|
|
2607
|
-
function isRgbColorObject(value) {
|
|
2608
|
-
return "r" in value && "g" in value && "b" in value;
|
|
2609
|
-
}
|
|
2610
|
-
function isOklchColorObject(value) {
|
|
2611
|
-
return "c" in value && "l" in value && "h" in value;
|
|
2612
|
-
}
|
|
2613
|
-
function isOkhstColorObject(value) {
|
|
2614
|
-
return "t" in value && "h" in value && "s" in value;
|
|
2615
|
-
}
|
|
2616
|
-
/** Validate a user-supplied `{ h, s, t }` OKHST object (s/t in 0–1). */
|
|
2617
|
-
function validateOkhstColor(value) {
|
|
2618
|
-
const { h, s, t } = value;
|
|
2619
|
-
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(t)) throw new Error("glaze.color: OkhstColor h/s/t must be finite numbers.");
|
|
2620
|
-
if (s > 1.5 || t > 1.5) throw new Error("glaze.color: OkhstColor s/t must be in 0–1 range. Did you mean the structured form { hue, saturation, tone } (which uses 0–100)?");
|
|
2621
|
-
}
|
|
2622
|
-
/**
|
|
2623
|
-
* Validate a user-supplied `opacity` override on `glaze.color()`.
|
|
2624
|
-
* Must be a finite number in `0..=1`.
|
|
2625
|
-
*/
|
|
2626
|
-
function validateStandaloneOpacity(value) {
|
|
2627
|
-
if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error(`glaze.color: opacity must be a finite number in 0–1 (got ${value}).`);
|
|
2628
|
-
}
|
|
2629
|
-
/**
|
|
2630
|
-
* Validate a structured `GlazeColorInput`. Range-checks the `hue` /
|
|
2631
|
-
* `saturation` / `tone` numerics (and any HC-pair second value)
|
|
2632
|
-
* before the resolver sees them so out-of-range or non-finite inputs
|
|
2633
|
-
* fail with a helpful, top-level error rather than producing a
|
|
2634
|
-
* NaN-laden token. `opacity` is checked here too so all input
|
|
2635
|
-
* validation lives in one place.
|
|
2636
|
-
*/
|
|
2637
|
-
function validateStructuredInput(input) {
|
|
2638
|
-
if (!Number.isFinite(input.hue)) throw new Error(`glaze.color: structured hue must be a finite number (got ${input.hue}).`);
|
|
2639
|
-
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}).`);
|
|
2640
|
-
const checkTone = (value, label) => {
|
|
2641
|
-
if (value === "max" || value === "min") return;
|
|
2642
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 100) throw new Error(`glaze.color: structured ${label} must be a finite number in 0–100 or 'max'/'min' (got ${String(value)}).`);
|
|
2643
|
-
};
|
|
2644
|
-
if (Array.isArray(input.tone)) {
|
|
2645
|
-
checkTone(input.tone[0], "tone[normal]");
|
|
2646
|
-
checkTone(input.tone[1], "tone[hc]");
|
|
2647
|
-
} else checkTone(input.tone, "tone");
|
|
2648
|
-
if (input.saturationFactor !== void 0) {
|
|
2649
|
-
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}).`);
|
|
2650
|
-
}
|
|
2651
|
-
if (input.opacity !== void 0) validateStandaloneOpacity(input.opacity);
|
|
2652
|
-
}
|
|
2653
|
-
/**
|
|
2654
|
-
* Validate a user-supplied `name` override. Rejects empty / whitespace-only
|
|
2655
|
-
* strings and names colliding with `glaze`'s reserved internal sentinels.
|
|
2656
|
-
*/
|
|
2657
|
-
function validateStandaloneName(name) {
|
|
2658
|
-
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.");
|
|
2659
|
-
if (RESERVED_STANDALONE_NAMES.has(name)) {
|
|
2660
|
-
const reserved = [...RESERVED_STANDALONE_NAMES].map((n) => `"${n}"`).join(", ");
|
|
2661
|
-
throw new Error(`glaze.color: name "${name}" is reserved (used internally). Reserved names are: ${reserved}. Pick a different name.`);
|
|
2662
|
-
}
|
|
2663
|
-
}
|
|
2664
|
-
/**
|
|
2665
|
-
* Extract an OKHSL color from any `GlazeColorValue` form. Also used by
|
|
2666
|
-
* `glaze.shadow()` so all shadow inputs (hex, color functions, OKHSL,
|
|
2667
|
-
* literal objects) go through one parser.
|
|
2668
|
-
*/
|
|
2669
|
-
function extractOkhslFromValue(value) {
|
|
2670
|
-
if (typeof value === "string") return parseColorString(value);
|
|
2671
|
-
if (Array.isArray(value)) throw new Error("glaze.color: RGB tuple [r, g, b] is no longer supported — use { r, g, b } instead.");
|
|
2672
|
-
if (isRgbColorObject(value)) {
|
|
2673
|
-
validateRgbColor(value);
|
|
2674
|
-
const [h, s, l] = srgbToOkhsl([
|
|
2675
|
-
value.r / 255,
|
|
2676
|
-
value.g / 255,
|
|
2677
|
-
value.b / 255
|
|
2678
|
-
]);
|
|
2679
|
-
return {
|
|
2680
|
-
h,
|
|
2681
|
-
s,
|
|
2682
|
-
l
|
|
2683
|
-
};
|
|
2684
|
-
}
|
|
2685
|
-
if (isOklchColorObject(value)) {
|
|
2686
|
-
validateOklchColor(value);
|
|
2687
|
-
return oklchComponentsToOkhsl(value.l, value.c, value.h);
|
|
2688
|
-
}
|
|
2689
|
-
if (isOkhstColorObject(value)) {
|
|
2690
|
-
validateOkhstColor(value);
|
|
2691
|
-
return okhstToOkhsl(value);
|
|
2692
|
-
}
|
|
2693
|
-
validateOkhslColor(value);
|
|
2694
|
-
return value;
|
|
2695
|
-
}
|
|
2696
|
-
/**
|
|
2697
|
-
* Build the `ColorMap` for a value-shorthand `glaze.color()` call.
|
|
2698
|
-
*
|
|
2699
|
-
* The user-facing color (`STANDALONE_VALUE`) defaults to `mode: 'auto'`
|
|
2700
|
-
* across every value-shorthand form.
|
|
2701
|
-
*
|
|
2702
|
-
* When the user requests `contrast` or relative `tone`, a hidden
|
|
2703
|
-
* `STANDALONE_SEED` def is synthesized at `mode: 'static'`. That keeps
|
|
2704
|
-
* the seed pinned to the literal user-provided color across all four
|
|
2705
|
-
* variants, so the contrast solver always anchors against it.
|
|
2706
|
-
*/
|
|
2707
|
-
function buildStandaloneValueDefs(main, options) {
|
|
2708
|
-
const seedHue = typeof options?.hue === "number" ? options.hue : main.h;
|
|
2709
|
-
const seedSaturation = options?.saturation ?? main.s * 100;
|
|
2710
|
-
const relativeHue = typeof options?.hue === "string" ? options.hue : void 0;
|
|
2711
|
-
const toneOption = options?.tone;
|
|
2712
|
-
const hasExternalBase = options?.base !== void 0;
|
|
2713
|
-
const needsSeedAnchor = !hasExternalBase && (options?.contrast !== void 0 || toneOption !== void 0 && !isAbsoluteTone(toneOption));
|
|
2714
|
-
if (options?.opacity !== void 0) validateStandaloneOpacity(options.opacity);
|
|
2715
|
-
const userName = options?.name;
|
|
2716
|
-
if (userName !== void 0) validateStandaloneName(userName);
|
|
2717
|
-
const primary = userName ?? STANDALONE_VALUE;
|
|
2718
|
-
const seedTone = toTone(main.l);
|
|
2719
|
-
const valueDef = {
|
|
2720
|
-
hue: relativeHue,
|
|
2721
|
-
saturation: options?.saturationFactor,
|
|
2722
|
-
tone: toneOption ?? seedTone,
|
|
2723
|
-
contrast: options?.contrast,
|
|
2724
|
-
mode: options?.mode ?? "auto",
|
|
2725
|
-
flip: options?.flip,
|
|
2726
|
-
opacity: options?.opacity,
|
|
2727
|
-
pastel: options?.pastel,
|
|
2728
|
-
role: options?.role,
|
|
2729
|
-
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
2730
|
-
};
|
|
2731
|
-
const defs = { [primary]: valueDef };
|
|
2732
|
-
if (needsSeedAnchor) defs[STANDALONE_SEED] = {
|
|
2733
|
-
hue: main.h,
|
|
2734
|
-
saturation: 1,
|
|
2735
|
-
tone: seedTone,
|
|
2736
|
-
mode: "static"
|
|
2737
|
-
};
|
|
2738
|
-
return {
|
|
2739
|
-
seedHue,
|
|
2740
|
-
seedSaturation,
|
|
2741
|
-
defs,
|
|
2742
|
-
primary
|
|
2743
|
-
};
|
|
2744
|
-
}
|
|
2745
|
-
function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveConfig, baseToken, exportData) {
|
|
2746
|
-
let cached;
|
|
2747
3502
|
const resolveOnce = () => {
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
3503
|
+
const version = getConfigVersion();
|
|
3504
|
+
if (cache && cache.version === version && cache.map) return cache.map;
|
|
3505
|
+
const effectiveConfig = getEffectiveConfig();
|
|
3506
|
+
const map = resolveAllColors(seed, defs, effectiveConfig, baseToken ? new Map([[STANDALONE_BASE, baseToken.resolve()]]) : void 0);
|
|
3507
|
+
cache = {
|
|
3508
|
+
map,
|
|
3509
|
+
version,
|
|
3510
|
+
effectiveConfig
|
|
3511
|
+
};
|
|
3512
|
+
return map;
|
|
2751
3513
|
};
|
|
2752
3514
|
const resolveStates = (options) => {
|
|
2753
3515
|
const cfg = getConfig();
|
|
@@ -2757,7 +3519,7 @@ function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effect
|
|
|
2757
3519
|
};
|
|
2758
3520
|
};
|
|
2759
3521
|
const tokenLike = (options) => {
|
|
2760
|
-
return buildTokenMap(resolveOnce(), "", resolveStates(options), resolveModes(options?.modes), options?.format,
|
|
3522
|
+
return buildTokenMap(resolveOnce(), "", resolveStates(options), resolveModes(options?.modes), options?.format ?? "oklch", getEffectiveConfig().pastel)[`#${primary}`];
|
|
2761
3523
|
};
|
|
2762
3524
|
return {
|
|
2763
3525
|
resolve() {
|
|
@@ -2766,14 +3528,32 @@ function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effect
|
|
|
2766
3528
|
token: tokenLike,
|
|
2767
3529
|
tasty: tokenLike,
|
|
2768
3530
|
json(options) {
|
|
2769
|
-
|
|
3531
|
+
const format = options?.format ?? "oklch";
|
|
3532
|
+
assertNativeFormat(format, "json");
|
|
3533
|
+
return buildJsonMap(resolveOnce(), resolveModes(options?.modes), format, getEffectiveConfig().pastel)[primary];
|
|
2770
3534
|
},
|
|
2771
3535
|
css(options) {
|
|
2772
|
-
|
|
3536
|
+
const format = options.format ?? "oklch";
|
|
3537
|
+
assertNativeFormat(format, "css");
|
|
3538
|
+
const resolved = resolveOnce().get(primary);
|
|
3539
|
+
const renamed = new Map([[options.name, resolved]]);
|
|
3540
|
+
let channelCtx;
|
|
3541
|
+
if (options.splitHue && format === "oklch") {
|
|
3542
|
+
assertAllPastel(renamed, resolveModes());
|
|
3543
|
+
channelCtx = {
|
|
3544
|
+
seedHue: seed.hue,
|
|
3545
|
+
baseName: options.name,
|
|
3546
|
+
prefix: "",
|
|
3547
|
+
defs: { [options.name]: defs[primary] },
|
|
3548
|
+
mode: "standalone",
|
|
3549
|
+
resolvedHue: resolved.light.h
|
|
3550
|
+
};
|
|
3551
|
+
}
|
|
3552
|
+
return buildCssMap(renamed, "", options.suffix ?? "-color", format, getEffectiveConfig().pastel, channelCtx);
|
|
2773
3553
|
},
|
|
2774
3554
|
dtcg(options) {
|
|
2775
3555
|
const modes = resolveModes(options?.modes);
|
|
2776
|
-
const doc = buildDtcgMap(resolveOnce(), "", modes, options?.colorSpace ?? "srgb",
|
|
3556
|
+
const doc = buildDtcgMap(resolveOnce(), "", modes, options?.colorSpace ?? "srgb", getEffectiveConfig().pastel);
|
|
2777
3557
|
const result = { light: doc.light[primary] };
|
|
2778
3558
|
if (doc.dark) result.dark = doc.dark[primary];
|
|
2779
3559
|
if (doc.lightContrast) result.lightContrast = doc.lightContrast[primary];
|
|
@@ -2781,7 +3561,7 @@ function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effect
|
|
|
2781
3561
|
return result;
|
|
2782
3562
|
},
|
|
2783
3563
|
dtcgResolver(options) {
|
|
2784
|
-
const doc = buildDtcgMap(resolveOnce(), "", resolveModes(options?.modes), options?.colorSpace ?? "srgb",
|
|
3564
|
+
const doc = buildDtcgMap(resolveOnce(), "", resolveModes(options?.modes), options?.colorSpace ?? "srgb", getEffectiveConfig().pastel);
|
|
2785
3565
|
const name = options.name;
|
|
2786
3566
|
const result = { light: { [name]: doc.light[primary] } };
|
|
2787
3567
|
if (doc.dark) result.dark = { [name]: doc.dark[primary] };
|
|
@@ -2790,9 +3570,13 @@ function createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effect
|
|
|
2790
3570
|
return buildDtcgResolver(result, options);
|
|
2791
3571
|
},
|
|
2792
3572
|
tailwind(options) {
|
|
2793
|
-
|
|
3573
|
+
const format = options.format ?? "oklch";
|
|
3574
|
+
assertNativeFormat(format, "tailwind");
|
|
3575
|
+
return buildTailwindMap(new Map([[options.name, resolveOnce().get(primary)]]), "", options.namespace ?? "color-", resolveModes(options?.modes), format, options.darkSelector ?? ".dark", options.highContrastSelector ?? ".high-contrast", getEffectiveConfig().pastel);
|
|
2794
3576
|
},
|
|
2795
|
-
export
|
|
3577
|
+
export(override) {
|
|
3578
|
+
return buildExport(override);
|
|
3579
|
+
}
|
|
2796
3580
|
};
|
|
2797
3581
|
}
|
|
2798
3582
|
/**
|
|
@@ -2841,90 +3625,99 @@ function createColorToken(input, configOverride) {
|
|
|
2841
3625
|
const hasExternalBase = baseToken !== void 0;
|
|
2842
3626
|
const needsSeedAnchor = !hasExternalBase && input.contrast !== void 0;
|
|
2843
3627
|
const defs = { [primary]: {
|
|
2844
|
-
tone: input.tone,
|
|
3628
|
+
tone: needsSeedAnchor ? pinExtremeTone(input.tone) : input.tone,
|
|
2845
3629
|
saturation: input.saturationFactor,
|
|
3630
|
+
darkHue: input.darkHue,
|
|
3631
|
+
darkSaturation: input.darkSaturationFactor,
|
|
2846
3632
|
mode: input.mode ?? "auto",
|
|
2847
|
-
|
|
3633
|
+
autoFlip: input.autoFlip,
|
|
2848
3634
|
contrast: input.contrast,
|
|
2849
3635
|
opacity: input.opacity,
|
|
2850
3636
|
pastel: input.pastel,
|
|
2851
3637
|
role: input.role,
|
|
2852
3638
|
base: hasExternalBase ? STANDALONE_BASE : needsSeedAnchor ? STANDALONE_SEED : void 0
|
|
2853
3639
|
} };
|
|
2854
|
-
if (needsSeedAnchor) {
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
3640
|
+
if (needsSeedAnchor) defs[STANDALONE_SEED] = {
|
|
3641
|
+
tone: pinExtremeTone(pairNormal(input.tone)),
|
|
3642
|
+
saturation: 1,
|
|
3643
|
+
mode: "static"
|
|
3644
|
+
};
|
|
3645
|
+
const localOverride = configOverride;
|
|
3646
|
+
return createColorTokenFromDefs({
|
|
3647
|
+
hue: input.hue,
|
|
3648
|
+
saturation: input.saturation,
|
|
3649
|
+
darkSaturation: input.darkSaturation
|
|
3650
|
+
}, defs, primary, localOverride, baseToken, (exportArg) => ({
|
|
3651
|
+
kind: "color",
|
|
3652
|
+
version: GLAZE_EXPORT_VERSION,
|
|
2865
3653
|
form: "structured",
|
|
2866
|
-
input: buildStructuredInputExport(input),
|
|
2867
|
-
config:
|
|
2868
|
-
});
|
|
2869
|
-
return createColorTokenFromDefs(input.hue, input.saturation, defs, primary, effectiveConfig, baseToken, exportData);
|
|
3654
|
+
input: buildStructuredInputExport(input, exportArg),
|
|
3655
|
+
config: freezeConfigForExport(localOverride, exportArg)
|
|
3656
|
+
}));
|
|
2870
3657
|
}
|
|
2871
3658
|
function createColorTokenFromValue(value, options, configOverride) {
|
|
2872
3659
|
const main = extractOkhslFromValue(value);
|
|
2873
3660
|
const linkingBase = toLinkingBase(resolveBaseToken(options?.base));
|
|
2874
|
-
const {
|
|
2875
|
-
const
|
|
2876
|
-
|
|
2877
|
-
|
|
3661
|
+
const { seed, defs, primary } = buildStandaloneValueDefs(main, options);
|
|
3662
|
+
const localOverride = sparseValueFormLocal(configOverride);
|
|
3663
|
+
return createColorTokenFromDefs(seed, defs, primary, localOverride, linkingBase, (exportArg) => ({
|
|
3664
|
+
kind: "color",
|
|
3665
|
+
version: GLAZE_EXPORT_VERSION,
|
|
2878
3666
|
form: "value",
|
|
2879
3667
|
input: value,
|
|
2880
|
-
...options !== void 0 ? { overrides: buildOverridesExport(options) } : {},
|
|
2881
|
-
config:
|
|
2882
|
-
});
|
|
2883
|
-
return createColorTokenFromDefs(seedHue, seedSaturation, defs, primary, effectiveConfig, linkingBase, exportData);
|
|
3668
|
+
...options !== void 0 ? { overrides: buildOverridesExport(options, exportArg) } : {},
|
|
3669
|
+
config: freezeConfigForExport(localOverride, exportArg)
|
|
3670
|
+
}));
|
|
2884
3671
|
}
|
|
2885
3672
|
/**
|
|
2886
3673
|
* Build a JSON-safe snapshot of `GlazeColorOverrides`. `base` is
|
|
2887
3674
|
* recursively serialized when it was originally a token; raw values are
|
|
2888
3675
|
* preserved as-is so `glaze.colorFrom(...)` round-trips them.
|
|
2889
3676
|
*/
|
|
2890
|
-
function buildOverridesExport(options) {
|
|
3677
|
+
function buildOverridesExport(options, exportArg) {
|
|
2891
3678
|
const out = {};
|
|
2892
3679
|
if (options.hue !== void 0) out.hue = options.hue;
|
|
2893
3680
|
if (options.saturation !== void 0) out.saturation = options.saturation;
|
|
2894
3681
|
if (options.tone !== void 0) out.tone = options.tone;
|
|
2895
3682
|
if (options.saturationFactor !== void 0) out.saturationFactor = options.saturationFactor;
|
|
3683
|
+
if (options.darkHue !== void 0) out.darkHue = options.darkHue;
|
|
3684
|
+
if (options.darkSaturation !== void 0) out.darkSaturation = options.darkSaturation;
|
|
3685
|
+
if (options.darkSaturationFactor !== void 0) out.darkSaturationFactor = options.darkSaturationFactor;
|
|
2896
3686
|
if (options.mode !== void 0) out.mode = options.mode;
|
|
2897
|
-
if (options.
|
|
3687
|
+
if (options.autoFlip !== void 0) out.autoFlip = options.autoFlip;
|
|
2898
3688
|
if (options.contrast !== void 0) out.contrast = options.contrast;
|
|
2899
3689
|
if (options.opacity !== void 0) out.opacity = options.opacity;
|
|
2900
3690
|
if (options.name !== void 0) out.name = options.name;
|
|
2901
3691
|
if (options.pastel !== void 0) out.pastel = options.pastel;
|
|
2902
3692
|
if (options.role !== void 0) out.role = options.role;
|
|
2903
|
-
if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export() : options.base;
|
|
3693
|
+
if (options.base !== void 0) out.base = isGlazeColorToken(options.base) ? options.base.export(exportArg) : options.base;
|
|
2904
3694
|
return out;
|
|
2905
3695
|
}
|
|
2906
|
-
function buildStructuredInputExport(input) {
|
|
3696
|
+
function buildStructuredInputExport(input, exportArg) {
|
|
2907
3697
|
const out = {
|
|
2908
3698
|
hue: input.hue,
|
|
2909
3699
|
saturation: input.saturation,
|
|
2910
3700
|
tone: input.tone
|
|
2911
3701
|
};
|
|
2912
3702
|
if (input.saturationFactor !== void 0) out.saturationFactor = input.saturationFactor;
|
|
3703
|
+
if (input.darkHue !== void 0) out.darkHue = input.darkHue;
|
|
3704
|
+
if (input.darkSaturation !== void 0) out.darkSaturation = input.darkSaturation;
|
|
3705
|
+
if (input.darkSaturationFactor !== void 0) out.darkSaturationFactor = input.darkSaturationFactor;
|
|
2913
3706
|
if (input.mode !== void 0) out.mode = input.mode;
|
|
2914
|
-
if (input.
|
|
3707
|
+
if (input.autoFlip !== void 0) out.autoFlip = input.autoFlip;
|
|
2915
3708
|
if (input.opacity !== void 0) out.opacity = input.opacity;
|
|
2916
3709
|
if (input.contrast !== void 0) out.contrast = input.contrast;
|
|
2917
3710
|
if (input.name !== void 0) out.name = input.name;
|
|
2918
3711
|
if (input.pastel !== void 0) out.pastel = input.pastel;
|
|
2919
3712
|
if (input.role !== void 0) out.role = input.role;
|
|
2920
|
-
if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export() : input.base;
|
|
3713
|
+
if (input.base !== void 0) out.base = isGlazeColorToken(input.base) ? input.base.export(exportArg) : input.base;
|
|
2921
3714
|
return out;
|
|
2922
3715
|
}
|
|
2923
3716
|
/**
|
|
2924
3717
|
* Discriminate a `GlazeColorTokenExport` from a raw `GlazeColorValue`.
|
|
2925
3718
|
*/
|
|
2926
3719
|
function isExportedToken(candidate) {
|
|
2927
|
-
return
|
|
3720
|
+
return isColorTokenExport(candidate);
|
|
2928
3721
|
}
|
|
2929
3722
|
function rehydrateOverrides(data) {
|
|
2930
3723
|
const out = {};
|
|
@@ -2932,8 +3725,11 @@ function rehydrateOverrides(data) {
|
|
|
2932
3725
|
if (data.saturation !== void 0) out.saturation = data.saturation;
|
|
2933
3726
|
if (data.tone !== void 0) out.tone = data.tone;
|
|
2934
3727
|
if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
|
|
3728
|
+
if (data.darkHue !== void 0) out.darkHue = data.darkHue;
|
|
3729
|
+
if (data.darkSaturation !== void 0) out.darkSaturation = data.darkSaturation;
|
|
3730
|
+
if (data.darkSaturationFactor !== void 0) out.darkSaturationFactor = data.darkSaturationFactor;
|
|
2935
3731
|
if (data.mode !== void 0) out.mode = data.mode;
|
|
2936
|
-
if (data.
|
|
3732
|
+
if (data.autoFlip !== void 0) out.autoFlip = data.autoFlip;
|
|
2937
3733
|
if (data.contrast !== void 0) out.contrast = data.contrast;
|
|
2938
3734
|
if (data.opacity !== void 0) out.opacity = data.opacity;
|
|
2939
3735
|
if (data.name !== void 0) out.name = data.name;
|
|
@@ -2949,8 +3745,11 @@ function rehydrateStructuredInput(data) {
|
|
|
2949
3745
|
tone: data.tone
|
|
2950
3746
|
};
|
|
2951
3747
|
if (data.saturationFactor !== void 0) out.saturationFactor = data.saturationFactor;
|
|
3748
|
+
if (data.darkHue !== void 0) out.darkHue = data.darkHue;
|
|
3749
|
+
if (data.darkSaturation !== void 0) out.darkSaturation = data.darkSaturation;
|
|
3750
|
+
if (data.darkSaturationFactor !== void 0) out.darkSaturationFactor = data.darkSaturationFactor;
|
|
2952
3751
|
if (data.mode !== void 0) out.mode = data.mode;
|
|
2953
|
-
if (data.
|
|
3752
|
+
if (data.autoFlip !== void 0) out.autoFlip = data.autoFlip;
|
|
2954
3753
|
if (data.opacity !== void 0) out.opacity = data.opacity;
|
|
2955
3754
|
if (data.contrast !== void 0) out.contrast = data.contrast;
|
|
2956
3755
|
if (data.name !== void 0) out.name = data.name;
|
|
@@ -2963,12 +3762,14 @@ function rehydrateStructuredInput(data) {
|
|
|
2963
3762
|
* Rehydrate a token from its `.export()` snapshot. Recursively rebuilds
|
|
2964
3763
|
* any base dependency. Inverse of `GlazeColorToken.export()`.
|
|
2965
3764
|
*
|
|
2966
|
-
* The stored `config` field
|
|
2967
|
-
*
|
|
2968
|
-
*
|
|
3765
|
+
* The stored `config` field is the freeze from export time — passed as
|
|
3766
|
+
* the instance local override so the rehydrated token stays pinned
|
|
3767
|
+
* against later `glaze.configure()` calls.
|
|
2969
3768
|
*/
|
|
2970
3769
|
function colorFromExport(data) {
|
|
2971
3770
|
if (data === null || typeof data !== "object") throw new Error(`glaze.colorFrom: expected an object from token.export(), got ${data === null ? "null" : typeof data}.`);
|
|
3771
|
+
assertExportKind(data, "color", "glaze.colorFrom");
|
|
3772
|
+
assertExportVersion(data, "glaze.colorFrom");
|
|
2972
3773
|
if (data.form !== "value" && data.form !== "structured") throw new Error(`glaze.colorFrom: invalid "form" field — expected "value" or "structured" (got ${JSON.stringify(data.form)}).`);
|
|
2973
3774
|
if (data.input === void 0) throw new Error(`glaze.colorFrom: missing "input" field — expected the original ${data.form === "value" ? "GlazeColorValue" : "GlazeColorInput"}.`);
|
|
2974
3775
|
if (data.form === "value") {
|
|
@@ -2978,18 +3779,173 @@ function colorFromExport(data) {
|
|
|
2978
3779
|
return createColorToken(rehydrateStructuredInput(data.input), data.config);
|
|
2979
3780
|
}
|
|
2980
3781
|
|
|
3782
|
+
//#endregion
|
|
3783
|
+
//#region src/theme.ts
|
|
3784
|
+
function createTheme(seed, initialColors, configOverride) {
|
|
3785
|
+
const { hue, saturation, darkHue, darkSaturation } = seed;
|
|
3786
|
+
let colorDefs = initialColors ? { ...initialColors } : {};
|
|
3787
|
+
let cache = null;
|
|
3788
|
+
function getEffectiveConfig() {
|
|
3789
|
+
const version = getConfigVersion();
|
|
3790
|
+
if (cache && cache.version === version) return cache.effectiveConfig;
|
|
3791
|
+
const effectiveConfig = mergeConfig(getConfig(), configOverride);
|
|
3792
|
+
cache = {
|
|
3793
|
+
map: null,
|
|
3794
|
+
version,
|
|
3795
|
+
effectiveConfig
|
|
3796
|
+
};
|
|
3797
|
+
return effectiveConfig;
|
|
3798
|
+
}
|
|
3799
|
+
function resolveCached() {
|
|
3800
|
+
const version = getConfigVersion();
|
|
3801
|
+
if (cache && cache.version === version && cache.map) return cache.map;
|
|
3802
|
+
const effectiveConfig = getEffectiveConfig();
|
|
3803
|
+
const map = resolveAllColors(seed, colorDefs, effectiveConfig);
|
|
3804
|
+
cache = {
|
|
3805
|
+
map,
|
|
3806
|
+
version,
|
|
3807
|
+
effectiveConfig
|
|
3808
|
+
};
|
|
3809
|
+
return map;
|
|
3810
|
+
}
|
|
3811
|
+
function invalidate() {
|
|
3812
|
+
cache = null;
|
|
3813
|
+
}
|
|
3814
|
+
function channelCtxFor(options, formatDefault, prefix) {
|
|
3815
|
+
const format = options?.format ?? formatDefault;
|
|
3816
|
+
if (!options?.splitHue || format !== "oklch") return void 0;
|
|
3817
|
+
assertAllPastel(resolveCached(), resolveModes(options?.modes));
|
|
3818
|
+
return {
|
|
3819
|
+
seedHue: hue,
|
|
3820
|
+
darkSeedHue: darkHue,
|
|
3821
|
+
baseName: options.name ?? "theme",
|
|
3822
|
+
prefix,
|
|
3823
|
+
defs: colorDefs,
|
|
3824
|
+
mode: "theme"
|
|
3825
|
+
};
|
|
3826
|
+
}
|
|
3827
|
+
return {
|
|
3828
|
+
get hue() {
|
|
3829
|
+
return hue;
|
|
3830
|
+
},
|
|
3831
|
+
get saturation() {
|
|
3832
|
+
return saturation;
|
|
3833
|
+
},
|
|
3834
|
+
get darkHue() {
|
|
3835
|
+
return darkHue;
|
|
3836
|
+
},
|
|
3837
|
+
get darkSaturation() {
|
|
3838
|
+
return darkSaturation;
|
|
3839
|
+
},
|
|
3840
|
+
getConfig() {
|
|
3841
|
+
return getEffectiveConfig();
|
|
3842
|
+
},
|
|
3843
|
+
colors(defs) {
|
|
3844
|
+
colorDefs = {
|
|
3845
|
+
...colorDefs,
|
|
3846
|
+
...defs
|
|
3847
|
+
};
|
|
3848
|
+
invalidate();
|
|
3849
|
+
},
|
|
3850
|
+
color(name, def) {
|
|
3851
|
+
if (def === void 0) return colorDefs[name];
|
|
3852
|
+
colorDefs[name] = def;
|
|
3853
|
+
invalidate();
|
|
3854
|
+
},
|
|
3855
|
+
remove(names) {
|
|
3856
|
+
const list = Array.isArray(names) ? names : [names];
|
|
3857
|
+
for (const name of list) delete colorDefs[name];
|
|
3858
|
+
invalidate();
|
|
3859
|
+
},
|
|
3860
|
+
has(name) {
|
|
3861
|
+
return name in colorDefs;
|
|
3862
|
+
},
|
|
3863
|
+
list() {
|
|
3864
|
+
return Object.keys(colorDefs);
|
|
3865
|
+
},
|
|
3866
|
+
reset() {
|
|
3867
|
+
colorDefs = {};
|
|
3868
|
+
invalidate();
|
|
3869
|
+
},
|
|
3870
|
+
export(override) {
|
|
3871
|
+
return {
|
|
3872
|
+
kind: "theme",
|
|
3873
|
+
version: GLAZE_EXPORT_VERSION,
|
|
3874
|
+
hue,
|
|
3875
|
+
saturation,
|
|
3876
|
+
...darkHue !== void 0 ? { darkHue } : {},
|
|
3877
|
+
...darkSaturation !== void 0 ? { darkSaturation } : {},
|
|
3878
|
+
colors: structuredClone(colorDefs),
|
|
3879
|
+
config: freezeConfigForExport(configOverride, override)
|
|
3880
|
+
};
|
|
3881
|
+
},
|
|
3882
|
+
extend(options) {
|
|
3883
|
+
const childSeed = {
|
|
3884
|
+
hue: options.hue ?? hue,
|
|
3885
|
+
saturation: options.saturation ?? saturation,
|
|
3886
|
+
darkHue: options.darkHue ?? darkHue,
|
|
3887
|
+
darkSaturation: options.darkSaturation ?? darkSaturation
|
|
3888
|
+
};
|
|
3889
|
+
const inheritedColors = {};
|
|
3890
|
+
for (const [name, def] of Object.entries(colorDefs)) if (def.inherit !== false) inheritedColors[name] = def;
|
|
3891
|
+
return createTheme(childSeed, options.colors ? {
|
|
3892
|
+
...inheritedColors,
|
|
3893
|
+
...options.colors
|
|
3894
|
+
} : { ...inheritedColors }, configOverride || options.config ? {
|
|
3895
|
+
...configOverride ?? {},
|
|
3896
|
+
...options.config ?? {}
|
|
3897
|
+
} : void 0);
|
|
3898
|
+
},
|
|
3899
|
+
resolve() {
|
|
3900
|
+
return new Map(resolveCached());
|
|
3901
|
+
},
|
|
3902
|
+
tokens(options) {
|
|
3903
|
+
const format = options?.format ?? "oklch";
|
|
3904
|
+
assertNativeFormat(format, "tokens");
|
|
3905
|
+
const modes = resolveModes(options?.modes);
|
|
3906
|
+
return buildFlatTokenMap(resolveCached(), "", modes, format, getEffectiveConfig().pastel);
|
|
3907
|
+
},
|
|
3908
|
+
tasty(options) {
|
|
3909
|
+
const cfg = getEffectiveConfig();
|
|
3910
|
+
const states = {
|
|
3911
|
+
dark: options?.states?.dark ?? cfg.states.dark,
|
|
3912
|
+
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
3913
|
+
};
|
|
3914
|
+
const modes = resolveModes(options?.modes);
|
|
3915
|
+
const format = options?.format ?? "oklch";
|
|
3916
|
+
const channelCtx = channelCtxFor(options, "oklch", "");
|
|
3917
|
+
return buildTokenMap(resolveCached(), "", states, modes, format, cfg.pastel, channelCtx);
|
|
3918
|
+
},
|
|
3919
|
+
json(options) {
|
|
3920
|
+
const format = options?.format ?? "oklch";
|
|
3921
|
+
assertNativeFormat(format, "json");
|
|
3922
|
+
const modes = resolveModes(options?.modes);
|
|
3923
|
+
return buildJsonMap(resolveCached(), modes, format, getEffectiveConfig().pastel);
|
|
3924
|
+
},
|
|
3925
|
+
css(options) {
|
|
3926
|
+
const format = options?.format ?? "oklch";
|
|
3927
|
+
assertNativeFormat(format, "css");
|
|
3928
|
+
const channelCtx = channelCtxFor(options, "oklch", "");
|
|
3929
|
+
return buildCssMap(resolveCached(), "", options?.suffix ?? "-color", format, getEffectiveConfig().pastel, channelCtx);
|
|
3930
|
+
},
|
|
3931
|
+
dtcg(options) {
|
|
3932
|
+
const modes = resolveModes(options?.modes);
|
|
3933
|
+
return buildDtcgMap(resolveCached(), "", modes, options?.colorSpace ?? "srgb", getEffectiveConfig().pastel);
|
|
3934
|
+
},
|
|
3935
|
+
dtcgResolver(options) {
|
|
3936
|
+
return buildDtcgResolver(buildDtcgMap(resolveCached(), "", resolveModes(options?.modes), options?.colorSpace ?? "srgb", getEffectiveConfig().pastel), options);
|
|
3937
|
+
},
|
|
3938
|
+
tailwind(options) {
|
|
3939
|
+
const format = options?.format ?? "oklch";
|
|
3940
|
+
assertNativeFormat(format, "tailwind");
|
|
3941
|
+
const modes = resolveModes(options?.modes);
|
|
3942
|
+
return buildTailwindMap(resolveCached(), "", options?.namespace ?? "color-", modes, format, options?.darkSelector ?? ".dark", options?.highContrastSelector ?? ".high-contrast", getEffectiveConfig().pastel);
|
|
3943
|
+
}
|
|
3944
|
+
};
|
|
3945
|
+
}
|
|
3946
|
+
|
|
2981
3947
|
//#endregion
|
|
2982
3948
|
//#region src/palette.ts
|
|
2983
|
-
/**
|
|
2984
|
-
* Palette factory.
|
|
2985
|
-
*
|
|
2986
|
-
* Composes multiple themes into a single token namespace with optional
|
|
2987
|
-
* theme-name prefixes and a "primary theme" that also surfaces an
|
|
2988
|
-
* unprefixed copy of its tokens. All seven export methods (`tokens` /
|
|
2989
|
-
* `tasty` / `json` / `css` / `dtcg` / `dtcgResolver` / `tailwind`) share a
|
|
2990
|
-
* `buildPaletteOutput` driver that handles validation, per-theme iteration,
|
|
2991
|
-
* prefix resolution, collision filtering, and primary duplication.
|
|
2992
|
-
*/
|
|
2993
3949
|
function resolvePrefix(options, themeName, defaultPrefix = false) {
|
|
2994
3950
|
const prefix = options?.prefix ?? defaultPrefix;
|
|
2995
3951
|
if (prefix === true) return `${themeName}-`;
|
|
@@ -3029,6 +3985,27 @@ function filterCollisions(resolved, prefix, seen, themeName, isPrimary) {
|
|
|
3029
3985
|
}
|
|
3030
3986
|
return filtered;
|
|
3031
3987
|
}
|
|
3988
|
+
function colorMapFromTheme(theme) {
|
|
3989
|
+
const defs = {};
|
|
3990
|
+
for (const name of theme.list()) {
|
|
3991
|
+
const def = theme.color(name);
|
|
3992
|
+
if (def !== void 0) defs[name] = def;
|
|
3993
|
+
}
|
|
3994
|
+
return defs;
|
|
3995
|
+
}
|
|
3996
|
+
function channelCtxForTheme(theme, themeName, passPrefix, themedPrefix, splitHue, format, modes, filtered) {
|
|
3997
|
+
if (!splitHue || format !== "oklch") return void 0;
|
|
3998
|
+
assertAllPastel(filtered, modes);
|
|
3999
|
+
return {
|
|
4000
|
+
seedHue: theme.hue,
|
|
4001
|
+
darkSeedHue: theme.darkHue,
|
|
4002
|
+
baseName: themeName,
|
|
4003
|
+
prefix: themedPrefix,
|
|
4004
|
+
defs: colorMapFromTheme(theme),
|
|
4005
|
+
mode: "theme",
|
|
4006
|
+
emitDeclarations: passPrefix === themedPrefix
|
|
4007
|
+
};
|
|
4008
|
+
}
|
|
3032
4009
|
/**
|
|
3033
4010
|
* Shared per-theme driver for `tokens` / `tasty` / `css`. `json` skips
|
|
3034
4011
|
* this because it doesn't do collision filtering or primary duplication.
|
|
@@ -3042,17 +4019,43 @@ function buildPaletteOutput(themes, paletteOptions, options, buildOne, merge, em
|
|
|
3042
4019
|
const resolved = theme.resolve();
|
|
3043
4020
|
const pastel = theme.getConfig().pastel;
|
|
3044
4021
|
const prefix = resolvePrefix(options, themeName, true);
|
|
3045
|
-
merge(acc, buildOne(filterCollisions(resolved, prefix, seen, themeName), prefix, pastel));
|
|
3046
|
-
if (themeName === effectivePrimary) merge(acc, buildOne(filterCollisions(resolved, "", seen, themeName, true), "", pastel));
|
|
4022
|
+
merge(acc, buildOne(filterCollisions(resolved, prefix, seen, themeName), prefix, pastel, themeName, theme));
|
|
4023
|
+
if (themeName === effectivePrimary) merge(acc, buildOne(filterCollisions(resolved, "", seen, themeName, true), "", pastel, themeName, theme));
|
|
3047
4024
|
}
|
|
3048
4025
|
return acc;
|
|
3049
4026
|
}
|
|
4027
|
+
/** Rebuild a theme from a `theme.export()` snapshot. */
|
|
4028
|
+
function createThemeFromExport(data, factory = "glaze.themeFrom") {
|
|
4029
|
+
if (data === null || typeof data !== "object") throw new Error(`${factory}: expected an object from theme.export(), got ${data === null ? "null" : typeof data}.`);
|
|
4030
|
+
assertExportKind(data, "theme", factory);
|
|
4031
|
+
assertExportVersion(data, factory);
|
|
4032
|
+
if (typeof data.hue !== "number" || typeof data.saturation !== "number") throw new Error(`${factory}: expected numeric "hue" and "saturation" fields.`);
|
|
4033
|
+
for (const field of ["darkHue", "darkSaturation"]) if (data[field] !== void 0 && typeof data[field] !== "number") throw new Error(`${factory}: expected "${field}" to be a number when present (got ${JSON.stringify(data[field])}).`);
|
|
4034
|
+
return createTheme({
|
|
4035
|
+
hue: data.hue,
|
|
4036
|
+
saturation: data.saturation,
|
|
4037
|
+
darkHue: data.darkHue,
|
|
4038
|
+
darkSaturation: data.darkSaturation
|
|
4039
|
+
}, data.colors, data.config);
|
|
4040
|
+
}
|
|
4041
|
+
/**
|
|
4042
|
+
* Rebuild a palette from a `palette.export()` snapshot.
|
|
4043
|
+
*/
|
|
4044
|
+
function createPaletteFromExport(data) {
|
|
4045
|
+
if (data === null || typeof data !== "object") throw new Error(`glaze.paletteFrom: expected an object from palette.export(), got ${data === null ? "null" : typeof data}.`);
|
|
4046
|
+
assertExportKind(data, "palette", "glaze.paletteFrom");
|
|
4047
|
+
assertExportVersion(data, "glaze.paletteFrom");
|
|
4048
|
+
if (data.themes === null || typeof data.themes !== "object") throw new Error(`glaze.paletteFrom: expected a "themes" object map of theme exports.`);
|
|
4049
|
+
const rebuilt = {};
|
|
4050
|
+
for (const [name, themeExport] of Object.entries(data.themes)) rebuilt[name] = createThemeFromExport(themeExport, `glaze.paletteFrom (theme "${name}")`);
|
|
4051
|
+
return createPalette(rebuilt, { primary: data.primary });
|
|
4052
|
+
}
|
|
3050
4053
|
function createPalette(themes, paletteOptions) {
|
|
3051
4054
|
validatePrimaryTheme(paletteOptions?.primary, themes);
|
|
3052
4055
|
const buildDtcgResult = (options) => {
|
|
3053
4056
|
const modes = resolveModes(options?.modes);
|
|
3054
4057
|
const colorSpace = options?.colorSpace ?? "srgb";
|
|
3055
|
-
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel) => buildDtcgMap(filtered, prefix, modes, colorSpace, pastel), (acc, part) => {
|
|
4058
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, _themeName, _theme) => buildDtcgMap(filtered, prefix, modes, colorSpace, pastel), (acc, part) => {
|
|
3056
4059
|
Object.assign(acc.light, part.light);
|
|
3057
4060
|
if (part.dark) acc.dark = Object.assign(acc.dark ?? {}, part.dark);
|
|
3058
4061
|
if (part.lightContrast) acc.lightContrast = Object.assign(acc.lightContrast ?? {}, part.lightContrast);
|
|
@@ -3060,9 +4063,34 @@ function createPalette(themes, paletteOptions) {
|
|
|
3060
4063
|
}, () => ({ light: {} }));
|
|
3061
4064
|
};
|
|
3062
4065
|
return {
|
|
4066
|
+
list() {
|
|
4067
|
+
return Object.keys(themes);
|
|
4068
|
+
},
|
|
4069
|
+
get primary() {
|
|
4070
|
+
return paletteOptions?.primary;
|
|
4071
|
+
},
|
|
4072
|
+
theme(name) {
|
|
4073
|
+
return themes[name];
|
|
4074
|
+
},
|
|
4075
|
+
themes() {
|
|
4076
|
+
return { ...themes };
|
|
4077
|
+
},
|
|
4078
|
+
export(override) {
|
|
4079
|
+
const themesExport = {};
|
|
4080
|
+
for (const [name, theme] of Object.entries(themes)) themesExport[name] = theme.export(override);
|
|
4081
|
+
const out = {
|
|
4082
|
+
kind: "palette",
|
|
4083
|
+
version: GLAZE_EXPORT_VERSION,
|
|
4084
|
+
themes: themesExport
|
|
4085
|
+
};
|
|
4086
|
+
if (paletteOptions?.primary !== void 0) out.primary = paletteOptions.primary;
|
|
4087
|
+
return out;
|
|
4088
|
+
},
|
|
3063
4089
|
tokens(options) {
|
|
4090
|
+
const format = options?.format ?? "oklch";
|
|
4091
|
+
assertNativeFormat(format, "tokens");
|
|
3064
4092
|
const modes = resolveModes(options?.modes);
|
|
3065
|
-
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel) => buildFlatTokenMap(filtered, prefix, modes,
|
|
4093
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel) => buildFlatTokenMap(filtered, prefix, modes, format, pastel), (acc, part) => {
|
|
3066
4094
|
for (const variant of Object.keys(part)) {
|
|
3067
4095
|
if (!acc[variant]) acc[variant] = {};
|
|
3068
4096
|
Object.assign(acc[variant], part[variant]);
|
|
@@ -3076,18 +4104,27 @@ function createPalette(themes, paletteOptions) {
|
|
|
3076
4104
|
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
3077
4105
|
};
|
|
3078
4106
|
const modes = resolveModes(options?.modes);
|
|
3079
|
-
|
|
4107
|
+
const format = options?.format ?? "oklch";
|
|
4108
|
+
return buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, themeName, theme) => {
|
|
4109
|
+
return buildTokenMap(filtered, prefix, states, modes, format, pastel, channelCtxForTheme(theme, themeName, prefix, resolvePrefix(options, themeName, true), options?.splitHue, format, modes, filtered));
|
|
4110
|
+
}, (acc, part) => Object.assign(acc, part), () => ({}));
|
|
3080
4111
|
},
|
|
3081
4112
|
json(options) {
|
|
4113
|
+
const format = options?.format ?? "oklch";
|
|
4114
|
+
assertNativeFormat(format, "json");
|
|
3082
4115
|
const modes = resolveModes(options?.modes);
|
|
3083
4116
|
const result = {};
|
|
3084
|
-
for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes,
|
|
4117
|
+
for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, format, theme.getConfig().pastel);
|
|
3085
4118
|
return result;
|
|
3086
4119
|
},
|
|
3087
4120
|
css(options) {
|
|
3088
4121
|
const suffix = options?.suffix ?? "-color";
|
|
3089
|
-
const format = options?.format ?? "
|
|
3090
|
-
|
|
4122
|
+
const format = options?.format ?? "oklch";
|
|
4123
|
+
assertNativeFormat(format, "css");
|
|
4124
|
+
const modes = resolveModes();
|
|
4125
|
+
const lines = buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, themeName, theme) => {
|
|
4126
|
+
return buildCssMap(filtered, prefix, suffix, format, pastel, channelCtxForTheme(theme, themeName, prefix, resolvePrefix(options, themeName, true), options?.splitHue, format, modes, filtered));
|
|
4127
|
+
}, (acc, part) => {
|
|
3091
4128
|
for (const key of [
|
|
3092
4129
|
"light",
|
|
3093
4130
|
"dark",
|
|
@@ -3117,9 +4154,10 @@ function createPalette(themes, paletteOptions) {
|
|
|
3117
4154
|
const modes = resolveModes(options?.modes);
|
|
3118
4155
|
const cssPrefix = options?.namespace ?? "color-";
|
|
3119
4156
|
const format = options?.format ?? "oklch";
|
|
4157
|
+
assertNativeFormat(format, "tailwind");
|
|
3120
4158
|
const darkSelector = options?.darkSelector ?? ".dark";
|
|
3121
4159
|
const highContrastSelector = options?.highContrastSelector ?? ".high-contrast";
|
|
3122
|
-
return emitTailwindCss(buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel) => buildTailwindLines(filtered, prefix, cssPrefix, format, pastel), (acc, part) => {
|
|
4160
|
+
return emitTailwindCss(buildPaletteOutput(themes, paletteOptions, options, (filtered, prefix, pastel, _themeName, _theme) => buildTailwindLines(filtered, prefix, cssPrefix, format, pastel), (acc, part) => {
|
|
3123
4161
|
for (const variant of [
|
|
3124
4162
|
"light",
|
|
3125
4163
|
"dark",
|
|
@@ -3136,140 +4174,6 @@ function createPalette(themes, paletteOptions) {
|
|
|
3136
4174
|
};
|
|
3137
4175
|
}
|
|
3138
4176
|
|
|
3139
|
-
//#endregion
|
|
3140
|
-
//#region src/theme.ts
|
|
3141
|
-
/**
|
|
3142
|
-
* Theme factory.
|
|
3143
|
-
*
|
|
3144
|
-
* Wraps a hue/saturation seed, a mutable `ColorMap`, and an optional
|
|
3145
|
-
* per-theme `GlazeConfigOverride`. Exposes `tokens()` / `tasty()` /
|
|
3146
|
-
* `json()` / `css()` / `dtcg()` / `dtcgResolver()` / `tailwind()` / `resolve()` /
|
|
3147
|
-
* `export()` / `extend()`.
|
|
3148
|
-
*
|
|
3149
|
-
* The per-theme config override is **merged over the live global config at
|
|
3150
|
-
* resolve time** so the theme still reacts to later `configure()` calls
|
|
3151
|
-
* for fields it didn't override. The merged config is memoized by
|
|
3152
|
-
* `configVersion` to avoid rebuilding it on every export call.
|
|
3153
|
-
*/
|
|
3154
|
-
function createTheme(hue, saturation, initialColors, configOverride) {
|
|
3155
|
-
let colorDefs = initialColors ? { ...initialColors } : {};
|
|
3156
|
-
let cache = null;
|
|
3157
|
-
function getEffectiveConfig() {
|
|
3158
|
-
const version = getConfigVersion();
|
|
3159
|
-
if (cache && cache.version === version) return cache.effectiveConfig;
|
|
3160
|
-
return mergeConfig(getConfig(), configOverride);
|
|
3161
|
-
}
|
|
3162
|
-
function resolveCached() {
|
|
3163
|
-
const version = getConfigVersion();
|
|
3164
|
-
if (cache && cache.version === version) return cache.map;
|
|
3165
|
-
const effectiveConfig = mergeConfig(getConfig(), configOverride);
|
|
3166
|
-
const map = resolveAllColors(hue, saturation, colorDefs, effectiveConfig);
|
|
3167
|
-
cache = {
|
|
3168
|
-
map,
|
|
3169
|
-
version,
|
|
3170
|
-
effectiveConfig
|
|
3171
|
-
};
|
|
3172
|
-
return map;
|
|
3173
|
-
}
|
|
3174
|
-
function invalidate() {
|
|
3175
|
-
cache = null;
|
|
3176
|
-
}
|
|
3177
|
-
return {
|
|
3178
|
-
get hue() {
|
|
3179
|
-
return hue;
|
|
3180
|
-
},
|
|
3181
|
-
get saturation() {
|
|
3182
|
-
return saturation;
|
|
3183
|
-
},
|
|
3184
|
-
getConfig() {
|
|
3185
|
-
return getEffectiveConfig();
|
|
3186
|
-
},
|
|
3187
|
-
colors(defs) {
|
|
3188
|
-
colorDefs = {
|
|
3189
|
-
...colorDefs,
|
|
3190
|
-
...defs
|
|
3191
|
-
};
|
|
3192
|
-
invalidate();
|
|
3193
|
-
},
|
|
3194
|
-
color(name, def) {
|
|
3195
|
-
if (def === void 0) return colorDefs[name];
|
|
3196
|
-
colorDefs[name] = def;
|
|
3197
|
-
invalidate();
|
|
3198
|
-
},
|
|
3199
|
-
remove(names) {
|
|
3200
|
-
const list = Array.isArray(names) ? names : [names];
|
|
3201
|
-
for (const name of list) delete colorDefs[name];
|
|
3202
|
-
invalidate();
|
|
3203
|
-
},
|
|
3204
|
-
has(name) {
|
|
3205
|
-
return name in colorDefs;
|
|
3206
|
-
},
|
|
3207
|
-
list() {
|
|
3208
|
-
return Object.keys(colorDefs);
|
|
3209
|
-
},
|
|
3210
|
-
reset() {
|
|
3211
|
-
colorDefs = {};
|
|
3212
|
-
invalidate();
|
|
3213
|
-
},
|
|
3214
|
-
export() {
|
|
3215
|
-
const out = {
|
|
3216
|
-
hue,
|
|
3217
|
-
saturation,
|
|
3218
|
-
colors: { ...colorDefs }
|
|
3219
|
-
};
|
|
3220
|
-
if (configOverride !== void 0) out.config = configOverride;
|
|
3221
|
-
return out;
|
|
3222
|
-
},
|
|
3223
|
-
extend(options) {
|
|
3224
|
-
const newHue = options.hue ?? hue;
|
|
3225
|
-
const newSat = options.saturation ?? saturation;
|
|
3226
|
-
const inheritedColors = {};
|
|
3227
|
-
for (const [name, def] of Object.entries(colorDefs)) if (def.inherit !== false) inheritedColors[name] = def;
|
|
3228
|
-
return createTheme(newHue, newSat, options.colors ? {
|
|
3229
|
-
...inheritedColors,
|
|
3230
|
-
...options.colors
|
|
3231
|
-
} : { ...inheritedColors }, configOverride || options.config ? {
|
|
3232
|
-
...configOverride ?? {},
|
|
3233
|
-
...options.config ?? {}
|
|
3234
|
-
} : void 0);
|
|
3235
|
-
},
|
|
3236
|
-
resolve() {
|
|
3237
|
-
return new Map(resolveCached());
|
|
3238
|
-
},
|
|
3239
|
-
tokens(options) {
|
|
3240
|
-
const modes = resolveModes(options?.modes);
|
|
3241
|
-
return buildFlatTokenMap(resolveCached(), "", modes, options?.format, getEffectiveConfig().pastel);
|
|
3242
|
-
},
|
|
3243
|
-
tasty(options) {
|
|
3244
|
-
const cfg = getEffectiveConfig();
|
|
3245
|
-
const states = {
|
|
3246
|
-
dark: options?.states?.dark ?? cfg.states.dark,
|
|
3247
|
-
highContrast: options?.states?.highContrast ?? cfg.states.highContrast
|
|
3248
|
-
};
|
|
3249
|
-
const modes = resolveModes(options?.modes);
|
|
3250
|
-
return buildTokenMap(resolveCached(), "", states, modes, options?.format, cfg.pastel);
|
|
3251
|
-
},
|
|
3252
|
-
json(options) {
|
|
3253
|
-
const modes = resolveModes(options?.modes);
|
|
3254
|
-
return buildJsonMap(resolveCached(), modes, options?.format, getEffectiveConfig().pastel);
|
|
3255
|
-
},
|
|
3256
|
-
css(options) {
|
|
3257
|
-
return buildCssMap(resolveCached(), "", options?.suffix ?? "-color", options?.format ?? "rgb", getEffectiveConfig().pastel);
|
|
3258
|
-
},
|
|
3259
|
-
dtcg(options) {
|
|
3260
|
-
const modes = resolveModes(options?.modes);
|
|
3261
|
-
return buildDtcgMap(resolveCached(), "", modes, options?.colorSpace ?? "srgb", getEffectiveConfig().pastel);
|
|
3262
|
-
},
|
|
3263
|
-
dtcgResolver(options) {
|
|
3264
|
-
return buildDtcgResolver(buildDtcgMap(resolveCached(), "", resolveModes(options?.modes), options?.colorSpace ?? "srgb", getEffectiveConfig().pastel), options);
|
|
3265
|
-
},
|
|
3266
|
-
tailwind(options) {
|
|
3267
|
-
const modes = resolveModes(options?.modes);
|
|
3268
|
-
return buildTailwindMap(resolveCached(), "", options?.namespace ?? "color-", modes, options?.format ?? "oklch", options?.darkSelector ?? ".dark", options?.highContrastSelector ?? ".high-contrast", getEffectiveConfig().pastel);
|
|
3269
|
-
}
|
|
3270
|
-
};
|
|
3271
|
-
}
|
|
3272
|
-
|
|
3273
4177
|
//#endregion
|
|
3274
4178
|
//#region src/glaze.ts
|
|
3275
4179
|
/**
|
|
@@ -3283,6 +4187,7 @@ function createTheme(hue, saturation, initialColors, configOverride) {
|
|
|
3283
4187
|
* - `shadow.ts` — standalone shadow factory (`glaze.shadow`)
|
|
3284
4188
|
* - `formatters.ts` — variant → string (`glaze.format`)
|
|
3285
4189
|
* - `config.ts` — global config singleton
|
|
4190
|
+
* - `serialize.ts` — authoring export type guards / version checks
|
|
3286
4191
|
*/
|
|
3287
4192
|
/**
|
|
3288
4193
|
* Create a single-hue glaze theme.
|
|
@@ -3293,18 +4198,28 @@ function createTheme(hue, saturation, initialColors, configOverride) {
|
|
|
3293
4198
|
* the theme still reacts to later `configure()` calls for fields it
|
|
3294
4199
|
* didn't override.
|
|
3295
4200
|
*
|
|
4201
|
+
* The object form also accepts `darkHue` / `darkSaturation` — a separate
|
|
4202
|
+
* seed pair for the dark and dark-high-contrast schemes. Both default to
|
|
4203
|
+
* their light counterparts, and an explicit `darkSaturation` opts the theme
|
|
4204
|
+
* out of the global `darkDesaturation` reduction.
|
|
4205
|
+
*
|
|
3296
4206
|
* @example
|
|
3297
4207
|
* ```ts
|
|
3298
4208
|
* const primary = glaze(280, 80);
|
|
3299
4209
|
* // or shorthand:
|
|
3300
4210
|
* const primary = glaze({ hue: 280, saturation: 80 });
|
|
4211
|
+
* // with a retuned dark seed:
|
|
4212
|
+
* const primary = glaze({ hue: 280, saturation: 80, darkHue: 268, darkSaturation: 65 });
|
|
3301
4213
|
* // with config override:
|
|
3302
4214
|
* const raw = glaze(280, 80, { lightTone: false });
|
|
3303
4215
|
* ```
|
|
3304
4216
|
*/
|
|
3305
4217
|
function glaze(hueOrOptions, saturation, config) {
|
|
3306
|
-
if (typeof hueOrOptions === "number") return createTheme(
|
|
3307
|
-
|
|
4218
|
+
if (typeof hueOrOptions === "number") return createTheme({
|
|
4219
|
+
hue: hueOrOptions,
|
|
4220
|
+
saturation: saturation ?? 100
|
|
4221
|
+
}, void 0, config);
|
|
4222
|
+
return createTheme(hueOrOptions, void 0, config);
|
|
3308
4223
|
}
|
|
3309
4224
|
/** Configure global glaze settings. */
|
|
3310
4225
|
glaze.configure = function configure$1(config) {
|
|
@@ -3314,10 +4229,15 @@ glaze.configure = function configure$1(config) {
|
|
|
3314
4229
|
glaze.palette = function palette(themes, options) {
|
|
3315
4230
|
return createPalette(themes, options);
|
|
3316
4231
|
};
|
|
3317
|
-
/**
|
|
3318
|
-
|
|
3319
|
-
|
|
4232
|
+
/**
|
|
4233
|
+
* Create a theme from a serialized `theme.export()` snapshot.
|
|
4234
|
+
* Prefer this over the legacy `glaze.from` alias.
|
|
4235
|
+
*/
|
|
4236
|
+
glaze.themeFrom = function themeFrom(data) {
|
|
4237
|
+
return createThemeFromExport(data);
|
|
3320
4238
|
};
|
|
4239
|
+
/** Compat alias for `glaze.themeFrom`. */
|
|
4240
|
+
glaze.from = glaze.themeFrom;
|
|
3321
4241
|
/**
|
|
3322
4242
|
* Create a standalone single-color token.
|
|
3323
4243
|
*
|
|
@@ -3333,8 +4253,8 @@ glaze.from = function from(data) {
|
|
|
3333
4253
|
* **arg2 — config override** (optional, all shapes):
|
|
3334
4254
|
* Overrides the resolve-relevant global config fields for this token.
|
|
3335
4255
|
* Fields that are omitted fall through to the live global config at
|
|
3336
|
-
*
|
|
3337
|
-
*
|
|
4256
|
+
* resolve time. Pass `false` for a tone window to disable clamping
|
|
4257
|
+
* entirely. `pastel` is instance-only (not set via `configure()`).
|
|
3338
4258
|
*
|
|
3339
4259
|
* ```ts
|
|
3340
4260
|
* // Bare string — no overrides
|
|
@@ -3353,8 +4273,8 @@ glaze.from = function from(data) {
|
|
|
3353
4273
|
*
|
|
3354
4274
|
* Defaults: every form defaults to `mode: 'auto'`. Value-shorthand forms
|
|
3355
4275
|
* (bare strings and value objects) preserve light tone exactly
|
|
3356
|
-
* (`lightTone: false`
|
|
3357
|
-
* tone windows
|
|
4276
|
+
* (`lightTone: false` locally). Structured form falls through to the live
|
|
4277
|
+
* global tone windows for omitted fields.
|
|
3358
4278
|
*
|
|
3359
4279
|
* Relative `tone: '+N'` and `contrast` anchor to the literal seed by
|
|
3360
4280
|
* default; when `base` is set they anchor to the base's resolved variant
|
|
@@ -3413,7 +4333,10 @@ glaze.fromHex = function fromHex(hex) {
|
|
|
3413
4333
|
const rgb = parseHex(hex);
|
|
3414
4334
|
if (!rgb) throw new Error(`glaze: invalid hex color "${hex}".`);
|
|
3415
4335
|
const [h, s] = srgbToOkhsl(rgb);
|
|
3416
|
-
return createTheme(
|
|
4336
|
+
return createTheme({
|
|
4337
|
+
hue: h,
|
|
4338
|
+
saturation: s * 100
|
|
4339
|
+
});
|
|
3417
4340
|
};
|
|
3418
4341
|
/**
|
|
3419
4342
|
* Create a theme from RGB values (0–255).
|
|
@@ -3425,15 +4348,18 @@ glaze.fromRgb = function fromRgb(r, g, b) {
|
|
|
3425
4348
|
g / 255,
|
|
3426
4349
|
b / 255
|
|
3427
4350
|
]);
|
|
3428
|
-
return createTheme(
|
|
4351
|
+
return createTheme({
|
|
4352
|
+
hue: h,
|
|
4353
|
+
saturation: s * 100
|
|
4354
|
+
});
|
|
3429
4355
|
};
|
|
3430
4356
|
/**
|
|
3431
4357
|
* Rehydrate a `glaze.color()` token from a `.export()` snapshot.
|
|
3432
4358
|
*
|
|
3433
4359
|
* The snapshot is a plain JSON-safe object containing the original
|
|
3434
4360
|
* input value, overrides (with any `base` token recursively serialized),
|
|
3435
|
-
* and the effective config
|
|
3436
|
-
*
|
|
4361
|
+
* and the effective config freeze from export time. The reconstructed
|
|
4362
|
+
* token pins that freeze as its local override.
|
|
3437
4363
|
*
|
|
3438
4364
|
* @example
|
|
3439
4365
|
* ```ts
|
|
@@ -3447,6 +4373,18 @@ glaze.fromRgb = function fromRgb(r, g, b) {
|
|
|
3447
4373
|
glaze.colorFrom = function colorFrom(data) {
|
|
3448
4374
|
return colorFromExport(data);
|
|
3449
4375
|
};
|
|
4376
|
+
/**
|
|
4377
|
+
* Rehydrate a palette from a `palette.export()` snapshot.
|
|
4378
|
+
*/
|
|
4379
|
+
glaze.paletteFrom = function paletteFrom(data) {
|
|
4380
|
+
return createPaletteFromExport(data);
|
|
4381
|
+
};
|
|
4382
|
+
/** Type guard for theme authoring snapshots. */
|
|
4383
|
+
glaze.isThemeExport = isThemeExport;
|
|
4384
|
+
/** Type guard for color-token authoring snapshots. */
|
|
4385
|
+
glaze.isColorTokenExport = isColorTokenExport;
|
|
4386
|
+
/** Type guard for palette authoring snapshots. */
|
|
4387
|
+
glaze.isPaletteExport = isPaletteExport;
|
|
3450
4388
|
/** Get the current global configuration (for testing/debugging). */
|
|
3451
4389
|
glaze.getConfig = function getConfig() {
|
|
3452
4390
|
return snapshotConfig();
|
|
@@ -3457,14 +4395,21 @@ glaze.resetConfig = function resetConfig$1() {
|
|
|
3457
4395
|
};
|
|
3458
4396
|
|
|
3459
4397
|
//#endregion
|
|
4398
|
+
exports.APCA_HC_ENHANCEMENT = APCA_HC_ENHANCEMENT;
|
|
4399
|
+
exports.APCA_MAX_LC = APCA_MAX_LC;
|
|
4400
|
+
exports.APCA_PRESETS = APCA_PRESETS;
|
|
4401
|
+
exports.GLAZE_EXPORT_VERSION = GLAZE_EXPORT_VERSION;
|
|
3460
4402
|
exports.REF_EPS = REF_EPS;
|
|
3461
4403
|
exports.apcaContrast = apcaContrast;
|
|
4404
|
+
exports.assertAllPastel = assertAllPastel;
|
|
4405
|
+
exports.assertNativeFormat = assertNativeFormat;
|
|
3462
4406
|
exports.contrastRatioFromLuminance = contrastRatioFromLuminance;
|
|
3463
4407
|
exports.cuspLightness = cuspLightness;
|
|
3464
4408
|
exports.findToneForContrast = findToneForContrast;
|
|
3465
4409
|
exports.findValueForMixContrast = findValueForMixContrast;
|
|
3466
4410
|
exports.formatHsl = formatHsl;
|
|
3467
4411
|
exports.formatOkhsl = formatOkhsl;
|
|
4412
|
+
exports.formatOkhst = formatOkhst;
|
|
3468
4413
|
exports.formatOklch = formatOklch;
|
|
3469
4414
|
exports.formatRgb = formatRgb;
|
|
3470
4415
|
exports.fromTone = fromTone;
|
|
@@ -3472,6 +4417,9 @@ exports.gamutClampedLuminance = gamutClampedLuminance;
|
|
|
3472
4417
|
exports.glaze = glaze;
|
|
3473
4418
|
exports.hslToSrgb = hslToSrgb;
|
|
3474
4419
|
exports.inferRoleFromName = inferRoleFromName;
|
|
4420
|
+
exports.isColorTokenExport = isColorTokenExport;
|
|
4421
|
+
exports.isPaletteExport = isPaletteExport;
|
|
4422
|
+
exports.isThemeExport = isThemeExport;
|
|
3475
4423
|
exports.normalizeRole = normalizeRole;
|
|
3476
4424
|
exports.okhslToLinearSrgb = okhslToLinearSrgb;
|
|
3477
4425
|
exports.okhslToOkhst = okhslToOkhst;
|
|
@@ -3485,6 +4433,7 @@ exports.parseHex = parseHex;
|
|
|
3485
4433
|
exports.parseHexAlpha = parseHexAlpha;
|
|
3486
4434
|
exports.relativeLuminanceFromLinearRgb = relativeLuminanceFromLinearRgb;
|
|
3487
4435
|
exports.resolveApcaTarget = resolveApcaTarget;
|
|
4436
|
+
exports.resolveContrastForLevel = resolveContrastForLevel;
|
|
3488
4437
|
exports.resolveContrastForMode = resolveContrastForMode;
|
|
3489
4438
|
exports.resolveMinContrast = resolveMinContrast;
|
|
3490
4439
|
exports.roleToPolarity = roleToPolarity;
|