@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,100 +1,107 @@
1
- import { EnumToken, ColorType } 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 { isIdentColor } from '../syntax.js';
8
- import { color2RgbToken, lch2RgbToken, lab2RgbToken, oklch2RgbToken, oklab2RgbToken, cmyk2RgbToken, hwb2RgbToken, hsl2RgbToken, hex2RgbToken, cmyk2rgbvalues } from './rgb.js';
1
+ import { ColorType, EnumToken } from '../../ast/types.js';
2
+ import { color2RgbToken, lch2RgbToken, lab2RgbToken, oklch2RgbToken, oklab2RgbToken, cmyk2RgbToken, hwb2RgbToken, hsl2RgbToken, hex2RgbToken } from './rgb.js';
9
3
  import { color2HslToken, lch2HslToken, lab2HslToken, oklch2HslToken, oklab2HslToken, cmyk2HslToken, hwb2HslToken, hex2HslToken, rgb2HslToken } from './hsl.js';
10
4
  import { color2hwbToken, cmyk2hwbToken, lch2hwbToken, lab2hwbToken, oklch2hwbToken, oklab2hwbToken, hsl2hwbToken, rgb2hwbToken } from './hwb.js';
11
5
  import { color2labToken, oklch2labToken, oklab2labToken, lch2labToken, cmyk2labToken, hwb2labToken, hsl2labToken, rgb2labToken, hex2labToken } from './lab.js';
12
6
  import { color2lchToken, oklch2lchToken, oklab2lchToken, lab2lchToken, cmyk2lchToken, hwb2lchToken, hsl2lchToken, rgb2lchToken, hex2lchToken } from './lch.js';
13
7
  import { color2oklabToken, oklch2oklabToken, lch2oklabToken, lab2oklabToken, cmyk2oklabToken, hwb2oklabToken, hsl2oklabToken, rgb2oklabToken, hex2oklabToken } from './oklab.js';
14
8
  import { color2oklchToken, lch2oklchToken, oklab2oklchToken, lab2oklchToken, cmyk2oklchToken, hwb2oklchToken, hsl2oklchToken, rgb2oklchToken, hex2oklchToken } from './oklch.js';
