1 | import { getRangeValue, parseAlpha } from "./NumberUtils.js";
|
2 | import { hslToRgb, hslaToRgba } from "./ColorUtils.js";
|
3 | var HslIndexes;
|
4 | (function (HslIndexes) {
|
5 | HslIndexes[HslIndexes["h"] = 1] = "h";
|
6 | HslIndexes[HslIndexes["s"] = 2] = "s";
|
7 | HslIndexes[HslIndexes["l"] = 3] = "l";
|
8 | HslIndexes[HslIndexes["a"] = 5] = "a";
|
9 | })(HslIndexes || (HslIndexes = {}));
|
10 | export class HslColorManager {
|
11 | constructor() {
|
12 | this.key = "hsl";
|
13 | this.stringPrefix = "hsl";
|
14 | }
|
15 | handleColor(color) {
|
16 | const colorValue = color.value, hslColor = colorValue.hsl ?? color.value;
|
17 | if (hslColor.h !== undefined && hslColor.s !== undefined && hslColor.l !== undefined) {
|
18 | return hslToRgb(hslColor);
|
19 | }
|
20 | }
|
21 | handleRangeColor(color) {
|
22 | const colorValue = color.value, hslColor = colorValue.hsl ?? color.value;
|
23 | if (hslColor.h !== undefined && hslColor.l !== undefined) {
|
24 | return hslToRgb({
|
25 | h: getRangeValue(hslColor.h),
|
26 | l: getRangeValue(hslColor.l),
|
27 | s: getRangeValue(hslColor.s),
|
28 | });
|
29 | }
|
30 | }
|
31 | parseString(input) {
|
32 | if (!input.startsWith("hsl")) {
|
33 | return;
|
34 | }
|
35 | const regex = /hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(,\s*([\d.%]+)\s*)?\)/i, result = regex.exec(input), minLength = 4, defaultAlpha = 1, radix = 10;
|
36 | return result
|
37 | ? hslaToRgba({
|
38 | a: result.length > minLength ? parseAlpha(result[HslIndexes.a]) : defaultAlpha,
|
39 | h: parseInt(result[HslIndexes.h], radix),
|
40 | l: parseInt(result[HslIndexes.l], radix),
|
41 | s: parseInt(result[HslIndexes.s], radix),
|
42 | })
|
43 | : undefined;
|
44 | }
|
45 | }
|