@tbela99/css-parser 1.1.1-alpha4 → 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 +8 -1
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +4195 -3375
  4. package/dist/index.cjs +5468 -4648
  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 -200
  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 +7 -34
  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 +171 -132
  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
@@ -0,0 +1,166 @@
1
+ import './utils/constants.js';
2
+ import { getComponents } from './utils/components.js';
3
+ import { color2srgbvalues, toPrecisionAngle, getNumber, getAngle } from './color.js';
4
+ import { srgb2oklab, lch2oklabvalues, getOKLABComponents, lab2oklabvalues, hwb2oklabvalues, hsl2oklabvalues, rgb2oklabvalues, hex2oklabvalues } from './oklab.js';
5
+ import { EnumToken, ColorType } from '../../ast/types.js';
6
+ import '../../ast/minify.js';
7
+ import '../../ast/walk.js';
8
+ import '../../parser/parse.js';
9
+ import '../../parser/tokenize.js';
10
+ import '../../parser/utils/config.js';
11
+ import { cmyk2srgbvalues } from './srgb.js';
12
+ import { labvalues2lchvalues } from './lch.js';
13
+ import '../../renderer/sourcemap/lib/encode.js';
14
+
15
+ function hex2oklchToken(token) {
16
+ const values = hex2oklchvalues(token);
17
+ return oklchToken(values);
18
+ }
19
+ function rgb2oklchToken(token) {
20
+ const values = rgb2oklchvalues(token);
21
+ if (values == null) {
22
+ return null;
23
+ }
24
+ return oklchToken(values);
25
+ }
26
+ function hsl2oklchToken(token) {
27
+ const values = hsl2oklchvalues(token);
28
+ if (values == null) {
29
+ return null;
30
+ }
31
+ return oklchToken(values);
32
+ }
33
+ function hwb2oklchToken(token) {
34
+ const values = hwb2oklchvalues(token);
35
+ if (values == null) {
36
+ return null;
37
+ }
38
+ return oklchToken(values);
39
+ }
40
+ function cmyk2oklchToken(token) {
41
+ const values = cmyk2oklchvalues(token);
42
+ if (values == null) {
43
+ return null;
44
+ }
45
+ return oklchToken(values);
46
+ }
47
+ function lab2oklchToken(token) {
48
+ const values = lab2oklchvalues(token);
49
+ if (values == null) {
50
+ return null;
51
+ }
52
+ return oklchToken(values);
53
+ }
54
+ function oklab2oklchToken(token) {
55
+ const values = oklab2oklchvalues(token);
56
+ if (values == null) {
57
+ return null;
58
+ }
59
+ return oklchToken(values);
60
+ }
61
+ function lch2oklchToken(token) {
62
+ const values = lch2oklchvalues(token);
63
+ if (values == null) {
64
+ return null;
65
+ }
66
+ return oklchToken(values);
67
+ }
68
+ function color2oklchToken(token) {
69
+ const values = color2srgbvalues(token);
70
+ if (values == null) {
71
+ return null;
72
+ }
73
+ // @ts-ignore
74
+ return oklchToken(srgb2oklch(...values));
75
+ }
76
+ function oklchToken(values) {
77
+ values[2] = toPrecisionAngle(values[2]);
78
+ const chi = [
79
+ { typ: EnumToken.NumberTokenType, val: String(values[0]) },
80
+ { typ: EnumToken.NumberTokenType, val: String(values[1]) },
81
+ { typ: EnumToken.NumberTokenType, val: String(values[2]) },
82
+ ];
83
+ if (values.length == 4) {
84
+ chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
85
+ typ: EnumToken.PercentageTokenType,
86
+ val: (values[3] * 100).toFixed()
87
+ });
88
+ }
89
+ return {
90
+ typ: EnumToken.ColorTokenType,
91
+ val: 'oklch',
92
+ chi,
93
+ kin: ColorType.OKLCH
94
+ };
95
+ }
96
+ function hex2oklchvalues(token) {
97
+ // @ts-ignore
98
+ return labvalues2lchvalues(...hex2oklabvalues(token));
99
+ }
100
+ function rgb2oklchvalues(token) {
101
+ const values = rgb2oklabvalues(token);
102
+ if (values == null) {
103
+ return null;
104
+ }
105
+ // @ts-ignore
106
+ return labvalues2lchvalues(...values);
107
+ }
108
+ function hsl2oklchvalues(token) {
109
+ // @ts-ignore
110
+ return labvalues2lchvalues(...hsl2oklabvalues(token));
111
+ }
112
+ function hwb2oklchvalues(token) {
113
+ // @ts-ignore
114
+ return labvalues2lchvalues(...hwb2oklabvalues(token));
115
+ }
116
+ function cmyk2oklchvalues(token) {
117
+ const values = cmyk2srgbvalues(token);
118
+ // @ts-ignore
119
+ return values == null ? null : srgb2oklch(...values);
120
+ }
121
+ function lab2oklchvalues(token) {
122
+ // @ts-ignore
123
+ return labvalues2lchvalues(...lab2oklabvalues(token));
124
+ }
125
+ function lch2oklchvalues(token) {
126
+ // @ts-ignore
127
+ return labvalues2lchvalues(...lch2oklabvalues(token));
128
+ }
129
+ function oklab2oklchvalues(token) {
130
+ // @ts-ignore
131
+ return labvalues2lchvalues(...getOKLABComponents(token));
132
+ }
133
+ function srgb2oklch(r, g, blue, alpha) {
134
+ // @ts-ignore
135
+ return labvalues2lchvalues(...srgb2oklab(r, g, blue, alpha));
136
+ }
137
+ function getOKLCHComponents(token) {
138
+ const components = getComponents(token);
139
+ if (components == null) {
140
+ return null;
141
+ }
142
+ for (let i = 0; i < components.length; i++) {
143
+ if (![EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.IdenTokenType].includes(components[i].typ)) {
144
+ return [];
145
+ }
146
+ }
147
+ // @ts-ignore
148
+ let t = components[0];
149
+ // @ts-ignore
150
+ const l = getNumber(t);
151
+ // @ts-ignore
152
+ t = components[1];
153
+ // @ts-ignore
154
+ const c = getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? .4 : 1);
155
+ // @ts-ignore
156
+ t = components[2];
157
+ // @ts-ignore
158
+ const h = getAngle(t) * 360;
159
+ // @ts-ignore
160
+ t = components[3];
161
+ // @ts-ignore
162
+ const alpha = t == null || (t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t);
163
+ return [l, c, h, alpha];
164
+ }
165
+
166
+ export { cmyk2oklchToken, cmyk2oklchvalues, color2oklchToken, getOKLCHComponents, hex2oklchToken, hex2oklchvalues, hsl2oklchToken, hsl2oklchvalues, hwb2oklchToken, hwb2oklchvalues, lab2oklchToken, lab2oklchvalues, lch2oklchToken, lch2oklchvalues, oklab2oklchToken, oklab2oklchvalues, rgb2oklchToken, rgb2oklchvalues, srgb2oklch };
@@ -1,4 +1,4 @@
1
- import { xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
1
+ import { lsrgb2srgbvalues, xyz2srgb, srgb2lsrgbvalues } from './srgb.js';
2
2
  import { multiplyMatrices } from './utils/matrix.js';
3
3
  import './utils/constants.js';
4
4
  import '../../ast/types.js';
@@ -8,7 +8,7 @@ import '../../parser/parse.js';
8
8
  import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
10
  import { srgb2xyz } from './xyz.js';
11
- import '../sourcemap/lib/encode.js';
11
+ import '../../renderer/sourcemap/lib/encode.js';
12
12
 
13
13
  function p32srgbvalues(r, g, b, alpha) {
14
14
  // @ts-ignore
@@ -16,7 +16,7 @@ function p32srgbvalues(r, g, b, alpha) {
16
16
  }
17
17
  function srgb2p3values(r, g, b, alpha) {
18
18
  // @ts-ignore
19
- return srgb2xyz(...xyz2lp3(...lp32p3(r, g, b, alpha)));
19
+ return lp32p3(...xyz2lp3(...srgb2xyz(r, g, b, alpha)));
20
20
  }
21
21
  function p32lp3(r, g, b, alpha) {
22
22
  // convert an array of display-p3 RGB values in the range 0.0 - 1.0
@@ -8,7 +8,7 @@ import '../../parser/parse.js';
8
8
  import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
10
  import { srgb2xyz } from './xyz.js';
11
- import '../sourcemap/lib/encode.js';
11
+ import '../../renderer/sourcemap/lib/encode.js';
12
12
 
13
13
  function rec20202srgb(r, g, b, a) {
14
14
  // @ts-ignore
@@ -22,28 +22,28 @@ function rec20202lrec2020(r, g, b, a) {
22
22
  // convert an array of rec2020 RGB values in the range 0.0 - 1.0
23
23
  // to linear light (un-companded) form.
24
24
  // ITU-R BT.2020-2 p.4
25
- const α = 1.09929682680944;
26
- const β = 0.018053968510807;
25
+ const alpha = 1.09929682680944;
26
+ const beta = 0.018053968510807;
27
27
  return [r, g, b].map(function (val) {
28
28
  let sign = val < 0 ? -1 : 1;
29
29
  let abs = Math.abs(val);
30
- if (abs < β * 4.5) {
30
+ if (abs < beta * 4.5) {
31
31
  return val / 4.5;
32
32
  }
33
- return sign * (Math.pow((abs + α - 1) / α, 1 / 0.45));
33
+ return sign * (Math.pow((abs + alpha - 1) / alpha, 1 / 0.45));
34
34
  }).concat([] );
35
35
  }
36
36
  function lrec20202rec2020(r, g, b, a) {
37
37
  // convert an array of linear-light rec2020 RGB in the range 0.0-1.0
38
38
  // to gamma corrected form
39
39
  // ITU-R BT.2020-2 p.4
40
- const α = 1.09929682680944;
41
- const β = 0.018053968510807;
40
+ const alpha = 1.09929682680944;
41
+ const beta = 0.018053968510807;
42
42
  return [r, g, b].map(function (val) {
43
43
  let sign = val < 0 ? -1 : 1;
44
44
  let abs = Math.abs(val);
45
- if (abs > β) {
46
- return sign * (α * Math.pow(abs, 0.45) - (α - 1));
45
+ if (abs > beta) {
46
+ return sign * (alpha * Math.pow(abs, 0.45) - (alpha - 1));
47
47
  }
48
48
  return 4.5 * val;
49
49
  }).concat(a == null || a == 1 ? [] : [a]);
@@ -52,7 +52,7 @@ function lrec20202xyz(r, g, b, a) {
52
52
  // convert an array of linear-light rec2020 values to CIE XYZ
53
53
  // using D65 (no chromatic adaptation)
54
54
  // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
55
- var M = [
55
+ let M = [
56
56
  [63426534 / 99577255, 20160776 / 139408157, 47086771 / 278816314],
57
57
  [26158966 / 99577255, 472592308 / 697040785, 8267143 / 139408157],
58
58
  [0, 19567812 / 697040785, 295819943 / 278816314],
@@ -62,7 +62,7 @@ function lrec20202xyz(r, g, b, a) {
62
62
  }
63
63
  function xyz2lrec2020(x, y, z, a) {
64
64
  // convert XYZ to linear-light rec2020
65
- var M = [
65
+ let M = [
66
66
  [30757411 / 17917100, -6372589 / 17917100, -4539589 / 17917100],
67
67
  [-19765991 / 29648200, 47925759 / 29648200, 467509 / 29648200],
68
68
  [792561 / 44930125, -1921689 / 44930125, 42328811 / 44930125],
@@ -1,14 +1,14 @@
1
- import { convert, getNumber } from './color.js';
2
- import { EnumToken } from '../../ast/types.js';
1
+ import { convertColor, getNumber } from './color.js';
2
+ import { ColorType, EnumToken } from '../../ast/types.js';
3
3
  import '../../ast/minify.js';
4
4
  import { walkValues } from '../../ast/walk.js';
5
5
  import '../../parser/parse.js';
6
6
  import '../../parser/tokenize.js';
7
7
  import '../../parser/utils/config.js';
8
- import { mathFuncs } from '../../syntax/syntax.js';
9
- import { reduceNumber } from '../render.js';
10
- import { ColorKind, colorRange } from './utils/constants.js';
8
+ import { mathFuncs } from '../syntax.js';
9
+ import { colorRange } from './utils/constants.js';
11
10
  import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
11
+ import '../../renderer/sourcemap/lib/encode.js';
12
12
 
13
13
  function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
14
14
  let r;
@@ -19,12 +19,12 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
19
19
  let values = {};
20
20
  // colorFuncColorSpace x,y,z or r,g,b
21
21
  const names = relativeKeys.startsWith('xyz') ? 'xyz' : relativeKeys.slice(-3);
22
- const converted = convert(original, ColorKind[relativeKeys.toUpperCase().replaceAll('-', '_')]);
22
+ const converted = convertColor(original, ColorType[relativeKeys.toUpperCase().replaceAll('-', '_')]);
23
23
  if (converted == null) {
24
24
  return null;
25
25
  }
26
26
  const children = converted.chi.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(t.typ));
27
- [r, g, b, alpha] = converted.kin == ColorKind.COLOR ? children.slice(1) : children;
27
+ [r, g, b, alpha] = converted.kin == ColorType.COLOR ? children.slice(1) : children;
28
28
  values = {
29
29
  [names[0]]: getValue(r, converted, names[0]),
30
30
  [names[1]]: getValue(g, converted, names[1]), // string,
@@ -57,12 +57,13 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
57
57
  return computeComponentValue(keys, converted, values);
58
58
  }
59
59
  function getValue(t, converted, component) {
60
- if (t == null) {
61
- return t;
62
- }
60
+ // if (t == null) {
61
+ //
62
+ // return t;
63
+ // }
63
64
  if (t.typ == EnumToken.PercentageTokenType) {
64
65
  let value = getNumber(t);
65
- let colorSpace = ColorKind[converted.kin].toLowerCase().replaceAll('-', '_');
66
+ let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll('-', '_');
66
67
  if (colorSpace in colorRange) {
67
68
  // @ts-ignore
68
69
  value *= colorRange[colorSpace][component].at(-1);
@@ -89,44 +90,55 @@ function computeComponentValue(expr, converted, values) {
89
90
  }
90
91
  }
91
92
  for (const [key, exp] of Object.entries(expr)) {
93
+ /*
92
94
  if (exp == null) {
95
+
93
96
  if (key in values) {
94
- if (typeof values[key] == 'number') {
95
- expr[key] = {
97
+
98
+ if (typeof values[<RelativeColorTypes>key] == 'number') {
99
+
100
+ expr[<RelativeColorTypes>key] = {
96
101
  typ: EnumToken.NumberTokenType,
97
- val: reduceNumber(values[key])
102
+ val: reduceNumber(<number>values[<RelativeColorTypes>key])
98
103
  };
99
- }
100
- else {
101
- expr[key] = values[key];
104
+ } else {
105
+
106
+ expr[<RelativeColorTypes>key] = <Token>values[<RelativeColorTypes>key];
102
107
  }
103
108
  }
104
- }
105
- else if ([EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.LengthTokenType].includes(exp.typ)) ;
109
+
110
+ } else
111
+ */
112
+ if ([EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.LengthTokenType].includes(exp.typ)) ;
106
113
  else if (exp.typ == EnumToken.IdenTokenType && exp.val in values) {
107
- if (typeof values[exp.val] == 'number') {
108
- expr[key] = {
109
- typ: EnumToken.NumberTokenType,
110
- val: reduceNumber(values[exp.val])
111
- };
112
- }
113
- else {
114
- expr[key] = values[exp.val];
115
- }
114
+ // if (typeof values[<RelativeColorTypes>exp.val] == 'number') {
115
+ //
116
+ // expr[<RelativeColorTypes>key] = {
117
+ // typ: EnumToken.NumberTokenType,
118
+ // val: reduceNumber(<number>values[<RelativeColorTypes>exp.val])
119
+ // };
120
+ // } else {
121
+ expr[key] = values[exp.val];
122
+ // }
116
123
  }
117
124
  else if (exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val)) {
118
125
  for (let { value, parent } of walkValues(exp.chi, exp)) {
119
- if (parent == null) {
120
- parent = exp;
121
- }
126
+ // if (parent == null) {
127
+ //
128
+ // parent = exp;
129
+ // }
130
+ /*
122
131
  if (value.typ == EnumToken.PercentageTokenType) {
123
- replaceValue(parent, value, getValue(value, converted, key));
124
- }
125
- else if (value.typ == EnumToken.IdenTokenType) {
132
+
133
+ replaceValue(parent as BinaryExpressionToken | FunctionToken | ParensToken, value, getValue(value, converted, <RelativeColorTypes>key));
134
+ } else
135
+ */
136
+ if (value.typ == EnumToken.IdenTokenType) {
126
137
  // @ts-ignore
127
- if (!(value.val in values || typeof Math[value.val.toUpperCase()] == 'number')) {
128
- return null;
129
- }
138
+ // if (!(value.val in values || typeof Math[(value as IdentToken).val.toUpperCase()] == 'number')) {
139
+ //
140
+ // return null;
141
+ // }
130
142
  // @ts-ignore
131
143
  replaceValue(parent, value, values[value.val] ?? {
132
144
  typ: EnumToken.NumberTokenType,
@@ -140,9 +152,10 @@ function computeComponentValue(expr, converted, values) {
140
152
  if (result.length == 1 && result[0].typ != EnumToken.BinaryExpressionTokenType) {
141
153
  expr[key] = result[0];
142
154
  }
143
- else {
144
- return null;
145
- }
155
+ // else {
156
+ //
157
+ // return null;
158
+ // }
146
159
  }
147
160
  }
148
161
  return expr;
@@ -0,0 +1,140 @@
1
+ import { color2srgbvalues, minmax } from './color.js';
2
+ import { COLORS_NAMES } from './utils/constants.js';
3
+ import { EnumToken, ColorType } from '../../ast/types.js';
4
+ import '../../ast/minify.js';
5
+ import '../../ast/walk.js';
6
+ import '../../parser/parse.js';
7
+ import '../../parser/tokenize.js';
8
+ import '../../parser/utils/config.js';
9
+ import { expandHexValue } from './hex.js';
10
+ import { hslvalues, hslvalues2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues } from './srgb.js';
11
+ import '../../renderer/sourcemap/lib/encode.js';
12
+
13
+ function srgb2rgb(value) {
14
+ return minmax(Math.round(value * 255), 0, 255);
15
+ }
16
+ function hex2RgbToken(token) {
17
+ return rgb2RgbToken(hex2rgbvalues(token));
18
+ }
19
+ function hsl2RgbToken(token) {
20
+ const values = hsl2rgbvalues(token);
21
+ if (values == null) {
22
+ return null;
23
+ }
24
+ return rgb2RgbToken(values);
25
+ }
26
+ function hwb2RgbToken(token) {
27
+ const values = hwb2rgbvalues(token);
28
+ if (values == null) {
29
+ return null;
30
+ }
31
+ return rgb2RgbToken(values);
32
+ }
33
+ function cmyk2RgbToken(token) {
34
+ const values = cmyk2rgbvalues(token);
35
+ if (values == null) {
36
+ return null;
37
+ }
38
+ return rgb2RgbToken(values);
39
+ }
40
+ function oklab2RgbToken(token) {
41
+ const values = oklab2rgbvalues(token);
42
+ if (values == null) {
43
+ return null;
44
+ }
45
+ return rgb2RgbToken(values);
46
+ }
47
+ function oklch2RgbToken(token) {
48
+ const values = oklch2rgbvalues(token);
49
+ if (values == null) {
50
+ return null;
51
+ }
52
+ return rgb2RgbToken(values);
53
+ }
54
+ function lab2RgbToken(token) {
55
+ const values = lab2rgbvalues(token);
56
+ if (values == null) {
57
+ return null;
58
+ }
59
+ return rgb2RgbToken(values);
60
+ }
61
+ function lch2RgbToken(token) {
62
+ const values = lch2rgbvalues(token);
63
+ if (values == null) {
64
+ return null;
65
+ }
66
+ return rgb2RgbToken(values);
67
+ }
68
+ function color2RgbToken(token) {
69
+ const values = color2srgbvalues(token);
70
+ if (values == null) {
71
+ return null;
72
+ }
73
+ return rgb2RgbToken(values.map((t, index) => index == 3 ? t : srgb2rgb(t)));
74
+ }
75
+ function rgb2RgbToken(values) {
76
+ const chi = [
77
+ { typ: EnumToken.NumberTokenType, val: String(values[0]) },
78
+ { typ: EnumToken.NumberTokenType, val: String(values[1]) },
79
+ { typ: EnumToken.NumberTokenType, val: String(values[2]) },
80
+ ];
81
+ if (values.length == 4) {
82
+ chi.push({ typ: EnumToken.PercentageTokenType, val: (values[3] * 100).toFixed() });
83
+ }
84
+ return {
85
+ typ: EnumToken.ColorTokenType,
86
+ val: 'rgb',
87
+ chi,
88
+ kin: ColorType.RGB
89
+ };
90
+ }
91
+ function hex2rgbvalues(token) {
92
+ const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
93
+ const rgb = [];
94
+ for (let i = 1; i < value.length; i += 2) {
95
+ rgb.push(parseInt(value.slice(i, i + 2), 16));
96
+ }
97
+ if (rgb.length == 4) {
98
+ if (rgb[3] == 255) {
99
+ rgb.pop();
100
+ }
101
+ else {
102
+ rgb[3] = +(rgb[3] / 255).toFixed(2);
103
+ }
104
+ }
105
+ return rgb;
106
+ }
107
+ function hwb2rgbvalues(token) {
108
+ return hwb2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
109
+ }
110
+ function hsl2rgbvalues(token) {
111
+ let { h, s, l, a } = hslvalues(token) ?? {};
112
+ if (h == null || s == null || l == null) {
113
+ return null;
114
+ }
115
+ return hslvalues2srgbvalues(h, s, l).map((t) => minmax(Math.round(t * 255), 0, 255)).concat(a == 1 || a == null ? [] : [a]);
116
+ }
117
+ function hsl2srgbvalues(token) {
118
+ let { h, s, l, a } = hslvalues(token) ?? {};
119
+ if (h == null || s == null || l == null) {
120
+ return null;
121
+ }
122
+ return hslvalues2srgbvalues(h, s, l).concat(a == 1 || a == null ? [] : [a]);
123
+ }
124
+ function cmyk2rgbvalues(token) {
125
+ return cmyk2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
126
+ }
127
+ function oklab2rgbvalues(token) {
128
+ return oklab2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
129
+ }
130
+ function oklch2rgbvalues(token) {
131
+ return oklch2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
132
+ }
133
+ function lab2rgbvalues(token) {
134
+ return lab2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
135
+ }
136
+ function lch2rgbvalues(token) {
137
+ return lch2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
138
+ }
139
+
140
+ export { cmyk2RgbToken, cmyk2rgbvalues, color2RgbToken, hex2RgbToken, hex2rgbvalues, hsl2RgbToken, hsl2rgbvalues, hsl2srgbvalues, hwb2RgbToken, hwb2rgbvalues, lab2RgbToken, lab2rgbvalues, lch2RgbToken, lch2rgbvalues, oklab2RgbToken, oklab2rgbvalues, oklch2RgbToken, oklch2rgbvalues, srgb2rgb };