@tbela99/css-parser 1.4.3 → 1.4.5

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 (166) hide show
  1. package/CHANGELOG.md +196 -2
  2. package/README.md +68 -1120
  3. package/dist/config.json.js +43 -2
  4. package/dist/index-umd-web.js +18118 -15886
  5. package/dist/index.cjs +16838 -14611
  6. package/dist/index.d.ts +1804 -1684
  7. package/dist/lib/ast/clone.js +4 -3
  8. package/dist/lib/ast/expand.js +2 -2
  9. package/dist/lib/ast/features/calc.js +59 -54
  10. package/dist/lib/ast/features/if.js +64 -17
  11. package/dist/lib/ast/features/inlinecssvariables.js +2 -2
  12. package/dist/lib/ast/features/prefix.js +343 -6
  13. package/dist/lib/ast/features/transform.js +7 -4
  14. package/dist/lib/ast/find.js +5 -5
  15. package/dist/lib/ast/math/expression.js +12 -8
  16. package/dist/lib/ast/minify.js +28 -33
  17. package/dist/lib/ast/transform/minify.js +135 -131
  18. package/dist/lib/ast/transform/utils.js +3 -2
  19. package/dist/lib/ast/types.js +69 -8
  20. package/dist/lib/ast/walk.js +4 -5
  21. package/dist/lib/fs/resolve.js +71 -57
  22. package/dist/lib/parser/declaration/list.js +92 -3
  23. package/dist/lib/parser/declaration/map.js +5 -5
  24. package/dist/lib/parser/parse.js +530 -466
  25. package/dist/lib/parser/tokenize.js +110 -4
  26. package/dist/lib/parser/utils/angle.js +29 -0
  27. package/dist/lib/parser/utils/at-rule-container.js +2 -2
  28. package/dist/lib/parser/utils/at-rule-font-feature-values.js +2 -2
  29. package/dist/lib/parser/utils/at-rule-generic.js +1 -1
  30. package/dist/lib/parser/utils/at-rule-media.js +2 -2
  31. package/dist/lib/parser/utils/at-rule-support.js +73 -8
  32. package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
  33. package/dist/lib/parser/utils/at-rule.js +2 -8
  34. package/dist/lib/parser/utils/declaration.js +179 -76
  35. package/dist/lib/parser/utils/selector.js +53 -11
  36. package/dist/lib/parser/utils/token.js +13 -11
  37. package/dist/lib/renderer/render.js +501 -59
  38. package/dist/lib/syntax/color/alpha.js +64 -0
  39. package/dist/lib/syntax/color/cmyk.js +2 -2
  40. package/dist/lib/syntax/color/color-mix.js +283 -220
  41. package/dist/lib/syntax/color/color.js +37 -28
  42. package/dist/lib/syntax/color/hex.js +2 -2
  43. package/dist/lib/syntax/color/hsl.js +3 -3
  44. package/dist/lib/syntax/color/hwb.js +7 -7
  45. package/dist/lib/syntax/color/lab.js +5 -5
  46. package/dist/lib/syntax/color/lch.js +4 -4
  47. package/dist/lib/syntax/color/oklab.js +3 -3
  48. package/dist/lib/syntax/color/oklch.js +3 -3
  49. package/dist/lib/syntax/color/relativecolor.js +97 -12
  50. package/dist/lib/syntax/color/srgb.js +5 -5
  51. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  52. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  53. package/dist/lib/syntax/color/utils/components.js +7 -3
  54. package/dist/lib/syntax/color/utils/distance.js +21 -4
  55. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  56. package/dist/lib/syntax/color/xyz.js +2 -2
  57. package/dist/lib/syntax/constants.js +29 -1
  58. package/dist/lib/syntax/syntax.js +307 -39
  59. package/dist/lib/validation/config.js +2 -2
  60. package/dist/lib/validation/config.json.js +32 -17
  61. package/dist/lib/validation/match.js +239 -104
  62. package/dist/lib/validation/parser/parse.js +0 -1
  63. package/dist/lib/validation/parser/typedef.js +6 -0
  64. package/dist/node.js +64 -22
  65. package/dist/web.js +65 -18
  66. package/package.json +26 -15
  67. package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  68. package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  69. package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  70. package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  71. package/.repl_history +0 -4
  72. package/badges/coverage.svg +0 -20
  73. package/deno.lock +0 -2861
  74. package/dist/lib/ast/clone.d.ts +0 -10
  75. package/dist/lib/ast/expand.d.ts +0 -14
  76. package/dist/lib/ast/features/calc.d.ts +0 -10
  77. package/dist/lib/ast/features/if.d.ts +0 -10
  78. package/dist/lib/ast/features/index.d.ts +0 -6
  79. package/dist/lib/ast/features/inlinecssvariables.d.ts +0 -15
  80. package/dist/lib/ast/features/prefix.d.ts +0 -8
  81. package/dist/lib/ast/features/shorthand.d.ts +0 -12
  82. package/dist/lib/ast/features/transform.d.ts +0 -10
  83. package/dist/lib/ast/features/type.d.ts +0 -15
  84. package/dist/lib/ast/find.d.ts +0 -165
  85. package/dist/lib/ast/math/expression.d.ts +0 -18
  86. package/dist/lib/ast/math/math.d.ts +0 -6
  87. package/dist/lib/ast/minify.d.ts +0 -19
  88. package/dist/lib/ast/transform/compute.d.ts +0 -8
  89. package/dist/lib/ast/transform/matrix.d.ts +0 -22
  90. package/dist/lib/ast/transform/minify.d.ts +0 -5
  91. package/dist/lib/ast/transform/perspective.d.ts +0 -3
  92. package/dist/lib/ast/transform/rotate.d.ts +0 -12
  93. package/dist/lib/ast/transform/scale.d.ts +0 -6
  94. package/dist/lib/ast/transform/skew.d.ts +0 -4
  95. package/dist/lib/ast/transform/translate.d.ts +0 -6
  96. package/dist/lib/ast/transform/utils.d.ts +0 -9
  97. package/dist/lib/ast/types.d.ts +0 -903
  98. package/dist/lib/ast/walk.d.ts +0 -162
  99. package/dist/lib/fs/resolve.d.ts +0 -20
  100. package/dist/lib/parser/declaration/list.d.ts +0 -16
  101. package/dist/lib/parser/declaration/map.d.ts +0 -15
  102. package/dist/lib/parser/declaration/set.d.ts +0 -9
  103. package/dist/lib/parser/node.d.ts +0 -7
  104. package/dist/lib/parser/parse.d.ts +0 -107
  105. package/dist/lib/parser/tokenize.d.ts +0 -57
  106. package/dist/lib/parser/utils/at-rule-container.d.ts +0 -5
  107. package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +0 -5
  108. package/dist/lib/parser/utils/at-rule-generic.d.ts +0 -5
  109. package/dist/lib/parser/utils/at-rule-import.d.ts +0 -5
  110. package/dist/lib/parser/utils/at-rule-media.d.ts +0 -5
  111. package/dist/lib/parser/utils/at-rule-page.d.ts +0 -5
  112. package/dist/lib/parser/utils/at-rule-page.js +0 -28
  113. package/dist/lib/parser/utils/at-rule-support.d.ts +0 -5
  114. package/dist/lib/parser/utils/at-rule-token.d.ts +0 -1
  115. package/dist/lib/parser/utils/at-rule-when-else.d.ts +0 -5
  116. package/dist/lib/parser/utils/at-rule.d.ts +0 -13
  117. package/dist/lib/parser/utils/cache.d.ts +0 -6
  118. package/dist/lib/parser/utils/config.d.ts +0 -2
  119. package/dist/lib/parser/utils/declaration-list.d.ts +0 -5
  120. package/dist/lib/parser/utils/declaration.d.ts +0 -18
  121. package/dist/lib/parser/utils/eq.d.ts +0 -1
  122. package/dist/lib/parser/utils/hash.d.ts +0 -21
  123. package/dist/lib/parser/utils/selector.d.ts +0 -5
  124. package/dist/lib/parser/utils/text.d.ts +0 -3
  125. package/dist/lib/parser/utils/token.d.ts +0 -14
  126. package/dist/lib/parser/utils/type.d.ts +0 -2
  127. package/dist/lib/renderer/render.d.ts +0 -28
  128. package/dist/lib/renderer/sourcemap/lib/encode.d.ts +0 -1
  129. package/dist/lib/renderer/sourcemap/sourcemap.d.ts +0 -26
  130. package/dist/lib/syntax/color/a98rgb.d.ts +0 -2
  131. package/dist/lib/syntax/color/cmyk.d.ts +0 -10
  132. package/dist/lib/syntax/color/color-mix.d.ts +0 -2
  133. package/dist/lib/syntax/color/color.d.ts +0 -42
  134. package/dist/lib/syntax/color/hex.d.ts +0 -16
  135. package/dist/lib/syntax/color/hsl.d.ts +0 -20
  136. package/dist/lib/syntax/color/hsv.d.ts +0 -2
  137. package/dist/lib/syntax/color/hwb.d.ts +0 -21
  138. package/dist/lib/syntax/color/lab.d.ts +0 -25
  139. package/dist/lib/syntax/color/lch.d.ts +0 -23
  140. package/dist/lib/syntax/color/oklab.d.ts +0 -22
  141. package/dist/lib/syntax/color/oklch.d.ts +0 -20
  142. package/dist/lib/syntax/color/p3.d.ts +0 -6
  143. package/dist/lib/syntax/color/prophotorgb.d.ts +0 -2
  144. package/dist/lib/syntax/color/rec2020.d.ts +0 -2
  145. package/dist/lib/syntax/color/relativecolor.d.ts +0 -13
  146. package/dist/lib/syntax/color/rgb.d.ts +0 -20
  147. package/dist/lib/syntax/color/srgb.d.ts +0 -23
  148. package/dist/lib/syntax/color/utils/components.d.ts +0 -2
  149. package/dist/lib/syntax/color/utils/distance.d.ts +0 -18
  150. package/dist/lib/syntax/color/utils/matrix.d.ts +0 -6
  151. package/dist/lib/syntax/color/xyz.d.ts +0 -5
  152. package/dist/lib/syntax/color/xyzd50.d.ts +0 -4
  153. package/dist/lib/syntax/constants.d.ts +0 -67
  154. package/dist/lib/syntax/syntax.d.ts +0 -38
  155. package/dist/lib/validation/config.d.ts +0 -14
  156. package/dist/lib/validation/json.d.ts +0 -2
  157. package/dist/lib/validation/match.d.ts +0 -38
  158. package/dist/lib/validation/parser/parse.d.ts +0 -8
  159. package/dist/lib/validation/parser/typedef.d.ts +0 -95
  160. package/dist/lib/validation/utils/list.d.ts +0 -4
  161. package/dist/lib/validation/utils/whitespace.d.ts +0 -2
  162. package/dist/node.d.ts +0 -207
  163. package/dist/web.d.ts +0 -169
  164. package/playground/index.html +0 -1328
  165. package/playground/sw.js +0 -55
  166. package/playground/tree.js +0 -176
