@tangible/ui 0.2.0 → 0.2.2
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/Card/Card.d.ts +18 -1
- package/components/Card/Card.js +41 -4
- package/components/Card/index.d.ts +1 -1
- 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/Popover/index.d.ts +1 -1
- package/components/index.d.ts +4 -1
- package/components/index.js +1 -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 +5 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +327 -8
- package/styles/all.expanded.unlayered.css +327 -8
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +2 -0
- package/styles/components/card.scss +94 -13
- package/styles/components/color-picker.scss +378 -0
- package/tui-manifest.json +117 -10
- package/utils/color/color.d.ts +61 -0
- package/utils/color/color.js +293 -0
package/tui-manifest.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
3
|
-
"generated": "2026-07-
|
|
2
|
+
"version": "0.2.2",
|
|
3
|
+
"generated": "2026-07-31T19:35:57.774Z",
|
|
4
4
|
"components": {
|
|
5
5
|
"Accordion": {
|
|
6
6
|
"props": {
|
|
@@ -374,13 +374,27 @@
|
|
|
374
374
|
},
|
|
375
375
|
"Card": {
|
|
376
376
|
"props": {
|
|
377
|
-
"
|
|
378
|
-
"type": "
|
|
379
|
-
"required": false
|
|
377
|
+
"variant": {
|
|
378
|
+
"type": "\"elevated\" | \"solid\" | \"outline\" | \"ghost\" | \"seamless\"",
|
|
379
|
+
"required": false,
|
|
380
|
+
"defaultValue": "'solid'",
|
|
381
|
+
"description": "Surface treatment."
|
|
382
|
+
},
|
|
383
|
+
"mode": {
|
|
384
|
+
"type": "\"inline\" | \"stacked\"",
|
|
385
|
+
"required": false,
|
|
386
|
+
"defaultValue": "'stacked'",
|
|
387
|
+
"description": "Content arrangement."
|
|
380
388
|
},
|
|
381
389
|
"elevated": {
|
|
382
390
|
"type": "boolean",
|
|
383
|
-
"required": false
|
|
391
|
+
"required": false,
|
|
392
|
+
"description": "@deprecated Use `variant=\"elevated\"`."
|
|
393
|
+
},
|
|
394
|
+
"inline": {
|
|
395
|
+
"type": "boolean",
|
|
396
|
+
"required": false,
|
|
397
|
+
"description": "@deprecated Use `mode=\"inline\"`."
|
|
384
398
|
},
|
|
385
399
|
"interactive": {
|
|
386
400
|
"type": "boolean",
|
|
@@ -407,11 +421,11 @@
|
|
|
407
421
|
"required": false,
|
|
408
422
|
"description": "@deprecated Use Card.Link for interactive cards. onClick without Card.Link has no keyboard/screen-reader semantics."
|
|
409
423
|
},
|
|
410
|
-
"
|
|
424
|
+
"id": {
|
|
411
425
|
"type": "string",
|
|
412
426
|
"required": false
|
|
413
427
|
},
|
|
414
|
-
"
|
|
428
|
+
"title": {
|
|
415
429
|
"type": "string",
|
|
416
430
|
"required": false
|
|
417
431
|
},
|
|
@@ -430,12 +444,15 @@
|
|
|
430
444
|
},
|
|
431
445
|
"cssTokens": [
|
|
432
446
|
"--tui-card-bg",
|
|
447
|
+
"--tui-card-bg-interact",
|
|
433
448
|
"--tui-card-border",
|
|
434
449
|
"--tui-card-border-width",
|
|
435
450
|
"--tui-card-border-interact",
|
|
451
|
+
"--tui-card-border-active",
|
|
436
452
|
"--tui-card-radius",
|
|
437
453
|
"--tui-card-shadow",
|
|
438
454
|
"--tui-card-shadow-interact",
|
|
455
|
+
"--tui-card-gap",
|
|
439
456
|
"--tui-card-padding",
|
|
440
457
|
"--tui-card-foot-basis"
|
|
441
458
|
],
|
|
@@ -799,6 +816,96 @@
|
|
|
799
816
|
"use": null,
|
|
800
817
|
"notFor": null
|
|
801
818
|
},
|
|
819
|
+
"ColorPicker": {
|
|
820
|
+
"props": {
|
|
821
|
+
"value": {
|
|
822
|
+
"type": "string",
|
|
823
|
+
"required": false,
|
|
824
|
+
"description": "Controlled value — a CSS colour OR a `var(--x[, fallback])` reference."
|
|
825
|
+
},
|
|
826
|
+
"onValueChange": {
|
|
827
|
+
"type": "(value: string) => void",
|
|
828
|
+
"required": false
|
|
829
|
+
},
|
|
830
|
+
"format": {
|
|
831
|
+
"type": "\"hex\" | \"rgb\" | \"hsl\" | \"hwb\" | \"oklch\"",
|
|
832
|
+
"required": false,
|
|
833
|
+
"description": "Controlled format for the value field."
|
|
834
|
+
},
|
|
835
|
+
"defaultFormat": {
|
|
836
|
+
"type": "\"hex\" | \"rgb\" | \"hsl\" | \"hwb\" | \"oklch\"",
|
|
837
|
+
"required": false,
|
|
838
|
+
"defaultValue": "hex",
|
|
839
|
+
"description": "Uncontrolled initial format."
|
|
840
|
+
},
|
|
841
|
+
"formats": {
|
|
842
|
+
"type": "ColorFormat[]",
|
|
843
|
+
"required": false,
|
|
844
|
+
"defaultValue": "['hex','rgb','hsl','hwb','oklch']",
|
|
845
|
+
"description": "Formats offered in the format selector, in order. The selector is hidden\nwhen only one is supplied."
|
|
846
|
+
},
|
|
847
|
+
"alpha": {
|
|
848
|
+
"type": "boolean",
|
|
849
|
+
"required": false,
|
|
850
|
+
"defaultValue": "true",
|
|
851
|
+
"description": "Show the alpha slider (and keep alpha in the emitted value)."
|
|
852
|
+
},
|
|
853
|
+
"disabled": {
|
|
854
|
+
"type": "boolean",
|
|
855
|
+
"required": false
|
|
856
|
+
},
|
|
857
|
+
"labels": {
|
|
858
|
+
"type": "ColorPickerLabels",
|
|
859
|
+
"required": false,
|
|
860
|
+
"description": "Override any user-facing string for i18n."
|
|
861
|
+
},
|
|
862
|
+
"className": {
|
|
863
|
+
"type": "string",
|
|
864
|
+
"required": false
|
|
865
|
+
},
|
|
866
|
+
"id": {
|
|
867
|
+
"type": "string",
|
|
868
|
+
"required": false
|
|
869
|
+
},
|
|
870
|
+
"style": {
|
|
871
|
+
"type": "CSSProperties",
|
|
872
|
+
"required": false
|
|
873
|
+
},
|
|
874
|
+
"title": {
|
|
875
|
+
"type": "string",
|
|
876
|
+
"required": false
|
|
877
|
+
},
|
|
878
|
+
"rel": {
|
|
879
|
+
"type": "string",
|
|
880
|
+
"required": false
|
|
881
|
+
},
|
|
882
|
+
"onClick": {
|
|
883
|
+
"type": "MouseEventHandler<HTMLDivElement>",
|
|
884
|
+
"required": false
|
|
885
|
+
}
|
|
886
|
+
},
|
|
887
|
+
"cssTokens": [
|
|
888
|
+
"--tui-color-area-radius",
|
|
889
|
+
"--tui-color-area-min-height",
|
|
890
|
+
"--tui-color-area-thumb-size",
|
|
891
|
+
"--tui-color-slider-radius",
|
|
892
|
+
"--tui-color-slider-height",
|
|
893
|
+
"--tui-color-slider-thumb-size"
|
|
894
|
+
],
|
|
895
|
+
"story": {
|
|
896
|
+
"title": "Forms/ColorPicker",
|
|
897
|
+
"tags": [
|
|
898
|
+
"autodocs"
|
|
899
|
+
]
|
|
900
|
+
},
|
|
901
|
+
"subcomponents": null,
|
|
902
|
+
"description": null,
|
|
903
|
+
"gotchas": null,
|
|
904
|
+
"examples": null,
|
|
905
|
+
"related": null,
|
|
906
|
+
"use": null,
|
|
907
|
+
"notFor": null
|
|
908
|
+
},
|
|
802
909
|
"Combobox": {
|
|
803
910
|
"props": {
|
|
804
911
|
"size": {
|
|
@@ -3324,9 +3431,9 @@
|
|
|
3324
3431
|
"membersync": 22,
|
|
3325
3432
|
"player": 24,
|
|
3326
3433
|
"reaction": 36,
|
|
3327
|
-
"system":
|
|
3434
|
+
"system": 95
|
|
3328
3435
|
},
|
|
3329
|
-
"total":
|
|
3436
|
+
"total": 227,
|
|
3330
3437
|
"manifest": "./icons/manifest.json"
|
|
3331
3438
|
}
|
|
3332
3439
|
}
|
|
@@ -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
|
+
}
|