@tangible/ui 0.1.0 → 0.2.1
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/components/Button/types.d.ts +5 -2
- package/components/Chip/Chip.d.ts +28 -0
- package/components/Chip/Chip.js +19 -3
- package/components/ColorPicker/ColorArea.d.ts +24 -0
- package/components/ColorPicker/ColorArea.js +62 -0
- package/components/ColorPicker/ColorField.d.ts +59 -0
- package/components/ColorPicker/ColorField.js +88 -0
- package/components/ColorPicker/ColorPicker.d.ts +36 -0
- package/components/ColorPicker/ColorPicker.js +215 -0
- package/components/ColorPicker/ColorSlider.d.ts +20 -0
- package/components/ColorPicker/ColorSlider.js +38 -0
- package/components/ColorPicker/index.d.ts +14 -0
- package/components/ColorPicker/index.js +7 -0
- package/components/ColorPicker/labels.d.ts +42 -0
- package/components/ColorPicker/labels.js +38 -0
- package/components/ColorPicker/tracks.d.ts +4 -0
- package/components/ColorPicker/tracks.js +4 -0
- package/components/ColorPicker/usePointerDrag.d.ts +13 -0
- package/components/ColorPicker/usePointerDrag.js +34 -0
- package/components/Combobox/Combobox.d.ts +1 -1
- package/components/Combobox/Combobox.js +2 -2
- package/components/Combobox/types.d.ts +13 -0
- package/components/IconButton/types.d.ts +2 -2
- package/components/MultiCombobox/MultiCombobox.d.ts +33 -0
- package/components/MultiCombobox/MultiCombobox.js +595 -0
- package/components/MultiCombobox/MultiComboboxContext.d.ts +8 -0
- package/components/MultiCombobox/MultiComboboxContext.js +36 -0
- package/components/MultiCombobox/index.d.ts +3 -0
- package/components/MultiCombobox/index.js +2 -0
- package/components/MultiCombobox/types.d.ts +244 -0
- package/components/MultiCombobox/types.js +7 -0
- package/components/MultiSelect/MultiSelect.js +22 -2
- package/components/Popover/index.d.ts +1 -1
- package/components/Select/Select.js +14 -0
- package/components/Slider/Slider.d.ts +2 -0
- package/components/Slider/Slider.js +29 -0
- package/components/Slider/index.d.ts +2 -0
- package/components/Slider/index.js +1 -0
- package/components/Slider/types.d.ts +39 -0
- package/components/Slider/types.js +1 -0
- package/components/index.d.ts +6 -0
- package/components/index.js +3 -0
- package/icons/icons.svg +1 -0
- package/icons/manifest.json +8 -0
- package/icons/registry.d.ts +2 -0
- package/icons/registry.js +1 -0
- package/icons/system/index.d.ts +2 -0
- package/icons/system/index.js +11 -0
- package/package.json +13 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +1063 -133
- package/styles/all.expanded.unlayered.css +1063 -133
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +6 -0
- package/styles/components/button.scss +45 -35
- package/styles/components/chip.scss +57 -0
- package/styles/components/color-picker.scss +378 -0
- package/styles/components/combobox.scss +16 -2
- package/styles/components/icon-button.scss +32 -26
- package/styles/components/input/index.scss +1 -2
- package/styles/components/multi-combobox.scss +457 -0
- package/styles/components/multi-select.scss +34 -18
- package/styles/components/pager.scss +4 -2
- package/styles/components/progress.scss +1 -1
- package/styles/components/segmented-control.scss +44 -18
- package/styles/components/select.scss +19 -3
- package/styles/components/slider.scss +184 -0
- package/styles/components/switch.scss +1 -1
- package/styles/core.css +1 -1
- package/styles/core.unlayered.css +1 -1
- package/styles/system/_tokens.scss +13 -0
- package/tui-manifest.json +307 -8
- package/utils/color/color.d.ts +61 -0
- package/utils/color/color.js +293 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type Rgb = {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
};
|
|
6
|
+
export type Hsv = {
|
|
7
|
+
h: number;
|
|
8
|
+
s: number;
|
|
9
|
+
v: number;
|
|
10
|
+
};
|
|
11
|
+
export type Hsva = Hsv & {
|
|
12
|
+
a: number;
|
|
13
|
+
};
|
|
14
|
+
export type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'hwb' | 'oklch';
|
|
15
|
+
export declare const COLOR_FORMATS: ColorFormat[];
|
|
16
|
+
/** A parsed picker value: a concrete color, a CSS variable reference, or invalid. */
|
|
17
|
+
export type ParsedColor = {
|
|
18
|
+
kind: 'color';
|
|
19
|
+
hsva: Hsva;
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'var';
|
|
22
|
+
raw: string;
|
|
23
|
+
name: string;
|
|
24
|
+
fallback?: string;
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'invalid';
|
|
27
|
+
raw: string;
|
|
28
|
+
};
|
|
29
|
+
export declare function hsvToRgb({ h, s, v }: Hsv): Rgb;
|
|
30
|
+
export declare function rgbToHsv({ r, g, b }: Rgb): Hsv;
|
|
31
|
+
export declare function rgbToHex({ r, g, b }: Rgb, a?: number): string;
|
|
32
|
+
export declare function hexToRgba(hex: string): Hsva | null;
|
|
33
|
+
export declare function rgbToHsl({ r, g, b }: Rgb): {
|
|
34
|
+
h: number;
|
|
35
|
+
s: number;
|
|
36
|
+
l: number;
|
|
37
|
+
};
|
|
38
|
+
export declare function hslToRgb(h: number, s: number, l: number): Rgb;
|
|
39
|
+
export declare function rgbToHwb({ r, g, b }: Rgb): {
|
|
40
|
+
h: number;
|
|
41
|
+
w: number;
|
|
42
|
+
b: number;
|
|
43
|
+
};
|
|
44
|
+
export declare function hwbToRgb(h: number, w: number, bl: number): Rgb;
|
|
45
|
+
export declare function rgbToOklch({ r, g, b }: Rgb): {
|
|
46
|
+
l: number;
|
|
47
|
+
c: number;
|
|
48
|
+
h: number;
|
|
49
|
+
};
|
|
50
|
+
export declare function oklchToRgb(L: number, C: number, H: number): Rgb;
|
|
51
|
+
export declare function parseColor(value: string | null | undefined): ParsedColor;
|
|
52
|
+
export declare function formatColor(hsva: Hsva, format: ColorFormat): string;
|
|
53
|
+
/**
|
|
54
|
+
* Try to resolve a `var(--x)` reference to a concrete color for the swatch. Uses
|
|
55
|
+
* getComputedStyle if a DOM element is available and the var is set; otherwise
|
|
56
|
+
* falls back to the var's own fallback, then null. NEVER throws and never
|
|
57
|
+
* requires the variable to be present.
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolveVarColor(parsed: Extract<ParsedColor, {
|
|
60
|
+
kind: 'var';
|
|
61
|
+
}>, el?: Element | null): string | null;
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// =============================================================================
|
|
2
|
+
// Color math for the ColorPicker
|
|
3
|
+
// =============================================================================
|
|
4
|
+
//
|
|
5
|
+
// Internal model is HSVA (matches the 2D area + hue/alpha sliders). Everything
|
|
6
|
+
// converts through sRGB. Formats: hex, rgb, hsl, hwb, oklch (selectable).
|
|
7
|
+
//
|
|
8
|
+
// A picker value is a CSS string. It may be a concrete color OR a `var(--x)`
|
|
9
|
+
// reference — variables are stored/emitted verbatim and are NOT required to
|
|
10
|
+
// resolve in the DOM (the field is often edited in an admin context where the
|
|
11
|
+
// theme var isn't present). resolveVarColor() attempts resolution opportunist-
|
|
12
|
+
// ically and falls back gracefully.
|
|
13
|
+
//
|
|
14
|
+
// OKLab/OKLCH matrices follow Björn Ottosson's reference.
|
|
15
|
+
// =============================================================================
|
|
16
|
+
export const COLOR_FORMATS = ['hex', 'rgb', 'hsl', 'hwb', 'oklch'];
|
|
17
|
+
const clamp01 = (n) => Math.min(1, Math.max(0, n));
|
|
18
|
+
const mod360 = (n) => ((n % 360) + 360) % 360;
|
|
19
|
+
const round = (n, dp = 0) => {
|
|
20
|
+
const f = 10 ** dp;
|
|
21
|
+
return Math.round(n * f) / f;
|
|
22
|
+
};
|
|
23
|
+
// -----------------------------------------------------------------------------
|
|
24
|
+
// HSV <-> RGB
|
|
25
|
+
// -----------------------------------------------------------------------------
|
|
26
|
+
export function hsvToRgb({ h, s, v }) {
|
|
27
|
+
h = mod360(h) / 60;
|
|
28
|
+
const c = v * s;
|
|
29
|
+
const x = c * (1 - Math.abs((h % 2) - 1));
|
|
30
|
+
const m = v - c;
|
|
31
|
+
let r = 0, g = 0, b = 0;
|
|
32
|
+
if (h < 1)
|
|
33
|
+
[r, g, b] = [c, x, 0];
|
|
34
|
+
else if (h < 2)
|
|
35
|
+
[r, g, b] = [x, c, 0];
|
|
36
|
+
else if (h < 3)
|
|
37
|
+
[r, g, b] = [0, c, x];
|
|
38
|
+
else if (h < 4)
|
|
39
|
+
[r, g, b] = [0, x, c];
|
|
40
|
+
else if (h < 5)
|
|
41
|
+
[r, g, b] = [x, 0, c];
|
|
42
|
+
else
|
|
43
|
+
[r, g, b] = [c, 0, x];
|
|
44
|
+
return { r: r + m, g: g + m, b: b + m };
|
|
45
|
+
}
|
|
46
|
+
export function rgbToHsv({ r, g, b }) {
|
|
47
|
+
const max = Math.max(r, g, b);
|
|
48
|
+
const min = Math.min(r, g, b);
|
|
49
|
+
const d = max - min;
|
|
50
|
+
let h = 0;
|
|
51
|
+
if (d !== 0) {
|
|
52
|
+
if (max === r)
|
|
53
|
+
h = ((g - b) / d) % 6;
|
|
54
|
+
else if (max === g)
|
|
55
|
+
h = (b - r) / d + 2;
|
|
56
|
+
else
|
|
57
|
+
h = (r - g) / d + 4;
|
|
58
|
+
h *= 60;
|
|
59
|
+
}
|
|
60
|
+
return { h: mod360(h), s: max === 0 ? 0 : d / max, v: max };
|
|
61
|
+
}
|
|
62
|
+
// -----------------------------------------------------------------------------
|
|
63
|
+
// RGB <-> Hex
|
|
64
|
+
// -----------------------------------------------------------------------------
|
|
65
|
+
const toHex2 = (n) => Math.round(clamp01(n) * 255)
|
|
66
|
+
.toString(16)
|
|
67
|
+
.padStart(2, '0');
|
|
68
|
+
export function rgbToHex({ r, g, b }, a = 1) {
|
|
69
|
+
const base = `#${toHex2(r)}${toHex2(g)}${toHex2(b)}`;
|
|
70
|
+
return a < 1 ? `${base}${toHex2(a)}` : base;
|
|
71
|
+
}
|
|
72
|
+
export function hexToRgba(hex) {
|
|
73
|
+
let h = hex.trim().replace(/^#/, '');
|
|
74
|
+
if (/^[0-9a-f]{3,4}$/i.test(h))
|
|
75
|
+
h = h.split('').map((c) => c + c).join('');
|
|
76
|
+
if (!/^[0-9a-f]{6}$|^[0-9a-f]{8}$/i.test(h))
|
|
77
|
+
return null;
|
|
78
|
+
const r = parseInt(h.slice(0, 2), 16) / 255;
|
|
79
|
+
const g = parseInt(h.slice(2, 4), 16) / 255;
|
|
80
|
+
const b = parseInt(h.slice(4, 6), 16) / 255;
|
|
81
|
+
const a = h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1;
|
|
82
|
+
return { ...rgbToHsv({ r, g, b }), a };
|
|
83
|
+
}
|
|
84
|
+
// -----------------------------------------------------------------------------
|
|
85
|
+
// RGB <-> HSL
|
|
86
|
+
// -----------------------------------------------------------------------------
|
|
87
|
+
export function rgbToHsl({ r, g, b }) {
|
|
88
|
+
const { h } = rgbToHsv({ r, g, b });
|
|
89
|
+
const max = Math.max(r, g, b);
|
|
90
|
+
const min = Math.min(r, g, b);
|
|
91
|
+
const l = (max + min) / 2;
|
|
92
|
+
const d = max - min;
|
|
93
|
+
const s = d === 0 ? 0 : d / (1 - Math.abs(2 * l - 1));
|
|
94
|
+
return { h, s, l };
|
|
95
|
+
}
|
|
96
|
+
export function hslToRgb(h, s, l) {
|
|
97
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
98
|
+
const hp = mod360(h) / 60;
|
|
99
|
+
const x = c * (1 - Math.abs((hp % 2) - 1));
|
|
100
|
+
const m = l - c / 2;
|
|
101
|
+
let r = 0, g = 0, b = 0;
|
|
102
|
+
if (hp < 1)
|
|
103
|
+
[r, g, b] = [c, x, 0];
|
|
104
|
+
else if (hp < 2)
|
|
105
|
+
[r, g, b] = [x, c, 0];
|
|
106
|
+
else if (hp < 3)
|
|
107
|
+
[r, g, b] = [0, c, x];
|
|
108
|
+
else if (hp < 4)
|
|
109
|
+
[r, g, b] = [0, x, c];
|
|
110
|
+
else if (hp < 5)
|
|
111
|
+
[r, g, b] = [x, 0, c];
|
|
112
|
+
else
|
|
113
|
+
[r, g, b] = [c, 0, x];
|
|
114
|
+
return { r: r + m, g: g + m, b: b + m };
|
|
115
|
+
}
|
|
116
|
+
// -----------------------------------------------------------------------------
|
|
117
|
+
// RGB <-> HWB
|
|
118
|
+
// -----------------------------------------------------------------------------
|
|
119
|
+
export function rgbToHwb({ r, g, b }) {
|
|
120
|
+
const { h } = rgbToHsv({ r, g, b });
|
|
121
|
+
const w = Math.min(r, g, b);
|
|
122
|
+
const bl = 1 - Math.max(r, g, b);
|
|
123
|
+
return { h, w, b: bl };
|
|
124
|
+
}
|
|
125
|
+
export function hwbToRgb(h, w, bl) {
|
|
126
|
+
if (w + bl >= 1) {
|
|
127
|
+
const gray = w / (w + bl);
|
|
128
|
+
return { r: gray, g: gray, b: gray };
|
|
129
|
+
}
|
|
130
|
+
const base = hsvToRgb({ h, s: 1, v: 1 });
|
|
131
|
+
const scale = (c) => c * (1 - w - bl) + w;
|
|
132
|
+
return { r: scale(base.r), g: scale(base.g), b: scale(base.b) };
|
|
133
|
+
}
|
|
134
|
+
// -----------------------------------------------------------------------------
|
|
135
|
+
// RGB <-> OKLCH (via OKLab — Björn Ottosson)
|
|
136
|
+
// -----------------------------------------------------------------------------
|
|
137
|
+
const srgbToLinear = (c) => c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
138
|
+
const linearToSrgb = (c) => c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055;
|
|
139
|
+
export function rgbToOklch({ r, g, b }) {
|
|
140
|
+
const lr = srgbToLinear(r);
|
|
141
|
+
const lg = srgbToLinear(g);
|
|
142
|
+
const lb = srgbToLinear(b);
|
|
143
|
+
const l = 0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb;
|
|
144
|
+
const m = 0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb;
|
|
145
|
+
const s = 0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb;
|
|
146
|
+
const l_ = Math.cbrt(l);
|
|
147
|
+
const m_ = Math.cbrt(m);
|
|
148
|
+
const s_ = Math.cbrt(s);
|
|
149
|
+
const L = 0.2104542553 * l_ + 0.793617785 * m_ - 0.0040720468 * s_;
|
|
150
|
+
const A = 1.9779984951 * l_ - 2.428592205 * m_ + 0.4505937099 * s_;
|
|
151
|
+
const B = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.808675766 * s_;
|
|
152
|
+
const C = Math.sqrt(A * A + B * B);
|
|
153
|
+
const H = C < 1e-6 ? 0 : mod360((Math.atan2(B, A) * 180) / Math.PI);
|
|
154
|
+
return { l: L, c: C, h: H };
|
|
155
|
+
}
|
|
156
|
+
export function oklchToRgb(L, C, H) {
|
|
157
|
+
const hr = (mod360(H) * Math.PI) / 180;
|
|
158
|
+
const A = C * Math.cos(hr);
|
|
159
|
+
const B = C * Math.sin(hr);
|
|
160
|
+
const l_ = L + 0.3963377774 * A + 0.2158037573 * B;
|
|
161
|
+
const m_ = L - 0.1055613458 * A - 0.0638541728 * B;
|
|
162
|
+
const s_ = L - 0.0894841775 * A - 1.291485548 * B;
|
|
163
|
+
const l = l_ ** 3;
|
|
164
|
+
const m = m_ ** 3;
|
|
165
|
+
const s = s_ ** 3;
|
|
166
|
+
const lr = 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s;
|
|
167
|
+
const lg = -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s;
|
|
168
|
+
const lb = -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s;
|
|
169
|
+
return {
|
|
170
|
+
r: clamp01(linearToSrgb(lr)),
|
|
171
|
+
g: clamp01(linearToSrgb(lg)),
|
|
172
|
+
b: clamp01(linearToSrgb(lb)),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
// -----------------------------------------------------------------------------
|
|
176
|
+
// Parse (concrete color OR var reference)
|
|
177
|
+
// -----------------------------------------------------------------------------
|
|
178
|
+
/** Split the arguments of a functional color, tolerant of `/` alpha and commas. */
|
|
179
|
+
const argsOf = (body) => body
|
|
180
|
+
.replace(/\//g, ' ')
|
|
181
|
+
.split(/[\s,]+/)
|
|
182
|
+
.filter(Boolean);
|
|
183
|
+
const pct = (s) => (s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s));
|
|
184
|
+
const alpha = (s) => (s == null ? 1 : clamp01(pct(s)));
|
|
185
|
+
export function parseColor(value) {
|
|
186
|
+
const raw = String(value ?? '').trim();
|
|
187
|
+
if (!raw)
|
|
188
|
+
return { kind: 'invalid', raw };
|
|
189
|
+
// CSS variable reference — stored verbatim, not required to resolve.
|
|
190
|
+
const varMatch = raw.match(/^var\(\s*(--[A-Za-z0-9-_]+)\s*(?:,\s*([\s\S]+?)\s*)?\)$/);
|
|
191
|
+
if (varMatch) {
|
|
192
|
+
return { kind: 'var', raw, name: varMatch[1], fallback: varMatch[2] };
|
|
193
|
+
}
|
|
194
|
+
// Hex
|
|
195
|
+
if (raw.startsWith('#')) {
|
|
196
|
+
const hsva = hexToRgba(raw);
|
|
197
|
+
return hsva ? { kind: 'color', hsva } : { kind: 'invalid', raw };
|
|
198
|
+
}
|
|
199
|
+
const fn = raw.match(/^([a-z]+)\(([^)]*)\)$/i);
|
|
200
|
+
if (fn) {
|
|
201
|
+
const name = fn[1].toLowerCase();
|
|
202
|
+
const p = argsOf(fn[2]);
|
|
203
|
+
try {
|
|
204
|
+
if (name === 'rgb' || name === 'rgba') {
|
|
205
|
+
const [r, g, b, a] = p;
|
|
206
|
+
const to01 = (s) => (s.endsWith('%') ? pct(s) : parseFloat(s) / 255);
|
|
207
|
+
return {
|
|
208
|
+
kind: 'color',
|
|
209
|
+
hsva: { ...rgbToHsv({ r: to01(r), g: to01(g), b: to01(b) }), a: alpha(a) },
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (name === 'hsl' || name === 'hsla') {
|
|
213
|
+
const [h, s, l, a] = p;
|
|
214
|
+
return {
|
|
215
|
+
kind: 'color',
|
|
216
|
+
hsva: { ...rgbToHsv(hslToRgb(parseFloat(h), pct(s), pct(l))), a: alpha(a) },
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
if (name === 'hwb') {
|
|
220
|
+
const [h, w, b, a] = p;
|
|
221
|
+
return {
|
|
222
|
+
kind: 'color',
|
|
223
|
+
hsva: { ...rgbToHsv(hwbToRgb(parseFloat(h), pct(w), pct(b))), a: alpha(a) },
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (name === 'oklch') {
|
|
227
|
+
const [l, c, h, a] = p;
|
|
228
|
+
return {
|
|
229
|
+
kind: 'color',
|
|
230
|
+
hsva: { ...rgbToHsv(oklchToRgb(pct(l), parseFloat(c), parseFloat(h))), a: alpha(a) },
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return { kind: 'invalid', raw };
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return { kind: 'invalid', raw };
|
|
239
|
+
}
|
|
240
|
+
// -----------------------------------------------------------------------------
|
|
241
|
+
// Format (HSVA -> string in a given format)
|
|
242
|
+
// -----------------------------------------------------------------------------
|
|
243
|
+
export function formatColor(hsva, format) {
|
|
244
|
+
const { a } = hsva;
|
|
245
|
+
const rgb = hsvToRgb(hsva);
|
|
246
|
+
switch (format) {
|
|
247
|
+
case 'hex':
|
|
248
|
+
return rgbToHex(rgb, a).toUpperCase();
|
|
249
|
+
case 'rgb': {
|
|
250
|
+
const r = round(rgb.r * 255);
|
|
251
|
+
const g = round(rgb.g * 255);
|
|
252
|
+
const b = round(rgb.b * 255);
|
|
253
|
+
return a < 1 ? `rgb(${r} ${g} ${b} / ${round(a, 2)})` : `rgb(${r} ${g} ${b})`;
|
|
254
|
+
}
|
|
255
|
+
case 'hsl': {
|
|
256
|
+
const { h, s, l } = rgbToHsl(rgb);
|
|
257
|
+
const base = `hsl(${round(h)} ${round(s * 100)}% ${round(l * 100)}%`;
|
|
258
|
+
return a < 1 ? `${base} / ${round(a, 2)})` : `${base})`;
|
|
259
|
+
}
|
|
260
|
+
case 'hwb': {
|
|
261
|
+
const { h, w, b } = rgbToHwb(rgb);
|
|
262
|
+
const base = `hwb(${round(h)} ${round(w * 100)}% ${round(b * 100)}%`;
|
|
263
|
+
return a < 1 ? `${base} / ${round(a, 2)})` : `${base})`;
|
|
264
|
+
}
|
|
265
|
+
case 'oklch': {
|
|
266
|
+
const { l, c, h } = rgbToOklch(rgb);
|
|
267
|
+
const base = `oklch(${round(l, 3)} ${round(c, 3)} ${round(h, 1)}`;
|
|
268
|
+
return a < 1 ? `${base} / ${round(a, 2)})` : `${base})`;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// -----------------------------------------------------------------------------
|
|
273
|
+
// Var resolution (opportunistic — never required)
|
|
274
|
+
// -----------------------------------------------------------------------------
|
|
275
|
+
/**
|
|
276
|
+
* Try to resolve a `var(--x)` reference to a concrete color for the swatch. Uses
|
|
277
|
+
* getComputedStyle if a DOM element is available and the var is set; otherwise
|
|
278
|
+
* falls back to the var's own fallback, then null. NEVER throws and never
|
|
279
|
+
* requires the variable to be present.
|
|
280
|
+
*/
|
|
281
|
+
export function resolveVarColor(parsed, el) {
|
|
282
|
+
try {
|
|
283
|
+
if (el && typeof getComputedStyle === 'function') {
|
|
284
|
+
const resolved = getComputedStyle(el).getPropertyValue(parsed.name).trim();
|
|
285
|
+
if (resolved)
|
|
286
|
+
return resolved;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
/* ignore — resolution is best-effort */
|
|
291
|
+
}
|
|
292
|
+
return parsed.fallback ?? null;
|
|
293
|
+
}
|