@tbela99/css-parser 0.3.0 → 0.4.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 (61) hide show
  1. package/{LICENSE → LICENSE.md} +1 -1
  2. package/README.md +191 -80
  3. package/dist/config.json.js +20 -1
  4. package/dist/index-umd-web.js +3210 -1352
  5. package/dist/index.cjs +3211 -1353
  6. package/dist/index.d.ts +910 -0
  7. package/dist/lib/ast/expand.js +1 -1
  8. package/dist/lib/ast/features/calc.js +1 -2
  9. package/dist/lib/ast/features/inlinecssvariables.js +1 -1
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/math/expression.js +38 -3
  12. package/dist/lib/ast/math/math.js +2 -2
  13. package/dist/lib/ast/minify.js +0 -1
  14. package/dist/lib/ast/types.js +1 -0
  15. package/dist/lib/ast/utils/minifyfeature.js +4 -3
  16. package/dist/lib/parser/declaration/list.js +1 -1
  17. package/dist/lib/parser/declaration/map.js +129 -26
  18. package/dist/lib/parser/declaration/set.js +1 -1
  19. package/dist/lib/parser/parse.js +325 -303
  20. package/dist/lib/parser/tokenize.js +220 -223
  21. package/dist/lib/parser/utils/declaration.js +1 -1
  22. package/dist/lib/parser/utils/syntax.js +159 -23
  23. package/dist/lib/parser/utils/type.js +2 -2
  24. package/dist/lib/renderer/color/a98rgb.js +64 -0
  25. package/dist/lib/renderer/color/color.js +521 -0
  26. package/dist/lib/renderer/color/colormix.js +337 -0
  27. package/dist/lib/renderer/color/hex.js +92 -0
  28. package/dist/lib/renderer/color/hsl.js +118 -0
  29. package/dist/lib/renderer/color/hsv.js +20 -0
  30. package/dist/lib/renderer/color/hwb.js +101 -0
  31. package/dist/lib/renderer/color/lab.js +136 -0
  32. package/dist/lib/renderer/color/lch.js +79 -0
  33. package/dist/lib/renderer/color/oklab.js +121 -0
  34. package/dist/lib/renderer/color/oklch.js +65 -0
  35. package/dist/lib/renderer/color/p3.js +57 -0
  36. package/dist/lib/renderer/color/prophotorgb.js +56 -0
  37. package/dist/lib/renderer/color/rec2020.js +70 -0
  38. package/dist/lib/renderer/color/relativecolor.js +152 -0
  39. package/dist/lib/renderer/color/rgb.js +44 -0
  40. package/dist/lib/renderer/color/srgb.js +261 -0
  41. package/dist/lib/renderer/color/utils/components.js +20 -0
  42. package/dist/lib/renderer/color/utils/constants.js +191 -0
  43. package/dist/lib/renderer/color/utils/matrix.js +35 -0
  44. package/dist/lib/renderer/color/xyz.js +64 -0
  45. package/dist/lib/renderer/color/xyzd50.js +33 -0
  46. package/dist/lib/renderer/render.js +61 -32
  47. package/dist/node/index.js +1 -1
  48. package/dist/node/load.js +1 -1
  49. package/dist/web/index.js +1 -1
  50. package/package.json +15 -14
  51. package/quickjs.sh +1 -0
  52. package/dist/lib/ast/features/utils/math.js +0 -95
  53. package/dist/lib/iterable/set.js +0 -48
  54. package/dist/lib/iterable/weakmap.js +0 -53
  55. package/dist/lib/renderer/utils/calccolor.js +0 -238
  56. package/dist/lib/renderer/utils/color.js +0 -371
  57. package/dist/lib/renderer/utils/hex.js +0 -124
  58. package/dist/lib/renderer/utils/hsl.js +0 -49
  59. package/dist/lib/renderer/utils/hsv.js +0 -15
  60. package/dist/lib/renderer/utils/hwb.js +0 -50
  61. package/dist/lib/renderer/utils/rgb.js +0 -66