15
- import { colorFuncColorSpace, colorPrecision, anglePrecision } from './utils/constants.js';
16
- import { getComponents } from './utils/components.js';
17
- import { oklch2srgbvalues, lch2srgbvalues, oklab2srgbvalues, lab2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
9
+ import { getColorComponents } from './utils/components.js';
10
+ import { oklch2srgbvalues, lch2srgbvalues, oklab2srgbvalues, lab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
18
11
  import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
19
12
  import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
20
- import { srgb2xyz_d50, srgb2xyz } from './xyz.js';
13
+ import { srgb2xyz_d65, srgb2xyz } from './xyz.js';
21
14
  import { p32srgbvalues, srgb2p3values } from './p3.js';
22
15
  import { xyzd502srgb } from './xyzd50.js';
23
16
  import { colorMix } from './color-mix.js';
24
17
  import { reduceHexValue, rgb2HexToken, color2HexToken, lch2HexToken, lab2HexToken, oklch2HexToken, oklab2HexToken, cmyk2HexToken, hwb2HexToken, hsl2HexToken } from './hex.js';
25
- import { parseRelativeColor } from './relativecolor.js';
18
+ import { parseRelativeColorComponents } from './relativecolor.js';
19
+ import { isIdentColor } from '../syntax.js';
26
20
  import { color2cmykToken, lch2cmykToken, lab2cmykToken, oklch2cmykToken, oklab2cmyk, hwb2cmykToken, hsl2cmykToken, rgb2cmykToken } from './cmyk.js';
27
21
  import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
28
- import { epsilon } from '../../ast/transform/utils.js';
29
- import '../../renderer/sourcemap/lib/encode.js';
22
+ import { definedPropertySettings, colorPrecision, colorFuncColorSpace, epsilon, anglePrecision } from '../constants.js';
23
+ import { trimArray } from '../../validation/match.js';
24
+ import { alpha } from './alpha.js';
25
+ import { equalsIgnoreCase } from '../../parser/utils/text.js';
30
26
 
31
27
  /**
32
28
  * Converts a color to another color space
33
29
  * @param token
34
30
  * @param to
35
31
  *
36
- * @private
37
- *
38
- * <code>
32
+ * ```ts
39
33
  *
40
34
  * const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
41
35
  * const result = convertColor(token, ColorType.LCH);
42
36
  *
43
- * </code>
37
+ * ```
44
38
  */
45
39
  function convertColor(token, to) {
46
40
  if (token.kin == ColorType.SYS ||
47
41
  token.kin == ColorType.DPSYS ||
48
- (isIdentColor(token) &&
49
- 'currentcolor' == token.val.toLowerCase())) {
42
+ (isIdentColor(token) && "currentcolor" == token.val.toLowerCase())) {
50
43
  return token;
51
44
  }
45
+ if (token.kin == ColorType.ALPHA) {
46
+ const args = token.chi.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
47
+ if (args.at(-2)?.typ === EnumToken.LiteralTokenType && "/" === args.at(-2)?.val) {
48
+ args.splice(args.length - 2, 1);
49
+ }
50
+ // @ts-expect-error
51
+ token = alpha(...trimArray(args.slice(1)));
52
+ if (token == null) {
53
+ return null;
54
+ }
55
+ }
52
56
  if (token.kin == ColorType.COLOR_MIX && to != ColorType.COLOR_MIX) {
53
57
  const children = token.chi.reduce((acc, t) => {
54
- if (t.typ == EnumToken.ColorTokenType) {
55
- acc.push([t]);
58
+ if (t.typ === EnumToken.CommaTokenType) {
59
+ acc.push([]);
56
60
  }
57
- else {
58
- if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ)) {
59
- acc[acc.length - 1].push(t);
60
- }
61
+ else if (t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType) {
62
+ acc[acc.length - 1].push(t);
61
63
  }
62
64
  return acc;
63
65
  }, [[]]);
64
- token = colorMix(children[0][1], children[0][2], children[1][0], children[1][1], children[2][0], children[2][1]);
66
+ token = colorMix(...children.flat(2));
65
67
  if (token == null) {
66
68
  return null;
67
69
  }
68
70
  }
69
- if (token.cal == 'rel' && ['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch', 'color'].includes(token.val.toLowerCase())) {
70
- const chi = getComponents(token);
71
- const offset = token.val == 'color' ? 2 : 1;
71
+ if (token.cal == "rel" &&
72
+ ["rgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch", "color"].some((t) => equalsIgnoreCase(t, token.val))) {
73
+ const chi = getColorComponents(token);
74
+ const offset = token.val == "color" ? 2 : 1;
72
75
  if (chi != null) {
73
76
  // @ts-ignore
74
77
  const color = chi[1];
75
- const components = parseRelativeColor(token.val.toLowerCase() == 'color' ? chi[offset].val : token.val, color, chi[offset + 1], chi[offset + 2], chi[offset + 3], chi[offset + 4]);
76
- if (components != null) {
77
- token = {
78
- ...token,
79
- chi: [...(token.val == 'color' ? [chi[offset]] : []), ...Object.values(components)],
80
- kin: ColorType[token.val.toUpperCase().replaceAll('-', '_')]
81
- };
82
- delete token.cal;
78
+ const components = parseRelativeColorComponents(equalsIgnoreCase(token.val, "color")
79
+ ? chi[offset].val
80
+ : token.val, color, chi[offset + 1], chi[offset + 2], chi[offset + 3], chi[offset + 4]);
81
+ if (components == null) {
82
+ return null;
83
83
  }
84
+ let { cal, ...tk } = {
85
+ ...token,
86
+ chi: [...(token.val == "color" ? [chi[offset]] : []), ...Object.values(components)],
87
+ kin: ColorType[token.val.toUpperCase().replaceAll("-", "_")],
88
+ };
89
+ Object.defineProperty(tk, "loc", { ...definedPropertySettings, value: token.loc });
90
+ token = tk;
84
91
  }
85
92
  }
86
93
  if (token.kin == to) {
87
94
  if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
88
95
  token.val = reduceHexValue(token.val);
89
- token.kin = token.val[0] == '#' ? ColorType.HEX : ColorType.LIT;
96
+ token.kin = token.val[0] == "#" ? ColorType.HEX : ColorType.LIT;
90
97
  }
91
98
  return token;
92
99
  }
93
100
  if (token.kin == ColorType.COLOR) {
94
- const colorSpace = token.chi.find(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
95
- if (colorSpace.val == ColorType[to].toLowerCase().replaceAll('_', '-')) {
101
+ const colorSpace = token.chi.find((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
102
+ if (colorSpace.val == ColorType[to].toLowerCase().replaceAll("_", "-")) {
96
103
  for (const chi of token.chi) {
97
- if (chi.typ == EnumToken.NumberTokenType && typeof chi.val == 'number') {
104
+ if (chi.typ == EnumToken.NumberTokenType && typeof chi.val == "number") {
98
105
  chi.val = toPrecisionValue(getNumber(chi));
99
106
  }
100
107
  }
@@ -169,7 +176,6 @@ function convertColor(token, to) {
169
176
  return oklch2cmykToken(token);
170
177
  case ColorType.LAB:
171
178
  return lab2cmykToken(token);
172
- //
173
179
  case ColorType.LCH:
174
180
  return lch2cmykToken(token);
175
181
  case ColorType.COLOR:
@@ -183,8 +189,8 @@ function convertColor(token, to) {
183
189
  const val = reduceHexValue(token.val);
184
190
  return {
185
191
  typ: EnumToken.ColorTokenType,
186
- val,
187
- kin: val[0] == '#' ? ColorType.HEX : ColorType.LIT
192
+ val: val,
193
+ kin: val[0] == "#" ? ColorType.HEX : ColorType.LIT,
188
194
  };
189
195
  }
190
196
  case ColorType.HSL:
@@ -331,7 +337,7 @@ function convertColor(token, to) {
331
337
  return color2oklchToken(token);
332
338
  }
333
339
  }
334
- else if (colorFuncColorSpace.includes(ColorType[to].toLowerCase().replaceAll('_', '-').toLowerCase().replaceAll('_', '-'))) {
340
+ else if (colorFuncColorSpace.includes(ColorType[to].toLowerCase().replaceAll("_", "-").toLowerCase().replaceAll("_", "-"))) {
335
341
  switch (token.kin) {
336
342
  case ColorType.HEX:
337
343
  case ColorType.LIT:
@@ -389,7 +395,7 @@ function hwb2colorToken(token, to) {
389
395
  return values2colortoken(values, to);
390
396
  }
391
397
  function cmyk2colorToken(token, to) {
392
- const values = cmyk2rgbvalues(token);
398
+ const values = cmyk2srgbvalues(token);
393
399
  if (values == null) {
394
400
  return null;
395
401
  }
@@ -463,58 +469,54 @@ function srgb2srgbcolorspace(val, to) {
463
469
  break;
464
470
  case ColorType.XYZ_D50:
465
471
  // @ts-ignore
466
- values.push(...srgb2xyz_d50(...val));
472
+ values.push(...srgb2xyz_d65(...val));
467
473
  break;
468
474
  }
469
475
  return values;
470
476
  }
471
477
  function minmax(value, min, max) {
472
- if (value < min) {
473
- return min;
474
- }
475
- if (value > max) {
476
- return max;
477
- }
478
- return value;
478
+ return value < min ? min : value > max ? max : value;
479
479
  }
480
480
  function color2srgbvalues(token) {
481
- const components = getComponents(token);
481
+ const components = getColorComponents(token);
482
482
  if (components == null) {
483
483
  return null;
484
484
  }
485
485
  const colorSpace = components.shift();
486
486
  let values = components.map((val) => getNumber(val));
487
487
  switch (colorSpace.val.toLowerCase()) {
488
- case 'display-p3':
488
+ case "display-p3":
489
489
  // @ts-ignore
490
490
  values = p32srgbvalues(...values);
491
491
  break;
492
- case 'srgb-linear':
492
+ case "srgb-linear":
493
493
  // @ts-ignore
494
494
  values = lsrgb2srgbvalues(...values);
495
495
  break;
496
- case 'prophoto-rgb':
496
+ case "prophoto-rgb":
497
497
  // @ts-ignore
498
498
  values = prophotorgb2srgbvalues(...values);
499
499
  break;
500
- case 'a98-rgb':
500
+ case "a98-rgb":
501
501
  // @ts-ignore
502
502
  values = a98rgb2srgbvalues(...values);
503
503
  break;
504
- case 'rec2020':
504
+ case "rec2020":
505
505
  // @ts-ignore
506
506
  values = rec20202srgb(...values);
507
507
  break;
508
- case 'xyz':
509
- case 'xyz-d65':
508
+ case "xyz":
509
+ case "xyz-d65":
510
510
  // @ts-ignore
511
511
  values = xyz2srgb(...values);
512
512
  break;
513
- case 'xyz-d50':
513
+ case "xyz-d50":
514
514
  // @ts-ignore
515
515
  values = xyzd502srgb(...values);
516
516
  break;
517
- // case srgb:
517
+ }
518
+ if (values.length == 4) {
519
+ values[3] = toPrecisionValue(values[3], 2);
518
520
  }
519
521
  return values;
520
522
  }
@@ -528,31 +530,34 @@ function values2colortoken(values, to) {
528
530
  if (values.length == 4) {
529
531
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
530
532
  typ: EnumToken.PercentageTokenType,
531
- val: values[3] * 100
533
+ val: toPrecisionValue(values[3], 2) * 100,
532
534
  });
533
535
  }
534
- const colorSpace = ColorType[to].toLowerCase().replaceAll('_', '-');
535
- return colorFuncColorSpace.includes(colorSpace) ? {
536
- typ: EnumToken.ColorTokenType,
537
- val: 'color',
538
- chi: [{ typ: EnumToken.IdenTokenType, val: colorSpace }].concat(chi),
539
- kin: ColorType.COLOR
540
- } : {
541
- typ: EnumToken.ColorTokenType,
542
- val: colorSpace,
543
- chi,
544
- kin: to
545
- };
536
+ const colorSpace = ColorType[to].toLowerCase().replaceAll("_", "-");
537
+ return colorFuncColorSpace.includes(colorSpace)
538
+ ? {
539
+ typ: EnumToken.ColorTokenType,
540
+ val: "color",
541
+ chi: [{ typ: EnumToken.IdenTokenType, val: colorSpace }].concat(chi),
542
+ kin: ColorType.COLOR,
543
+ }
544
+ : {
545
+ typ: EnumToken.ColorTokenType,
546
+ val: colorSpace,
547
+ chi,
548
+ kin: to,
549
+ };
546
550
  }
547
551
  function getNumber(token) {
548
- if (token.typ == EnumToken.IdenTokenType && token.val == 'none') {
552
+ if (token.typ == EnumToken.IdenTokenType && token.val == "none") {
549
553
  return 0;
550
554
  }
551
555
  let val;
552
556
  // @ts-ignore
553
- if (typeof token.val != 'number' && token.val?.typ == EnumToken.FractionTokenType) {
554
- // @ts-ignore
555
- val = token.val.l.val / token.val.r.val;
557
+ if (typeof token.val != "number" && token.val?.typ == EnumToken.FractionTokenType) {
558
+ val = (token.val.l.val /
559
+ // @ts-expect-error
560
+ token.val.r.val);
556
561
  }
557
562
  else {
558
563
  val = token.val;
@@ -566,22 +571,22 @@ function getNumber(token) {
566
571
  */
567
572
  function getAngle(token) {
568
573
  if (token.typ == EnumToken.IdenTokenType) {
569
- if (token.val == 'none') {
574
+ if (token.val == "none") {
570
575
  return 0;
571
576
  }
572
577
  }
573
578
  if (token.typ == EnumToken.AngleTokenType) {
574
579
  switch (token.unit) {
575
- case 'deg':
580
+ case "deg":
576
581
  // @ts-ignore
577
582
  return token.val / 360;
578
- case 'rad':
583
+ case "rad":
579
584
  // @ts-ignore
580
585
  return token.val / (2 * Math.PI);
581
- case 'grad':
586
+ case "grad":
582
587
  // @ts-ignore
583
588
  return token.val / 400;
584
- case 'turn':
589
+ case "turn":
585
590
  // @ts-ignore
586
591
  return +token.val;
587
592
  }
@@ -589,12 +594,13 @@ function getAngle(token) {
589
594
  // @ts-ignore
590
595
  return token.val / 360;
591
596
  }
592
- function toPrecisionValue(value) {
593
- value = +value.toFixed(colorPrecision);
597
+ function toPrecisionValue(value, precision = colorPrecision) {
598
+ const div = Math.pow(10, precision);
599
+ value = Math.round(value * div) / div;
594
600
  return Math.abs(value) < epsilon ? 0 : value;
595
601
  }
596
- function toPrecisionAngle(angle) {
597
- angle = +angle.toPrecision(colorPrecision);
602
+ function toPrecisionAngle(angle, precision = colorPrecision) {
603
+ angle = toPrecisionValue(angle, precision);
598
604
  if (Math.abs(angle) >= 360) {
599
605
  angle %= 360;
600
606
  }
@@ -1,39 +1,28 @@
1
1
  import { EnumToken, ColorType } 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
2
  import { color2srgbvalues, getNumber } from './color.js';
8
3
  import { srgb2rgb, cmyk2rgbvalues, hwb2rgbvalues, hsl2rgbvalues } from './rgb.js';
9
- import { COLORS_NAMES, NAMES_COLORS } from './utils/constants.js';
10
- import { getComponents } from './utils/components.js';
4
+ import { getColorComponents } from './utils/components.js';
11
5
  import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
12
- import '../../renderer/sourcemap/lib/encode.js';
6
+ import { COLORS_NAMES, NAMES_COLORS } from '../constants.js';
13
7
 
14
8
  function toHexString(acc, value) {
15
- return acc + value.toString(16).padStart(2, '0');
9
+ return acc + value.toString(16).padStart(2, "0");
16
10
  }
17
11
  function reduceHexValue(value) {
18
- if (value[0] != '#') {
12
+ if (value[0] != "#") {
19
13
  value = COLORS_NAMES[value.toLowerCase()] ?? value;
20
14
  }
21
15
  const named_color = NAMES_COLORS[expandHexValue(value)];
22
16
  if (value.length == 7) {
23
- if (value[1] == value[2] &&
24
- value[3] == value[4] &&
25
- value[5] == value[6]) {
17
+ if (value[1] == value[2] && value[3] == value[4] && value[5] == value[6]) {
26
18
  value = `#${value[1]}${value[3]}${value[5]}`;
27
19
  }
28
20
  }
29
21
  else if (value.length == 9) {
30
- if (value[1] == value[2] &&
31
- value[3] == value[4] &&
32
- value[5] == value[6] &&
33
- value[7] == value[8]) {
34
- value = `#${value[1]}${value[3]}${value[5]}${value[7] == 'f' ? '' : value[7]}`;
22
+ if (value[1] == value[2] && value[3] == value[4] && value[5] == value[6] && value[7] == value[8]) {
23
+ value = `#${value[1]}${value[3]}${value[5]}${value[7] == "f" ? "" : value[7]}`;
35
24
  }
36
- if (value.endsWith('ff')) {
25
+ if (value.endsWith("ff")) {
37
26
  value = value.slice(0, -2);
38
27
  }
39
28
  }
@@ -81,49 +70,49 @@ function color2HexToken(token) {
81
70
  if (value == null) {
82
71
  return null;
83
72
  }
84
- return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, '0'), '#'));
73
+ return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, "0"), "#"));
85
74
  }
86
75
  function oklab2HexToken(token) {
87
76
  let value = oklab2srgbvalues(token);
88
77
  if (value == null) {
89
78
  return null;
90
79
  }
91
- return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, '0'), '#'));
80
+ return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, "0"), "#"));
92
81
  }
93
82
  function oklch2HexToken(token) {
94
83
  let value = oklch2srgbvalues(token);
95
84
  if (value == null) {
96
85
  return null;
97
86
  }
98
- return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, '0'), '#'));
87
+ return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, "0"), "#"));
99
88
  }
100
89
  function lab2HexToken(token) {
101
90
  let value = lab2srgbvalues(token);
102
91
  if (value == null) {
103
92
  return null;
104
93
  }
105
- return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, '0'), '#'));
94
+ return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, "0"), "#"));
106
95
  }
107
96
  function lch2HexToken(token) {
108
97
  let value = lch2srgbvalues(token);
109
98
  if (value == null) {
110
99
  return null;
111
100
  }
112
- return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, '0'), '#'));
101
+ return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, "0"), "#"));
113
102
  }
