@tenphi/glaze 0.7.0 → 0.8.0

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
@@ -812,6 +812,7 @@ let globalConfig = {
812
812
  lightLightness: [10, 100],
813
813
  darkLightness: [15, 95],
814
814
  darkDesaturation: .1,
815
+ darkCurve: .5,
815
816
  states: {
816
817
  dark: "@dark",
817
818
  highContrast: "@high-contrast"
@@ -959,23 +960,42 @@ function topoSort(defs) {
959
960
  for (const name of Object.keys(defs)) visit(name);
960
961
  return result;
961
962
  }
962
- function mapLightnessLight(l, mode) {
963
- if (mode === "static") return l;
963
+ function mapLightnessLight(l, mode, isHighContrast) {
964
+ if (mode === "static" || isHighContrast) return l;
964
965
  const [lo, hi] = globalConfig.lightLightness;
965
966
  return l * (hi - lo) / 100 + lo;
966
967
  }
967
- function mapLightnessDark(l, mode) {
968
+ function mobiusCurve(t, beta) {
969
+ if (beta >= 1) return t;
970
+ return t / (t + beta * (1 - t));
971
+ }
972
+ function mapLightnessDark(l, mode, isHighContrast) {
968
973
  if (mode === "static") return l;
969
- const [lo, hi] = globalConfig.darkLightness;
970
- if (mode === "fixed") return l * (hi - lo) / 100 + lo;
971
- return (100 - l) * (hi - lo) / 100 + lo;
974
+ const beta = globalConfig.darkCurve;
975
+ if (isHighContrast) {
976
+ if (mode === "fixed") return l;
977
+ return 100 * mobiusCurve((100 - l) / 100, beta);
978
+ }
979
+ const [darkLo, darkHi] = globalConfig.darkLightness;
980
+ if (mode === "fixed") return l * (darkHi - darkLo) / 100 + darkLo;
981
+ const [lightLo, lightHi] = globalConfig.lightLightness;
982
+ const t = (lightHi - (l * (lightHi - lightLo) / 100 + lightLo)) / (lightHi - lightLo);
983
+ return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
984
+ }
985
+ function lightMappedToDark(lightL, isHighContrast) {
986
+ const beta = globalConfig.darkCurve;
987
+ if (isHighContrast) return 100 * mobiusCurve((100 - lightL) / 100, beta);
988
+ const [lightLo, lightHi] = globalConfig.lightLightness;
989
+ const [darkLo, darkHi] = globalConfig.darkLightness;
990
+ const t = (lightHi - clamp(lightL, lightLo, lightHi)) / (lightHi - lightLo);
991
+ return darkLo + (darkHi - darkLo) * mobiusCurve(t, beta);
972
992
  }
973
993
  function mapSaturationDark(s, mode) {
974
994
  if (mode === "static") return s;
975
995
  return s * (1 - globalConfig.darkDesaturation);
976
996
  }
977
- function schemeLightnessRange(isDark, mode) {
978
- if (mode === "static") return [0, 1];
997
+ function schemeLightnessRange(isDark, mode, isHighContrast) {
998
+ if (mode === "static" || isHighContrast) return [0, 1];
979
999
  const [lo, hi] = isDark ? globalConfig.darkLightness : globalConfig.lightLightness;
980
1000
  return [lo / 100, hi / 100];
981
1001
  }
@@ -1035,26 +1055,26 @@ function resolveDependentColor(name, def, ctx, isHighContrast, isDark, effective
1035
1055
  else {
1036
1056
  const parsed = parseRelativeOrAbsolute(isHighContrast ? pairHC(rawLightness) : pairNormal(rawLightness));
1037
1057
  if (parsed.relative) {
1038
- let delta = parsed.value;
1039
- if (isDark && mode === "auto") delta = -delta;
1040
- preferredL = clamp(baseL + delta, 0, 100);
1041
- } else if (isDark) preferredL = mapLightnessDark(parsed.value, mode);
1042
- else preferredL = mapLightnessLight(parsed.value, mode);
1058
+ const delta = parsed.value;
1059
+ if (isDark && mode === "auto") preferredL = lightMappedToDark(clamp(getSchemeVariant(baseResolved, false, isHighContrast).l * 100 + delta, 0, 100), isHighContrast);
1060
+ else preferredL = clamp(baseL + delta, 0, 100);
1061
+ } else if (isDark) preferredL = mapLightnessDark(parsed.value, mode, isHighContrast);
1062
+ else preferredL = mapLightnessLight(parsed.value, mode, isHighContrast);
1043
1063
  }
1044
1064
  const rawContrast = def.contrast;
1045
1065
  if (rawContrast !== void 0) {
1046
1066
  const minCr = isHighContrast ? pairHC(rawContrast) : pairNormal(rawContrast);
1047
1067
  const effectiveSat = isDark ? mapSaturationDark(satFactor * ctx.saturation / 100, mode) : satFactor * ctx.saturation / 100;
1048
1068
  const baseLinearRgb = okhslToLinearSrgb(baseVariant.h, baseVariant.s, baseVariant.l);
1049
- const lightnessRange = schemeLightnessRange(isDark, mode);
1069
+ const windowRange = schemeLightnessRange(isDark, mode, isHighContrast);
1050
1070
  return {
1051
1071
  l: findLightnessForContrast({
1052
1072
  hue: effectiveHue,
1053
1073
  saturation: effectiveSat,
1054
- preferredLightness: clamp(preferredL / 100, lightnessRange[0], lightnessRange[1]),
1074
+ preferredLightness: clamp(preferredL / 100, windowRange[0], windowRange[1]),
1055
1075
  baseLinearRgb,
1056
1076
  contrast: minCr,
1057
- lightnessRange
1077
+ lightnessRange: [0, 1]
1058
1078
  }).lightness * 100,
1059
1079
  satFactor
1060
1080
  };
@@ -1091,13 +1111,13 @@ function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
1091
1111
  let finalL;
1092
1112
  let finalSat;
1093
1113
  if (isDark && isRoot) {
1094
- finalL = mapLightnessDark(lightL, mode);
1114
+ finalL = mapLightnessDark(lightL, mode, isHighContrast);
1095
1115
  finalSat = mapSaturationDark(satFactor * ctx.saturation / 100, mode);
1096
1116
  } else if (isDark && !isRoot) {
1097
1117
  finalL = lightL;
1098
1118
  finalSat = mapSaturationDark(satFactor * ctx.saturation / 100, mode);
1099
1119
  } else if (isRoot) {
1100
- finalL = mapLightnessLight(lightL, mode);
1120
+ finalL = mapLightnessLight(lightL, mode, isHighContrast);
1101
1121
  finalSat = satFactor * ctx.saturation / 100;
1102
1122
  } else {
1103
1123
  finalL = lightL;
@@ -1563,6 +1583,7 @@ glaze.configure = function configure(config) {
1563
1583
  lightLightness: config.lightLightness ?? globalConfig.lightLightness,
1564
1584
  darkLightness: config.darkLightness ?? globalConfig.darkLightness,
1565
1585
  darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
1586
+ darkCurve: config.darkCurve ?? globalConfig.darkCurve,
1566
1587
  states: {
1567
1588
  dark: config.states?.dark ?? globalConfig.states.dark,
1568
1589
  highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
@@ -1662,6 +1683,7 @@ glaze.resetConfig = function resetConfig() {
1662
1683
  lightLightness: [10, 100],
1663
1684
  darkLightness: [15, 95],
1664
1685
  darkDesaturation: .1,
1686
+ darkCurve: .5,
1665
1687
  states: {
1666
1688
  dark: "@dark",
1667
1689
  highContrast: "@high-contrast"