@tenphi/glaze 0.0.0-snapshot.c84faa6 → 0.0.0-snapshot.e76f494

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/dist/index.d.mts CHANGED
@@ -6,6 +6,7 @@
6
6
  * against a base color. Used by glaze when resolving dependent colors
7
7
  * with `contrast`.
8
8
  */
9
+ type LinearRgb = [number, number, number];
9
10
  type ContrastPreset = 'AA' | 'AAA' | 'AA-large' | 'AAA-large';
10
11
  type MinContrast$1 = number | ContrastPreset;
11
12
  interface FindLightnessForContrastOptions {
@@ -42,6 +43,39 @@ declare function resolveMinContrast(value: MinContrast$1): number;
42
43
  * against a base color, staying as close to `preferredLightness` as possible.
43
44
  */
44
45
  declare function findLightnessForContrast(options: FindLightnessForContrastOptions): FindLightnessForContrastResult;
46
+ interface FindValueForMixContrastOptions {
47
+ /** Preferred mix parameter (0–1). */
48
+ preferredValue: number;
49
+ /** Base color as linear sRGB. */
50
+ baseLinearRgb: LinearRgb;
51
+ /** Target color as linear sRGB. */
52
+ targetLinearRgb: LinearRgb;
53
+ /** WCAG contrast target. */
54
+ contrast: MinContrast$1;
55
+ /**
56
+ * Compute the luminance of the mixed color at parameter t.
57
+ * For opaque: luminance of OKHSL-interpolated color.
58
+ * For transparent: luminance of alpha-composited color over base.
59
+ */
60
+ luminanceAtValue: (t: number) => number;
61
+ /** Convergence threshold. Default: 1e-4. */
62
+ epsilon?: number;
63
+ /** Maximum binary-search iterations per branch. Default: 20. */
64
+ maxIterations?: number;
65
+ }
66
+ interface FindValueForMixContrastResult {
67
+ /** Chosen mix parameter (0–1). */
68
+ value: number;
69
+ /** Achieved WCAG contrast ratio. */
70
+ contrast: number;
71
+ /** Whether the target was reached. */
72
+ met: boolean;
73
+ }
74
+ /**
75
+ * Find the mix parameter (ratio or opacity) that satisfies a WCAG 2 contrast
76
+ * target against a base color, staying as close to `preferredValue` as possible.
77
+ */
78
+ declare function findValueForMixContrast(options: FindValueForMixContrastOptions): FindValueForMixContrastResult;
45
79
  //#endregion
46
80
  //#region src/types.d.ts
47
81
  /** A value or [normal, high-contrast] pair. */
@@ -144,7 +178,41 @@ interface ShadowColorDef {
144
178
  /** Override default tuning. Merged field-by-field with global `shadowTuning`. */
145
179
  tuning?: ShadowTuning;
146
180
  }
147
- type ColorDef = RegularColorDef | ShadowColorDef;
181
+ interface MixColorDef {
182
+ type: 'mix';
183
+ /** Background/base color name — the "from" color. */
184
+ base: string;
185
+ /** Target color name — the "to" color to mix toward. */
186
+ target: string;
187
+ /**
188
+ * Mix ratio 0–100 (0 = pure base, 100 = pure target).
189
+ * In 'transparent' blend mode, this controls the opacity of the target.
190
+ * Supports [normal, highContrast] pair.
191
+ */
192
+ value: HCPair<number>;
193
+ /**
194
+ * Blending mode. Default: 'opaque'.
195
+ * - 'opaque': produces a solid color by interpolating base and target.
196
+ * - 'transparent': produces the target color with alpha = value/100.
197
+ */
198
+ blend?: 'opaque' | 'transparent';
199
+ /**
200
+ * Interpolation color space for opaque blending. Default: 'okhsl'.
201
+ * - 'okhsl': perceptually uniform, consistent with Glaze's internal model.
202
+ * - 'srgb': linear sRGB interpolation, matches browser compositing.
203
+ *
204
+ * Ignored for 'transparent' blend (always composites in linear sRGB).
205
+ */
206
+ space?: 'okhsl' | 'srgb';
207
+ /**
208
+ * Minimum WCAG contrast between the base and the resulting color.
209
+ * In 'opaque' mode, adjusts the mix ratio to meet contrast.
210
+ * In 'transparent' mode, adjusts opacity to meet contrast against the composite.
211
+ * Supports [normal, highContrast] pair.
212
+ */
213
+ contrast?: HCPair<MinContrast>;
214
+ }
215
+ type ColorDef = RegularColorDef | ShadowColorDef | MixColorDef;
148
216
  type ColorMap = Record<string, ColorDef>;
149
217
  /** Resolved color for a single scheme variant. */
150
218
  interface ResolvedColorVariant {
@@ -447,5 +515,5 @@ declare function formatHsl(h: number, s: number, l: number): string;
447
515
  */
448
516
  declare function formatOklch(h: number, s: number, l: number): string;
449
517
  //#endregion
450
- export { type AdaptationMode, type ColorDef, type ColorMap, type ContrastPreset, type FindLightnessForContrastOptions, type FindLightnessForContrastResult, type GlazeColorFormat, type GlazeColorInput, type GlazeColorToken, type GlazeConfig, type GlazeCssOptions, type GlazeCssResult, type GlazeExtendOptions, type GlazeJsonOptions, type GlazeOutputModes, type GlazePalette, type GlazeShadowInput, type GlazeTheme, type GlazeThemeExport, type GlazeTokenOptions, type HCPair, type HexColor, type MinContrast, type OkhslColor, type RegularColorDef, type RelativeValue, type ResolvedColor, type ResolvedColorVariant, type ShadowColorDef, type ShadowTuning, contrastRatioFromLuminance, findLightnessForContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
518
+ export { type AdaptationMode, type ColorDef, type ColorMap, type ContrastPreset, type FindLightnessForContrastOptions, type FindLightnessForContrastResult, type FindValueForMixContrastOptions, type FindValueForMixContrastResult, type GlazeColorFormat, type GlazeColorInput, type GlazeColorToken, type GlazeConfig, type GlazeCssOptions, type GlazeCssResult, type GlazeExtendOptions, type GlazeJsonOptions, type GlazeOutputModes, type GlazePalette, type GlazeShadowInput, type GlazeTheme, type GlazeThemeExport, type GlazeTokenOptions, type HCPair, type HexColor, type MinContrast, type MixColorDef, type OkhslColor, type RegularColorDef, type RelativeValue, type ResolvedColor, type ResolvedColorVariant, type ShadowColorDef, type ShadowTuning, contrastRatioFromLuminance, findLightnessForContrast, findValueForMixContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
451
519
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -516,7 +516,7 @@ const cacheOrder = [];
516
516
  * rendering pipeline: gamma-encode, clamp to sRGB gamut [0,1], then linearize.
517
517
  * This avoids over/under-estimating luminance for out-of-gamut OKHSL colors.
518
518
  */
519
- function gamutClampedLuminance(linearRgb) {
519
+ function gamutClampedLuminance$1(linearRgb) {
520
520
  const r = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0]))));
521
521
  const g = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1]))));