@@ -1,34 +1,34 @@
1
1
  import { ColorType, EnumToken } from '../../ast/types.js';
2
- import { color2RgbToken, lch2RgbToken, lab2RgbToken, oklch2RgbToken, oklab2RgbToken, cmyk2RgbToken, hwb2RgbToken, hsl2RgbToken, hex2RgbToken, cmyk2rgbvalues } from './rgb.js';
2
+ import { color2RgbToken, lch2RgbToken, lab2RgbToken, oklch2RgbToken, oklab2RgbToken, cmyk2RgbToken, hwb2RgbToken, hsl2RgbToken, hex2RgbToken } from './rgb.js';
3
3
  import { color2HslToken, lch2HslToken, lab2HslToken, oklch2HslToken, oklab2HslToken, cmyk2HslToken, hwb2HslToken, hex2HslToken, rgb2HslToken } from './hsl.js';
4
4
  import { color2hwbToken, cmyk2hwbToken, lch2hwbToken, lab2hwbToken, oklch2hwbToken, oklab2hwbToken, hsl2hwbToken, rgb2hwbToken } from './hwb.js';
5
5
  import { color2labToken, oklch2labToken, oklab2labToken, lch2labToken, cmyk2labToken, hwb2labToken, hsl2labToken, rgb2labToken, hex2labToken } from './lab.js';
