1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.string2rbg = void 0;
|
7 | const color_string_1 = __importDefault(require("color-string"));
|
8 | function hue2rgb(p, q, m) {
|
9 | let t = m;
|
10 | if (t < 0)
|
11 | t += 1;
|
12 | if (t > 1)
|
13 | t -= 1;
|
14 | if (t < 1 / 6)
|
15 | return p + (q - p) * 6 * t;
|
16 | if (t < 1 / 2)
|
17 | return q;
|
18 | if (t < 2 / 3)
|
19 | return p + (q - p) * (2 / 3 - t) * 6;
|
20 | return p;
|
21 | }
|
22 | function hsl2rbg(hsl) {
|
23 | const h = hsl[0] / 360;
|
24 | const s = hsl[1] / 100;
|
25 | const l = hsl[2] / 100;
|
26 | const a = hsl[3];
|
27 | if (s === 0)
|
28 | return [l * 255, l * 255, l * 255, a];
|
29 | const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
30 | const p = 2 * l - q;
|
31 | const r = hue2rgb(p, q, h + 1 / 3);
|
32 | const g = hue2rgb(p, q, h);
|
33 | const b = hue2rgb(p, q, h - 1 / 3);
|
34 | return [r * 255, g * 255, b * 255, a];
|
35 | }
|
36 | function string2rbg(s) {
|
37 | const color = color_string_1.default.get(s);
|
38 | if (!color)
|
39 | return null;
|
40 | const { model, value } = color;
|
41 | if (model === 'rgb')
|
42 | return value;
|
43 | if (model === 'hsl')
|
44 | return hsl2rbg(value);
|
45 | return null;
|
46 | }
|
47 | exports.string2rbg = string2rbg;
|
48 |
|
\ | No newline at end of file |