@barqjs/aria 0.0.0-stage → 0.2.0-next.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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -2
  3. package/package.json +347 -4
  4. package/src/breadcrumbs.tsx +296 -0
  5. package/src/button.tsx +342 -0
  6. package/src/calendar.tsx +1389 -0
  7. package/src/checkbox.tsx +570 -0
  8. package/src/collections.ts +976 -0
  9. package/src/color.ts +562 -0
  10. package/src/colorpicker.tsx +1478 -0
  11. package/src/combobox.tsx +2481 -0
  12. package/src/command.tsx +1184 -0
  13. package/src/date.ts +652 -0
  14. package/src/datefield.tsx +951 -0
  15. package/src/datepicker.tsx +556 -0
  16. package/src/dialog.tsx +890 -0
  17. package/src/disclosure.tsx +659 -0
  18. package/src/dom.ts +706 -0
  19. package/src/drawer.ts +674 -0
  20. package/src/focus.ts +1370 -0
  21. package/src/form.tsx +94 -0
  22. package/src/gridlist.tsx +563 -0
  23. package/src/i18n.ts +823 -0
  24. package/src/index.ts +1270 -0
  25. package/src/interactions/description.ts +73 -0
  26. package/src/interactions/flags.ts +35 -0
  27. package/src/interactions/focus-events.ts +218 -0
  28. package/src/interactions/focusable.ts +86 -0
  29. package/src/interactions/hover.ts +213 -0
  30. package/src/interactions/interact-outside.ts +123 -0
  31. package/src/interactions/keyboard.ts +105 -0
  32. package/src/interactions/listeners.ts +78 -0
  33. package/src/interactions/long-press.ts +160 -0
  34. package/src/interactions/modality.ts +416 -0
  35. package/src/interactions/move.ts +257 -0
  36. package/src/interactions/open-link.ts +193 -0
  37. package/src/interactions/press.ts +898 -0
  38. package/src/interactions/prevent-focus.ts +102 -0
  39. package/src/interactions/scroll-wheel.ts +43 -0
  40. package/src/interactions/text-selection.ts +77 -0
  41. package/src/interactions.ts +81 -0
  42. package/src/label.ts +128 -0
  43. package/src/link.tsx +381 -0
  44. package/src/listbox.tsx +721 -0
  45. package/src/live.ts +204 -0
  46. package/src/menu.tsx +2290 -0
  47. package/src/menubar.tsx +575 -0
  48. package/src/navigation.ts +735 -0
  49. package/src/numberfield.tsx +838 -0
  50. package/src/otp.ts +430 -0
  51. package/src/overlays.ts +1247 -0
  52. package/src/platform.ts +74 -0
  53. package/src/presence.ts +164 -0
  54. package/src/questionnaire.tsx +1712 -0
  55. package/src/radio.tsx +567 -0
  56. package/src/register-dom.ts +12 -0
  57. package/src/scroller.ts +1165 -0
  58. package/src/select.tsx +1665 -0
  59. package/src/selection.ts +1415 -0
  60. package/src/slider.tsx +1042 -0
  61. package/src/splitter.ts +1152 -0
  62. package/src/static-collection.ts +211 -0
  63. package/src/switch.tsx +186 -0
  64. package/src/table.tsx +0 -0
  65. package/src/tabs.tsx +1018 -0
  66. package/src/tag.tsx +452 -0
  67. package/src/textfield.tsx +417 -0
  68. package/src/toast.ts +658 -0
  69. package/src/toggle-input.tsx +48 -0
  70. package/src/toggle.ts +321 -0
  71. package/src/togglegroup.ts +365 -0
  72. package/src/toolbar.tsx +179 -0
  73. package/src/tooltip.tsx +657 -0
  74. package/src/utils.ts +758 -0
  75. package/src/validation.ts +447 -0
  76. package/src/virtualizer.tsx +674 -0