6
6
  import { color2lchToken, oklch2lchToken, oklab2lchToken, lab2lchToken, cmyk2lchToken, hwb2lchToken, hsl2lchToken, rgb2lchToken, hex2lchToken } from './lch.js';
7
7
  import { color2oklabToken, oklch2oklabToken, lch2oklabToken, lab2oklabToken, cmyk2oklabToken, hwb2oklabToken, hsl2oklabToken, rgb2oklabToken, hex2oklabToken } from './oklab.js';
8
8
  import { color2oklchToken, lch2oklchToken, oklab2oklchToken, lab2oklchToken, cmyk2oklchToken, hwb2oklchToken, hsl2oklchToken, rgb2oklchToken, hex2oklchToken } from './oklch.js';
9
- import { getComponents } from './utils/components.js';
10
- 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';
11
11
  import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
12
12
  import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
13
- import { srgb2xyz_d50, srgb2xyz } from './xyz.js';
13
+ import { srgb2xyz_d65, srgb2xyz } from './xyz.js';
14
14
  import { p32srgbvalues, srgb2p3values } from './p3.js';
15
15
  import { xyzd502srgb } from './xyzd50.js';
16
16
  import { colorMix } from './color-mix.js';
17
17
  import { reduceHexValue, rgb2HexToken, color2HexToken, lch2HexToken, lab2HexToken, oklch2HexToken, oklab2HexToken, cmyk2HexToken, hwb2HexToken, hsl2HexToken } from './hex.js';
18
- import { parseRelativeColor } from './relativecolor.js';
18
+ import { parseRelativeColorComponents } from './relativecolor.js';
19
19
  import { isIdentColor } from '../syntax.js';
20
20
  import { color2cmykToken, lch2cmykToken, lab2cmykToken, oklch2cmykToken, oklab2cmyk, hwb2cmykToken, hsl2cmykToken, rgb2cmykToken } from './cmyk.js';
21
21
  import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
22
- import { epsilon } from '../../ast/transform/utils.js';
23
- import { definedPropertySettings, colorFuncColorSpace, colorPrecision, anglePrecision } from '../constants.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';
24
26
 