@@ -0,0 +1,337 @@
1
+ import '../../parser/parse.js';
2
+ import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../../parser/utils/syntax.js';
3
+ import { EnumToken } from '../../ast/types.js';
4
+ import '../../ast/minify.js';
5
+ import { getNumber } from './color.js';
6
+ import { srgb2rgb } from './rgb.js';
7
+ import { powerlessColorComponent } from './utils/constants.js';
8
+ import { getComponents } from './utils/components.js';
9
+ import { srgb2hwb } from './hwb.js';
10
+ import { srgb2hsl } from './hsl.js';
11
+ import { srgbvalues, srgb2lsrgbvalues } from './srgb.js';
12
+ import { srgb2lch, xyz2lchvalues } from './lch.js';
13
+ import { srgb2lab } from './lab.js';
14
+ import { srgb2p3values } from './p3.js';
15
+ import { eq } from '../../parser/utils/eq.js';
16
+ import { srgb2oklch } from './oklch.js';
17
+ import { srgb2oklab } from './oklab.js';
18
+ import { srgb2a98values } from './a98rgb.js';
19
+ import { srgb2prophotorgbvalues } from './prophotorgb.js';
20
+ import { srgb2xyz } from './xyz.js';
21
+ import { XYZ_D65_to_D50, xyzd502lch } from './xyzd50.js';
22
+ import { srgb2rec2020values } from './rec2020.js';
23
+ import '../sourcemap/lib/encode.js';
24
+
25
+ function interpolateHue(interpolationMethod, h1, h2) {
26
+ switch (interpolationMethod.val) {
27
+ case 'longer':
28
+ if (h2 - h1 < 180 && h2 - h1 > 0) {
29
+ h1 += 360;
30
+ }
31
+ else if (h2 - h1 <= 0 && h2 - h1 > -180) {
32
+ h2 += 360;
33
+ }
34
+ break;
35
+ case 'increasing':
36
+ if (h2 < h1) {
37
+ h2 += 360;
38
+ }
39
+ break;
40
+ case 'decreasing':
41
+ if (h2 > h1) {
42
+ h1 += 360;
43
+ }
44
+ break;
45
+ case 'shorter':
46
+ default:
47
+ // shorter
48
+ if (h2 - h1 > 180) {
49
+ h1 += 360;
50
+ }
51
+ else if (h2 - h1 < -180) {
52
+ h2 += 360;
53
+ }
54
+ break;
55
+ }
56
+ return [h1, h2];
57
+ }
58
+ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color2, percentage2) {
59
+ if (hueInterpolationMethod != null && isRectangularOrthogonalColorspace(colorSpace)) {
60
+ return null;
61
+ }
62
+ if (isPolarColorspace(colorSpace) && hueInterpolationMethod == null) {
63
+ hueInterpolationMethod = { typ: EnumToken.IdenTokenType, val: 'shorter' };
64
+ }
65
+ if (percentage1 == null) {
66
+ if (percentage2 == null) {
67
+ // @ts-ignore
68
+ percentage1 = { typ: EnumToken.NumberTokenType, val: '.5' };
69
+ // @ts-ignore
70
+ percentage2 = { typ: EnumToken.NumberTokenType, val: '.5' };
71
+ }
72
+ else {
73
+ if (+percentage2.val <= 0) {
74
+ return null;
75
+ }
76
+ if (+percentage2.val >= 100) {
77
+ // @ts-ignore
78
+ percentage2 = { typ: EnumToken.NumberTokenType, val: '1' };
79
+ }
80
+ // @ts-ignore
81
+ percentage1 = { typ: EnumToken.NumberTokenType, val: String(1 - percentage2.val / 100) };
82
+ }
83
+ }
84
+ else {
85
+ // @ts-ignore
86
+ if (percentage1.val <= 0) {
87
+ return null;
88
+ }
89
+ if (percentage2 == null) {
90
+ // @ts-ignore
91
+ if (percentage1.val >= 100) {
92
+ // @ts-ignore
93
+ percentage1 = { typ: EnumToken.NumberTokenType, val: '1' };
94
+ }
95
+ // @ts-ignore
96
+ percentage2 = { typ: EnumToken.NumberTokenType, val: String(1 - percentage1.val / 100) };
97
+ }
98
+ else {
99
+ // @ts-ignore
100
+ if (percentage2.val <= 0) {
101
+ return null;
102
+ }
103
+ }
104
+ }
105
+ let values1 = srgbvalues(color1);
106
+ let values2 = srgbvalues(color2);
107
+ if (values1 == null || values2 == null) {
108
+ return null;
109
+ }
110
+ const components1 = getComponents(color1);
111
+ const components2 = getComponents(color2);
112
+ if (eq(components1[3], powerlessColorComponent) && values2.length == 4) {
113
+ values1[3] = values2[3];
114
+ }
115
+ if (eq(components2[3], powerlessColorComponent) && values1.length == 4) {
116
+ values2[3] = values1[3];
117
+ }
118
+ const p1 = getNumber(percentage1);
119
+ const p2 = getNumber(percentage2);
120
+ const mul1 = values1.length == 4 ? values1.pop() : 1;
121
+ const mul2 = values2.length == 4 ? values2.pop() : 1;
122
+ const mul = mul1 * p1 + mul2 * p2;
123
+ // @ts-ignore
124
+ const calculate = () => [colorSpace].concat(values1.map((v1, i) => {
125
+ return {
126
+ // @ts-ignore
127
+ typ: EnumToken.NumberTokenType, val: String((mul1 * v1 * p1 + mul2 * values2[i] * p2) / mul)
128
+ };
129
+ }).concat(mul == 1 ? [] : [{
130
+ typ: EnumToken.NumberTokenType, val: String(mul)
131
+ }]));
132
+ switch (colorSpace.val) {
133
+ case 'srgb':
134
+ break;
135
+ case 'display-p3':
136
+ // @ts-ignore
137
+ values1 = srgb2p3values(...values1);
138
+ // @ts-ignore
139
+ values2 = srgb2p3values(...values2);
140
+ break;
141
+ case 'a98-rgb':
142
+ // @ts-ignore
143
+ values1 = srgb2a98values(...values1);
144
+ // @ts-ignore
145
+ values2 = srgb2a98values(...values2);
146
+ break;
147
+ case 'prophoto-rgb':
148
+ // @ts-ignore
149
+ values1 = srgb2prophotorgbvalues(...values1);
150
+ // @ts-ignore
151
+ values2 = srgb2prophotorgbvalues(...values2);
152
+ break;
153
+ case 'srgb-linear':
154
+ // @ts-ignore
155
+ values1 = srgb2lsrgbvalues(...values1);
156
+ // @ts-ignore
157
+ values2 = srgb2lsrgbvalues(...values2);
158
+ break;
159
+ case 'rec2020':
160
+ // @ts-ignore
161
+ values1 = srgb2rec2020values(...values1);
162
+ // @ts-ignore
163
+ values2 = srgb2rec2020values(...values2);
164
+ break;
165
+ case 'xyz':
166
+ case 'xyz-d65':
167
+ case 'xyz-d50':
168
+ // @ts-ignore
169
+ values1 = srgb2xyz(...values1);
170
+ // @ts-ignore
171
+ values2 = srgb2xyz(...values2);
172
+ if (colorSpace.val == 'xyz-d50') {
173
+ // @ts-ignore
174
+ values1 = XYZ_D65_to_D50(...values1);
175
+ // @ts-ignore
176
+ values2 = XYZ_D65_to_D50(...values2);
177
+ }
178
+ break;
179
+ case 'rgb':
180
+ // @ts-ignore
181
+ values1 = srgb2rgb(...values1);
182
+ // @ts-ignore
183
+ values2 = srgb2rgb(...values2);
184
+ break;
185
+ case 'hsl':
186
+ // @ts-ignore
187
+ values1 = srgb2hsl(...values1);
188
+ // @ts-ignore
189
+ values2 = srgb2hsl(...values2);
190
+ break;
191
+ case 'hwb':
192
+ // @ts-ignore
193
+ values1 = srgb2hwb(...values1);
194
+ // @ts-ignore
195
+ values2 = srgb2hwb(...values2);
196
+ break;
197
+ case 'lab':
198
+ // @ts-ignore
199
+ values1 = srgb2lab(...values1);
200
+ // @ts-ignore
201
+ values2 = srgb2lab(...values2);
202
+ break;
203
+ case 'lch':
204
+ // @ts-ignore
205
+ values1 = srgb2lch(...values1);
206
+ // @ts-ignore
207
+ values2 = srgb2lch(...values2);
208
+ break;
209
+ case 'oklab':
210
+ // @ts-ignore
211
+ values1 = srgb2oklab(...values1);
212
+ // @ts-ignore
213
+ values2 = srgb2oklab(...values2);
214
+ break;
215
+ case 'oklch':
216
+ // @ts-ignore
217
+ values1 = srgb2oklch(...values1);
218
+ // @ts-ignore
219
+ values2 = srgb2oklch(...values2);
220
+ break;
221
+ default:
222
+ return null;
223
+ }
224
+ const lchSpaces = ['lch', 'oklch'];
225
+ // powerless
226
+ if (lchSpaces.includes(color1.kin) || lchSpaces.includes(colorSpace.val)) {
227
+ if (eq(components1[2], powerlessColorComponent) || values1[2] == 0) {
228
+ values1[2] = values2[2];
229
+ }
230
+ }
231
+ // powerless
232
+ if (lchSpaces.includes(color1.kin) || lchSpaces.includes(colorSpace.val)) {
233
+ if (eq(components2[2], powerlessColorComponent) || values2[2] == 0) {
234
+ values2[2] = values1[2];
235
+ }
236
+ }
237
+ if (hueInterpolationMethod != null) {
238
+ let hueIndex = 2;
239
+ let multiplier = 1;
240
+ if (['hwb', 'hsl'].includes(colorSpace.val)) {
241
+ hueIndex = 0;
242
+ multiplier = 360;
243
+ }
244
+ const [h1, h2] = interpolateHue(hueInterpolationMethod, values1[hueIndex] * multiplier, values2[hueIndex] * multiplier);
245
+ values1[hueIndex] = h1 / multiplier;
246
+ values2[hueIndex] = h2 / multiplier;
247
+ }
248
+ switch (colorSpace.val) {
249
+ case 'xyz':
250
+ case 'xyz-d65':
251
+ case 'xyz-d50':
252
+ let values = values1.map((v1, i) => (mul1 * v1 * p1 + mul2 * values2[i] * p2) / mul)
253
+ .concat(mul == 1 ? [] : [mul]);
254
+ if (colorSpace.val == 'xyz-d50') {
255
+ // @ts-ignore
256
+ values = xyzd502lch(...values);
257
+ }
258
+ else {
259
+ // @ts-ignore
260
+ values = xyz2lchvalues(...values);
261
+ }
262
+ // @ts-ignore
263
+ return {
264
+ typ: EnumToken.ColorTokenType,
265
+ val: 'lch',
266
+ chi: values.map(v => {
267
+ return {
268
+ typ: EnumToken.NumberTokenType,
269
+ val: String(v)
270
+ };
271
+ }),
272
+ kin: 'lch'
273
+ };
274
+ case 'srgb':
275
+ case 'srgb-linear':
276
+ case 'a98-rgb':
277
+ case 'rec2020':
278
+ // @ts-ignore
279
+ return {
280
+ typ: EnumToken.ColorTokenType,
281
+ val: 'color',
282
+ chi: calculate(),
283
+ kin: 'color',
284
+ cal: 'col'
285
+ };
286
+ case 'rgb':
287
+ case 'hsl':
288
+ case 'hwb':
289
+ case 'lab':
290
+ case 'lch':
291
+ case 'oklab':
292
+ case 'oklch':
293
+ if (['hsl', 'hwb'].includes(colorSpace.val)) {
294
+ // @ts-ignore
295
+ if (values1[2] < 0) {
296
+ // @ts-ignore
297
+ values1[2] += 1;
298
+ }
299
+ // @ts-ignore
300
+ if (values2[2] < 0) {
301
+ // @ts-ignore
302
+ values2[2] += 1;
303
+ }
304
+ }
305
+ else if (['lch', 'oklch'].includes(colorSpace.val)) {
306
+ // @ts-ignore
307
+ if (values1[2] < 0) {
308
+ // @ts-ignore
309
+ values1[2] += 360;
310
+ }
311
+ // @ts-ignore
312
+ if (values2[2] < 0) {
313
+ // @ts-ignore
314
+ values2[2] += 360;
315
+ }
316
+ }
317
+ // @ts-ignore
318
+ const result = {
319
+ typ: EnumToken.ColorTokenType,
320
+ val: colorSpace.val,
321
+ chi: calculate().slice(1),
322
+ kin: colorSpace.val
323
+ };
324
+ if (colorSpace.val == 'hsl' || colorSpace.val == 'hwb') {
325
+ // @ts-ignore
326
+ result.chi[0] = { typ: EnumToken.AngleTokenType, val: String(result.chi[0].val * 360), unit: 'deg' };
327
+ // @ts-ignore
328
+ result.chi[1] = { typ: EnumToken.PercentageTokenType, val: String(result.chi[1].val * 100) };
329
+ // @ts-ignore
330
+ result.chi[2] = { typ: EnumToken.PercentageTokenType, val: String(result.chi[2].val * 100) };
331
+ }
332
+ return result;
333
+ }
334
+ return null;
335
+ }
336
+
337
+ export { colorMix };
@@ -0,0 +1,92 @@
1
+ import { EnumToken } from '../../ast/types.js';
2
+ import '../../ast/minify.js';
3
+ import '../../parser/parse.js';
4
+ import { getNumber, minmax } from './color.js';
5
+ import { hsl2rgb, hwb2rgb, cmyk2rgb, oklab2rgb, oklch2rgb, lab2rgb, lch2rgb } from './rgb.js';
6
+ import { NAMES_COLORS } from './utils/constants.js';
7
+ import { getComponents } from './utils/components.js';
8
+ import '../sourcemap/lib/encode.js';
9
+
10
+ function toHexString(acc, value) {
11
+ return acc + value.toString(16).padStart(2, '0');
12
+ }
13
+ function reduceHexValue(value) {
14
+ const named_color = NAMES_COLORS[expandHexValue(value)];
15
+ if (value.length == 7) {
16
+ if (value[1] == value[2] &&
17
+ value[3] == value[4] &&
18
+ value[5] == value[6]) {
19
+ value = `#${value[1]}${value[3]}${value[5]}`;
20
+ }
21
+ }
22
+ else if (value.length == 9) {
23
+ if (value[1] == value[2] &&
24
+ value[3] == value[4] &&
25
+ value[5] == value[6] &&
26
+ value[7] == value[8]) {
27
+ value = `#${value[1]}${value[3]}${value[5]}${value[7]}`;
28
+ }
29
+ }
30
+ return named_color != null && named_color.length <= value.length ? named_color : value;
31
+ }
32
+ function expandHexValue(value) {
33
+ if (value.length == 4) {
34
+ return `#${value[1]}${value[1]}${value[2]}${value[2]}${value[3]}${value[3]}`;
35
+ }
36
+ if (value.length == 5) {
37
+ return `#${value[1]}${value[1]}${value[2]}${value[2]}${value[3]}${value[3]}${value[4]}${value[4]}`;
38
+ }
39
+ return value;
40
+ }
41
+ function rgb2hex(token) {
42
+ let value = '#';
43
+ let t;
44
+ // @ts-ignore
45
+ const components = getComponents(token);
46
+ // @ts-ignore
47
+ for (let i = 0; i < 3; i++) {
48
+ // @ts-ignore
49
+ t = components[i];
50
+ // @ts-ignore
51
+ value += (t.typ == EnumToken.Iden && t.val == 'none' ? '0' : Math.round(getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 255 : 1))).toString(16).padStart(2, '0');
52
+ }
53
+ // @ts-ignore
54
+ if (components.length == 4) {
55
+ // @ts-ignore
56
+ t = components[3];
57
+ // @ts-ignore
58
+ const v = (t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t);
59
+ // @ts-ignore
60
+ if (v < 1) {
61
+ // @ts-ignore
62
+ value += Math.round(255 * getNumber(t)).toString(16).padStart(2, '0');
63
+ }
64
+ }
65
+ return value;
66
+ }
67
+ function hsl2hex(token) {
68
+ return `${hsl2rgb(token).reduce(toHexString, '#')}`;
69
+ }
70
+ function hwb2hex(token) {
71
+ return `${hwb2rgb(token).reduce(toHexString, '#')}`;
72
+ }
73
+ function cmyk2hex(token) {
74
+ return `#${cmyk2rgb(token).reduce(toHexString, '')}`;
75
+ }
76
+ function oklab2hex(token) {
77
+ return `${oklab2rgb(token).reduce(toHexString, '#')}`;
78
+ }
79
+ function oklch2hex(token) {
80
+ return `${oklch2rgb(token).reduce(toHexString, '#')}`;
81
+ }
82
+ function lab2hex(token) {
83
+ return `${lab2rgb(token).reduce(toHexString, '#')}`;
84
+ }
85
+ function lch2hex(token) {
86
+ return `${lch2rgb(token).reduce(toHexString, '#')}`;
87
+ }
88
+ function srgb2hexvalues(r, g, b, alpha) {
89
+ return [r, g, b].concat(alpha == null || alpha == 1 ? [] : [alpha]).reduce((acc, value) => acc + minmax(Math.round(255 * value), 0, 255).toString(16).padStart(2, '0'), '#');
90
+ }
91
+
92
+ export { cmyk2hex, expandHexValue, hsl2hex, hwb2hex, lab2hex, lch2hex, oklab2hex, oklch2hex, reduceHexValue, rgb2hex, srgb2hexvalues };
@@ -0,0 +1,118 @@
1
+ import { hwb2hsv } from './hsv.js';
2
+ import { getNumber } from './color.js';
3
+ import { hex2rgb, lab2rgb, lch2rgb, oklab2rgb, oklch2rgb } from './rgb.js';
4
+ import './utils/constants.js';
5
+ import { getComponents } from './utils/components.js';
6
+ import { eq } from '../../parser/utils/eq.js';
7
+ import { EnumToken } from '../../ast/types.js';
8
+ import '../../ast/minify.js';
9
+ import '../../parser/parse.js';
10
+ import { hslvalues } from './srgb.js';
11
+ import '../sourcemap/lib/encode.js';
12
+
13
+ function hex2hsl(token) {
14
+ // @ts-ignore
15
+ return rgb2hslvalues(...hex2rgb(token));
16
+ }
17
+ function rgb2hsl(token) {
18
+ const chi = getComponents(token);
19
+ // @ts-ignore
20
+ let t = chi[0];
21
+ // @ts-ignore
22
+ let r = getNumber(t);
23
+ // @ts-ignore
24
+ t = chi[1];
25
+ // @ts-ignore
26
+ let g = getNumber(t);
27
+ // @ts-ignore
28
+ t = chi[2];
29
+ // @ts-ignore
30
+ let b = getNumber(t);
31
+ // @ts-ignore
32
+ t = chi[3];
33
+ // @ts-ignore
34
+ let a = null;
35
+ if (t != null && !eq(t, { typ: EnumToken.IdenTokenType, val: 'none' })) {
36
+ // @ts-ignore
37
+ a = getNumber(t) / 255;
38
+ }
39
+ const values = [r, g, b];
40
+ if (a != null && a != 1) {
41
+ values.push(a);
42
+ }
43
+ // @ts-ignore
44
+ return rgb2hslvalues(...values);
45
+ }
46
+ // https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
47
+ function hsv2hsl(h, s, v, a) {
48
+ const result = [
49
+ //[hue, saturation, lightness]
50
+ //Range should be between 0 - 1
51
+ h, //Hue stays the same
52
+ //Saturation is very different between the two color spaces
53
+ //If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
54
+ //Otherwise sat*val/(2-(2-sat)*val)
55
+ //Conditional is not operating with hue, it is reassigned!
56
+ s * v / ((h = (2 - s) * v) < 1 ? h : 2 - h),
57
+ h / 2, //Lightness is (2-sat)*val/2
58
+ ];
59
+ if (a != null) {
60
+ result.push(a);
61
+ }
62
+ return result;
63
+ }
64
+ function hwb2hsl(token) {
65
+ // @ts-ignore
66
+ return hsv2hsl(...hwb2hsv(...Object.values(hslvalues(token))));
67
+ }
68
+ function lab2hsl(token) {
69
+ // @ts-ignore
70
+ return rgb2hslvalues(...lab2rgb(token));
71
+ }
72
+ function lch2hsl(token) {
73
+ // @ts-ignore
74
+ return rgb2hslvalues(...lch2rgb(token));
75
+ }
76
+ function oklab2hsl(token) {
77
+ // @ts-ignore
78
+ return rgb2hslvalues(...oklab2rgb(token));
79
+ }
80
+ function oklch2hsl(token) {
81
+ // @ts-ignore
82
+ return rgb2hslvalues(...oklch2rgb(token));
83
+ }
84
+ function rgb2hslvalues(r, g, b, a = null) {
85
+ return srgb2hsl(r / 255, g / 255, b / 255, a);
86
+ }
87
+ function srgb2hsl(r, g, b, a = null) {
88
+ let max = Math.max(r, g, b);
89
+ let min = Math.min(r, g, b);
90
+ let h = 0;
91
+ let s = 0;
92
+ let l = (max + min) / 2;
93
+ if (max != min) {
94
+ let d = max - min;
95
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
96
+ switch (max) {
97
+ case r:
98
+ h = (g - b) / d + (g < b ? 6 : 0);
99
+ break;
100
+ case g:
101
+ h = (b - r) / d + 2;
102
+ break;
103
+ case b:
104
+ h = (r - g) / d + 4;
105
+ break;
106
+ }
107
+ h /= 6;
108
+ }
109
+ const hsl = [h, s, l];
110
+ if (a != null && a < 1) {
111
+ // @ts-ignore
112
+ return hsl.concat([a]);
113
+ }
114
+ // @ts-ignore
115
+ return hsl;
116
+ }
117
+
118
+ export { hex2hsl, hsv2hsl, hwb2hsl, lab2hsl, lch2hsl, oklab2hsl, oklch2hsl, rgb2hsl, rgb2hslvalues, srgb2hsl };
@@ -0,0 +1,20 @@
1
+ function hwb2hsv(h, w, b, a) {
2
+ // @ts-ignore
3
+ return [h, 1 - w / (1 - b), 1 - b, a];
4
+ }
5
+ // https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
6
+ function hsl2hsv(h, s, l, a = null) {
7
+ s *= l < .5 ? l : 1 - l;
8
+ const result = [
9
+ //Range should be between 0 - 1
10
+ h, //Hue stays the same
11
+ 2 * s / (l + s), //Saturation
12
+ l + s //Value
13
+ ];
14
+ if (a != null) {
15
+ result.push(a);
16
+ }
17
+ return result;
18
+ }
19
+
20
+ export { hsl2hsv, hwb2hsv };
@@ -0,0 +1,101 @@
1
+ import { hsl2hsv } from './hsv.js';
2
+ import './utils/constants.js';
3
+ import { getComponents } from './utils/components.js';
4
+ import { getNumber, getAngle } from './color.js';
5
+ import { EnumToken } from '../../ast/types.js';
6
+ import '../../ast/minify.js';
7
+ import '../../parser/parse.js';
8
+ import { lab2srgb, lch2srgb, oklab2srgb, oklch2srgb } from './srgb.js';
9
+ import { eq } from '../../parser/utils/eq.js';
10
+ import '../sourcemap/lib/encode.js';
11
+
12
+ function rgb2hwb(token) {
13
+ // @ts-ignore
14
+ return srgb2hwb(...getComponents(token).map((t, index) => {
15
+ if (index == 3 && eq(t, { typ: EnumToken.IdenTokenType, val: 'none' })) {
16
+ return 1;
17
+ }
18
+ return getNumber(t) / 255;
19
+ }));
20
+ }
21
+ function hsl2hwb(token) {
22
+ // @ts-ignore
23
+ return hsl2hwbvalues(...getComponents(token).map((t, index) => {
24
+ if (index == 3 && eq(t, { typ: EnumToken.IdenTokenType, val: 'none' })) {
25
+ return 1;
26
+ }
27
+ if (index == 0) {
28
+ return getAngle(t);
29
+ }
30
+ return getNumber(t);
31
+ }));
32
+ }
33
+ function lab2hwb(token) {
34
+ // @ts-ignore
35
+ return srgb2hwb(...lab2srgb(token));
36
+ }
37
+ function lch2hwb(token) {
38
+ // @ts-ignore
39
+ return srgb2hwb(...lch2srgb(token));
40
+ }
41
+ function oklab2hwb(token) {
42
+ // @ts-ignore
43
+ return srgb2hwb(...oklab2srgb(token));
44
+ }
45
+ function oklch2hwb(token) {
46
+ // @ts-ignore
47
+ return srgb2hwb(...oklch2srgb(token));
48
+ }
49
+ function rgb2hue(r, g, b, fallback = 0) {
50
+ let value = rgb2value(r, g, b);
51
+ let whiteness = rgb2whiteness(r, g, b);
52
+ let delta = value - whiteness;
53
+ if (delta > 0) {
54
+ // calculate segment
55
+ let segment = value === r ? (g - b) / delta : (value === g
56
+ ? (b - r) / delta
57
+ : (r - g) / delta);
58
+ // calculate shift
59
+ let shift = value === r ? segment < 0
60
+ ? 360 / 60
61
+ : 0 / 60 : (value === g
62
+ ? 120 / 60
63
+ : 240 / 60);
64
+ // calculate hue
65
+ return (segment + shift) * 60;
66
+ }
67
+ return fallback;
68
+ }
69
+ function rgb2value(r, g, b) {
70
+ return Math.max(r, g, b);
71
+ }
72
+ function rgb2whiteness(r, g, b) {
73
+ return Math.min(r, g, b);
74
+ }
75
+ function srgb2hwb(r, g, b, a = null, fallback = 0) {
76
+ r *= 100;
77
+ g *= 100;
78
+ b *= 100;
79
+ let hue = rgb2hue(r, g, b, fallback);
80
+ let whiteness = rgb2whiteness(r, g, b);
81
+ let value = Math.round(rgb2value(r, g, b));
82
+ let blackness = 100 - value;
83
+ const result = [hue / 360, whiteness / 100, blackness / 100];
84
+ if (a != null) {
85
+ result.push(a);
86
+ }
87
+ return result;
88
+ }
89
+ function hsv2hwb(h, s, v, a = null) {
90
+ const result = [h, (1 - s) * v, 1 - v];
91
+ if (a != null) {
92
+ result.push(a);
93
+ }
94
+ return result;
95
+ }
96
+ function hsl2hwbvalues(h, s, l, a = null) {
97
+ // @ts-ignore
98
+ return hsv2hwb(...hsl2hsv(h, s, l, a));
99
+ }
100
+
101
+ export { hsl2hwb, hsl2hwbvalues, hsv2hwb, lab2hwb, lch2hwb, oklab2hwb, oklch2hwb, rgb2hwb, srgb2hwb };