@tbela99/css-parser 1.1.1 → 1.2.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 (101) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +4195 -3363
  4. package/dist/index.cjs +4199 -3367
  5. package/dist/index.d.ts +42 -31
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +37 -35
  8. package/dist/lib/ast/features/inlinecssvariables.js +25 -17
  9. package/dist/lib/ast/features/prefix.js +22 -19
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/features/transform.js +17 -2
  12. package/dist/lib/ast/math/expression.js +184 -159
  13. package/dist/lib/ast/math/math.js +22 -20
  14. package/dist/lib/ast/minify.js +249 -199
  15. package/dist/lib/ast/transform/compute.js +48 -38
  16. package/dist/lib/ast/transform/matrix.js +6 -5
  17. package/dist/lib/ast/transform/minify.js +31 -34
  18. package/dist/lib/ast/transform/utils.js +76 -16
  19. package/dist/lib/ast/types.js +32 -1
  20. package/dist/lib/fs/resolve.js +1 -14
  21. package/dist/lib/parser/declaration/list.js +1 -1
  22. package/dist/lib/parser/declaration/map.js +1 -1
  23. package/dist/lib/parser/declaration/set.js +1 -1
  24. package/dist/lib/parser/parse.js +19 -95
  25. package/dist/lib/parser/tokenize.js +1 -13
  26. package/dist/lib/parser/utils/declaration.js +1 -1
  27. package/dist/lib/parser/utils/type.js +1 -1
  28. package/dist/lib/renderer/render.js +44 -168
  29. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  30. package/dist/lib/syntax/color/cmyk.js +104 -0
  31. package/dist/lib/{renderer → syntax}/color/color-mix.js +20 -21
  32. package/dist/lib/syntax/color/color.js +581 -0
  33. package/dist/lib/syntax/color/hex.js +179 -0
  34. package/dist/lib/syntax/color/hsl.js +201 -0
  35. package/dist/lib/syntax/color/hwb.js +185 -0
  36. package/dist/lib/syntax/color/lab.js +262 -0
  37. package/dist/lib/syntax/color/lch.js +194 -0
  38. package/dist/lib/syntax/color/oklab.js +237 -0
  39. package/dist/lib/syntax/color/oklch.js +166 -0
  40. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  41. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  42. package/dist/lib/{renderer → syntax}/color/relativecolor.js +53 -40
  43. package/dist/lib/syntax/color/rgb.js +140 -0
  44. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  45. package/dist/lib/{renderer → syntax}/color/utils/components.js +7 -7
  46. package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
  47. package/dist/lib/syntax/color/utils/distance.js +30 -0
  48. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  49. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  50. package/dist/lib/syntax/syntax.js +77 -67
  51. package/dist/lib/syntax/utils.js +70 -0
  52. package/dist/lib/validation/at-rules/container.js +1 -1
  53. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  54. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  55. package/dist/lib/validation/at-rules/document.js +1 -1
  56. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  57. package/dist/lib/validation/at-rules/import.js +1 -1
  58. package/dist/lib/validation/at-rules/keyframes.js +14 -13
  59. package/dist/lib/validation/at-rules/layer.js +1 -1
  60. package/dist/lib/validation/at-rules/media.js +1 -1
  61. package/dist/lib/validation/at-rules/namespace.js +1 -1
  62. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  63. package/dist/lib/validation/at-rules/page.js +1 -1
  64. package/dist/lib/validation/at-rules/supports.js +1 -1
  65. package/dist/lib/validation/at-rules/when.js +1 -1
  66. package/dist/lib/validation/atrule.js +2 -2
  67. package/dist/lib/validation/config.js +4 -3
  68. package/dist/lib/validation/config.json.js +1 -1
  69. package/dist/lib/validation/parser/parse.js +12 -7
  70. package/dist/lib/validation/selector.js +9 -8
  71. package/dist/lib/validation/syntax.js +170 -120
  72. package/dist/lib/validation/syntaxes/complex-selector-list.js +13 -19
  73. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  74. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  75. package/dist/lib/validation/syntaxes/family-name.js +36 -39
  76. package/dist/lib/validation/syntaxes/keyframe-selector.js +14 -22
  77. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  78. package/dist/lib/validation/syntaxes/relative-selector-list.js +27 -25
  79. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  80. package/dist/lib/validation/syntaxes/url.js +35 -33
  81. package/dist/lib/validation/utils/list.js +10 -9
  82. package/dist/lib/validation/utils/whitespace.js +1 -1
  83. package/dist/node/index.js +4 -2
  84. package/dist/web/index.js +4 -2
  85. package/package.json +4 -4
  86. package/.editorconfig +0 -484
  87. package/dist/lib/ast/transform/convert.js +0 -33
  88. package/dist/lib/ast/utils/utils.js +0 -104
  89. package/dist/lib/renderer/color/color.js +0 -654
  90. package/dist/lib/renderer/color/hex.js +0 -105
  91. package/dist/lib/renderer/color/hsl.js +0 -125
  92. package/dist/lib/renderer/color/hwb.js +0 -103
  93. package/dist/lib/renderer/color/lab.js +0 -148
  94. package/dist/lib/renderer/color/lch.js +0 -90
  95. package/dist/lib/renderer/color/oklab.js +0 -131
  96. package/dist/lib/renderer/color/oklch.js +0 -75
  97. package/dist/lib/renderer/color/rgb.js +0 -50
  98. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  99. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  100. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  101. /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