25
27
  /**
26
28
  * Converts a color to another color space
27
29
  * @param token
28
30
  * @param to
29
31
  *
30
- * @private
31
- *
32
32
  * ```ts
33
33
  *
34
34
  * const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
@@ -42,6 +42,17 @@ function convertColor(token, to) {
42
42
  (isIdentColor(token) && "currentcolor" == token.val.toLowerCase())) {
43
43
  return token;
44
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
+ }
45
56
  if (token.kin == ColorType.COLOR_MIX && to != ColorType.COLOR_MIX) {
46
57
  const children = token.chi.reduce((acc, t) => {
47
58
  if (t.typ === EnumToken.CommaTokenType) {
@@ -52,19 +63,19 @@ function convertColor(token, to) {
52
63
  }
53
64
  return acc;
54
65
  }, [[]]);
55
- 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));
56
67
  if (token == null) {
57
68
  return null;
58
69
  }
59
70
  }
60
71
  if (token.cal == "rel" &&
61
- ["rgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch", "color"].includes(token.val.toLowerCase())) {
62
- const chi = getComponents(token);
72
+ ["rgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch", "color"].some((t) => equalsIgnoreCase(t, token.val))) {
73
+ const chi = getColorComponents(token);
63
74
  const offset = token.val == "color" ? 2 : 1;
64
75
  if (chi != null) {
65
76
  // @ts-ignore
66
77
  const color = chi[1];
67
- const components = parseRelativeColor(token.val.toLowerCase() == "color"
78
+ const components = parseRelativeColorComponents(equalsIgnoreCase(token.val, "color")
68
79
  ? chi[offset].val
69
80
  : token.val, color, chi[offset + 1], chi[offset + 2], chi[offset + 3], chi[offset + 4]);
70
81
  if (components == null) {
@@ -384,7 +395,7 @@ function hwb2colorToken(token, to) {
384
395
  return values2colortoken(values, to);
385
396
  }
386
397
  function cmyk2colorToken(token, to) {
387
- const values = cmyk2rgbvalues(token);
398
+ const values = cmyk2srgbvalues(token);
388
399
  if (values == null) {
389
400
  return null;
390
401
  }
@@ -458,22 +469,16 @@ function srgb2srgbcolorspace(val, to) {
458
469
  break;
459
470
  case ColorType.XYZ_D50:
460
471
  // @ts-ignore
461
- values.push(...srgb2xyz_d50(...val));
472
+ values.push(...srgb2xyz_d65(...val));
462
473
  break;
463
474
  }
464
475
  return values;
465
476
  }
466
477
  function minmax(value, min, max) {
467
- if (value < min) {
468
- return min;
469
- }
470
- if (value > max) {
471
- return max;
472
- }
473
- return value;
478
+ return value < min ? min : value > max ? max : value;
474
479
  }
475
480
  function color2srgbvalues(token) {
476
- const components = getComponents(token);
481
+ const components = getColorComponents(token);
477
482
  if (components == null) {
478
483
  return null;
479
484
  }
@@ -510,6 +515,9 @@ function color2srgbvalues(token) {
510
515
  values = xyzd502srgb(...values);
511
516
  break;
512
517
  }
518
+ if (values.length == 4) {
519
+ values[3] = toPrecisionValue(values[3], 2);
520
+ }
513
521
  return values;
514
522
  }
515
523
  function values2colortoken(values, to) {
@@ -522,7 +530,7 @@ function values2colortoken(values, to) {
522
530
  if (values.length == 4) {
523
531
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
524
532
  typ: EnumToken.PercentageTokenType,
525
- val: values[3] * 100,
533
+ val: toPrecisionValue(values[3], 2) * 100,
526
534
  });
527
535
  }
528
536
  const colorSpace = ColorType[to].toLowerCase().replaceAll("_", "-");
@@ -586,12 +594,13 @@ function getAngle(token) {
586
594
  // @ts-ignore
587
595
  return token.val / 360;
588
596
  }
589
- function toPrecisionValue(value) {
590
- value = +value.toFixed(colorPrecision);
597
+ function toPrecisionValue(value, precision = colorPrecision) {
598
+ const div = Math.pow(10, precision);
599
+ value = Math.round(value * div) / div;
591
600
  return Math.abs(value) < epsilon ? 0 : value;
592
601
  }
593
- function toPrecisionAngle(angle) {
594
- angle = +angle.toPrecision(colorPrecision);
602
+ function toPrecisionAngle(angle, precision = colorPrecision) {
603
+ angle = toPrecisionValue(angle, precision);
595
604
  if (Math.abs(angle) >= 360) {
596
605
  angle %= 360;
597
606
  }
@@ -1,7 +1,7 @@
1
1
  import { EnumToken, ColorType } from '../../ast/types.js';
2
2
  import { color2srgbvalues, getNumber } from './color.js';
3
3
  import { srgb2rgb, cmyk2rgbvalues, hwb2rgbvalues, hsl2rgbvalues } from './rgb.js';
4
- import { getComponents } from './utils/components.js';
4
+ import { getColorComponents } from './utils/components.js';
5
5
  import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
6
6
  import { COLORS_NAMES, NAMES_COLORS } from '../constants.js';
7
7
 
@@ -112,7 +112,7 @@ function rgb2hexvalues(token) {
112
112
  let value = "#";
113
113
  let t;
114
114
  // @ts-ignore
115
- const components = getComponents(token);
115
+ const components = getColorComponents(token);
116
116
  if (components == null || components.length < 3) {
117
117
  return null;
118
118
  }
@@ -1,7 +1,7 @@
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 { getComponents } from './utils/components.js';
4
+ import { getColorComponents } from './utils/components.js';
5
5
  import { hex2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hslvalues } from './srgb.js';
6
6
  import { EnumToken, ColorType } from '../../ast/types.js';
7
7
 
@@ -76,7 +76,7 @@ function hslToken(values) {
76
76
  if (values.length == 4 && values[3] != 1) {
77
77
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
78
78
  typ: EnumToken.PercentageTokenType,
79
- val: values[3] * 100,
79
+ val: toPrecisionValue(values[3], 2) * 100,
80
80
  });
81
81
  }
82
82
  return {
@@ -87,7 +87,7 @@ function hslToken(values) {
87
87
  };
88
88
  }
89
89
  function rgb2hslvalues(token) {
90
- const chi = getComponents(token);
90
+ const chi = getColorComponents(token);
91
91
  if (chi == null || chi.length < 3) {
92
92
  return null;
93
93
  }
@@ -1,5 +1,5 @@
1
1
  import { hsl2hsv } from './hsv.js';
2
- import { getComponents } from './utils/components.js';
2
+ import { getColorComponents } from './utils/components.js';
3
3
  import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getAngle, getNumber } from './color.js';
4
4
  import { EnumToken, ColorType } from '../../ast/types.js';
5
5
  import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
@@ -63,14 +63,14 @@ function color2hwbToken(token) {
63
63
  function hwbToken(values) {
64
64
  values[0] = toPrecisionAngle(values[0] * 360);
65
65
  const chi = [
66
- { typ: EnumToken.NumberTokenType, val: values[0] },
67
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1]) * 100 },
68
- { 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) },
69
69
  ];
70
70
  if (values.length == 4) {
71
71
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
72
72
  typ: EnumToken.PercentageTokenType,
73
- val: values[3] * 100,
73
+ val: toPrecisionValue(values[3], 2) * 100
74
74
  });
75
75
  }
76
76
  return {
@@ -82,7 +82,7 @@ function hwbToken(values) {
82
82
  }
83
83
  function rgb2hwbvalues(token) {
84
84
  // @ts-ignore
85
- return srgb2hwb(...getComponents(token).map((t, index) => {
85
+ return srgb2hwb(...getColorComponents(token).map((t, index) => {
86
86
  if (index == 3) {
87
87
  return getNumber(t);
88
88
  }
@@ -95,7 +95,7 @@ function cmyk2hwbvalues(token) {
95
95
  }
96
96
  function hsl2hwbvalues(token) {
97
97
  // @ts-ignore
98
- return hslvalues2hwbvalues(...getComponents(token).map((t, index) => {
98
+ return hslvalues2hwbvalues(...getColorComponents(token).map((t, index) => {
99
99
  if (index == 3 && t.typ == EnumToken.IdenTokenType && t.val == "none") {
100
100
  return 1;
101
101
  }
@@ -1,6 +1,6 @@
1
1
  import { e, k, D50 } from '../constants.js';
2
- import { getComponents } from './utils/components.js';
3
- import { srgb2xyz_d50, XYZ_D50_to_D65 } from './xyz.js';
2
+ import { getColorComponents } from './utils/components.js';
3
+ import { srgb2xyz_d65, XYZ_D50_to_D65 } from './xyz.js';
4
4
  import { oklch2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, xyz2srgb } from './srgb.js';
5
5
  import { getLCHComponents } from './lch.js';
6
6
  import { getOKLABComponents, OKLab_to_XYZ } from './oklab.js';
@@ -77,7 +77,7 @@ function labToken(values) {
77
77
  if (values.length == 4) {
78
78
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
79
79
  typ: EnumToken.PercentageTokenType,
80
- val: values[3] * 100,
80
+ val: toPrecisionValue(values[3], 2) * 100,
81
81
  });
82
82
  }
83
83
  return {
@@ -151,7 +151,7 @@ function color2labvalues(token) {
151
151
  }
152
152
  function srgb2labvalues(r, g, b, a) {
153
153
  // @ts-ignore */
