@tenphi/glaze 0.0.0-snapshot.7c1fc7d → 0.0.0-snapshot.7f3fb7f

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.mjs CHANGED
@@ -79,8 +79,8 @@ const OKLab_to_linear_sRGB_coefficients = [
79
79
  .73956515,
80
80
  -.45954404,
81
81
  .08285427,
82
- .12541073,
83
- -.14503204
82
+ .1254107,
83
+ .14503204
84
84
  ]],
85
85
  [[.13110757611180954, 1.813339709266608], [
86
86
  1.35733652,
@@ -252,10 +252,9 @@ const getCs = (L, a, b, cusp) => {
252
252
  ];
253
253
  };
254
254
  /**
255
- * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
256
- * Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
255
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
257
256
  */
258
- function okhslToLinearSrgb(h, s, l) {
257
+ function okhslToOklab(h, s, l) {
259
258
  const L = toeInv(l);
260
259
  let a = 0;
261
260
  let b = 0;
@@ -282,11 +281,18 @@ function okhslToLinearSrgb(h, s, l) {
282
281
  a = c * a_;
283
282
  b = c * b_;
284
283
  }
285
- return OKLabToLinearSRGB([
284
+ return [
286
285
  L,
287
286
  a,
288
287
  b
289
- ]);
288
+ ];
289
+ }
290
+ /**
291
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
292
+ * Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
293
+ */
294
+ function okhslToLinearSrgb(h, s, l) {
295
+ return OKLabToLinearSRGB(okhslToOklab(h, s, l));
290
296
  }
291
297
  /**
292
298
  * Compute relative luminance Y from linear sRGB channels.
@@ -325,40 +331,15 @@ function okhslToSrgb(h, s, l) {
325
331
  ];
326
332
  }
327
333
  /**
328
- * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
334
+ * Compute WCAG 2 relative luminance from linear sRGB, matching the browser
335
+ * rendering pipeline: gamma-encode, clamp to sRGB gamut [0,1], then linearize.
336
+ * This avoids over/under-estimating luminance for out-of-gamut OKHSL colors.
329
337
  */
330
- function okhslToOklab(h, s, l) {
331
- const L = toeInv(l);
332
- let a = 0;
333
- let b = 0;
334
- const hNorm = constrainAngle(h) / 360;
335
- if (L !== 0 && L !== 1 && s !== 0) {
336
- const a_ = Math.cos(TAU * hNorm);
337
- const b_ = Math.sin(TAU * hNorm);
338
- const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
339
- const mid = .8;
340
- const midInv = 1.25;
341
- let t, k0, k1, k2;
342
- if (s < mid) {
343
- t = midInv * s;
344
- k0 = 0;
345
- k1 = mid * c0;
346
- k2 = 1 - k1 / cMid;
347
- } else {
348
- t = 5 * (s - .8);
349
- k0 = cMid;
350
- k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
351
- k2 = 1 - k1 / (cMax - cMid);
352
- }
353
- const c = k0 + t * k1 / (1 - k2 * t);
354
- a = c * a_;
355
- b = c * b_;
356
- }
357
- return [
358
- L,
359
- a,
360
- b
361
- ];
338
+ function gamutClampedLuminance(linearRgb) {
339
+ const r = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0]))));
340
+ const g = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1]))));
341
+ const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
342
+ return .2126 * r + .7152 * g + .0722 * b;
362
343
  }