114
103
  function hexToken(value) {
115
104
  value = reduceHexValue(value);
116
105
  return {
117
106
  typ: EnumToken.ColorTokenType,
118
107
  val: value,
119
- kin: value[0] == '#' ? ColorType.HEX : ColorType.LIT
108
+ kin: value[0] == "#" ? ColorType.HEX : ColorType.LIT,
120
109
  };
121
110
  }
122
111
  function rgb2hexvalues(token) {
123
- let value = '#';
112
+ let value = "#";
124
113
  let t;
125
114
  // @ts-ignore
126
- const components = getComponents(token);
115
+ const components = getColorComponents(token);
127
116
  if (components == null || components.length < 3) {
128
117
  return null;
129
118
  }
@@ -131,8 +120,13 @@ function rgb2hexvalues(token) {
131
120
  for (let i = 0; i < 3; i++) {
132
121
  // @ts-ignore
133
122
  t = components[i];
134
- // @ts-ignore
135
- value += (t.typ == EnumToken.Iden && t.val == 'none' ? '0' : Math.round(getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 255 : 1))).toString(16).padStart(2, '0');
123
+ value +=
124
+ ( // @ts-expect-error
125
+ t.typ == EnumToken.Iden && t.val == "none"
126
+ ? "0"
127
+ : Math.round(getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 255 : 1)))
128
+ .toString(16)
129
+ .padStart(2, "0");
136
130
  }