package/src/color.ts ADDED
@@ -0,0 +1,562 @@
1
+ /**
2
+ * Colours as values, in the three spaces a picker needs.
3
+ *
4
+ * A colour picker is not one control but four — an area, a hue slider, an
5
+ * alpha slider, a hex field — and each edits a different SPACE. Dragging in a
6
+ * saturation/brightness area is HSB; the hue wheel is HSL or HSB; the field is
7
+ * hex, which is RGB. So a colour has to be convertible between all three
8
+ * without drifting, and that means:
9
+ *
10
+ * - **Hue survives a round trip through grey.** Converting `hsb(210, 0%, 50%)`
11
+ * to RGB loses the hue — every grey is the same three numbers — and back
12
+ * again would answer 0. So a conversion carries the hue rather than
13
+ * recomputing it, and dragging saturation to zero and back does not spin the
14
+ * wheel to red.
15
+ * - **Every channel knows its own range and step.** A hue is 0–360 and wraps;
16
+ * a saturation is 0–100; a red is 0–255. A slider that assumed 0–100 for all
17
+ * of them would give the wrong value for two of the three.
18
+ * - **A colour is immutable.** `withChannelValue` answers a new one, so a
19
+ * drag can hold the colour it started from and compare by content.
20
+ */
21
+
22
+ export type ColorSpace = "rgb" | "hsl" | "hsb";
23
+
24
+ export type ColorChannel =
25
+ | "hue"
26
+ | "saturation"
27
+ | "brightness"
28
+ | "lightness"
29
+ | "red"
30
+ | "green"
31
+ | "blue"
32
+ | "alpha";
33
+
34
+ export type ColorFormat = "hex" | "hexa" | "rgb" | "rgba" | "hsl" | "hsla" | "hsb" | "hsba";
35
+
36
+ export interface ColorChannelRange {
37
+ minValue: number;
38
+ maxValue: number;
39
+ step: number;
40
+ /** How far Page Up and Page Down move. */
41
+ pageSize: number;
42
+ }
43
+
44
+ const RANGES: Record<ColorChannel, ColorChannelRange> = {
45
+ hue: { minValue: 0, maxValue: 360, step: 1, pageSize: 15 },
46
+ saturation: { minValue: 0, maxValue: 100, step: 1, pageSize: 10 },
47
+ brightness: { minValue: 0, maxValue: 100, step: 1, pageSize: 10 },
48
+ lightness: { minValue: 0, maxValue: 100, step: 1, pageSize: 10 },
49
+ red: { minValue: 0, maxValue: 255, step: 1, pageSize: 17 },
50
+ green: { minValue: 0, maxValue: 255, step: 1, pageSize: 17 },
51
+ blue: { minValue: 0, maxValue: 255, step: 1, pageSize: 17 },
52
+ alpha: { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 },
53
+ };
54
+
55
+ const CHANNEL_NAMES: Record<ColorChannel, string> = {
56
+ hue: "Hue",
57
+ saturation: "Saturation",
58
+ brightness: "Brightness",
59
+ lightness: "Lightness",
60
+ red: "Red",
61
+ green: "Green",
62
+ blue: "Blue",
63
+ alpha: "Alpha",
64
+ };
65
+
66
+ function clamp(value: number, min: number, max: number): number {
67
+ return Math.min(Math.max(value, min), max);
68
+ }
69
+
70
+ function round(value: number, places = 0): number {
71
+ const scale = 10 ** places;
72
+ return Math.round(value * scale) / scale;
73
+ }
74
+
75
+ /** A colour, in whichever space it was written in. */
76
+ export abstract class Color {
77
+ abstract readonly alpha: number;
78
+ abstract getColorSpace(): ColorSpace;
79
+ abstract toFormat(format: ColorFormat): Color;
80
+ abstract toString(format?: ColorFormat | "css"): string;
81
+ abstract getChannelValue(channel: ColorChannel): number;
82
+ abstract withChannelValue(channel: ColorChannel, value: number): Color;
83
+ abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
84
+
85
+ getChannelRange(channel: ColorChannel): ColorChannelRange {
86
+ return RANGES[channel];
87
+ }
88
+
89
+ getChannelName(channel: ColorChannel): string {
90
+ return CHANNEL_NAMES[channel];
91
+ }
92
+
93
+ /** The colour as a 24-bit integer, for a canvas or a comparison. */
94
+ toHexInt(): number {
95
+ const rgb = this.toFormat("rgb") as RGBColor;
96
+ return (rgb.red << 16) | (rgb.green << 8) | rgb.blue;
97
+ }
98
+
99
+ /** Whether two colours are the same colour, whatever they were written in. */
100
+ isEqual(other: Color): boolean {
101
+ return this.toString("rgba") === other.toString("rgba");
102
+ }
103
+ }
104
+
105
+ // ---------------------------------------------------------------------------
106
+ // The three spaces
107
+ // ---------------------------------------------------------------------------
108
+
109
+ export class RGBColor extends Color {
110
+ readonly red: number;
111
+ readonly green: number;
112
+ readonly blue: number;
113
+ readonly alpha: number;
114
+ /**
115
+ * The hue this colour came from, when it came from one.
116
+ *
117
+ * A grey has no hue to recover — 0, 0, 0 is every hue at once — so a
118
+ * conversion out of HSL or HSB carries it, and dragging saturation to zero
119
+ * and back does not spin the wheel to red.
120
+ */
121
+ readonly #hue: number;
122
+
123
+ constructor(red: number, green: number, blue: number, alpha = 1, hue = 0) {
124
+ super();
125
+ this.red = clamp(Math.round(red), 0, 255);
126
+ this.green = clamp(Math.round(green), 0, 255);
127
+ this.blue = clamp(Math.round(blue), 0, 255);
128
+ this.alpha = clamp(alpha, 0, 1);
129
+ this.#hue = hue;
130
+ }
131
+
132
+ get sourceHue(): number {
133
+ return this.#hue;
134
+ }
135
+
136
+ getColorSpace(): ColorSpace {
137
+ return "rgb";
138
+ }
139
+
140
+ getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
141
+ return ["red", "green", "blue"];
142
+ }
143
+
144
+ getChannelValue(channel: ColorChannel): number {
145
+ switch (channel) {
146
+ case "red":
147
+ return this.red;
148
+ case "green":
149
+ return this.green;
150
+ case "blue":
151
+ return this.blue;
152
+ case "alpha":
153
+ return this.alpha;
154
+ default:
155
+ return this.toFormat(channel === "brightness" ? "hsb" : "hsl").getChannelValue(channel);
156
+ }
157
+ }
158
+
159
+ withChannelValue(channel: ColorChannel, value: number): Color {
160
+ const range = RANGES[channel];
161
+ const next = clamp(value, range.minValue, range.maxValue);
162
+ switch (channel) {
163
+ case "red":
164
+ return new RGBColor(next, this.green, this.blue, this.alpha, this.#hue);
165
+ case "green":
166
+ return new RGBColor(this.red, next, this.blue, this.alpha, this.#hue);
167
+ case "blue":
168
+ return new RGBColor(this.red, this.green, next, this.alpha, this.#hue);
169
+ case "alpha":
170
+ return new RGBColor(this.red, this.green, this.blue, next, this.#hue);
171
+ default:
172
+ return this.toFormat(channel === "brightness" ? "hsb" : "hsl")
173
+ .withChannelValue(channel, next)
174
+ .toFormat("rgb");
175
+ }
176
+ }
177
+
178
+ toFormat(format: ColorFormat): Color {
179
+ switch (format) {
180
+ case "hsl":
181
+ case "hsla":
182
+ return rgbToHsl(this);
183
+ case "hsb":
184
+ case "hsba":
185
+ return rgbToHsb(this);
186
+ default:
187
+ return this;
188
+ }
189
+ }
190
+
191
+ toString(format: ColorFormat | "css" = "css"): string {
192
+ switch (format) {
193
+ case "hex":
194
+ return `#${hex(this.red)}${hex(this.green)}${hex(this.blue)}`;
195
+ case "hexa":
196
+ return `#${hex(this.red)}${hex(this.green)}${hex(this.blue)}${hex(
197
+ Math.round(this.alpha * 255),
198
+ )}`;
199
+ case "rgb":
200
+ return `rgb(${this.red}, ${this.green}, ${this.blue})`;
201
+ case "css":
202
+ case "rgba":
203
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${round(this.alpha, 2)})`;
204
+ default:
205
+ return this.toFormat(format).toString(format);
206
+ }
207
+ }
208
+ }
209
+
210
+ export class HSLColor extends Color {
211
+ readonly hue: number;
212
+ readonly saturation: number;
213
+ readonly lightness: number;
214
+ readonly alpha: number;
215
+
216
+ constructor(hue: number, saturation: number, lightness: number, alpha = 1) {
217
+ super();
218
+ // Wraps rather than clamps: a hue is an angle, and 370 is 10.
219
+ this.hue = ((hue % 360) + 360) % 360;
220
+ this.saturation = clamp(saturation, 0, 100);
221
+ this.lightness = clamp(lightness, 0, 100);
222
+ this.alpha = clamp(alpha, 0, 1);
223
+ }
224
+
225
+ getColorSpace(): ColorSpace {
226
+ return "hsl";
227
+ }
228
+
229
+ getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
230
+ return ["hue", "saturation", "lightness"];
231
+ }
232
+
233
+ getChannelValue(channel: ColorChannel): number {
234
+ switch (channel) {
235
+ case "hue":
236
+ return this.hue;
237
+ case "saturation":
238
+ return this.saturation;
239
+ case "lightness":
240
+ return this.lightness;
241
+ case "alpha":
242
+ return this.alpha;
243
+ default:
244
+ return (this.toFormat("rgb") as RGBColor).getChannelValue(channel);
245
+ }
246
+ }
247
+
248
+ withChannelValue(channel: ColorChannel, value: number): Color {
249
+ const range = RANGES[channel];
250
+ const next = channel === "hue" ? value : clamp(value, range.minValue, range.maxValue);
251
+ switch (channel) {
252
+ case "hue":
253
+ return new HSLColor(next, this.saturation, this.lightness, this.alpha);
254
+ case "saturation":
255
+ return new HSLColor(this.hue, next, this.lightness, this.alpha);
256
+ case "lightness":
257
+ return new HSLColor(this.hue, this.saturation, next, this.alpha);
258
+ case "alpha":
259
+ return new HSLColor(this.hue, this.saturation, this.lightness, next);
260
+ default:
261
+ return (this.toFormat("rgb") as RGBColor).withChannelValue(channel, next).toFormat("hsl");
262
+ }
263
+ }
264
+
265
+ toFormat(format: ColorFormat): Color {
266
+ switch (format) {
267
+ case "hsl":
268
+ case "hsla":
269
+ return this;
270
+ case "hsb":
271
+ case "hsba":
272
+ return hslToHsb(this);
273
+ default:
274
+ return hslToRgb(this);
275
+ }
276
+ }
277
+
278
+ toString(format: ColorFormat | "css" = "css"): string {
279
+ switch (format) {
280
+ case "hsl":
281
+ return `hsl(${round(this.hue, 2)}, ${round(this.saturation, 2)}%, ${round(this.lightness, 2)}%)`;
282
+ case "css":
283
+ case "hsla":
284
+ return `hsla(${round(this.hue, 2)}, ${round(this.saturation, 2)}%, ${round(this.lightness, 2)}%, ${round(this.alpha, 2)})`;
285
+ default:
286
+ return this.toFormat(format).toString(format);
287
+ }
288
+ }
289
+ }
290
+
291
+ export class HSBColor extends Color {
292
+ readonly hue: number;
293
+ readonly saturation: number;
294
+ readonly brightness: number;
295
+ readonly alpha: number;
296
+
297
+ constructor(hue: number, saturation: number, brightness: number, alpha = 1) {
298
+ super();
299
+ this.hue = ((hue % 360) + 360) % 360;
300
+ this.saturation = clamp(saturation, 0, 100);
301
+ this.brightness = clamp(brightness, 0, 100);
302
+ this.alpha = clamp(alpha, 0, 1);
303
+ }
304
+
305
+ getColorSpace(): ColorSpace {
306
+ return "hsb";
307
+ }
308
+
309
+ getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
310
+ return ["hue", "saturation", "brightness"];
311
+ }
312
+
313
+ getChannelValue(channel: ColorChannel): number {
314
+ switch (channel) {
315
+ case "hue":
316
+ return this.hue;
317
+ case "saturation":
318
+ return this.saturation;
319
+ case "brightness":
320
+ return this.brightness;
321
+ case "alpha":
322
+ return this.alpha;
323
+ default:
324
+ return (this.toFormat("rgb") as RGBColor).getChannelValue(channel);
325
+ }
326
+ }
327
+
328
+ withChannelValue(channel: ColorChannel, value: number): Color {
329
+ const range = RANGES[channel];
330
+ const next = channel === "hue" ? value : clamp(value, range.minValue, range.maxValue);
331
+ switch (channel) {
332
+ case "hue":
333
+ return new HSBColor(next, this.saturation, this.brightness, this.alpha);
334
+ case "saturation":
335
+ return new HSBColor(this.hue, next, this.brightness, this.alpha);
336
+ case "brightness":
337
+ return new HSBColor(this.hue, this.saturation, next, this.alpha);
338
+ case "alpha":
339
+ return new HSBColor(this.hue, this.saturation, this.brightness, next);
340
+ default:
341
+ return (this.toFormat("rgb") as RGBColor).withChannelValue(channel, next).toFormat("hsb");
342
+ }
343
+ }
344
+
345
+ toFormat(format: ColorFormat): Color {
346
+ switch (format) {
347
+ case "hsb":
348
+ case "hsba":
349
+ return this;
350
+ case "hsl":
351
+ case "hsla":
352
+ return hsbToHsl(this);
353
+ default:
354
+ return hsbToRgb(this);
355
+ }
356
+ }
357
+
358
+ toString(format: ColorFormat | "css" = "css"): string {
359
+ switch (format) {
360
+ case "hsb":
361
+ return `hsb(${round(this.hue, 2)}, ${round(this.saturation, 2)}%, ${round(this.brightness, 2)}%)`;
362
+ case "hsba":
363
+ return `hsba(${round(this.hue, 2)}, ${round(this.saturation, 2)}%, ${round(this.brightness, 2)}%, ${round(this.alpha, 2)})`;
364
+ // `hsb` is not a CSS colour function, so the CSS form is the HSL one.
365
+ case "css":
366
+ return this.toFormat("hsla").toString("hsla");
367
+ default:
368
+ return this.toFormat(format).toString(format);
369
+ }
370
+ }
371
+ }
372
+
373
+ // ---------------------------------------------------------------------------
374
+ // Conversions
375
+ // ---------------------------------------------------------------------------
376
+
377
+ function hex(value: number): string {
378
+ return clamp(Math.round(value), 0, 255).toString(16).padStart(2, "0");
379
+ }
380
+
381
+ function rgbToHsl(color: RGBColor): HSLColor {
382
+ const red = color.red / 255;
383
+ const green = color.green / 255;
384
+ const blue = color.blue / 255;
385
+ const max = Math.max(red, green, blue);
386
+ const min = Math.min(red, green, blue);
387
+ const lightness = (max + min) / 2;
388
+
389
+ if (max === min) {
390
+ // Grey: the hue it CAME from, not zero.
391
+ return new HSLColor(color.sourceHue, 0, lightness * 100, color.alpha);
392
+ }
393
+
394
+ const delta = max - min;
395
+ const saturation = lightness > 0.5 ? delta / (2 - max - min) : delta / (max + min);
396
+ let hue: number;
397
+ if (max === red) hue = ((green - blue) / delta + (green < blue ? 6 : 0)) * 60;
398
+ else if (max === green) hue = ((blue - red) / delta + 2) * 60;
399
+ else hue = ((red - green) / delta + 4) * 60;
400
+
401
+ return new HSLColor(hue, saturation * 100, lightness * 100, color.alpha);
402
+ }
403
+
404
+ function hslToRgb(color: HSLColor): RGBColor {
405
+ const hue = color.hue / 360;
406
+ const saturation = color.saturation / 100;
407
+ const lightness = color.lightness / 100;
408
+
409
+ if (saturation === 0) {
410
+ const grey = lightness * 255;
411
+ return new RGBColor(grey, grey, grey, color.alpha, color.hue);
412
+ }
413
+
414
+ const q =
415
+ lightness < 0.5
416
+ ? lightness * (1 + saturation)
417
+ : lightness + saturation - lightness * saturation;
418
+ const p = 2 * lightness - q;
419
+ const channel = (t: number): number => {
420
+ let at = t;
421
+ if (at < 0) at += 1;
422
+ if (at > 1) at -= 1;
423
+ if (at < 1 / 6) return p + (q - p) * 6 * at;
424
+ if (at < 1 / 2) return q;
425
+ if (at < 2 / 3) return p + (q - p) * (2 / 3 - at) * 6;
426
+ return p;
427
+ };
428
+
429
+ return new RGBColor(
430
+ channel(hue + 1 / 3) * 255,
431
+ channel(hue) * 255,
432
+ channel(hue - 1 / 3) * 255,
433
+ color.alpha,
434
+ color.hue,
435
+ );
436
+ }
437
+
438
+ function rgbToHsb(color: RGBColor): HSBColor {
439
+ const red = color.red / 255;
440
+ const green = color.green / 255;
441
+ const blue = color.blue / 255;
442
+ const max = Math.max(red, green, blue);
443
+ const min = Math.min(red, green, blue);
444
+ const delta = max - min;
445
+
446
+ if (delta === 0) return new HSBColor(color.sourceHue, 0, max * 100, color.alpha);
447
+
448
+ let hue: number;
449
+ if (max === red) hue = ((green - blue) / delta + (green < blue ? 6 : 0)) * 60;
450
+ else if (max === green) hue = ((blue - red) / delta + 2) * 60;
451
+ else hue = ((red - green) / delta + 4) * 60;
452
+
453
+ return new HSBColor(hue, (delta / max) * 100, max * 100, color.alpha);
454
+ }
455
+
456
+ function hsbToRgb(color: HSBColor): RGBColor {
457
+ const hue = color.hue / 60;
458
+ const saturation = color.saturation / 100;
459
+ const brightness = color.brightness / 100;
460
+
461
+ const sector = Math.floor(hue) % 6;
462
+ const fraction = hue - Math.floor(hue);
463
+ const p = brightness * (1 - saturation);
464
+ const q = brightness * (1 - fraction * saturation);
465
+ const t = brightness * (1 - (1 - fraction) * saturation);
466
+
467
+ const triples: [number, number, number][] = [
468
+ [brightness, t, p],
469
+ [q, brightness, p],
470
+ [p, brightness, t],
471
+ [p, q, brightness],
472
+ [t, p, brightness],
473
+ [brightness, p, q],
474
+ ];
475
+ const [red = 0, green = 0, blue = 0] = triples[sector] ?? [];
476
+
477
+ return new RGBColor(red * 255, green * 255, blue * 255, color.alpha, color.hue);
478
+ }
479
+
480
+ function hsbToHsl(color: HSBColor): HSLColor {
481
+ const brightness = color.brightness / 100;
482
+ const saturation = color.saturation / 100;
483
+ const lightness = brightness * (1 - saturation / 2);
484
+ const denominator = Math.min(lightness, 1 - lightness);
485
+ const hslSaturation = denominator === 0 ? 0 : (brightness - lightness) / denominator;
486
+ return new HSLColor(color.hue, hslSaturation * 100, lightness * 100, color.alpha);
487
+ }
488
+
489
+ function hslToHsb(color: HSLColor): HSBColor {
490
+ const lightness = color.lightness / 100;
491
+ const saturation = color.saturation / 100;
492
+ const brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
493
+ const hsbSaturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
494
+ return new HSBColor(color.hue, hsbSaturation * 100, brightness * 100, color.alpha);
495
+ }
496
+
497
+ // ---------------------------------------------------------------------------
498
+ // Reading
499
+ // ---------------------------------------------------------------------------
500
+
501
+ const HEX = /^#([0-9a-f]{3,8})$/i;
502
+ const FUNCTION = /^(rgba?|hsla?|hsba?)\(([^)]*)\)$/i;
503
+
504
+ function part(text: string, max: number): number {
505
+ const trimmed = text.trim();
506
+ if (trimmed.endsWith("%")) return (Number.parseFloat(trimmed) / 100) * max;
507
+ return Number.parseFloat(trimmed);
508
+ }
509
+
510
+ /**
511
+ * A colour from a string, or a throw naming what was given.
512
+ *
513
+ * Accepts what CSS accepts — `#abc`, `#aabbcc`, `#aabbccdd`, `rgb()`, `rgba()`,
514
+ * `hsl()`, `hsla()` — and `hsb()`/`hsba()`, which CSS does not have but a
515
+ * picker's brightness area is written in.
516
+ */
517
+ export function parseColor(value: string): Color {
518
+ const text = value.trim();
519
+
520
+ const hexMatch = HEX.exec(text);
521
+ if (hexMatch !== null) {
522
+ const digits = hexMatch[1] as string;
523
+ const expand = (at: number): number =>
524
+ Number.parseInt(
525
+ digits.length <= 4 ? (digits[at] as string).repeat(2) : digits.slice(at * 2, at * 2 + 2),
526
+ 16,
527
+ );
528
+ if (digits.length !== 3 && digits.length !== 4 && digits.length !== 6 && digits.length !== 8) {
529
+ throw new RangeError(`Not a colour: ${JSON.stringify(value)}`);
530
+ }
531
+ const hasAlpha = digits.length === 4 || digits.length === 8;
532
+ return new RGBColor(expand(0), expand(1), expand(2), hasAlpha ? expand(3) / 255 : 1);
533
+ }
534
+
535
+ const call = FUNCTION.exec(text);
536
+ if (call === null) throw new RangeError(`Not a colour: ${JSON.stringify(value)}`);
537
+
538
+ const name = (call[1] as string).toLowerCase();
539
+ const args = (call[2] as string).split(/[,/]/).map((entry) => entry.trim());
540
+ if (args.length < 3) throw new RangeError(`Not a colour: ${JSON.stringify(value)}`);
541
+ const alpha = args.length > 3 ? part(args[3] as string, 1) : 1;
542
+
543
+ if (name.startsWith("rgb")) {
544
+ return new RGBColor(
545
+ part(args[0] as string, 255),
546
+ part(args[1] as string, 255),
547
+ part(args[2] as string, 255),
548
+ alpha,
549
+ );
550
+ }
551
+ const hue = Number.parseFloat(args[0] as string);
552
+ const saturation = part(args[1] as string, 100);
553
+ const third = part(args[2] as string, 100);
554
+ return name.startsWith("hsl")
555
+ ? new HSLColor(hue, saturation, third, alpha)
556
+ : new HSBColor(hue, saturation, third, alpha);
557
+ }
558
+
559
+ /** Black, as a starting point for a picker with no value. */
560
+ export function defaultColor(): Color {
561
+ return new HSBColor(0, 100, 100, 1);
562
+ }