@@ -1,654 +0,0 @@
1
- import { EnumToken } from '../../ast/types.js';
2
- import '../../ast/minify.js';
3
- import '../../ast/walk.js';
4
- import '../../parser/parse.js';
5
- import '../../parser/tokenize.js';
6
- import '../../parser/utils/config.js';
7
- import { srgb2rgb, lch2rgb, lab2rgb, oklch2rgb, oklab2rgb, hwb2rgb, hsl2rgb, hex2rgb } from './rgb.js';
8
- import { srgb2hsl, lch2hsl, lab2hsl, oklch2hsl, oklab2hsl, hwb2hsl, hex2hsl, rgb2hsl } from './hsl.js';
9
- import { lch2hwb, lab2hwb, oklch2hwb, oklab2hwb, hsl2hwb, rgb2hwb } from './hwb.js';
10
- import { srgb2lab, oklch2lab, oklab2lab, lch2lab, hwb2lab, hsl2lab, rgb2lab, hex2lab } from './lab.js';
11
- import { srgb2lch, oklch2lch, oklab2lch, lab2lch, hwb2lch, hsl2lch, rgb2lch, hex2lch } from './lch.js';
12
- import { srgb2oklab, oklch2oklab, lch2oklab, lab2oklab, hwb2oklab, hsl2oklab, rgb2oklab, hex2oklab } from './oklab.js';
13
- import { srgb2oklch, lch2oklch, oklab2oklch, lab2oklch, hwb2oklch, hsl2oklch, rgb2oklch, hex2oklch } from './oklch.js';
14
- import { ColorKind, colorFuncColorSpace } from './utils/constants.js';
15
- import { getComponents } from './utils/components.js';
16
- import { xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues, lch2srgb, oklab2srgb, lab2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb } from './srgb.js';
17
- import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
18
- import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
19
- import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
20
- import { srgb2xyz } from './xyz.js';
21
- import { p32srgbvalues, srgb2p3values } from './p3.js';
22
- import { xyzd502srgb, XYZ_D65_to_D50 } from './xyzd50.js';
23
- import '../sourcemap/lib/encode.js';
24
-
25
- function convert(token, to) {
26
- if (token.kin == to) {
27
- return token;
28
- }
29
- if (token.kin == ColorKind.COLOR) {
30
- const colorSpace = token.chi.find(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
31
- if (colorSpace.val == ColorKind[to].toLowerCase().replaceAll('_', '-')) {
32
- return token;
33
- }
34
- }
35
- let values = [];
36
- if (to == ColorKind.HSL) {
37
- let t;
38
- switch (token.kin) {
39
- case ColorKind.RGB:
40
- case ColorKind.RGBA:
41
- t = rgb2hsl(token);
42
- if (t == null) {
43
- return null;
44
- }
45
- values.push(...t);
46
- break;
47
- case ColorKind.HEX:
48
- case ColorKind.LIT:
49
- values.push(...hex2hsl(token));
50
- break;
51
- case ColorKind.HWB:
52
- values.push(...hwb2hsl(token));
53
- break;
54
- case ColorKind.OKLAB:
55
- t = oklab2hsl(token);
56
- if (t == null) {
57
- return null;
58
- }
59
- values.push(...t);
60
- break;
61
- case ColorKind.OKLCH:
62
- t = oklch2hsl(token);
63
- if (t == null) {
64
- return null;
65
- }
66
- values.push(...t);
67
- break;
68
- case ColorKind.LAB:
69
- values.push(...lab2hsl(token));
70
- break;
71
- case ColorKind.LCH:
72
- values.push(...lch2hsl(token));
73
- break;
74
- case ColorKind.COLOR:
75
- // @ts-ignore
76
- values.push(...srgb2hsl(...color2srgbvalues(token)));
77
- break;
78
- }
79
- if (values.length > 0) {
80
- return values2hsltoken(values);
81
- }
82
- }
83
- else if (to == ColorKind.HWB) {
84
- switch (token.kin) {
85
- case ColorKind.RGB:
86
- case ColorKind.RGBA:
87
- values.push(...rgb2hwb(token));
88
- break;
89
- case ColorKind.HEX:
90
- case ColorKind.LIT:
91
- values.push(...hex2hsl(token));
92
- break;
93
- case ColorKind.HSL:
94
- case ColorKind.HSLA:
95
- values.push(...hsl2hwb(token));
96
- break;
97
- case ColorKind.OKLAB:
98
- values.push(...oklab2hwb(token));
99
- break;
100
- case ColorKind.OKLCH:
101
- values.push(...oklch2hwb(token));
102
- break;
103
- case ColorKind.LAB:
104
- values.push(...lab2hwb(token));
105
- break;
106
- case ColorKind.LCH:
107
- values.push(...lch2hwb(token));
108
- break;
109
- }
110
- if (values.length > 0) {
111
- return values2hwbtoken(values);
112
- }
113
- }
114
- else if (to == ColorKind.RGB) {
115
- let t;
116
- switch (token.kin) {
117
- case ColorKind.HEX:
118
- case ColorKind.LIT:
119
- values.push(...hex2rgb(token));
120
- break;
121
- case ColorKind.HSL:
122
- t = hsl2rgb(token);
123
- if (t == null) {
124
- return null;
125
- }
126
- values.push(...t);
127
- break;
128
- case ColorKind.HWB:
129
- t = hwb2rgb(token);
130
- if (t == null) {
131
- return null;
132
- }
133
- values.push(...t);
134
- break;
135
- case ColorKind.OKLAB:
136
- t = oklab2rgb(token);
137
- if (t == null) {
138
- return null;
139
- }
140
- values.push(...t);
141
- break;
142
- case ColorKind.OKLCH:
143
- t = oklch2rgb(token);
144
- if (t == null) {
145
- return null;
146
- }
147
- values.push(...t);
148
- break;
149
- case ColorKind.LAB:
150
- t = lab2rgb(token);
151
- if (t == null) {
152
- return null;
153
- }
154
- values.push(...t);
155
- break;
156
- case ColorKind.LCH:
157
- t = lch2rgb(token);
158
- if (t == null) {
159
- return null;
160
- }
161
- values.push(...t);
162
- break;
163
- case ColorKind.COLOR:
164
- // @ts-ignore
165
- values.push(...srgb2rgb(...color2srgbvalues(token)));
166
- break;
167
- }
168
- if (values.length > 0) {
169
- return values2rgbtoken(values);
170
- }
171
- }
172
- else if (to == ColorKind.LAB) {
173
- switch (token.kin) {
174
- case ColorKind.HEX:
175
- case ColorKind.LIT:
176
- values.push(...hex2lab(token));
177
- break;
178
- case ColorKind.RGB:
179
- case ColorKind.RGBA:
180
- values.push(...rgb2lab(token));
181
- break;
182
- case ColorKind.HSL:
183
- case ColorKind.HSLA:
184
- values.push(...hsl2lab(token));
185
- break;
186
- case ColorKind.HWB:
187
- values.push(...hwb2lab(token));
188
- break;
189
- case ColorKind.LCH:
190
- values.push(...lch2lab(token));
191
- break;
192
- case ColorKind.OKLAB:
193
- values.push(...oklab2lab(token));
194
- break;
195
- case ColorKind.OKLCH:
196
- values.push(...oklch2lab(token));
197
- break;
198
- case ColorKind.COLOR:
199
- // @ts-ignore
200
- values.push(...srgb2lab(...color2srgbvalues(token)));
201
- break;
202
- }
203
- if (values.length > 0) {
204
- return values2colortoken(values, to);
205
- }
206
- }
207
- else if (to == ColorKind.LCH) {
208
- switch (token.kin) {
209
- case ColorKind.HEX:
210
- case ColorKind.LIT:
211
- values.push(...hex2lch(token));
212
- break;
213
- case ColorKind.RGB:
214
- case ColorKind.RGBA:
215
- values.push(...rgb2lch(token));
216
- break;
217
- case ColorKind.HSL:
218
- case ColorKind.HSLA:
219
- values.push(...hsl2lch(token));
220
- break;
221
- case ColorKind.HWB:
222
- values.push(...hwb2lch(token));
223
- break;
224
- case ColorKind.LAB:
225
- values.push(...lab2lch(token));
226
- break;
227
- case ColorKind.OKLAB:
228
- values.push(...oklab2lch(token));
229
- break;
230
- case ColorKind.OKLCH:
231
- values.push(...oklch2lch(token));
232
- break;
233
- case ColorKind.COLOR:
234
- // @ts-ignore
235
- values.push(...srgb2lch(...color2srgbvalues(token)));
236
- break;
237
- }
238
- if (values.length > 0) {
239
- return values2colortoken(values, to);
240
- }
241
- }
242
- else if (to == ColorKind.OKLAB) {
243
- switch (token.kin) {
244
- case ColorKind.HEX:
245
- case ColorKind.LIT:
246
- values.push(...hex2oklab(token));
247
- break;
248
- case ColorKind.RGB:
249
- case ColorKind.RGBA:
250
- values.push(...rgb2oklab(token));
251
- break;
252
- case ColorKind.HSL:
253
- case ColorKind.HSLA:
254
- values.push(...hsl2oklab(token));
255
- break;
256
- case ColorKind.HWB:
257
- values.push(...hwb2oklab(token));
258
- break;
259
- case ColorKind.LAB:
260
- values.push(...lab2oklab(token));
261
- break;
262
- case ColorKind.LCH:
263
- values.push(...lch2oklab(token));
264
- break;
265
- case ColorKind.OKLCH:
266
- values.push(...oklch2oklab(token));
267
- break;
268
- case ColorKind.COLOR:
269
- // @ts-ignore
270
- values.push(...srgb2oklab(...color2srgbvalues(token)));
271
- break;
272
- }
273
- if (values.length > 0) {
274
- return values2colortoken(values, to);
275
- }
276
- }
277
- else if (to == ColorKind.OKLCH) {
278
- switch (token.kin) {
279
- case ColorKind.HEX:
280
- case ColorKind.LIT:
281
- values.push(...hex2oklch(token));
282
- break;
283
- case ColorKind.RGB:
284
- case ColorKind.RGBA:
285
- values.push(...rgb2oklch(token));
286
- break;
287
- case ColorKind.HSL:
288
- case ColorKind.HSLA:
289
- values.push(...hsl2oklch(token));
290
- break;
291
- case ColorKind.HWB:
292
- values.push(...hwb2oklch(token));
293
- break;
294
- case ColorKind.LAB:
295
- values.push(...lab2oklch(token));
296
- break;
297
- case ColorKind.OKLAB:
298
- values.push(...oklab2oklch(token));
299
- break;
300
- case ColorKind.LCH:
301
- values.push(...lch2oklch(token));
302
- break;
303
- case ColorKind.COLOR:
304
- // @ts-ignore
305
- values.push(...srgb2oklch(...color2srgbvalues(token)));
306
- // switch (to) {
307
- //
308
- // case ColorKind.SRGB:
309
- //
310
- // values.push(...val);
311
- // break;
312
- //
313
- // case ColorKind.SRGB_LINEAR:
314
- //
315
- // // @ts-ignore
316
- // values.push(...srgb2lsrgbvalues(...val));
317
- // break;
318
- //
319
- // case ColorKind.DISPLAY_P3:
320
- //
321
- // // @ts-ignore
322
- // values.push(...srgb2p3values(...val));
323
- // break;
324
- //
325
- // case ColorKind.PROPHOTO_RGB:
326
- //
327
- // // @ts-ignore
328
- // values.push(...srgb2prophotorgbvalues(...val));
329
- // break;
330
- //
331
- // case ColorKind.A98_RGB:
332
- //
333
- // // @ts-ignore
334
- // values.push(...srgb2a98values(...val));
335
- // break;
336
- //
337
- // case ColorKind.REC2020:
338
- //
339
- // // @ts-ignore
340
- // values.push(...srgb2rec2020values(...val));
341
- // break;
342
- //
343
- // case ColorKind.XYZ:
344
- // case ColorKind.XYZ_D65:
345
- //
346
- // // @ts-ignore
347
- // values.push(...srgb2xyz(...val));
348
- // break;
349
- //
350
- // case ColorKind.XYZ_D50:
351
- //
352
- // // @ts-ignore
353
- // values.push(...(XYZ_D65_to_D50(...srgb2xyz(...val))));
354
- // break;
355
- // }
356
- break;
357
- }
358
- if (values.length > 0) {
359
- return values2colortoken(values, to);
360
- }
361
- }
362
- else if (colorFuncColorSpace.includes(ColorKind[to].toLowerCase().replaceAll('_', '-'))) {
363
- let t;
364
- switch (token.kin) {
365
- case ColorKind.HEX:
366
- case ColorKind.LIT:
367
- values.push(...hex2srgb(token));
368
- break;
369
- case ColorKind.RGB:
370
- case ColorKind.RGBA:
371
- t = rgb2srgb(token);
372
- if (t == null) {
373
- return null;
374
- }
375
- values.push(...t);
376
- break;
377
- case ColorKind.HSL:
378
- case ColorKind.HSLA:
379
- t = hsl2srgb(token);
380
- if (t == null) {
381
- return null;
382
- }
383
- values.push(...t);
384
- break;
385
- case ColorKind.HWB:
386
- t = hwb2srgb(token);
387
- if (t == null) {
388
- return null;
389
- }
390
- values.push(...t);
391
- break;
392
- case ColorKind.LAB:
393
- t = lab2srgb(token);
394
- if (t == null) {
395
- return null;
396
- }
397
- values.push(...t);
398
- break;
399
- case ColorKind.OKLAB:
400
- t = oklab2srgb(token);
401
- if (t == null) {
402
- return null;
403
- }
404
- values.push(...t);
405
- break;
406
- case ColorKind.LCH:
407
- t = lch2srgb(token);
408
- if (t == null) {
409
- return null;
410
- }
411
- values.push(...t);
412
- break;
413
- case ColorKind.OKLCH:
414
- t = color2srgbvalues(token);
415
- if (t == null) {
416
- return null;
417
- }
418
- values.push(...t);
419
- break;
420
- case ColorKind.COLOR:
421
- const val = color2srgbvalues(token);
422
- if (val == null) {
423
- return null;
424
- }
425
- switch (to) {
426
- case ColorKind.SRGB:
427
- values.push(...val);
428
- break;
429
- case ColorKind.SRGB_LINEAR:
430
- // @ts-ignore
431
- values.push(...srgb2lsrgbvalues(...val));
432
- break;
433
- case ColorKind.DISPLAY_P3:
434
- // @ts-ignore
435
- values.push(...srgb2p3values(...val));
436
- break;
437
- case ColorKind.PROPHOTO_RGB:
438
- // @ts-ignore
439
- values.push(...srgb2prophotorgbvalues(...val));
440
- break;
441
- case ColorKind.A98_RGB:
442
- // @ts-ignore
443
- values.push(...srgb2a98values(...val));
444
- break;
445
- case ColorKind.REC2020:
446
- // @ts-ignore
447
- values.push(...srgb2rec2020values(...val));
448
- break;
449
- case ColorKind.XYZ:
450
- case ColorKind.XYZ_D65:
451
- // @ts-ignore
452
- values.push(...srgb2xyz(...val));
453
- break;
454
- case ColorKind.XYZ_D50:
455
- // @ts-ignore
456
- values.push(...(XYZ_D65_to_D50(...srgb2xyz(...val))));
457
- break;
458
- }
459
- break;
460
- }
461
- if (values.length > 0) {
462
- return values2colortoken(values, to);
463
- }
464
- }
465
- return null;
466
- }
467
- function minmax(value, min, max) {
468
- if (value < min) {
469
- return min;
470
- }
471
- if (value > max) {
472
- return max;
473
- }
474
- return value;
475
- }
476
- function color2srgbvalues(token) {
477
- const components = getComponents(token);
478
- if (components == null) {
479
- return null;
480
- }
481
- const colorSpace = components.shift();
482
- let values = components.map((val) => getNumber(val));
483
- switch (colorSpace.val) {
484
- case 'display-p3':
485
- // @ts-ignore
486
- values = p32srgbvalues(...values);
487
- break;
488
- case 'srgb-linear':
489
- // @ts-ignore
490
- values = lsrgb2srgbvalues(...values);
491
- break;
492
- case 'prophoto-rgb':
493
- // @ts-ignore
494
- values = prophotorgb2srgbvalues(...values);
495
- break;
496
- case 'a98-rgb':
497
- // @ts-ignore
498
- values = a98rgb2srgbvalues(...values);
499
- break;
500
- case 'rec2020':
501
- // @ts-ignore
502
- values = rec20202srgb(...values);
503
- break;
504
- case 'xyz':
505
- case 'xyz-d65':
506
- // @ts-ignore
507
- values = xyz2srgb(...values);
508
- break;
509
- case 'xyz-d50':
510
- // @ts-ignore
511
- values = xyzd502srgb(...values);
512
- break;
513
- // case srgb:
514
- }
515
- return values;
516
- }
517
- function values2hsltoken(values) {
518
- const to = ColorKind.HSL;
519
- const chi = [
520
- { typ: EnumToken.AngleTokenType, val: String(values[0] * 360), unit: 'deg' },
521
- { typ: EnumToken.PercentageTokenType, val: String(values[1] * 100) },
522
- { typ: EnumToken.PercentageTokenType, val: String(values[2] * 100) },
523
- ];
524
- if (values.length == 4) {
525
- chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
526
- }
527
- return {
528
- typ: EnumToken.ColorTokenType,
529
- val: ColorKind[to].toLowerCase().replaceAll('_', '-'),
530
- chi,
531
- kin: to
532
- };
533
- }
534
- function values2rgbtoken(values) {
535
- const to = ColorKind.RGB;
536
- const chi = [
537
- { typ: EnumToken.NumberTokenType, val: String(values[0]) },
538
- { typ: EnumToken.NumberTokenType, val: String(values[1]) },
539
- { typ: EnumToken.NumberTokenType, val: String(values[2]) },
540
- ];
541
- if (values.length == 4) {
542
- chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
543
- }
544
- return {
545
- typ: EnumToken.ColorTokenType,
546
- val: ColorKind[to],
547
- chi,
548
- kin: to
549
- };
550
- }
551
- function values2hwbtoken(values) {
552
- const to = ColorKind.HWB;
553
- const chi = [
554
- { typ: EnumToken.AngleTokenType, val: String(values[0] * 360), unit: 'deg' },
555
- { typ: EnumToken.PercentageTokenType, val: String(values[1] * 100) },
556
- { typ: EnumToken.PercentageTokenType, val: String(values[2] * 100) },
557
- ];
558
- if (values.length == 4) {
559
- chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
560
- }
561
- return {
562
- typ: EnumToken.ColorTokenType,
563
- val: ColorKind[to],
564
- chi,
565
- kin: to
566
- };
567
- }
568
- function values2colortoken(values, to) {
569
- const chi = [
570
- { typ: EnumToken.NumberTokenType, val: String(values[0]) },
571
- { typ: EnumToken.NumberTokenType, val: String(values[1]) },
572
- { typ: EnumToken.NumberTokenType, val: String(values[2]) },
573
- ];
574
- if (values.length == 4) {
575
- chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
576
- }
577
- const colorSpace = ColorKind[to].toLowerCase().replaceAll('_', '-');
578
- return colorFuncColorSpace.includes(colorSpace) ? {
579
- typ: EnumToken.ColorTokenType,
580
- val: 'color',
581
- chi: [{ typ: EnumToken.IdenTokenType, val: colorSpace }].concat(chi),
582
- kin: ColorKind.COLOR
583
- } : {
584
- typ: EnumToken.ColorTokenType,
585
- val: colorSpace,
586
- chi,
587
- kin: to
588
- };
589
- }
590
- /**
591
- * clamp color values
592
- * @param token
593
- */
594
- function clamp(token) {
595
- if (token.kin == ColorKind.RGB || token.kin == ColorKind.RGBA) {
596
- token.chi.filter((token) => ![EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType].includes(token.typ)).forEach((token, index) => {
597
- if (index <= 2) {
598
- if (token.typ == EnumToken.NumberTokenType) {
599
- token.val = String(minmax(+token.val, 0, 255));
600
- }
601
- else if (token.typ == EnumToken.PercentageTokenType) {
602
- token.val = String(minmax(+token.val, 0, 100));
603
- }
604
- }
605
- else {
606
- if (token.typ == EnumToken.NumberTokenType) {
607
- token.val = String(minmax(+token.val, 0, 1));
608
- }
609
- else if (token.typ == EnumToken.PercentageTokenType) {
610
- token.val = String(minmax(+token.val, 0, 100));
611
- }
612
- }
613
- });
614
- }
615
- return token;
616
- }
617
- function getNumber(token) {
618
- if (token.typ == EnumToken.IdenTokenType && token.val == 'none') {
619
- return 0;
620
- }
621
- // @ts-ignore
622
- return token.typ == EnumToken.PercentageTokenType ? token.val / 100 : +token.val;
623
- }
624
- /**
625
- * convert angle to turn
626
- * @param token
627
- */
628
- function getAngle(token) {
629
- if (token.typ == EnumToken.IdenTokenType) {
630
- if (token.val == 'none') {
631
- return 0;
632
- }
633
- }
634
- if (token.typ == EnumToken.AngleTokenType) {
635
- switch (token.unit) {
636
- case 'deg':
637
- // @ts-ignore
638
- return token.val / 360;
639
- case 'rad':
640
- // @ts-ignore
641
- return token.val / (2 * Math.PI);
642
- case 'grad':
643
- // @ts-ignore
644
- return token.val / 400;
645
- case 'turn':
646
- // @ts-ignore
647
- return +token.val;
648
- }
649
- }
650
- // @ts-ignore
651
- return token.val / 360;
652
- }
653
-
654
- export { clamp, color2srgbvalues, convert, getAngle, getNumber, minmax, values2hsltoken };