137
131
  // @ts-ignore
138
132
  if (components.length == 4) {
@@ -143,7 +137,9 @@ function rgb2hexvalues(token) {
143
137
  // @ts-ignore
144
138
  if (v < 1) {
145
139
  // @ts-ignore
146
- value += Math.round(255 * getNumber(t)).toString(16).padStart(2, '0');
140
+ value += Math.round(255 * getNumber(t))
141
+ .toString(16)
142
+ .padStart(2, "0");
147
143
  }
148
144
  }
149
145
  return value;
@@ -156,7 +152,7 @@ function hsl2hexvalues(token) {
156
152
  if (t.length == 4) {
157
153
  t[3] = srgb2rgb(t[3]);
158
154
  }
159
- return `${t.reduce(toHexString, '#')}`;
155
+ return `${t.reduce(toHexString, "#")}`;
160
156
  }
161
157
  function hwb2hexvalues(token) {
162
158
  const t = hwb2rgbvalues(token);
@@ -166,7 +162,7 @@ function hwb2hexvalues(token) {
166
162
  if (t.length == 4) {
167
163
  t[3] = srgb2rgb(t[3]);
168
164
  }
169
- return `${t.reduce(toHexString, '#')}`;
165
+ return `${t.reduce(toHexString, "#")}`;
170
166
  }
171
167
  function cmyk2hexvalues(token) {
172
168
  const t = cmyk2rgbvalues(token);
@@ -176,7 +172,7 @@ function cmyk2hexvalues(token) {
176
172
  if (t.length == 4) {
177
173
  t[3] = srgb2rgb(t[3]);
178
174
  }
179
- return `#${t.reduce(toHexString, '')}`;
175
+ return `#${t.reduce(toHexString, "")}`;
180
176
  }
181
177
 
182
178
  export { cmyk2HexToken, cmyk2hexvalues, color2HexToken, expandHexValue, hsl2HexToken, hsl2hexvalues, hwb2HexToken, hwb2hexvalues, lab2HexToken, lch2HexToken, oklab2HexToken, oklch2HexToken, reduceHexValue, rgb2HexToken, rgb2hexvalues };
@@ -1,16 +1,9 @@
1
1
  import { hwb2hsv } from './hsv.js';
2
2
  import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber } from './color.js';
3
3
  import { lch2rgbvalues, lab2rgbvalues, cmyk2rgbvalues } from './rgb.js';
4
- import './utils/constants.js';
5
- import { getComponents } from './utils/components.js';
4
+ import { getColorComponents } from './utils/components.js';
6
5
  import { hex2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hslvalues } from './srgb.js';
7
6
  import { EnumToken, ColorType } from '../../ast/types.js';
8
- import '../../ast/minify.js';
9
- import '../../ast/walk.js';
10
- import '../../parser/parse.js';
11
- import '../../parser/tokenize.js';
12
- import '../../parser/utils/config.js';
13
- import '../../renderer/sourcemap/lib/encode.js';
14
7
 
15
8
  function hex2HslToken(token) {
16
9
  // @ts-ignore
@@ -81,20 +74,20 @@ function hslToken(values) {
81
74
  { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2]) * 100 },
82
75
  ];
83
76
  if (values.length == 4 && values[3] != 1) {
84
- chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
77
+ chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
85
78
  typ: EnumToken.PercentageTokenType,
86
- val: values[3] * 100
79
+ val: toPrecisionValue(values[3], 2) * 100,
87
80
  });