522
522
  const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
@@ -527,7 +527,7 @@ function cachedLuminance(h, s, l) {
527
527
  const key = `${h}|${s}|${lRounded}`;
528
528
  const cached = luminanceCache.get(key);
529
529
  if (cached !== void 0) return cached;
530
- const y = gamutClampedLuminance(okhslToLinearSrgb(h, s, lRounded));
530
+ const y = gamutClampedLuminance$1(okhslToLinearSrgb(h, s, lRounded));
531
531
  if (luminanceCache.size >= CACHE_SIZE) {
532
532
  const evict = cacheOrder.shift();
533
533
  luminanceCache.delete(evict);
@@ -647,10 +647,10 @@ function coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter) {
647
647
  function findLightnessForContrast(options) {
648
648
  const { hue, saturation, preferredLightness, baseLinearRgb, contrast: contrastInput, lightnessRange = [0, 1], epsilon = 1e-4, maxIterations = 14 } = options;
649
649
  const target = resolveMinContrast(contrastInput);
650
- const searchTarget = target + .1;
651
- const yBase = gamutClampedLuminance(baseLinearRgb);
650
+ const searchTarget = target + .01;
651
+ const yBase = gamutClampedLuminance$1(baseLinearRgb);
652
652
  const crPref = contrastRatioFromLuminance(cachedLuminance(hue, saturation, preferredLightness), yBase);
653
- if (crPref >= target) return {
653
+ if (crPref >= searchTarget) return {
654
654
  lightness: preferredLightness,
655
655
  contrast: crPref,
656
656
  met: true,
@@ -699,6 +699,135 @@ function findLightnessForContrast(options) {
699
699
  candidates.sort((a, b) => b.contrast - a.contrast);
700
700
  return candidates[0];
701
701
  }
702
+ /**
703
+ * Binary-search one branch [lo, hi] for the nearest passing mix value
704
+ * to `preferred`.
705
+ */
706
+ function searchMixBranch(lo, hi, yBase, target, epsilon, maxIter, preferred, luminanceAt) {
707
+ const crLo = contrastRatioFromLuminance(luminanceAt(lo), yBase);
708
+ const crHi = contrastRatioFromLuminance(luminanceAt(hi), yBase);
709
+ if (crLo < target && crHi < target) {
710
+ if (crLo >= crHi) return {
711
+ lightness: lo,
712
+ contrast: crLo,
713
+ met: false
714
+ };
715
+ return {
716
+ lightness: hi,
717
+ contrast: crHi,
718
+ met: false
719
+ };
720
+ }
721
+ let low = lo;
722
+ let high = hi;
723
+ for (let i = 0; i < maxIter; i++) {
724
+ if (high - low < epsilon) break;
725
+ const mid = (low + high) / 2;
726
+ if (contrastRatioFromLuminance(luminanceAt(mid), yBase) >= target) if (mid < preferred) low = mid;
727
+ else high = mid;
728
+ else if (mid < preferred) high = mid;
729
+ else low = mid;
730
+ }
731
+ const crLow = contrastRatioFromLuminance(luminanceAt(low), yBase);
732
+ const crHigh = contrastRatioFromLuminance(luminanceAt(high), yBase);
733
+ const lowPasses = crLow >= target;
734
+ const highPasses = crHigh >= target;
735
+ if (lowPasses && highPasses) {
736
+ if (Math.abs(low - preferred) <= Math.abs(high - preferred)) return {
737
+ lightness: low,
738
+ contrast: crLow,
739
+ met: true
740
+ };
741
+ return {
742
+ lightness: high,
743
+ contrast: crHigh,
744
+ met: true
745
+ };
746
+ }
747
+ if (lowPasses) return {
748
+ lightness: low,
749
+ contrast: crLow,
750
+ met: true
751
+ };
752
+ if (highPasses) return {
753
+ lightness: high,
754
+ contrast: crHigh,
755
+ met: true
756
+ };
757
+ return crLow >= crHigh ? {
758
+ lightness: low,
759
+ contrast: crLow,
760
+ met: false
761
+ } : {
762
+ lightness: high,
763
+ contrast: crHigh,
764
+ met: false
765
+ };
766
+ }
767
+ /**
768
+ * Find the mix parameter (ratio or opacity) that satisfies a WCAG 2 contrast
769
+ * target against a base color, staying as close to `preferredValue` as possible.
770
+ */
771
+ function findValueForMixContrast(options) {
772
+ const { preferredValue, baseLinearRgb, contrast: contrastInput, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
773
+ const target = resolveMinContrast(contrastInput);
774
+ const searchTarget = target + .01;
775
+ const yBase = gamutClampedLuminance$1(baseLinearRgb);
776
+ const crPref = contrastRatioFromLuminance(luminanceAtValue(preferredValue), yBase);
777
+ if (crPref >= searchTarget) return {
778
+ value: preferredValue,
779
+ contrast: crPref,
780
+ met: true
781
+ };
782
+ const darkerResult = preferredValue > 0 ? searchMixBranch(0, preferredValue, yBase, searchTarget, epsilon, maxIterations, preferredValue, luminanceAtValue) : null;
783
+ const lighterResult = preferredValue < 1 ? searchMixBranch(preferredValue, 1, yBase, searchTarget, epsilon, maxIterations, preferredValue, luminanceAtValue) : null;
784
+ if (darkerResult) darkerResult.met = darkerResult.contrast >= target;
785
+ if (lighterResult) lighterResult.met = lighterResult.contrast >= target;
786
+ const darkerPasses = darkerResult?.met ?? false;
787
+ const lighterPasses = lighterResult?.met ?? false;
788
+ if (darkerPasses && lighterPasses) {
789
+ if (Math.abs(darkerResult.lightness - preferredValue) <= Math.abs(lighterResult.lightness - preferredValue)) return {
790
+ value: darkerResult.lightness,
791
+ contrast: darkerResult.contrast,
792
+ met: true
793
+ };
794
+ return {
795
+ value: lighterResult.lightness,
796
+ contrast: lighterResult.contrast,
797
+ met: true
798
+ };
799
+ }
800
+ if (darkerPasses) return {
801
+ value: darkerResult.lightness,
802
+ contrast: darkerResult.contrast,
803
+ met: true
804
+ };
805
+ if (lighterPasses) return {
806
+ value: lighterResult.lightness,
807
+ contrast: lighterResult.contrast,
808
+ met: true
809
+ };
810
+ const candidates = [];
811
+ if (darkerResult) candidates.push({
812
+ ...darkerResult,
813
+ branch: "lower"
814
+ });
815
+ if (lighterResult) candidates.push({
816
+ ...lighterResult,
817
+ branch: "upper"
818
+ });
819
+ if (candidates.length === 0) return {
820
+ value: preferredValue,
821
+ contrast: crPref,
822
+ met: false
823
+ };
824
+ candidates.sort((a, b) => b.contrast - a.contrast);
825
+ return {
826
+ value: candidates[0].lightness,
827
+ contrast: candidates[0].contrast,
828
+ met: candidates[0].met
829
+ };
830
+ }
702
831
 
703
832
  //#endregion
704
833
  //#region src/glaze.ts
@@ -730,6 +859,9 @@ function pairHC(p) {
730
859
  function isShadowDef(def) {
731
860
  return def.type === "shadow";
732
861
  }
862
+ function isMixDef(def) {
863
+ return def.type === "mix";
864
+ }
733
865
  const DEFAULT_SHADOW_TUNING = {
734
866
  saturationFactor: .18,
735
867
  maxSaturation: .25,
@@ -797,6 +929,13 @@ function validateColorDefs(defs) {
797
929
  }
798
930
  continue;
799
931
  }
932
+ if (isMixDef(def)) {
933
+ if (!names.has(def.base)) throw new Error(`glaze: mix "${name}" references non-existent base "${def.base}".`);
934
+ if (!names.has(def.target)) throw new Error(`glaze: mix "${name}" references non-existent target "${def.target}".`);
935
+ if (isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
936
+ if (isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
937
+ continue;
938
+ }
800
939
  const regDef = def;
801
940
  if (regDef.contrast !== void 0 && !regDef.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
802
941
  if (regDef.lightness !== void 0 && !isAbsoluteLightness(regDef.lightness) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "lightness" without "base".`);
@@ -815,6 +954,9 @@ function validateColorDefs(defs) {
815
954
  if (isShadowDef(def)) {
816
955
  dfs(def.bg);
817
956
  if (def.fg) dfs(def.fg);
957
+ } else if (isMixDef(def)) {
958
+ dfs(def.base);
959
+ dfs(def.target);
818
960
  } else {
819
961
  const regDef = def;
820
962
  if (regDef.base) dfs(regDef.base);
@@ -834,6 +976,9 @@ function topoSort(defs) {
834
976
  if (isShadowDef(def)) {
835
977
  visit(def.bg);
836
978
  if (def.fg) visit(def.fg);
979
+ } else if (isMixDef(def)) {
980
+ visit(def.base);
981
+ visit(def.target);
837
982
  } else {
838
983
  const regDef = def;
839
984
  if (regDef.base) visit(regDef.base);
@@ -949,6 +1094,7 @@ function getSchemeVariant(color, isDark, isHighContrast) {
949
1094
  }
950
1095
  function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
951
1096
  if (isShadowDef(def)) return resolveShadowForScheme(def, ctx, isDark, isHighContrast);
1097
+ if (isMixDef(def)) return resolveMixForScheme(def, ctx, isDark, isHighContrast);
952
1098
  const regDef = def;
953
1099
  const mode = regDef.mode ?? "auto";
954
1100
  const isRoot = isAbsoluteLightness(regDef.lightness) && !regDef.base;
@@ -994,6 +1140,89 @@ function resolveShadowForScheme(def, ctx, isDark, isHighContrast) {
994
1140
  const tuning = resolveShadowTuning(def.tuning);
995
1141
  return computeShadow(bgVariant, fgVariant, intensity, tuning);
996
1142
  }
1143
+ function variantToLinearRgb(v) {
1144
+ return okhslToLinearSrgb(v.h, v.s, v.l);
1145
+ }
1146
+ function gamutClampedLuminance(linearRgb) {
1147
+ const r = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0]))));
1148
+ const g = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1]))));
1149
+ const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
1150
+ return .2126 * r + .7152 * g + .0722 * b;
1151
+ }
1152
+ /**
1153
+ * Resolve hue for OKHSL mixing, handling achromatic colors.
1154
+ * When one color has no saturation, its hue is meaningless —
1155
+ * use the hue from the color that has saturation (matches CSS
1156
+ * color-mix "missing component" behavior).
1157
+ */
1158
+ function mixHue(base, target, t) {
1159
+ const SAT_EPSILON = 1e-6;
1160
+ const baseHasSat = base.s > SAT_EPSILON;
1161
+ const targetHasSat = target.s > SAT_EPSILON;
1162
+ if (baseHasSat && targetHasSat) return circularLerp(base.h, target.h, t);
1163
+ if (targetHasSat) return target.h;
1164
+ return base.h;
1165
+ }
1166
+ function linearSrgbLerp(base, target, t) {
1167
+ return [
1168
+ base[0] + (target[0] - base[0]) * t,
1169
+ base[1] + (target[1] - base[1]) * t,
1170
+ base[2] + (target[2] - base[2]) * t
1171
+ ];
1172
+ }
1173
+ function linearRgbToVariant(rgb) {
1174
+ const [h, s, l] = srgbToOkhsl([
1175
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[0]))),
1176
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[1]))),
1177
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[2])))
1178
+ ]);
1179
+ return {
1180
+ h,
1181
+ s,
1182
+ l,
1183
+ alpha: 1
1184
+ };
1185
+ }
1186
+ function resolveMixForScheme(def, ctx, isDark, isHighContrast) {
1187
+ const baseResolved = ctx.resolved.get(def.base);
1188
+ const targetResolved = ctx.resolved.get(def.target);
1189
+ const baseVariant = getSchemeVariant(baseResolved, isDark, isHighContrast);
1190
+ const targetVariant = getSchemeVariant(targetResolved, isDark, isHighContrast);
1191
+ let t = clamp(isHighContrast ? pairHC(def.value) : pairNormal(def.value), 0, 100) / 100;
1192
+ const blend = def.blend ?? "opaque";
1193
+ const space = def.space ?? "okhsl";
1194
+ const baseLinear = variantToLinearRgb(baseVariant);
1195
+ const targetLinear = variantToLinearRgb(targetVariant);
1196
+ if (def.contrast !== void 0) {
1197
+ const minCr = isHighContrast ? pairHC(def.contrast) : pairNormal(def.contrast);
1198
+ let luminanceAt;
1199
+ if (blend === "transparent") luminanceAt = (v) => gamutClampedLuminance(linearSrgbLerp(baseLinear, targetLinear, v));
1200
+ else if (space === "srgb") luminanceAt = (v) => gamutClampedLuminance(linearSrgbLerp(baseLinear, targetLinear, v));
1201
+ else luminanceAt = (v) => {
1202
+ return gamutClampedLuminance(okhslToLinearSrgb(mixHue(baseVariant, targetVariant, v), baseVariant.s + (targetVariant.s - baseVariant.s) * v, baseVariant.l + (targetVariant.l - baseVariant.l) * v));
1203
+ };
1204
+ t = findValueForMixContrast({
1205
+ preferredValue: t,
1206
+ baseLinearRgb: baseLinear,
1207
+ targetLinearRgb: targetLinear,
1208
+ contrast: minCr,
1209
+ luminanceAtValue: luminanceAt
1210
+ }).value;
1211
+ }
1212
+ if (blend === "transparent") return {
1213
+ h: targetVariant.h,
1214
+ s: targetVariant.s,
1215
+ l: targetVariant.l,
1216
+ alpha: clamp(t, 0, 1)
1217
+ };
1218
+ if (space === "srgb") return linearRgbToVariant(linearSrgbLerp(baseLinear, targetLinear, t));
1219
+ return {
1220
+ h: mixHue(baseVariant, targetVariant, t),
1221
+ s: clamp(baseVariant.s + (targetVariant.s - baseVariant.s) * t, 0, 1),
1222
+ l: clamp(baseVariant.l + (targetVariant.l - baseVariant.l) * t, 0, 1),
1223
+ alpha: 1
1224
+ };
1225
+ }
997
1226
  function resolveAllColors(hue, saturation, defs) {
998
1227
  validateColorDefs(defs);
999
1228
  const order = topoSort(defs);
@@ -1004,7 +1233,8 @@ function resolveAllColors(hue, saturation, defs) {
1004
1233
  resolved: /* @__PURE__ */ new Map()
1005
1234
  };
1006
1235
  function defMode(def) {
1007
- return isShadowDef(def) ? void 0 : def.mode ?? "auto";
1236
+ if (isShadowDef(def) || isMixDef(def)) return void 0;
1237
+ return def.mode ?? "auto";
1008
1238
  }
1009
1239
  const lightMap = /* @__PURE__ */ new Map();
1010
1240
  for (const name of order) {
@@ -1442,5 +1672,5 @@ glaze.resetConfig = function resetConfig() {
1442
1672
  };
1443
1673
 
1444
1674
  //#endregion
1445
- export { contrastRatioFromLuminance, findLightnessForContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
1675
+ export { contrastRatioFromLuminance, findLightnessForContrast, findValueForMixContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
1446
1676
  //# sourceMappingURL=index.mjs.map