154
- const result = xyz2lab(...srgb2xyz_d50(r, g, b));
154
+ const result = xyz2lab(...srgb2xyz_d65(r, g, b));
155
155
  // Fixes achromatic RGB colors having a _slight_ chroma due to floating-point errors
156
156
  // and approximated computations in sRGB <-> CIELab.
157
157
  // See: https://github.com/d3/d3-color/pull/46
@@ -192,7 +192,7 @@ function lchvalues2labvalues(l, c, h, a = null) {
192
192
  return result;
193
193
  }
194
194
  function getLABComponents(token) {
195
- const components = getComponents(token);
195
+ const components = getColorComponents(token);
196
196
  if (components == null) {
197
197
  return null;
198
198
  }
@@ -1,4 +1,4 @@
1
- import { getComponents } from './utils/components.js';
1
+ import { getColorComponents } from './utils/components.js';
2
2
  import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber, getAngle } from './color.js';
3
3
  import { EnumToken, ColorType } from '../../ast/types.js';
4
4
  import { srgb2labvalues, xyz2lab, oklch2labvalues, oklab2labvalues, getLABComponents, hwb2labvalues, hsl2labvalues, rgb2labvalues, hex2labvalues } from './lab.js';
@@ -72,12 +72,12 @@ function lchToken(values) {
72
72
  const chi = [
73
73
  { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
74
74
  { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
75
- { typ: EnumToken.NumberTokenType, val: values[2] },
75
+ { typ: EnumToken.NumberTokenType, val: toPrecisionAngle(values[2]) },
76
76
  ];
77
77
  if (values.length == 4) {
78
78
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
79
79
  typ: EnumToken.PercentageTokenType,
80
- val: values[3] * 100,
80
+ val: toPrecisionValue(values[3], 2) * 100,
81
81
  });
82
82
  }
83
83
  return {
@@ -159,7 +159,7 @@ function xyz2lchvalues(x, y, z, alpha) {
159
159
  return alpha == null || alpha == 1 ? lch : lch.concat(alpha);
160
160
  }
161
161
  function getLCHComponents(token) {
162
- const components = getComponents(token);
162
+ const components = getColorComponents(token);
163
163
  if (components == null) {
164
164
  return null;
165
165
  }
@@ -1,4 +1,4 @@
1
- import { getComponents } from './utils/components.js';
1
+ import { getColorComponents } from './utils/components.js';
2
2
  import { multiplyMatrices } from './utils/matrix.js';
3
3
  import { srgb2lsrgbvalues, lch2srgbvalues, lab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, lsrgb2srgbvalues } from './srgb.js';
4
4
  import { color2srgbvalues, toPrecisionValue, getNumber } from './color.js';
@@ -78,7 +78,7 @@ function oklabToken(values) {
78
78
  if (values.length == 4) {
79
79
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
80
80
  typ: EnumToken.PercentageTokenType,
81
- val: values[3] * 100,
81
+ val: toPrecisionValue(values[3], 2) * 100,
82
82
  });
83
83
  }
84
84
  return {
@@ -155,7 +155,7 @@ function srgb2oklab(r, g, blue, alpha) {
155
155
  return alpha == null || alpha == 1 ? [l, a, b] : [l, a, b, alpha];
156
156
  }
157
157
  function getOKLABComponents(token) {
158
- const components = getComponents(token);
158
+ const components = getColorComponents(token);
159
159
  if (components == null || components.length < 3) {
160
160
  return null;
161
161
  }
@@ -1,4 +1,4 @@
1
- import { getComponents } from './utils/components.js';
1
+ import { getColorComponents } from './utils/components.js';
2
2
  import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber, getAngle } from './color.js';
3
3
  import { EnumToken, ColorType } from '../../ast/types.js';
4
4
  import { labvalues2lchvalues } from './lch.js';
@@ -76,7 +76,7 @@ function oklchToken(values) {
76
76
  if (values.length == 4) {
77
77
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
78
78
  typ: EnumToken.PercentageTokenType,
79
- val: values[3] * 100,
79
+ val: toPrecisionValue(values[3], 2) * 100,
80
80
  });
81
81
  }
82
82
  return {
@@ -140,7 +140,7 @@ function srgb2oklch(r, g, blue, alpha) {
140
140
  return labvalues2lchvalues(...srgb2oklab(r, g, blue, alpha));
141
141
  }
142
142
  function getOKLCHComponents(token) {
143
- const components = getComponents(token);
143
+ const components = getColorComponents(token);
144
144
  if (components == null) {
145
145
  return null;
146
146
  }
@@ -1,10 +1,22 @@
1
1
  import { convertColor, getNumber } from './color.js';
2
- import { ColorType, EnumToken } from '../../ast/types.js';
2
+ import { EnumToken, ColorType } from '../../ast/types.js';
3
3
  import { walkValues } from '../../ast/walk.js';
4
4
  import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
5
- import { colorRange, mathFuncs } from '../constants.js';
5
+ import { colorsFunc, colorFuncColorSpace, colorRange, mathFuncs } from '../constants.js';
6
+ import { equalsIgnoreCase } from '../../parser/utils/text.js';
7
+ import { getColorComponents } from './utils/components.js';
6
8
 
7
- function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
9
+ /**
10
+ * Parse relative color components
11
+ * @param relativeKeys
12
+ * @param original
13
+ * @param rExp
14
+ * @param gExp
15
+ * @param bExp
16
+ * @param aExp
17
+ * @returns
18
+ */
19
+ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp, aExp) {
8
20
  let r;
9
21
  let g;
10
22
  let b;
@@ -14,9 +26,68 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
14
26
  // colorFuncColorSpace x,y,z or r,g,b
15
27
  const names = relativeKeys.startsWith("xyz")
16
28
  ? "xyz"
17
- : ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].includes(relativeKeys.toLowerCase())
29
+ : ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, relativeKeys))
18
30
  ? "rgb"
19
31
  : relativeKeys.slice(-3);
32
+ const allComponents = [rExp, gExp, bExp, aExp];
33
+ const components = getColorComponents(original);
34
+ const validKeys = names.split("");
35
+ let val = "";
36
+ if (components != null) {
37
+ allComponents.push(...components);
38
+ }
39
+ // ensure all components are valid for the color space
40
+ for (const component of allComponents) {
41
+ if (component == null) {
42
+ continue;
43
+ }
44
+ if (component.typ == EnumToken.IdenTokenType) {
45
+ val = component.val.toLowerCase();
46
+ if (
47
+ // @ts-expect-error
48
+ typeof Math[val.toUpperCase()] !== "number" &&
49
+ val != "in" &&
50
+ val != "hue" &&
51
+ val != "from" &&
52
+ val != "alpha" &&
53
+ val != "none" &&
54
+ val != "shorter" &&
55
+ val != "longer" &&
56
+ val != "increasing" &&
57
+ val != "decreasing" &&
58
+ !colorsFunc.includes(val) &&
59
+ !colorFuncColorSpace.includes(val) &&
60
+ !validKeys.includes(val)) {
61
+ return null;
62
+ }
63
+ continue;
64
+ }
65
+ if (component.typ === EnumToken.MathFunctionTokenType &&
66
+ equalsIgnoreCase("calc", component.val)) {
67
+ for (const { value } of walkValues(component.chi)) {
68
+ if (value.typ == EnumToken.IdenTokenType) {
69
+ val = value.val.toLowerCase();
70
+ if (
71
+ // @ts-expect-error
72
+ typeof Math[val.toUpperCase()] !== "number" &&
73
+ val != "in" &&
74
+ val != "hue" &&
75
+ val != "from" &&
76
+ val != "alpha" &&
77
+ val != "none" &&
78
+ val != "shorter" &&
79
+ val != "longer" &&
80
+ val != "increasing" &&
81
+ val != "decreasing" &&
82
+ !colorsFunc.includes(val) &&
83
+ !colorFuncColorSpace.includes(val) &&
84
+ !validKeys.includes(val)) {
85
+ return null;
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
20
91
  const converted = (convertColor(original, ColorType[relativeKeys.toUpperCase().replaceAll("-", "_")]));
21
92
  if (converted == null) {
22
93
  return null;
@@ -62,21 +133,29 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
62
133
  }
63
134
  : aExp),
64
135
  };
65
- const result = computeComponentValue(keys, converted, values);
136
+ const result = computeComponentValue(keys, values);
66
137
  if (result?.alpha?.typ == EnumToken.NumberTokenType && result.alpha.val === 1) {
67
138
  const { alpha, ...components } = result;
68
139
  return components;
69
140
  }
70
141
  return result;
71
- // return computeComponentValue(keys, converted, values);
72
142
  }
143
+ /**
144
+ * Get token numeric value
145
+ * @param t
146
+ * @param converted
147
+ * @param component
148
+ * @returns
149
+ */
73
150
  function getValue(t, converted, component) {
74
151
  if (t.typ == EnumToken.PercentageTokenType) {
75
152
  let value = getNumber(t);
76
- let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll("-", "_");
77
- if (colorSpace in colorRange) {
78
- // @ts-ignore
79
- value *= colorRange[colorSpace][component].at(-1);
153
+ if (converted != null) {
154
+ let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll("-", "_");
155
+ if (colorSpace in colorRange) {
156
+ // @ts-ignore
157
+ value *= colorRange[colorSpace][component].at(-1);
158
+ }
80
159
  }
81
160
  return {
82
161
  typ: EnumToken.NumberTokenType,
@@ -85,7 +164,13 @@ function getValue(t, converted, component) {
85
164
  }
86
165
  return t;
87
166
  }
88
- function computeComponentValue(expr, converted, values) {
167
+ /**
168
+ * Compute component value
169
+ * @param expr
170
+ * @param values
171
+ * @returns
172
+ */
173
+ function computeComponentValue(expr, values) {
89
174
  for (const object of [values, expr]) {
90
175
  if ("h" in object) {
91
176
  // normalize hue
@@ -155,4 +240,4 @@ function replaceValue(parent, value, newValue) {
155
240
  }
156
241
  }
157
242
 
158
- export { parseRelativeColor, replaceValue };
243
+ export { parseRelativeColorComponents, replaceValue };
@@ -1,4 +1,4 @@
1
- import { getComponents } from './utils/components.js';
1
+ import { getColorComponents } from './utils/components.js';
2
2
  import { color2srgbvalues, getNumber, getAngle } from './color.js';
3
3
  import { EnumToken, ColorType } from '../../ast/types.js';
4
4
  import { Lab_to_sRGB, getLABComponents, lchvalues2labvalues } from './lab.js';
@@ -43,7 +43,7 @@ function srgbvalues(token) {
43
43
  return null;
44
44
  }
45
45
  function rgb2srgb(token) {
46
- return (getComponents(token)?.map?.((t, index) => index == 3
46
+ return (getColorComponents(token)?.map?.((t, index) => index == 3
47
47
  ? t.typ == EnumToken.IdenTokenType && t.val == "none"
48
48
  ? 1
49
49
  : getNumber(t)
@@ -52,7 +52,7 @@ function rgb2srgb(token) {
52
52
  255) ?? null);
53
53
  }
54
54
  function rgb2srgbvalues(token) {
55
- return (getComponents(token)?.map?.((t, index) => index == 3
55
+ return (getColorComponents(token)?.map?.((t, index) => index == 3
56
56
  ? getNumber(t)
57
57
  : getNumber(t) / 255) ?? null);
58
58
  }
@@ -95,7 +95,7 @@ function hsl2srgb(token) {
95
95
  return hslvalues2srgbvalues(h, s, l, a);
96
96
  }
97
97
  function cmyk2srgbvalues(token) {
98
- const components = getComponents(token);
98
+ const components = getColorComponents(token);
99
99
  if (components == null) {
100
100
  return null;
101
101
  }
@@ -149,7 +149,7 @@ function oklch2srgbvalues(token) {
149
149
  return rgb;
150
150
  }
151
151
  function hslvalues(token) {
152
- const components = getComponents(token);
152
+ const components = getColorComponents(token);
153
153
  if (components == null) {
154
154
  return null;
155
155
  }
@@ -0,0 +1,18 @@
1
+ import { ColorType } from '../../../ast/types.js';
2
+
3
+ /**
4
+ * Return the color type of the color components
5
+ */
6
+ function getColorType(color) {
7
+ if (color.kin === ColorType.COLOR_MIX || color.cal === "rel") {
8
+ return color.kin === ColorType.COLOR_MIX
9
+ ? // @ts-expect-error
10
+ ColorType[
11
+ // @ts-expect-error
12
+ color.chi[2]?.val?.toUpperCase?.()?.replace?.("-", "_")]
13
+ : getColorType(color.chi[2]);
14
+ }
15
+ return color.kin;
16
+ }
17
+
18
+ export { getColorType };
@@ -0,0 +1,46 @@
1
+ import { equalsIgnoreCase } from '../../../parser/utils/text.js';
2
+ import { getColorComponents } from './components.js';
3
+
4
+ /**
5
+ * Return the color space of the color
6
+ * @param color
7
+ * @returns
8
+ */
9
+ function getColorSpace(color) {
10
+ if (!("chi" in color)) {
11
+ return "rgb";
12
+ }
13
+ let name;
14
+ const components = getColorComponents(color);
15
+ if (components == null || components.length < 3) {
16
+ return null;
17
+ }
18
+ if (equalsIgnoreCase("color", color.val)) {
19
+ if (equalsIgnoreCase("from", components[0].val)) {
20
+ name = components[2].val;
21
+ }
22
+ else {
23
+ name = components[0].val;
24
+ }
25
+ }
26
+ else if (equalsIgnoreCase("color-mix", color.val)) {
27
+ if (equalsIgnoreCase("in", components[0].val)) {
28
+ name = components[1].val;
29
+ }
30
+ else {
31
+ return "oklab";
32
+ }
33
+ }
34
+ else {
35
+ name = color.val;
36
+ }
37
+ return name == "device-cmyk"
38
+ ? "cmyk"
39
+ : name.startsWith("xyz")
40
+ ? "xyz"
41
+ : ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, name))
42
+ ? "rgb"
43
+ : name.slice(-3);
44
+ }
45
+
46
+ export { getColorSpace };
@@ -3,8 +3,9 @@ import { walkValues } from '../../../ast/walk.js';
3
3
  import { COLORS_NAMES } from '../../constants.js';
4
4
  import { expandHexValue } from '../hex.js';
5
5
  import { isColor, parseColor } from '../../syntax.js';
6
+ import { equalsIgnoreCase } from '../../../parser/utils/text.js';
6
7
 
7
- function getComponents(token) {
8
+ function getColorComponents(token) {
8
9
  if (token.typ === EnumToken.IdenTokenType) {
9
10
  if (isColor(token)) {
10
11
  parseColor(token);
@@ -14,6 +15,9 @@ function getComponents(token) {
14
15
  }
15
16
  }
16
17
  if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
18
+ if (equalsIgnoreCase('currentcolor', token.val)) {
19
+ return null;
20
+ }
17
21
  const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
18
22
  // @ts-ignore
19
23
  return value
@@ -51,7 +55,7 @@ function getComponents(token) {
51
55
  }
52
56
  }
53
57
  }
54
- if (child.typ == EnumToken.ColorTokenType && "currentcolor" === child.val.toLowerCase()) {
58
+ if (child.typ == EnumToken.ColorTokenType && equalsIgnoreCase("currentcolor", child.val)) {
55
59
  return null;
56
60
  }
57
61
  result.push(child);
@@ -59,4 +63,4 @@ function getComponents(token) {
59
63
  return result;
60
64
  }
61
65
 
62
- export { getComponents };
66
+ export { getColorComponents };