88
81
  }
89
82
  return {
90
83
  typ: EnumToken.ColorTokenType,
91
- val: 'hsl',
84
+ val: "hsl",
92
85
  chi,
93
- kin: ColorType.HSL
86
+ kin: ColorType.HSL,
94
87
  };
95
88
  }
96
89
  function rgb2hslvalues(token) {
97
- const chi = getComponents(token);
90
+ const chi = getColorComponents(token);
98
91
  if (chi == null || chi.length < 3) {
99
92
  return null;
100
93
  }
@@ -132,7 +125,7 @@ function hsv2hsl(h, s, v, a) {
132
125
  //If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
133
126
  //Otherwise sat*val/(2-(2-sat)*val)
134
127
  //Conditional is not operating with hue, it is reassigned!
135
- s * v / ((h = (2 - s) * v) < 1 ? h : 2 - h),
128
+ (s * v) / ((h = (2 - s) * v) < 1 ? h : 2 - h),
136
129
  h / 2, //Lightness is (2-sat)*val/2
137
130
  ];
138
131
  if (a != null) {
@@ -1,15 +1,8 @@
1
1
  import { hsl2hsv } from './hsv.js';
2
- import './utils/constants.js';
3
- import { getComponents } from './utils/components.js';
2
+ import { getColorComponents } from './utils/components.js';
4
3
  import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getAngle, getNumber } from './color.js';
5
- import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
6
4
  import { EnumToken, ColorType } from '../../ast/types.js';
7
- import '../../ast/minify.js';
8
- import '../../ast/walk.js';
9
- import '../../parser/parse.js';
10
- import '../../parser/tokenize.js';
11
- import '../../parser/utils/config.js';
12
- import '../../renderer/sourcemap/lib/encode.js';
5
+ import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
13
6
 
14
7
  function rgb2hwbToken(token) {
15
8
  const values = rgb2hwbvalues(token);
@@ -70,26 +63,26 @@ function color2hwbToken(token) {
70
63
  function hwbToken(values) {
71
64
  values[0] = toPrecisionAngle(values[0] * 360);
72
65
  const chi = [
73
- { typ: EnumToken.NumberTokenType, val: values[0] },
74
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1]) * 100 },
75
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2]) * 100 },
66
+ { typ: EnumToken.NumberTokenType, val: toPrecisionAngle(values[0]) },
67
+ { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1] * 100) },
68
+ { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2] * 100) },
76
69
  ];