363
344
  const linearSrgbToOklab = (rgb) => {
364
345
  return transform(cbrt3(transform(rgb, linear_sRGB_to_LMS_M)), LMS_to_OKLab_M);
@@ -450,12 +431,13 @@ function formatOkhsl(h, s, l) {
450
431
  return `okhsl(${fmt$1(h, 2)} ${fmt$1(s, 2)}% ${fmt$1(l, 2)}%)`;
451
432
  }
452
433
  /**
453
- * Format OKHSL values as a CSS `rgb(R G B)` string with rounded integer values.
434
+ * Format OKHSL values as a CSS `rgb(R G B)` string.
435
+ * Uses 2 decimal places to avoid 8-bit quantization contrast loss.
454
436
  * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
455
437
  */
456
438
  function formatRgb(h, s, l) {
457
439
  const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
458
- return `rgb(${Math.round(r * 255)} ${Math.round(g * 255)} ${Math.round(b * 255)})`;
440
+ return `rgb(${parseFloat((r * 255).toFixed(2))} ${parseFloat((g * 255).toFixed(2))} ${parseFloat((b * 255).toFixed(2))})`;
459
441
  }
460
442
  /**
461
443
  * Format OKHSL values as a CSS `hsl(H S% L%)` string.
@@ -511,17 +493,6 @@ function resolveMinContrast(value) {
511
493
  const CACHE_SIZE = 512;
512
494
  const luminanceCache = /* @__PURE__ */ new Map();
513
495
  const cacheOrder = [];
514
- /**
515
- * Compute WCAG 2 relative luminance from linear sRGB, matching the browser
516
- * rendering pipeline: gamma-encode, clamp to sRGB gamut [0,1], then linearize.
517
- * This avoids over/under-estimating luminance for out-of-gamut OKHSL colors.
518
- */
519
- function gamutClampedLuminance(linearRgb) {
520
- const r = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[0]))));
521
- const g = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[1]))));
522
- const b = sRGBGammaToLinear(Math.max(0, Math.min(1, sRGBLinearToGamma(linearRgb[2]))));
523
- return .2126 * r + .7152 * g + .0722 * b;
524
- }
525
496
  function cachedLuminance(h, s, l) {
526
497
  const lRounded = Math.round(l * 1e4) / 1e4;
527
498
  const key = `${h}|${s}|${lRounded}`;
@@ -647,7 +618,7 @@ function coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter) {
647
618
  function findLightnessForContrast(options) {
648
619
  const { hue, saturation, preferredLightness, baseLinearRgb, contrast: contrastInput, lightnessRange = [0, 1], epsilon = 1e-4, maxIterations = 14 } = options;
649
620
  const target = resolveMinContrast(contrastInput);
650
- const searchTarget = target + .01;
621
+ const searchTarget = target * 1.005;
651
622
  const yBase = gamutClampedLuminance(baseLinearRgb);
652
623
  const crPref = contrastRatioFromLuminance(cachedLuminance(hue, saturation, preferredLightness), yBase);
653
624
  if (crPref >= searchTarget) return {
@@ -699,6 +670,135 @@ function findLightnessForContrast(options) {
699
670
  candidates.sort((a, b) => b.contrast - a.contrast);
700
671
  return candidates[0];
701
672
  }
673
+ /**
674
+ * Binary-search one branch [lo, hi] for the nearest passing mix value
675
+ * to `preferred`.
676
+ */
677
+ function searchMixBranch(lo, hi, yBase, target, epsilon, maxIter, preferred, luminanceAt) {
678
+ const crLo = contrastRatioFromLuminance(luminanceAt(lo), yBase);
679
+ const crHi = contrastRatioFromLuminance(luminanceAt(hi), yBase);
680
+ if (crLo < target && crHi < target) {
681
+ if (crLo >= crHi) return {
682
+ lightness: lo,
683
+ contrast: crLo,
684
+ met: false
685
+ };
686
+ return {
687
+ lightness: hi,
688
+ contrast: crHi,
689
+ met: false
690
+ };
691
+ }
692
+ let low = lo;
693
+ let high = hi;
694
+ for (let i = 0; i < maxIter; i++) {
695
+ if (high - low < epsilon) break;
696
+ const mid = (low + high) / 2;
697
+ if (contrastRatioFromLuminance(luminanceAt(mid), yBase) >= target) if (mid < preferred) low = mid;
698
+ else high = mid;
699
+ else if (mid < preferred) high = mid;
700
+ else low = mid;
701
+ }
702
+ const crLow = contrastRatioFromLuminance(luminanceAt(low), yBase);
703
+ const crHigh = contrastRatioFromLuminance(luminanceAt(high), yBase);
704
+ const lowPasses = crLow >= target;
705
+ const highPasses = crHigh >= target;
706
+ if (lowPasses && highPasses) {
707
+ if (Math.abs(low - preferred) <= Math.abs(high - preferred)) return {
708
+ lightness: low,
709
+ contrast: crLow,
710
+ met: true
711
+ };
712
+ return {
713
+ lightness: high,
714
+ contrast: crHigh,
715
+ met: true
716
+ };
717
+ }
718
+ if (lowPasses) return {
719
+ lightness: low,
720
+ contrast: crLow,
721
+ met: true
722
+ };
723
+ if (highPasses) return {
724
+ lightness: high,
725
+ contrast: crHigh,
726
+ met: true
727
+ };
728
+ return crLow >= crHigh ? {
729
+ lightness: low,
730
+ contrast: crLow,
731
+ met: false
732
+ } : {
733
+ lightness: high,
734
+ contrast: crHigh,
735
+ met: false
736
+ };
737
+ }
738
+ /**
739
+ * Find the mix parameter (ratio or opacity) that satisfies a WCAG 2 contrast
740
+ * target against a base color, staying as close to `preferredValue` as possible.
741
+ */
742
+ function findValueForMixContrast(options) {
743
+ const { preferredValue, baseLinearRgb, contrast: contrastInput, luminanceAtValue, epsilon = 1e-4, maxIterations = 20 } = options;
744
+ const target = resolveMinContrast(contrastInput);
745
+ const searchTarget = target * 1.005;
746
+ const yBase = gamutClampedLuminance(baseLinearRgb);
747
+ const crPref = contrastRatioFromLuminance(luminanceAtValue(preferredValue), yBase);
748
+ if (crPref >= searchTarget) return {
749
+ value: preferredValue,
750
+ contrast: crPref,
751
+ met: true
752
+ };
753
+ const darkerResult = preferredValue > 0 ? searchMixBranch(0, preferredValue, yBase, searchTarget, epsilon, maxIterations, preferredValue, luminanceAtValue) : null;
754
+ const lighterResult = preferredValue < 1 ? searchMixBranch(preferredValue, 1, yBase, searchTarget, epsilon, maxIterations, preferredValue, luminanceAtValue) : null;
755
+ if (darkerResult) darkerResult.met = darkerResult.contrast >= target;
756
+ if (lighterResult) lighterResult.met = lighterResult.contrast >= target;
757
+ const darkerPasses = darkerResult?.met ?? false;
758
+ const lighterPasses = lighterResult?.met ?? false;
759
+ if (darkerPasses && lighterPasses) {
760
+ if (Math.abs(darkerResult.lightness - preferredValue) <= Math.abs(lighterResult.lightness - preferredValue)) return {
761
+ value: darkerResult.lightness,
762
+ contrast: darkerResult.contrast,
763
+ met: true
764
+ };
765
+ return {
766
+ value: lighterResult.lightness,
767
+ contrast: lighterResult.contrast,
768
+ met: true
769
+ };
770
+ }
771
+ if (darkerPasses) return {
772
+ value: darkerResult.lightness,
773
+ contrast: darkerResult.contrast,
774
+ met: true
775
+ };
776
+ if (lighterPasses) return {
777
+ value: lighterResult.lightness,
778
+ contrast: lighterResult.contrast,
779
+ met: true
780
+ };
781
+ const candidates = [];
782
+ if (darkerResult) candidates.push({
783
+ ...darkerResult,
784
+ branch: "lower"
785
+ });
786
+ if (lighterResult) candidates.push({
787
+ ...lighterResult,
788
+ branch: "upper"
789
+ });
790
+ if (candidates.length === 0) return {
791
+ value: preferredValue,
792
+ contrast: crPref,
793
+ met: false
794
+ };
795
+ candidates.sort((a, b) => b.contrast - a.contrast);
796
+ return {
797
+ value: candidates[0].lightness,
798
+ contrast: candidates[0].contrast,
799
+ met: candidates[0].met
800
+ };
801
+ }
702
802
 
703
803
  //#endregion
704
804
  //#region src/glaze.ts
@@ -730,6 +830,9 @@ function pairHC(p) {
730
830
  function isShadowDef(def) {
731
831
  return def.type === "shadow";
732
832
  }
833
+ function isMixDef(def) {
834
+ return def.type === "mix";
835
+ }
733
836
  const DEFAULT_SHADOW_TUNING = {
734
837
  saturationFactor: .18,
735
838
  maxSaturation: .25,
@@ -797,6 +900,13 @@ function validateColorDefs(defs) {
797
900
  }
798
901
  continue;
799
902
  }
903
+ if (isMixDef(def)) {
904
+ if (!names.has(def.base)) throw new Error(`glaze: mix "${name}" references non-existent base "${def.base}".`);
905
+ if (!names.has(def.target)) throw new Error(`glaze: mix "${name}" references non-existent target "${def.target}".`);
906
+ if (isShadowDef(defs[def.base])) throw new Error(`glaze: mix "${name}" base "${def.base}" references a shadow color.`);
907
+ if (isShadowDef(defs[def.target])) throw new Error(`glaze: mix "${name}" target "${def.target}" references a shadow color.`);
908
+ continue;
909
+ }
800
910
  const regDef = def;
801
911
  if (regDef.contrast !== void 0 && !regDef.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
802
912
  if (regDef.lightness !== void 0 && !isAbsoluteLightness(regDef.lightness) && !regDef.base) throw new Error(`glaze: color "${name}" has relative "lightness" without "base".`);
@@ -815,6 +925,9 @@ function validateColorDefs(defs) {
815
925
  if (isShadowDef(def)) {
816
926
  dfs(def.bg);
817
927
  if (def.fg) dfs(def.fg);
928
+ } else if (isMixDef(def)) {
929
+ dfs(def.base);
930
+ dfs(def.target);
818
931
  } else {
819
932
  const regDef = def;
820
933
  if (regDef.base) dfs(regDef.base);
@@ -834,6 +947,9 @@ function topoSort(defs) {
834
947
  if (isShadowDef(def)) {
835
948
  visit(def.bg);
836
949
  if (def.fg) visit(def.fg);
950
+ } else if (isMixDef(def)) {
951
+ visit(def.base);
952
+ visit(def.target);
837
953
  } else {
838
954
  const regDef = def;
839
955
  if (regDef.base) visit(regDef.base);
@@ -858,6 +974,11 @@ function mapSaturationDark(s, mode) {
858
974
  if (mode === "static") return s;
859
975
  return s * (1 - globalConfig.darkDesaturation);
860
976
  }
977
+ function schemeLightnessRange(isDark, mode) {
978
+ if (mode === "static") return [0, 1];
979
+ const [lo, hi] = isDark ? globalConfig.darkLightness : globalConfig.lightLightness;
980
+ return [lo / 100, hi / 100];
981
+ }
861
982
  function clamp(v, min, max) {
862
983
  return Math.max(min, Math.min(max, v));
863
984
  }
@@ -925,13 +1046,15 @@ function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effective
925
1046
  const minCr = isHighContrast ? pairHC(rawContrast) : pairNormal(rawContrast);
926
1047
  const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode) : satFactor * ctx.saturation / 100;
927
1048
  const baseLinearRgb = okhslToLinearSrgb(baseVariant.h, baseVariant.s, baseVariant.l);
1049
+ const lightnessRange = schemeLightnessRange(isDark, mode);
928
1050
  return {
929
1051
  l: findLightnessForContrast({
930
1052
  hue: effectiveHue,
931
1053
  saturation: effectiveSat,
932
- preferredLightness: preferredL / 100,
1054
+ preferredLightness: clamp(preferredL / 100, lightnessRange[0], lightnessRange[1]),
933
1055
  baseLinearRgb,
934
- contrast: minCr
1056
+ contrast: minCr,
1057
+ lightnessRange
935
1058
  }).lightness * 100,
936
1059
  satFactor
937
1060
  };
@@ -949,6 +1072,7 @@ function getSchemeVariant(color, isDark, isHighContrast) {
949
1072
  }
950
1073
  function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
951
1074
  if (isShadowDef(def)) return resolveShadowForScheme(def, ctx, isDark, isHighContrast);
1075
+ if (isMixDef(def)) return resolveMixForScheme(def, ctx, isDark, isHighContrast);
952
1076
  const regDef = def;
953
1077
  const mode = regDef.mode ?? "auto";
954
1078
  const isRoot = isAbsoluteLightness(regDef.lightness) && !regDef.base;
@@ -994,6 +1118,83 @@ function resolveShadowForScheme(def, ctx, isDark, isHighContrast) {
994
1118
  const tuning = resolveShadowTuning(def.tuning);
995
1119
  return computeShadow(bgVariant, fgVariant, intensity, tuning);
996
1120
  }
1121
+ function variantToLinearRgb(v) {
1122
+ return okhslToLinearSrgb(v.h, v.s, v.l);
1123
+ }
1124
+ /**
1125
+ * Resolve hue for OKHSL mixing, handling achromatic colors.
1126
+ * When one color has no saturation, its hue is meaningless —
1127
+ * use the hue from the color that has saturation (matches CSS
1128
+ * color-mix "missing component" behavior).
1129
+ */
1130
+ function mixHue(base, target, t) {
1131
+ const SAT_EPSILON = 1e-6;
1132
+ const baseHasSat = base.s > SAT_EPSILON;
1133
+ const targetHasSat = target.s > SAT_EPSILON;
1134
+ if (baseHasSat && targetHasSat) return circularLerp(base.h, target.h, t);
1135
+ if (targetHasSat) return target.h;
1136
+ return base.h;
1137
+ }
1138
+ function linearSrgbLerp(base, target, t) {
1139
+ return [
1140
+ base[0] + (target[0] - base[0]) * t,
1141
+ base[1] + (target[1] - base[1]) * t,
1142
+ base[2] + (target[2] - base[2]) * t
1143
+ ];
1144
+ }
1145
+ function linearRgbToVariant(rgb) {
1146
+ const [h, s, l] = srgbToOkhsl([
1147
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[0]))),
1148
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[1]))),
1149
+ Math.max(0, Math.min(1, sRGBLinearToGamma(rgb[2])))
1150
+ ]);
1151
+ return {
1152
+ h,
1153
+ s,
1154
+ l,
1155
+ alpha: 1
1156
+ };
1157
+ }
1158
+ function resolveMixForScheme(def, ctx, isDark, isHighContrast) {
1159
+ const baseResolved = ctx.resolved.get(def.base);
1160
+ const targetResolved = ctx.resolved.get(def.target);
1161
+ const baseVariant = getSchemeVariant(baseResolved, isDark, isHighContrast);
1162
+ const targetVariant = getSchemeVariant(targetResolved, isDark, isHighContrast);
1163
+ let t = clamp(isHighContrast ? pairHC(def.value) : pairNormal(def.value), 0, 100) / 100;
1164
+ const blend = def.blend ?? "opaque";
1165
+ const space = def.space ?? "okhsl";
1166
+ const baseLinear = variantToLinearRgb(baseVariant);
1167
+ const targetLinear = variantToLinearRgb(targetVariant);
1168
+ if (def.contrast !== void 0) {
1169
+ const minCr = isHighContrast ? pairHC(def.contrast) : pairNormal(def.contrast);
1170
+ let luminanceAt;
1171
+ if (blend === "transparent") luminanceAt = (v) => gamutClampedLuminance(linearSrgbLerp(baseLinear, targetLinear, v));
1172
+ else if (space === "srgb") luminanceAt = (v) => gamutClampedLuminance(linearSrgbLerp(baseLinear, targetLinear, v));
1173
+ else luminanceAt = (v) => {
1174
+ return gamutClampedLuminance(okhslToLinearSrgb(mixHue(baseVariant, targetVariant, v), baseVariant.s + (targetVariant.s - baseVariant.s) * v, baseVariant.l + (targetVariant.l - baseVariant.l) * v));
1175
+ };
1176
+ t = findValueForMixContrast({
1177
+ preferredValue: t,
1178
+ baseLinearRgb: baseLinear,
1179
+ targetLinearRgb: targetLinear,
1180
+ contrast: minCr,
1181
+ luminanceAtValue: luminanceAt
1182
+ }).value;
1183
+ }
1184
+ if (blend === "transparent") return {
1185
+ h: targetVariant.h,
1186
+ s: targetVariant.s,
1187
+ l: targetVariant.l,
1188
+ alpha: clamp(t, 0, 1)
1189
+ };
1190
+ if (space === "srgb") return linearRgbToVariant(linearSrgbLerp(baseLinear, targetLinear, t));
1191
+ return {
1192
+ h: mixHue(baseVariant, targetVariant, t),
1193
+ s: clamp(baseVariant.s + (targetVariant.s - baseVariant.s) * t, 0, 1),
1194
+ l: clamp(baseVariant.l + (targetVariant.l - baseVariant.l) * t, 0, 1),
1195
+ alpha: 1
1196
+ };
1197
+ }
997
1198
  function resolveAllColors(hue, saturation, defs) {
998
1199
  validateColorDefs(defs);
999
1200
  const order = topoSort(defs);
@@ -1004,7 +1205,8 @@ function resolveAllColors(hue, saturation, defs) {
1004
1205
  resolved: /* @__PURE__ */ new Map()
1005
1206
  };
1006
1207
  function defMode(def) {
1007
- return isShadowDef(def) ? void 0 : def.mode ?? "auto";
1208
+ if (isShadowDef(def) || isMixDef(def)) return void 0;
1209
+ return def.mode ?? "auto";
1008
1210
  }
1009
1211
  const lightMap = /* @__PURE__ */ new Map();
1010
1212
  for (const name of order) {
@@ -1442,5 +1644,5 @@ glaze.resetConfig = function resetConfig() {
1442
1644
  };
1443
1645
 
1444
1646
  //#endregion
1445
- export { contrastRatioFromLuminance, findLightnessForContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
1647
+ export { contrastRatioFromLuminance, findLightnessForContrast, findValueForMixContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, gamutClampedLuminance, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
1446
1648
  //# sourceMappingURL=index.mjs.map