@fragiola/primitives 0.0.0-stage → 0.1.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/CHANGELOG.md +63 -0
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/async-list-Bv6ff5Oj.js +81 -0
- package/dist/async-list-Bv6ff5Oj.js.map +1 -0
- package/dist/async-list.d.ts +2 -0
- package/dist/async-list.js +1 -0
- package/dist/calendar-keys-DGYpjLwv.d.ts +12 -0
- package/dist/calendar-system-B5U5HcPQ.d.ts +415 -0
- package/dist/calendar-systems-Daa6Q_ca.js +659 -0
- package/dist/calendar-systems-Daa6Q_ca.js.map +1 -0
- package/dist/calendar-systems.d.ts +3 -0
- package/dist/calendar-systems.js +1 -0
- package/dist/carousel-LRbWqbAx.js +375 -0
- package/dist/carousel-LRbWqbAx.js.map +1 -0
- package/dist/carousel.d.ts +2 -0
- package/dist/carousel.js +1 -0
- package/dist/cascade-B155Xvvv.js +64 -0
- package/dist/cascade-B155Xvvv.js.map +1 -0
- package/dist/cascade.d.ts +2 -0
- package/dist/cascade.js +1 -0
- package/dist/clipboard-Cy2cbGEW.js +33 -0
- package/dist/clipboard-Cy2cbGEW.js.map +1 -0
- package/dist/clipboard.d.ts +2 -0
- package/dist/clipboard.js +1 -0
- package/dist/collection-Dbk-PB5S.js +609 -0
- package/dist/collection-Dbk-PB5S.js.map +1 -0
- package/dist/collection.d.ts +2 -0
- package/dist/collection.js +3 -0
- package/dist/color-sURV5Irg.js +699 -0
- package/dist/color-sURV5Irg.js.map +1 -0
- package/dist/color.d.ts +2 -0
- package/dist/color.js +1 -0
- package/dist/command-DueCXi65.js +104 -0
- package/dist/command-DueCXi65.js.map +1 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +1 -0
- package/dist/date-CrbFSJKQ.js +215 -0
- package/dist/date-CrbFSJKQ.js.map +1 -0
- package/dist/date.d.ts +4 -0
- package/dist/date.js +1 -0
- package/dist/direction-CTnVLpmd.d.ts +8 -0
- package/dist/direction-DJDPvnxJ.js +13 -0
- package/dist/direction-DJDPvnxJ.js.map +1 -0
- package/dist/direction.d.ts +2 -0
- package/dist/direction.js +1 -0
- package/dist/drag-session-CSr06B5v.js +41 -0
- package/dist/drag-session-CSr06B5v.js.map +1 -0
- package/dist/file-DSLtfzbi.js +263 -0
- package/dist/file-DSLtfzbi.js.map +1 -0
- package/dist/file.d.ts +2 -0
- package/dist/file.js +1 -0
- package/dist/focus-BI-VvM0f.js +75 -0
- package/dist/focus-BI-VvM0f.js.map +1 -0
- package/dist/focus.d.ts +2 -0
- package/dist/focus.js +3 -0
- package/dist/form-DovwKtqz.js +501 -0
- package/dist/form-DovwKtqz.js.map +1 -0
- package/dist/form.d.ts +2 -0
- package/dist/form.js +1 -0
- package/dist/hover-BakyPYnC.js +62 -0
- package/dist/hover-BakyPYnC.js.map +1 -0
- package/dist/hover.d.ts +2 -0
- package/dist/hover.js +1 -0
- package/dist/id-B0Y8iZrc.js +13 -0
- package/dist/id-B0Y8iZrc.js.map +1 -0
- package/dist/id.d.ts +2 -0
- package/dist/id.js +1 -0
- package/dist/image-D4X_yRYw.js +44 -0
- package/dist/image-D4X_yRYw.js.map +1 -0
- package/dist/image.d.ts +2 -0
- package/dist/image.js +1 -0
- package/dist/index-B0rwIZw9.d.ts +51 -0
- package/dist/index-B4wF1isn.d.ts +128 -0
- package/dist/index-BHiBRE01.d.ts +20 -0
- package/dist/index-BNlMm-0u.d.ts +17 -0
- package/dist/index-BapNrjY9.d.ts +48 -0
- package/dist/index-Bd-tVb7C.d.ts +6 -0
- package/dist/index-BjE8nDiX.d.ts +125 -0
- package/dist/index-BpbT-FsX.d.ts +117 -0
- package/dist/index-CBgGcTeX.d.ts +37 -0
- package/dist/index-CGThV_Md.d.ts +100 -0
- package/dist/index-CMig3TXL.d.ts +28 -0
- package/dist/index-CWFRduRd.d.ts +212 -0
- package/dist/index-CkrHWyU_.d.ts +33 -0
- package/dist/index-Cwu6f7y1.d.ts +18 -0
- package/dist/index-D10LDdR6.d.ts +139 -0
- package/dist/index-D146vm21.d.ts +104 -0
- package/dist/index-DGKhEvNr.d.ts +107 -0
- package/dist/index-DbAu4s16.d.ts +48 -0
- package/dist/index-DdaCj6IO.d.ts +54 -0
- package/dist/index-DeHcNdjC.d.ts +68 -0
- package/dist/index-DeR4ncrW.d.ts +93 -0
- package/dist/index-HIksgK0e.d.ts +70 -0
- package/dist/index-IZB0mlZl.d.ts +71 -0
- package/dist/index-IfDZj87U.d.ts +28 -0
- package/dist/index-SrTuVBms.d.ts +75 -0
- package/dist/index-T9UcYvkf.d.ts +339 -0
- package/dist/index-fRTojAoR.d.ts +31 -0
- package/dist/index-fuLUke9n.d.ts +6 -0
- package/dist/index-l63esAck.d.ts +104 -0
- package/dist/index-raSUg9U7.d.ts +38 -0
- package/dist/index-twgzPKvZ.d.ts +278 -0
- package/dist/index-wBpjRykh.d.ts +125 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +42 -0
- package/dist/inert-DlGToy3q.js +80 -0
- package/dist/inert-DlGToy3q.js.map +1 -0
- package/dist/inert.d.ts +2 -0
- package/dist/inert.js +1 -0
- package/dist/layer-stack-BEtzaFC1.js +118 -0
- package/dist/layer-stack-BEtzaFC1.js.map +1 -0
- package/dist/layers-C-N4rrZX.js +24 -0
- package/dist/layers-C-N4rrZX.js.map +1 -0
- package/dist/layers.d.ts +2 -0
- package/dist/layers.js +3 -0
- package/dist/list-navigation-CbWv4DK5.js +40 -0
- package/dist/list-navigation-CbWv4DK5.js.map +1 -0
- package/dist/month-year-BG5imFUq.js +108 -0
- package/dist/month-year-BG5imFUq.js.map +1 -0
- package/dist/month-year.d.ts +3 -0
- package/dist/month-year.js +1 -0
- package/dist/number-BFUio97d.js +316 -0
- package/dist/number-BFUio97d.js.map +1 -0
- package/dist/number.d.ts +2 -0
- package/dist/number.js +1 -0
- package/dist/overlay-BwPyiM3T.js +78 -0
- package/dist/overlay-BwPyiM3T.js.map +1 -0
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.js +1 -0
- package/dist/pagination-BkVzaIGP.js +51 -0
- package/dist/pagination-BkVzaIGP.js.map +1 -0
- package/dist/pagination.d.ts +2 -0
- package/dist/pagination.js +1 -0
- package/dist/plain-date-idQH1P6X.d.ts +29 -0
- package/dist/plain-date-time-B8PGahhe.d.ts +99 -0
- package/dist/pointer-CuDtD9XA.js +189 -0
- package/dist/pointer-CuDtD9XA.js.map +1 -0
- package/dist/pointer.d.ts +2 -0
- package/dist/pointer.js +3 -0
- package/dist/positioning-DDUQ75mB.js +148 -0
- package/dist/positioning-DDUQ75mB.js.map +1 -0
- package/dist/positioning.d.ts +2 -0
- package/dist/positioning.js +1 -0
- package/dist/presence-BhKDOmII.js +86 -0
- package/dist/presence-BhKDOmII.js.map +1 -0
- package/dist/presence.d.ts +2 -0
- package/dist/presence.js +1 -0
- package/dist/range-o0_GBhyY.js +158 -0
- package/dist/range-o0_GBhyY.js.map +1 -0
- package/dist/range.d.ts +2 -0
- package/dist/range.js +1 -0
- package/dist/scroll-area-BYNhv36t.js +447 -0
- package/dist/scroll-area-BYNhv36t.js.map +1 -0
- package/dist/scroll-area.d.ts +2 -0
- package/dist/scroll-area.js +1 -0
- package/dist/scroll-lock-JzRNOA1O.js +42 -0
- package/dist/scroll-lock-JzRNOA1O.js.map +1 -0
- package/dist/scroll-lock.d.ts +2 -0
- package/dist/scroll-lock.js +1 -0
- package/dist/segments-eRu5G_Bd.js +587 -0
- package/dist/segments-eRu5G_Bd.js.map +1 -0
- package/dist/segments.d.ts +3 -0
- package/dist/segments.js +1 -0
- package/dist/selection-Cor67w_V.js +287 -0
- package/dist/selection-Cor67w_V.js.map +1 -0
- package/dist/selection.d.ts +2 -0
- package/dist/selection.js +1 -0
- package/dist/swipe.d.ts +2 -0
- package/dist/swipe.js +1 -0
- package/dist/tabbable-BPiLssIv.js +35 -0
- package/dist/tabbable-BPiLssIv.js.map +1 -0
- package/dist/time-Ct6oq9kt.js +208 -0
- package/dist/time-Ct6oq9kt.js.map +1 -0
- package/dist/time-zone-DegkzwST.js +305 -0
- package/dist/time-zone-DegkzwST.js.map +1 -0
- package/dist/time-zone.d.ts +2 -0
- package/dist/time-zone.js +1 -0
- package/dist/time.d.ts +2 -0
- package/dist/time.js +1 -0
- package/dist/toast-BjRpJalM.js +400 -0
- package/dist/toast-BjRpJalM.js.map +1 -0
- package/dist/toast.d.ts +2 -0
- package/dist/toast.js +1 -0
- package/dist/unit-grid-DJy8lb5D.d.ts +21 -0
- package/dist/virtual-BlX1RuHD.js +306 -0
- package/dist/virtual-BlX1RuHD.js.map +1 -0
- package/dist/virtual.d.ts +2 -0
- package/dist/virtual.js +1 -0
- package/dist/watch-swipe-BIucfr9O.js +301 -0
- package/dist/watch-swipe-BIucfr9O.js.map +1 -0
- package/package.json +234 -3
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
// src/color/color-value.ts
|
|
2
|
+
const RANGES = {
|
|
3
|
+
red: {
|
|
4
|
+
min: 0,
|
|
5
|
+
max: 255,
|
|
6
|
+
step: 1,
|
|
7
|
+
largeStep: 17
|
|
8
|
+
},
|
|
9
|
+
green: {
|
|
10
|
+
min: 0,
|
|
11
|
+
max: 255,
|
|
12
|
+
step: 1,
|
|
13
|
+
largeStep: 17
|
|
14
|
+
},
|
|
15
|
+
blue: {
|
|
16
|
+
min: 0,
|
|
17
|
+
max: 255,
|
|
18
|
+
step: 1,
|
|
19
|
+
largeStep: 17
|
|
20
|
+
},
|
|
21
|
+
hue: {
|
|
22
|
+
min: 0,
|
|
23
|
+
max: 360,
|
|
24
|
+
step: 1,
|
|
25
|
+
largeStep: 15
|
|
26
|
+
},
|
|
27
|
+
saturation: {
|
|
28
|
+
min: 0,
|
|
29
|
+
max: 100,
|
|
30
|
+
step: 1,
|
|
31
|
+
largeStep: 10
|
|
32
|
+
},
|
|
33
|
+
lightness: {
|
|
34
|
+
min: 0,
|
|
35
|
+
max: 100,
|
|
36
|
+
step: 1,
|
|
37
|
+
largeStep: 10
|
|
38
|
+
},
|
|
39
|
+
brightness: {
|
|
40
|
+
min: 0,
|
|
41
|
+
max: 100,
|
|
42
|
+
step: 1,
|
|
43
|
+
largeStep: 10
|
|
44
|
+
},
|
|
45
|
+
alpha: {
|
|
46
|
+
min: 0,
|
|
47
|
+
max: 1,
|
|
48
|
+
step: .01,
|
|
49
|
+
largeStep: .1
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const CHANNELS = {
|
|
53
|
+
rgb: [
|
|
54
|
+
"red",
|
|
55
|
+
"green",
|
|
56
|
+
"blue"
|
|
57
|
+
],
|
|
58
|
+
hsl: [
|
|
59
|
+
"hue",
|
|
60
|
+
"saturation",
|
|
61
|
+
"lightness"
|
|
62
|
+
],
|
|
63
|
+
hsb: [
|
|
64
|
+
"hue",
|
|
65
|
+
"saturation",
|
|
66
|
+
"brightness"
|
|
67
|
+
]
|
|
68
|
+
};
|
|
69
|
+
/** A channel's bounds and steps. */
|
|
70
|
+
function colorChannelRange(channel) {
|
|
71
|
+
return { ...RANGES[channel] };
|
|
72
|
+
}
|
|
73
|
+
/** The three color channels of a space, in order (alpha, which every space has, aside). */
|
|
74
|
+
function colorChannels(space) {
|
|
75
|
+
return CHANNELS[space];
|
|
76
|
+
}
|
|
77
|
+
/** Whether `channel` is one of `space`'s (alpha is every space's). */
|
|
78
|
+
function hasColorChannel(space, channel) {
|
|
79
|
+
return channel === "alpha" || CHANNELS[space].includes(channel);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The space a control edits `channel` in (spec/color.md, "Channels"): `preferred` (the control's
|
|
83
|
+
* `colorSpace`) when given, else the value's own space when it has the channel, else the
|
|
84
|
+
* channel's own space (`red`, `green`, `blue`: rgb; `lightness`: hsl; `hue`, `saturation`,
|
|
85
|
+
* `brightness`: hsb).
|
|
86
|
+
*/
|
|
87
|
+
function colorSpaceFor(channel, color, preferred) {
|
|
88
|
+
if (preferred !== void 0) return preferred;
|
|
89
|
+
if (hasColorChannel(color.space, channel)) return color.space;
|
|
90
|
+
if (channel === "red" || channel === "green" || channel === "blue") return "rgb";
|
|
91
|
+
return channel === "lightness" ? "hsl" : "hsb";
|
|
92
|
+
}
|
|
93
|
+
/** A channel's value. `channel` must be one of the color's space (else a `RangeError`). */
|
|
94
|
+
function getColorChannel(color, channel) {
|
|
95
|
+
if (!hasColorChannel(color.space, channel)) throw new RangeError(`a ${color.space} color has no ${channel}`);
|
|
96
|
+
return color[channel];
|
|
97
|
+
}
|
|
98
|
+
/** `value` clamped to the channel's bounds. */
|
|
99
|
+
function clampColorChannel(channel, value) {
|
|
100
|
+
const { min, max } = RANGES[channel];
|
|
101
|
+
return Math.min(Math.max(value, min), max);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A new color with `channel` set to `value`, clamped to its bounds. `channel` must be one of the
|
|
105
|
+
* color's space (else a `RangeError`): convert the color first (`convertColor`).
|
|
106
|
+
*/
|
|
107
|
+
function setColorChannel(color, channel, value) {
|
|
108
|
+
if (!hasColorChannel(color.space, channel)) throw new RangeError(`a ${color.space} color has no ${channel}`);
|
|
109
|
+
return {
|
|
110
|
+
...color,
|
|
111
|
+
[channel]: clampColorChannel(channel, value)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Whether two colors are the same value: the same space and the same channels. */
|
|
115
|
+
function isSameColor(a, b) {
|
|
116
|
+
if (a === b) return true;
|
|
117
|
+
if (a === null || a === void 0 || b === null || b === void 0) return false;
|
|
118
|
+
if (a.space !== b.space || a.alpha !== b.alpha) return false;
|
|
119
|
+
return CHANNELS[a.space].every((channel) => getColorChannel(a, channel) === getColorChannel(b, channel));
|
|
120
|
+
}
|
|
121
|
+
const AREA_DEFAULTS = {
|
|
122
|
+
rgb: ["red", "green"],
|
|
123
|
+
hsl: ["saturation", "lightness"],
|
|
124
|
+
hsb: ["saturation", "brightness"]
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* A ColorArea's space and channels: `colorSpace`, else the value's own space when it has both
|
|
128
|
+
* given channels, else the space of the more specific channel (lightness: hsl; brightness: hsb;
|
|
129
|
+
* red, green, blue: rgb; else hsb); the channels not given are the space's defaults (rgb: red and
|
|
130
|
+
* green; hsl: saturation and lightness; hsb: saturation and brightness).
|
|
131
|
+
*/
|
|
132
|
+
function colorAreaChannels(color, options = {}) {
|
|
133
|
+
const given = [options.xChannel, options.yChannel].filter((channel) => channel !== void 0);
|
|
134
|
+
let space = options.colorSpace;
|
|
135
|
+
if (space === void 0) {
|
|
136
|
+
if (given.every((channel) => CHANNELS[color.space].includes(channel))) space = color.space;
|
|
137
|
+
else if (given.includes("lightness")) space = "hsl";
|
|
138
|
+
else if (given.includes("brightness")) space = "hsb";
|
|
139
|
+
else if (given.some((channel) => CHANNELS.rgb.includes(channel))) space = "rgb";
|
|
140
|
+
else space = "hsb";
|
|
141
|
+
}
|
|
142
|
+
const [x, y] = AREA_DEFAULTS[space];
|
|
143
|
+
const xChannel = options.xChannel ?? (options.yChannel === x ? y : x);
|
|
144
|
+
const yChannel = options.yChannel ?? (xChannel === y ? x : y);
|
|
145
|
+
return {
|
|
146
|
+
space,
|
|
147
|
+
xChannel,
|
|
148
|
+
yChannel
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// src/color/convert-color.ts
|
|
152
|
+
/**
|
|
153
|
+
* The precision of a converted channel (spec/color.md, "Precision"): red, green and blue are
|
|
154
|
+
* whole numbers (8 bits, what a hex string holds), hue, saturation, lightness and brightness keep
|
|
155
|
+
* 2 decimals, alpha 4 (a hex string's 1/255 steps survive the round trip).
|
|
156
|
+
*/
|
|
157
|
+
function roundTo(value, decimals) {
|
|
158
|
+
const factor = 10 ** decimals;
|
|
159
|
+
return Math.round(value * factor) / factor + 0;
|
|
160
|
+
}
|
|
161
|
+
/** A channel from 0 to 1 as a byte; the float noise of `0.5 - 0.4` (25.4999…) cut first. */
|
|
162
|
+
function byte(value) {
|
|
163
|
+
return Math.round(Number((value * 255).toFixed(6)));
|
|
164
|
+
}
|
|
165
|
+
function rgbOf(red, green, blue, alpha) {
|
|
166
|
+
return {
|
|
167
|
+
space: "rgb",
|
|
168
|
+
red: byte(red),
|
|
169
|
+
green: byte(green),
|
|
170
|
+
blue: byte(blue),
|
|
171
|
+
alpha
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function hslToRgb(color) {
|
|
175
|
+
const s = color.saturation / 100;
|
|
176
|
+
const l = color.lightness / 100;
|
|
177
|
+
const amount = s * Math.min(l, 1 - l);
|
|
178
|
+
const at = (n) => {
|
|
179
|
+
const k = (n + color.hue / 30) % 12;
|
|
180
|
+
return l - amount * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
181
|
+
};
|
|
182
|
+
return rgbOf(at(0), at(8), at(4), color.alpha);
|
|
183
|
+
}
|
|
184
|
+
function hsbToRgb(color) {
|
|
185
|
+
const s = color.saturation / 100;
|
|
186
|
+
const v = color.brightness / 100;
|
|
187
|
+
const at = (n) => {
|
|
188
|
+
const k = (n + color.hue / 60) % 6;
|
|
189
|
+
return v - v * s * Math.max(0, Math.min(k, 4 - k, 1));
|
|
190
|
+
};
|
|
191
|
+
return rgbOf(at(5), at(3), at(1), color.alpha);
|
|
192
|
+
}
|
|
193
|
+
/** The hue (degrees), the largest and the smallest of an rgb color's channels (0 to 1). */
|
|
194
|
+
function hueOf$1(color) {
|
|
195
|
+
const r = color.red / 255;
|
|
196
|
+
const g = color.green / 255;
|
|
197
|
+
const b = color.blue / 255;
|
|
198
|
+
const max = Math.max(r, g, b);
|
|
199
|
+
const min = Math.min(r, g, b);
|
|
200
|
+
const delta = max - min;
|
|
201
|
+
let hue = 0;
|
|
202
|
+
if (delta !== 0) {
|
|
203
|
+
if (max === r) hue = (g - b) / delta % 6;
|
|
204
|
+
else if (max === g) hue = (b - r) / delta + 2;
|
|
205
|
+
else hue = (r - g) / delta + 4;
|
|
206
|
+
hue *= 60;
|
|
207
|
+
if (hue < 0) hue += 360;
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
hue: roundTo(hue, 2) % 360,
|
|
211
|
+
max,
|
|
212
|
+
min
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function rgbToHsl(color) {
|
|
216
|
+
const { hue, max, min } = hueOf$1(color);
|
|
217
|
+
const l = (max + min) / 2;
|
|
218
|
+
const delta = max - min;
|
|
219
|
+
return {
|
|
220
|
+
space: "hsl",
|
|
221
|
+
hue,
|
|
222
|
+
saturation: roundTo((delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1))) * 100, 2),
|
|
223
|
+
lightness: roundTo(l * 100, 2),
|
|
224
|
+
alpha: color.alpha
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
function rgbToHsb(color) {
|
|
228
|
+
const { hue, max, min } = hueOf$1(color);
|
|
229
|
+
return {
|
|
230
|
+
space: "hsb",
|
|
231
|
+
hue,
|
|
232
|
+
saturation: roundTo((max === 0 ? 0 : (max - min) / max) * 100, 2),
|
|
233
|
+
brightness: roundTo(max * 100, 2),
|
|
234
|
+
alpha: color.alpha
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function hslToHsb(color) {
|
|
238
|
+
const s = color.saturation / 100;
|
|
239
|
+
const l = color.lightness / 100;
|
|
240
|
+
const v = l + s * Math.min(l, 1 - l);
|
|
241
|
+
return {
|
|
242
|
+
space: "hsb",
|
|
243
|
+
hue: color.hue,
|
|
244
|
+
saturation: roundTo(v === 0 ? 0 : 2 * (1 - l / v) * 100, 2),
|
|
245
|
+
brightness: roundTo(v * 100, 2),
|
|
246
|
+
alpha: color.alpha
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function hsbToHsl(color) {
|
|
250
|
+
const s = color.saturation / 100;
|
|
251
|
+
const v = color.brightness / 100;
|
|
252
|
+
const l = v * (1 - s / 2);
|
|
253
|
+
const lowest = Math.min(l, 1 - l);
|
|
254
|
+
return {
|
|
255
|
+
space: "hsl",
|
|
256
|
+
hue: color.hue,
|
|
257
|
+
saturation: roundTo(lowest === 0 ? 0 : (v - l) / lowest * 100, 2),
|
|
258
|
+
lightness: roundTo(l * 100, 2),
|
|
259
|
+
alpha: color.alpha
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* `color` in `space` (spec/color.md, "Conversion"): the same object when it already is in it;
|
|
264
|
+
* hsl and hsb convert directly (the hue kept), the others through rgb. Each converted channel is
|
|
265
|
+
* rounded to its precision.
|
|
266
|
+
*/
|
|
267
|
+
function convertColor(color, space) {
|
|
268
|
+
if (color.space === space) return color;
|
|
269
|
+
if (color.space === "hsl" && space === "hsb") return hslToHsb(color);
|
|
270
|
+
if (color.space === "hsb" && space === "hsl") return hsbToHsl(color);
|
|
271
|
+
const rgb = color.space === "rgb" ? color : color.space === "hsl" ? hslToRgb(color) : hsbToRgb(color);
|
|
272
|
+
if (space === "rgb") return rgb;
|
|
273
|
+
return space === "hsl" ? rgbToHsl(rgb) : rgbToHsb(rgb);
|
|
274
|
+
}
|
|
275
|
+
// src/color/format-color.ts
|
|
276
|
+
function hexByte(value) {
|
|
277
|
+
return Math.round(value).toString(16).padStart(2, "0");
|
|
278
|
+
}
|
|
279
|
+
function number(value) {
|
|
280
|
+
return String(roundTo(value, 2));
|
|
281
|
+
}
|
|
282
|
+
/** `color` as text, lower-case hex or a CSS function with commas (`rgb(255, 0, 0)`). */
|
|
283
|
+
function formatColor(color, format = "css") {
|
|
284
|
+
if (format === "css") {
|
|
285
|
+
if (color.space === "rgb") return formatColor(color, color.alpha < 1 ? "rgba" : "rgb");
|
|
286
|
+
return formatColor(color, color.alpha < 1 ? "hsla" : "hsl");
|
|
287
|
+
}
|
|
288
|
+
if (format === "hex" || format === "hexa") {
|
|
289
|
+
const rgb = convertColor(color, "rgb");
|
|
290
|
+
const digits = `#${hexByte(rgb.red)}${hexByte(rgb.green)}${hexByte(rgb.blue)}`;
|
|
291
|
+
return format === "hexa" || rgb.alpha < 1 ? `${digits}${hexByte(rgb.alpha * 255)}` : digits;
|
|
292
|
+
}
|
|
293
|
+
const converted = convertColor(color, format.replace(/a$/, ""));
|
|
294
|
+
const alpha = format.endsWith("a") ? `, ${String(roundTo(converted.alpha, 4))}` : "";
|
|
295
|
+
if (converted.space === "rgb") return `${format}(${converted.red}, ${converted.green}, ${converted.blue}${alpha})`;
|
|
296
|
+
const last = converted.space === "hsl" ? converted.lightness : converted.brightness;
|
|
297
|
+
return `${format}(${number(converted.hue)}, ${number(converted.saturation)}%, ${number(last)}%${alpha})`;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* The `Intl.NumberFormat` options a channel's value is written with (spec/color.md, "Channel
|
|
301
|
+
* text"), and the scale its value is multiplied by first: hue in degrees (`unit: "degree"`),
|
|
302
|
+
* saturation, lightness, brightness and alpha as percents, red, green and blue as whole numbers.
|
|
303
|
+
* The digits follow the channel's step.
|
|
304
|
+
*/
|
|
305
|
+
function colorChannelFormat(channel) {
|
|
306
|
+
switch (channel) {
|
|
307
|
+
case "hue": return {
|
|
308
|
+
format: {
|
|
309
|
+
style: "unit",
|
|
310
|
+
unit: "degree",
|
|
311
|
+
unitDisplay: "narrow",
|
|
312
|
+
maximumFractionDigits: 0
|
|
313
|
+
},
|
|
314
|
+
scale: 1
|
|
315
|
+
};
|
|
316
|
+
case "saturation":
|
|
317
|
+
case "lightness":
|
|
318
|
+
case "brightness": return {
|
|
319
|
+
format: {
|
|
320
|
+
style: "percent",
|
|
321
|
+
maximumFractionDigits: 0
|
|
322
|
+
},
|
|
323
|
+
scale: .01
|
|
324
|
+
};
|
|
325
|
+
case "alpha": return {
|
|
326
|
+
format: {
|
|
327
|
+
style: "percent",
|
|
328
|
+
maximumFractionDigits: 0
|
|
329
|
+
},
|
|
330
|
+
scale: 1
|
|
331
|
+
};
|
|
332
|
+
default: return {
|
|
333
|
+
format: { maximumFractionDigits: 0 },
|
|
334
|
+
scale: 1
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* A channel's value as text for `locale` (BCP 47; default: the runtime's), through `Intl`: `"120°"`,
|
|
340
|
+
* `"50%"` (`"50 %"` in de-DE), `"128"`. A value given wins over the color's.
|
|
341
|
+
*/
|
|
342
|
+
function formatColorChannel(color, channel, locale, value = getColorChannel(color, channel)) {
|
|
343
|
+
const { format, scale } = colorChannelFormat(channel);
|
|
344
|
+
return new Intl.NumberFormat(locale, format).format(value * scale);
|
|
345
|
+
}
|
|
346
|
+
// src/color/color-gradient.ts
|
|
347
|
+
/** The hue's stops: the primaries and secondaries, every 60°, where the hue ramp turns. */
|
|
348
|
+
const HUE_STOPS = [
|
|
349
|
+
0,
|
|
350
|
+
60,
|
|
351
|
+
120,
|
|
352
|
+
180,
|
|
353
|
+
240,
|
|
354
|
+
300,
|
|
355
|
+
360
|
|
356
|
+
];
|
|
357
|
+
function stops(colors) {
|
|
358
|
+
return colors.map((color) => formatColor(color, "css")).join(", ");
|
|
359
|
+
}
|
|
360
|
+
/** The colors along `channel`, the color's other channels kept, as linear stops. */
|
|
361
|
+
function channelStops(color, channel) {
|
|
362
|
+
if (channel === "hue") return HUE_STOPS.map((hue) => setColorChannel(color, "hue", hue));
|
|
363
|
+
const { min, max } = colorChannelRange(channel);
|
|
364
|
+
return (channel === "lightness" ? [
|
|
365
|
+
min,
|
|
366
|
+
50,
|
|
367
|
+
max
|
|
368
|
+
] : [min, max]).map((value) => setColorChannel(color, channel, value));
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* The CSS `linear-gradient` of a slider's track (spec/color.md, "Gradients"): `channel` from its
|
|
372
|
+
* `min` to its `max` along `direction`, the color's other channels kept, except the hue track,
|
|
373
|
+
* which shows every hue at full saturation (and, in hsb, full brightness; in hsl, 50% lightness).
|
|
374
|
+
* Every stop is a linear ramp in sRGB, so the gradient is exact. The alpha track keeps the
|
|
375
|
+
* color's other channels, opaque at its end.
|
|
376
|
+
*/
|
|
377
|
+
function colorChannelGradient(color, channel, direction = "to right") {
|
|
378
|
+
if (!hasColorChannel(color.space, channel)) throw new RangeError(`a ${color.space} color has no ${channel}`);
|
|
379
|
+
let base = channel === "alpha" ? color : setColorChannel(color, "alpha", 1);
|
|
380
|
+
if (channel === "hue") base = base.space === "hsl" ? {
|
|
381
|
+
...base,
|
|
382
|
+
saturation: 100,
|
|
383
|
+
lightness: 50
|
|
384
|
+
} : base.space === "hsb" ? {
|
|
385
|
+
...base,
|
|
386
|
+
saturation: 100,
|
|
387
|
+
brightness: 100
|
|
388
|
+
} : base;
|
|
389
|
+
return `linear-gradient(${direction}, ${stops(channelStops(base, channel))})`;
|
|
390
|
+
}
|
|
391
|
+
function gradient(direction, colors) {
|
|
392
|
+
return `linear-gradient(${direction}, ${colors.join(", ")})`;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* A ColorArea's background (spec/color.md, "Gradients"): two gradient layers that give, at every
|
|
396
|
+
* point, the color with `xChannel` and `yChannel` at that point and its third channel kept, exactly
|
|
397
|
+
* (sRGB compositing): two rgb channels add up through `screen`; a lightness or brightness axis is
|
|
398
|
+
* a black (and white) layer over the other channel's ramp; a saturation axis beside a hue axis
|
|
399
|
+
* fades the hue ramp to gray. `x` runs from the inline start (`rtl`: from the right), `y` from
|
|
400
|
+
* the bottom. The channels are two different color channels of the color's space (no alpha).
|
|
401
|
+
*/
|
|
402
|
+
function colorAreaBackground(color, xChannel, yChannel, rtl = false) {
|
|
403
|
+
const space = colorChannels(color.space);
|
|
404
|
+
if (xChannel === yChannel || !space.includes(xChannel) || !space.includes(yChannel)) throw new RangeError(`${xChannel} and ${yChannel} are not two channels of a ${color.space} color`);
|
|
405
|
+
const opaque = setColorChannel(color, "alpha", 1);
|
|
406
|
+
const xDirection = rtl ? "to left" : "to right";
|
|
407
|
+
if (opaque.space === "rgb") {
|
|
408
|
+
const zero = {
|
|
409
|
+
...opaque,
|
|
410
|
+
[xChannel]: 0,
|
|
411
|
+
[yChannel]: 0
|
|
412
|
+
};
|
|
413
|
+
const black = {
|
|
414
|
+
space: "rgb",
|
|
415
|
+
red: 0,
|
|
416
|
+
green: 0,
|
|
417
|
+
blue: 0,
|
|
418
|
+
alpha: 1
|
|
419
|
+
};
|
|
420
|
+
const xMax = setColorChannel(zero, xChannel, 255);
|
|
421
|
+
const yMax = setColorChannel(black, yChannel, 255);
|
|
422
|
+
return {
|
|
423
|
+
backgroundImage: [gradient(xDirection, [formatColor(zero), formatColor(xMax)]), gradient("to top", [formatColor(black), formatColor(yMax)])].join(", "),
|
|
424
|
+
backgroundBlendMode: "screen"
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
const shade = xChannel === "lightness" || xChannel === "brightness" ? "x" : yChannel === "lightness" || yChannel === "brightness" ? "y" : xChannel === "saturation" ? "x" : "y";
|
|
428
|
+
const shadeChannel = shade === "x" ? xChannel : yChannel;
|
|
429
|
+
const rampChannel = shade === "x" ? yChannel : xChannel;
|
|
430
|
+
const shadeDirection = shade === "x" ? xDirection : "to top";
|
|
431
|
+
const rampDirection = shade === "x" ? "to top" : xDirection;
|
|
432
|
+
let overlay;
|
|
433
|
+
let ramp = opaque;
|
|
434
|
+
if (shadeChannel === "brightness") {
|
|
435
|
+
ramp = setColorChannel(opaque, "brightness", 100);
|
|
436
|
+
overlay = gradient(shadeDirection, ["rgb(0, 0, 0)", "rgba(0, 0, 0, 0)"]);
|
|
437
|
+
} else if (shadeChannel === "lightness") {
|
|
438
|
+
ramp = setColorChannel(opaque, "lightness", 50);
|
|
439
|
+
overlay = gradient(shadeDirection, [
|
|
440
|
+
"rgb(0, 0, 0) 0%",
|
|
441
|
+
"rgba(0, 0, 0, 0) 50%",
|
|
442
|
+
"rgba(255, 255, 255, 0) 50%",
|
|
443
|
+
"rgb(255, 255, 255) 100%"
|
|
444
|
+
]);
|
|
445
|
+
} else {
|
|
446
|
+
ramp = setColorChannel(opaque, "saturation", 100);
|
|
447
|
+
overlay = gradient(shadeDirection, [formatColor(setColorChannel(opaque, "saturation", 0)), formatColor(setColorChannel(setColorChannel(opaque, "saturation", 0), "alpha", 0))]);
|
|
448
|
+
}
|
|
449
|
+
const rampStops = rampChannel === "hue" ? channelStops(ramp, "hue") : [setColorChannel(ramp, rampChannel, 0), setColorChannel(ramp, rampChannel, 100)];
|
|
450
|
+
return {
|
|
451
|
+
backgroundImage: [overlay, `linear-gradient(${rampDirection}, ${stops(rampStops)})`].join(", "),
|
|
452
|
+
backgroundBlendMode: "normal"
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* A ColorWheel's track (spec/color.md, "Gradients"): every hue at full saturation, from 0° at the
|
|
457
|
+
* top, clockwise (the `conic-gradient`'s own start and direction).
|
|
458
|
+
*/
|
|
459
|
+
function colorWheelGradient() {
|
|
460
|
+
return `conic-gradient(${stops(HUE_STOPS.map((hue) => ({
|
|
461
|
+
space: "hsl",
|
|
462
|
+
hue,
|
|
463
|
+
saturation: 100,
|
|
464
|
+
lightness: 50,
|
|
465
|
+
alpha: 1
|
|
466
|
+
})))})`;
|
|
467
|
+
}
|
|
468
|
+
// src/color/color-keys.ts
|
|
469
|
+
/**
|
|
470
|
+
* The axis and value a key moves a ColorArea's thumb to (spec/color-area.md, "Keyboard"), before
|
|
471
|
+
* snapping and clamping; `null` when the key does not move it. ArrowLeft and ArrowRight move `x`
|
|
472
|
+
* (swapped in RTL), ArrowUp and ArrowDown `y`, with Shift by the large step; Page Up and Page
|
|
473
|
+
* Down move `y` by its large step; Home and End set `x` to its `min` and `max`.
|
|
474
|
+
*/
|
|
475
|
+
function colorAreaKeyTarget(key, shiftKey, options) {
|
|
476
|
+
const { x, y, xRange, yRange } = options;
|
|
477
|
+
const xAmount = shiftKey ? xRange.largeStep : xRange.step;
|
|
478
|
+
const yAmount = shiftKey ? yRange.largeStep : yRange.step;
|
|
479
|
+
const right = options.rtl ? -1 : 1;
|
|
480
|
+
switch (key) {
|
|
481
|
+
case "ArrowRight": return {
|
|
482
|
+
axis: "x",
|
|
483
|
+
value: x + right * xAmount
|
|
484
|
+
};
|
|
485
|
+
case "ArrowLeft": return {
|
|
486
|
+
axis: "x",
|
|
487
|
+
value: x - right * xAmount
|
|
488
|
+
};
|
|
489
|
+
case "ArrowUp": return {
|
|
490
|
+
axis: "y",
|
|
491
|
+
value: y + yAmount
|
|
492
|
+
};
|
|
493
|
+
case "ArrowDown": return {
|
|
494
|
+
axis: "y",
|
|
495
|
+
value: y - yAmount
|
|
496
|
+
};
|
|
497
|
+
case "PageUp": return {
|
|
498
|
+
axis: "y",
|
|
499
|
+
value: y + yRange.largeStep
|
|
500
|
+
};
|
|
501
|
+
case "PageDown": return {
|
|
502
|
+
axis: "y",
|
|
503
|
+
value: y - yRange.largeStep
|
|
504
|
+
};
|
|
505
|
+
case "Home": return {
|
|
506
|
+
axis: "x",
|
|
507
|
+
value: xRange.min
|
|
508
|
+
};
|
|
509
|
+
case "End": return {
|
|
510
|
+
axis: "x",
|
|
511
|
+
value: xRange.max
|
|
512
|
+
};
|
|
513
|
+
default: return null;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/** `hue` turned into `[0, 360)`, with the hue's step precision (whole degrees by default). */
|
|
517
|
+
function wrapHue(hue, step = 1) {
|
|
518
|
+
const decimals = (String(step).split(".")[1] ?? "").length;
|
|
519
|
+
const snapped = Math.round(hue / step) * step;
|
|
520
|
+
return Number(((snapped % 360 + 360) % 360).toFixed(decimals));
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* The hue a key moves a ColorWheel to (spec/color-wheel.md, "Keyboard"), wrapped into `[0, 360)`;
|
|
524
|
+
* `null` when the key does not move it. ArrowRight and ArrowUp increase it by `step` (Shift: by
|
|
525
|
+
* `largeStep`), ArrowLeft and ArrowDown decrease it; Page Up and Page Down by `largeStep`; Home
|
|
526
|
+
* sets 0 and End the last step before 360.
|
|
527
|
+
*/
|
|
528
|
+
function colorWheelKeyTarget(key, shiftKey, hue, step = 1, largeStep = 15) {
|
|
529
|
+
const amount = shiftKey ? largeStep : step;
|
|
530
|
+
switch (key) {
|
|
531
|
+
case "ArrowRight":
|
|
532
|
+
case "ArrowUp": return wrapHue(hue + amount, step);
|
|
533
|
+
case "ArrowLeft":
|
|
534
|
+
case "ArrowDown": return wrapHue(hue - amount, step);
|
|
535
|
+
case "PageUp": return wrapHue(hue + largeStep, step);
|
|
536
|
+
case "PageDown": return wrapHue(hue - largeStep, step);
|
|
537
|
+
case "Home": return 0;
|
|
538
|
+
case "End": return wrapHue(360 - step, step);
|
|
539
|
+
default: return null;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
// src/color/color-wheel-geometry.ts
|
|
543
|
+
/**
|
|
544
|
+
* The hue at a point of a ColorWheel (spec/color-wheel.md, "Pointer"): its angle around the box's
|
|
545
|
+
* centre, 0° at the top, clockwise, in `[0, 360)`.
|
|
546
|
+
*/
|
|
547
|
+
function hueAtPoint(box, point) {
|
|
548
|
+
const dx = point.x - (box.left + box.width / 2);
|
|
549
|
+
const dy = point.y - (box.top + box.height / 2);
|
|
550
|
+
return (Math.atan2(dx, -dy) * 180 / Math.PI + 360) % 360;
|
|
551
|
+
}
|
|
552
|
+
/** Whether a point lies on a ColorWheel's ring: between the inner and the outer radius (px). */
|
|
553
|
+
function isOnColorWheel(box, point, innerRadius, outerRadius) {
|
|
554
|
+
const dx = point.x - (box.left + box.width / 2);
|
|
555
|
+
const dy = point.y - (box.top + box.height / 2);
|
|
556
|
+
const distance = Math.hypot(dx, dy);
|
|
557
|
+
return distance >= innerRadius && distance <= outerRadius;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Where a ColorWheel's thumb sits for `hue`: its offset (px) from the wheel's centre, on the
|
|
561
|
+
* circle of `radius` (the ring's middle), 0° at the top, clockwise.
|
|
562
|
+
*/
|
|
563
|
+
function colorWheelThumbOffset(hue, radius) {
|
|
564
|
+
const radians = hue * Math.PI / 180;
|
|
565
|
+
const round = (value) => Math.round(value * 100) / 100 + 0;
|
|
566
|
+
return {
|
|
567
|
+
x: round(radius * Math.sin(radians)),
|
|
568
|
+
y: round(-radius * Math.cos(radians))
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
// src/color/parse-color.ts
|
|
572
|
+
const HEX = /^#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
573
|
+
const FUNCTION = /^(rgba?|hsla?|hsba?)\(\s*([^()]*?)\s*\)$/i;
|
|
574
|
+
const NUMBER = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i;
|
|
575
|
+
function parseHex(digits) {
|
|
576
|
+
const full = digits.length <= 4 ? [...digits].map((digit) => digit + digit).join("") : digits;
|
|
577
|
+
const byte = (index) => Number.parseInt(full.slice(index * 2, index * 2 + 2), 16);
|
|
578
|
+
return {
|
|
579
|
+
space: "rgb",
|
|
580
|
+
red: byte(0),
|
|
581
|
+
green: byte(1),
|
|
582
|
+
blue: byte(2),
|
|
583
|
+
alpha: full.length === 8 ? roundTo(byte(3) / 255, 4) : 1
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
/** A number, with the suffix it may carry (`%`, `deg`), or `null`. */
|
|
587
|
+
function numberOf(text, suffix) {
|
|
588
|
+
let body = text;
|
|
589
|
+
if (suffix !== "" && body.toLowerCase().endsWith(suffix)) body = body.slice(0, -suffix.length);
|
|
590
|
+
return NUMBER.test(body) ? Number(body) : null;
|
|
591
|
+
}
|
|
592
|
+
/** An alpha: a number from 0 to 1, or a percent. */
|
|
593
|
+
function alphaOf(text) {
|
|
594
|
+
if (text === void 0) return 1;
|
|
595
|
+
const value = text.endsWith("%") ? (numberOf(text, "%") ?? NaN) / 100 : numberOf(text, "");
|
|
596
|
+
return value === null || Number.isNaN(value) ? null : roundTo(clampColorChannel("alpha", value), 4);
|
|
597
|
+
}
|
|
598
|
+
/** The arguments of `rgb(…)`, `hsl(…)` or `hsb(…)`: commas, or spaces with a `/` before alpha. */
|
|
599
|
+
function argumentsOf(body) {
|
|
600
|
+
if (body.includes(",")) {
|
|
601
|
+
const parts = body.split(",").map((part) => part.trim());
|
|
602
|
+
return parts.length === 3 || parts.length === 4 ? parts : null;
|
|
603
|
+
}
|
|
604
|
+
const [channels = "", alpha, ...rest] = body.split("/");
|
|
605
|
+
if (rest.length > 0) return null;
|
|
606
|
+
const parts = channels.trim().split(/\s+/);
|
|
607
|
+
if (parts.length !== 3) return null;
|
|
608
|
+
return alpha === void 0 ? parts : [...parts, alpha.trim()];
|
|
609
|
+
}
|
|
610
|
+
/** A hue in degrees, turned into `[0, 360)`. */
|
|
611
|
+
function hueOf(text) {
|
|
612
|
+
const value = numberOf(text, "deg");
|
|
613
|
+
return value === null ? null : roundTo((value % 360 + 360) % 360, 2);
|
|
614
|
+
}
|
|
615
|
+
function parseFunction(name, body) {
|
|
616
|
+
const parts = argumentsOf(body);
|
|
617
|
+
if (parts === null) return null;
|
|
618
|
+
const [first = "", second = "", third = "", fourth] = parts;
|
|
619
|
+
const alpha = alphaOf(fourth);
|
|
620
|
+
if (alpha === null) return null;
|
|
621
|
+
const space = name.toLowerCase().replace(/a$/, "");
|
|
622
|
+
if (space === "rgb") {
|
|
623
|
+
const [red, green, blue] = [
|
|
624
|
+
first,
|
|
625
|
+
second,
|
|
626
|
+
third
|
|
627
|
+
].map((part) => {
|
|
628
|
+
const value = numberOf(part, "");
|
|
629
|
+
return value === null ? null : Math.round(clampColorChannel("red", value));
|
|
630
|
+
});
|
|
631
|
+
if (red == null || green == null || blue == null) return null;
|
|
632
|
+
return {
|
|
633
|
+
space: "rgb",
|
|
634
|
+
red,
|
|
635
|
+
green,
|
|
636
|
+
blue,
|
|
637
|
+
alpha
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
const hue = hueOf(first);
|
|
641
|
+
const saturation = numberOf(second, "%");
|
|
642
|
+
const last = numberOf(third, "%");
|
|
643
|
+
if (hue === null || saturation === null || last === null) return null;
|
|
644
|
+
const percent = (value) => roundTo(clampColorChannel("saturation", value), 2);
|
|
645
|
+
return space === "hsl" ? {
|
|
646
|
+
space: "hsl",
|
|
647
|
+
hue,
|
|
648
|
+
saturation: percent(saturation),
|
|
649
|
+
lightness: percent(last),
|
|
650
|
+
alpha
|
|
651
|
+
} : {
|
|
652
|
+
space: "hsb",
|
|
653
|
+
hue,
|
|
654
|
+
saturation: percent(saturation),
|
|
655
|
+
brightness: percent(last),
|
|
656
|
+
alpha
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* A color from its text (spec/color.md, "Parsing"): hex (`#rgb`, `#rgba`, `#rrggbb`,
|
|
661
|
+
* `#rrggbbaa`, the `#` optional), `rgb()` / `rgba()`, `hsl()` / `hsla()` and `hsb()` / `hsba()`
|
|
662
|
+
* with commas or spaces (`/` before the alpha); `null` when it is none of them. A color given is
|
|
663
|
+
* returned as it is, so a component can take a `Color` or a string.
|
|
664
|
+
*/
|
|
665
|
+
function parseColor(value) {
|
|
666
|
+
if (typeof value !== "string") return value;
|
|
667
|
+
const text = value.trim();
|
|
668
|
+
const hex = HEX.exec(text);
|
|
669
|
+
if (hex !== null) return parseHex(hex[1]);
|
|
670
|
+
const call = FUNCTION.exec(text);
|
|
671
|
+
if (call === null) return null;
|
|
672
|
+
return parseFunction(call[1], call[2]);
|
|
673
|
+
}
|
|
674
|
+
// src/color/eye-dropper.ts
|
|
675
|
+
function eyeDropperOf(element) {
|
|
676
|
+
const api = element.ownerDocument.defaultView?.EyeDropper;
|
|
677
|
+
return typeof api === "function" ? api : void 0;
|
|
678
|
+
}
|
|
679
|
+
/** Whether `element`'s window has the EyeDropper API (spec/color-picker.md, "EyeDropperTrigger"). */
|
|
680
|
+
function supportsEyeDropper(element) {
|
|
681
|
+
return eyeDropperOf(element) !== void 0;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Opens the browser's eyedropper from `element`'s window and resolves with the color the user
|
|
685
|
+
* picked (an opaque rgb color), or `null` when the user cancelled it (Escape), the browser refused
|
|
686
|
+
* it or has no EyeDropper API. It never rejects: a cancelled pick is no error for a picker.
|
|
687
|
+
*/
|
|
688
|
+
async function openEyeDropper(element, signal) {
|
|
689
|
+
const EyeDropper = eyeDropperOf(element);
|
|
690
|
+
if (EyeDropper === void 0) return null;
|
|
691
|
+
try {
|
|
692
|
+
return parseColor((await new EyeDropper().open(signal === void 0 ? void 0 : { signal })).sRGBHex);
|
|
693
|
+
} catch {
|
|
694
|
+
return null;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
export { hasColorChannel as C, getColorChannel as S, setColorChannel as T, clampColorChannel as _, hueAtPoint as a, colorChannels as b, colorWheelKeyTarget as c, colorChannelGradient as d, colorWheelGradient as f, convertColor as g, formatColorChannel as h, colorWheelThumbOffset as i, wrapHue as l, formatColor as m, supportsEyeDropper as n, isOnColorWheel as o, colorChannelFormat as p, parseColor as r, colorAreaKeyTarget as s, openEyeDropper as t, colorAreaBackground as u, colorAreaChannels as v, isSameColor as w, colorSpaceFor as x, colorChannelRange as y };
|
|
698
|
+
|
|
699
|
+
//# sourceMappingURL=color-sURV5Irg.js.map
|