77
70
  if (values.length == 4) {
78
- chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
71
+ chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
79
72
  typ: EnumToken.PercentageTokenType,
80
- val: values[3] * 100
73
+ val: toPrecisionValue(values[3], 2) * 100
81
74
  });
82
75
  }
83
76
  return {
84
77
  typ: EnumToken.ColorTokenType,
85
- val: 'hwb',
78
+ val: "hwb",
86
79
  chi,
87
- kin: ColorType.HWB
80
+ kin: ColorType.HWB,
88
81
  };
89
82
  }
90
83
  function rgb2hwbvalues(token) {
91
84
  // @ts-ignore
92
- return srgb2hwb(...getComponents(token).map((t, index) => {
85
+ return srgb2hwb(...getColorComponents(token).map((t, index) => {
93
86
  if (index == 3) {
94
87
  return getNumber(t);
95
88
  }
@@ -102,8 +95,8 @@ function cmyk2hwbvalues(token) {
102
95
  }
103
96
  function hsl2hwbvalues(token) {
104
97
  // @ts-ignore
105
- return hslvalues2hwbvalues(...getComponents(token).map((t, index) => {
106
- if (index == 3 && (t.typ == EnumToken.IdenTokenType && t.val == 'none')) {
98
+ return hslvalues2hwbvalues(...getColorComponents(token).map((t, index) => {
99
+ if (index == 3 && t.typ == EnumToken.IdenTokenType && t.val == "none") {
107
100
  return 1;
108
101
  }
109
102
  if (index == 0) {
@@ -147,15 +140,9 @@ function rgb2hue(r, g, b, fallback = 0) {
147
140
  let delta = value - whiteness;
148
141
  if (delta > 0) {
149
142
  // calculate segment
150
- let segment = value === r ? (g - b) / delta : (value === g
151
- ? (b - r) / delta
152
- : (r - g) / delta);
143
+ let segment = value === r ? (g - b) / delta : value === g ? (b - r) / delta : (r - g) / delta;
153
144
  // calculate shift
154
- let shift = value === r ? segment < 0
155
- ? 360 / 60
156
- : 0 / 60 : (value === g
157
- ? 120 / 60
158
- : 240 / 60);
145
+ let shift = value === r ? (segment < 0 ? 360 / 60 : 0 / 60) : value === g ? 120 / 60 : 240 / 60;
159
146
  // calculate hue
160
147
  return (segment + shift) * 60;
161
148
  }