@fundar/data-chart-telling 0.0.48 → 0.0.49

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.
@@ -23,6 +23,40 @@ export function paletteColor(index, palette) {
23
23
  return 'currentColor';
24
24
  return palette[index % palette.length];
25
25
  }
26
+ function clamp01(v) {
27
+ return Math.max(0, Math.min(1, v));
28
+ }
29
+ function linearToSrgb8(c) {
30
+ const s = c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
31
+ return clamp01(s) * 255;
32
+ }
33
+ /**
34
+ * Parses `oklch(L C H)` directly instead of round-tripping through the
35
+ * browser's computed style: Chromium/Firefox serialize computed oklch()
36
+ * values back as oklch() (not rgb()), so the generic browser-fallback
37
+ * below sees an unchanged string on the second pass and its
38
+ * infinite-loop guard mistakes that for a parse failure.
39
+ */
40
+ function parseOklch(color) {
41
+ const match = /^oklch\(\s*([\d.]+)(%)?\s+([\d.]+)\s+([\d.]+)/i.exec(color.trim());
42
+ if (!match)
43
+ return null;
44
+ const L = match[2] ? Number(match[1]) / 100 : Number(match[1]);
45
+ const C = Number(match[3]);
46
+ const H = (Number(match[4]) * Math.PI) / 180;
47
+ const a = C * Math.cos(H);
48
+ const b = C * Math.sin(H);
49
+ const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
50
+ const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
51
+ const s_ = L - 0.0894841775 * a - 1.291485548 * b;
52
+ const l = l_ ** 3;
53
+ const m = m_ ** 3;
54
+ const s = s_ ** 3;
55
+ const r = 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s;
56
+ const g = -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s;
57
+ const bl = -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s;
58
+ return [linearToSrgb8(r), linearToSrgb8(g), linearToSrgb8(bl)];
59
+ }
26
60
  function parseRgb(color) {
27
61
  const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());
28
62
  if (hex) {
@@ -33,6 +67,9 @@ function parseRgb(color) {
33
67
  const rgb = /^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/i.exec(color);
34
68
  if (rgb)
35
69
  return [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])];
70
+ const oklch = parseOklch(color);
71
+ if (oklch)
72
+ return oklch;
36
73
  // Named colors, hsl(), and any other CSS color syntax: resolved to
37
74
  // `rgb(...)` via the browser's computed style, then reparsed.
38
75
  if (!isBrowser)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fundar/data-chart-telling",
3
- "version": "0.0.48",
3
+ "version": "0.0.49",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"