@seanyeh77/ui 0.14.0 → 0.15.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.js CHANGED
@@ -1195,6 +1195,17 @@ function contrast(a, b) {
1195
1195
  const y = lum(b);
1196
1196
  return (Math.max(x, y) + .05) / (Math.min(x, y) + .05);
1197
1197
  }
1198
+ /** The accent's text color (accent-text): the accent itself when it reads on both bg and surface (4.5:1); else, in
1199
+ * light, the accent mixed with black 4 % at a time, in dark with white 5 % at a time, until it reads on both. Only
1200
+ * text changes: fills, bars, rings and dots keep the accent. */
1201
+ function accentText(hex, dark, bg, surface) {
1202
+ const reads = (c) => contrast(c, bg) >= 4.5 && contrast(c, surface) >= 4.5;
1203
+ const toward = dark ? "#ffffff" : "#000000";
1204
+ const step = dark ? .05 : .04;
1205
+ let text = hex;
1206
+ for (let k = 1; k * step <= 1 && !reads(text); k++) text = mixHex(hex, toward, k * step);
1207
+ return text;
1208
+ }
1198
1209
  /** Derives every accent token from one color (自訂強調色的換算 in docs/guidelines.md).
1199
1210
  * Returns false for anything but #rrggbb. quiet: apply without storing. */
1200
1211
  function setAccent(hex, quiet = false) {
@@ -1205,8 +1216,7 @@ function setAccent(hex, quiet = false) {
1205
1216
  const cs = getComputedStyle(root);
1206
1217
  const surface = (cs.getPropertyValue("--surface") || (dark ? "#393530" : "#ffffff")).trim();
1207
1218
  const bg = (cs.getPropertyValue("--bg") || (dark ? "#2f2a25" : "#fcfbfb")).trim();
1208
- let text = hex;
1209
- if (dark) for (let t = 0; t <= 1 && contrast(text, surface) < 4.5; t += .05) text = mixHex(hex, "#ffffff", t);
1219
+ const text = accentText(hex, dark, bg.startsWith("#") ? bg : dark ? "#2f2a25" : "#fcfbfb", surface.startsWith("#") ? surface : dark ? "#393530" : "#ffffff");
1210
1220
  const ink = "#171512";
1211
1221
  const vars = {
1212
1222
  "--accent": hex,