@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
@@ -1,45 +1,45 @@
1
- import { ColorKind, COLORS_NAMES } from './utils/constants.js';
1
+ import { COLORS_NAMES } from './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
3
  import { color2srgbvalues, getNumber, getAngle } from './color.js';
4
- import { EnumToken } from '../../ast/types.js';
4
+ import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
5
+ import { ColorType, EnumToken } from '../../ast/types.js';
5
6
  import '../../ast/minify.js';
6
7
  import '../../ast/walk.js';
7
8
  import '../../parser/parse.js';
8
9
  import '../../parser/tokenize.js';
9
10
  import '../../parser/utils/config.js';
10
11
  import { expandHexValue } from './hex.js';
11
- import { lch2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
12
- import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
12
+ import { lchvalues2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
13
13
  import { getLCHComponents } from './lch.js';
14
14
  import { getOKLCHComponents } from './oklch.js';
15
15
  import { XYZ_to_lin_sRGB } from './xyz.js';
16
- import '../sourcemap/lib/encode.js';
16
+ import '../../renderer/sourcemap/lib/encode.js';
17
17
 
18
18
  // from https://www.w3.org/TR/css-color-4/#color-conversion-code
19
19
  // srgb-linear -> srgb
20
20
  // 0 <= r, g, b <= 1
21
21
  function srgbvalues(token) {
22
22
  switch (token.kin) {
23
- case ColorKind.LIT:
24
- case ColorKind.HEX:
25
- return hex2srgb(token);
26
- case ColorKind.RGB:
27
- case ColorKind.RGBA:
23
+ case ColorType.LIT:
24
+ case ColorType.HEX:
25
+ return hex2srgbvalues(token);
26
+ case ColorType.RGB:
27
+ case ColorType.RGBA:
28
28
  return rgb2srgb(token);
29
- case ColorKind.HSL:
30
- case ColorKind.HSLA:
29
+ case ColorType.HSL:
30
+ case ColorType.HSLA:
31
31
  return hsl2srgb(token);
32
- case ColorKind.HWB:
33
- return hwb2srgb(token);
34
- case ColorKind.LAB:
35
- return lab2srgb(token);
36
- case ColorKind.LCH:
37
- return lch2srgb(token);
38
- case ColorKind.OKLAB:
39
- return oklab2srgb(token);
40
- case ColorKind.OKLCH:
41
- return oklch2srgb(token);
42
- case ColorKind.COLOR:
32
+ case ColorType.HWB:
33
+ return hwb2srgbvalues(token);
34
+ case ColorType.LAB:
35
+ return lab2srgbvalues(token);
36
+ case ColorType.LCH:
37
+ return lch2srgbvalues(token);
38
+ case ColorType.OKLAB:
39
+ return oklab2srgbvalues(token);
40
+ case ColorType.OKLCH:
41
+ return oklch2srgbvalues(token);
42
+ case ColorType.COLOR:
43
43
  return color2srgbvalues(token);
44
44
  }
45
45
  return null;
@@ -47,24 +47,31 @@ function srgbvalues(token) {
47
47
  function rgb2srgb(token) {
48
48
  return getComponents(token)?.map?.((t, index) => index == 3 ? ((t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t)) : (t.typ == EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255) ?? null;
49
49
  }
50
- function hex2srgb(token) {
51
- const value = expandHexValue(token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
50
+ function rgb2srgbvalues(token) {
51
+ return getComponents(token)?.map?.((t, index) => index == 3 ? getNumber(t) : getNumber(t) / 255) ?? null;
52
+ }
53
+ function hex2srgbvalues(token) {
54
+ const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
52
55
  const rgb = [];
53
56
  for (let i = 1; i < value.length; i += 2) {
54
57
  rgb.push(parseInt(value.slice(i, i + 2), 16) / 255);
55
58
  }
59
+ if (rgb.length == 4) {
60
+ rgb[3] = +rgb[3].toFixed(2);
61
+ }
56
62
  return rgb;
57
63
  }
58
- function xyz2srgb(x, y, z) {
64
+ // xyz d65 input
65
+ function xyz2srgb(x, y, z, alpha = null) {
59
66
  // @ts-ignore
60
- return lsrgb2srgbvalues(...XYZ_to_lin_sRGB(x, y, z));
67
+ return lsrgb2srgbvalues(...XYZ_to_lin_sRGB(x, y, z, alpha));
61
68
  }
62
- function hwb2srgb(token) {
69
+ function hwb2srgbvalues(token) {
63
70
  const { h: hue, s: white, l: black, a: alpha } = hslvalues(token) ?? {};
64
71
  if (hue == null || white == null || black == null) {
65
72
  return [];
66
73
  }
67
- const rgb = hsl2srgbvalues(hue, 1, .5);
74
+ const rgb = hslvalues2srgbvalues(hue, 1, .5);
68
75
  for (let i = 0; i < 3; i++) {
69
76
  rgb[i] *= (1 - white - black);
70
77
  rgb[i] = rgb[i] + white;
@@ -79,9 +86,9 @@ function hsl2srgb(token) {
79
86
  if (h == null || s == null || l == null) {
80
87
  return null;
81
88
  }
82
- return hsl2srgbvalues(h, s, l, a);
89
+ return hslvalues2srgbvalues(h, s, l, a);
83
90
  }
84
- function cmyk2srgb(token) {
91
+ function cmyk2srgbvalues(token) {
85
92
  const components = getComponents(token);
86
93
  if (components == null) {
87
94
  return null;
@@ -107,16 +114,21 @@ function cmyk2srgb(token) {
107
114
  1 - Math.min(1, m * (1 - k) + k),
108
115
  1 - Math.min(1, y * (1 - k) + k)
109
116
  ];
110
- // @ts-ignore
111
- if (token.chi.length >= 9) {
112
- // @ts-ignore
113
- t = token.chi[8];
114
- // @ts-ignore
115
- rgb.push(getNumber(t));
117
+ if (components.length == 5) {
118
+ rgb.push(getNumber(components[4]));
116
119
  }
120
+ // @ts-ignore
121
+ // if (token.chi.length >= 9) {
122
+ //
123
+ // // @ts-ignore
124
+ // t = <NumberToken | PercentageToken>token.chi[8];
125
+ //
126
+ // // @ts-ignore
127
+ // rgb.push(getNumber(t));
128
+ // }
117
129
  return rgb;
118
130
  }
119
- function oklab2srgb(token) {
131
+ function oklab2srgbvalues(token) {
120
132
  const [l, a, b, alpha] = getOKLABComponents(token) ?? [];
121
133
  if (l == null || a == null || b == null) {
122
134
  return null;
@@ -127,13 +139,13 @@ function oklab2srgb(token) {
127
139
  }
128
140
  return rgb;
129
141
  }
130
- function oklch2srgb(token) {
142
+ function oklch2srgbvalues(token) {
131
143
  const [l, c, h, alpha] = getOKLCHComponents(token) ?? [];
132
144
  if (l == null || c == null || h == null) {
133
145
  return null;
134
146
  }
135
147
  // @ts-ignore
136
- const rgb = OKLab_to_sRGB(...lch2labvalues(l, c, h));
148
+ const rgb = OKLab_to_sRGB(...lchvalues2labvalues(l, c, h));
137
149
  if (alpha != 1) {
138
150
  rgb.push(alpha);
139
151
  }
@@ -164,7 +176,7 @@ function hslvalues(token) {
164
176
  }
165
177
  return a == null ? { h, s, l } : { h, s, l, a };
166
178
  }
167
- function hsl2srgbvalues(h, s, l, a = null) {
179
+ function hslvalues2srgbvalues(h, s, l, a = null) {
168
180
  let v = l <= .5 ? l * (1.0 + s) : l + s - l * s;
169
181
  let r = l;
170
182
  let g = l;
@@ -217,7 +229,7 @@ function hsl2srgbvalues(h, s, l, a = null) {
217
229
  }
218
230
  return values;
219
231
  }
220
- function lab2srgb(token) {
232
+ function lab2srgbvalues(token) {
221
233
  const [l, a, b, alpha] = getLABComponents(token) ?? [];
222
234
  if (l == null || a == null || b == null) {
223
235
  return null;
@@ -228,9 +240,9 @@ function lab2srgb(token) {
228
240
  }
229
241
  return rgb;
230
242
  }
231
- function lch2srgb(token) {
243
+ function lch2srgbvalues(token) {
232
244
  // @ts-ignore
233
- const [l, a, b, alpha] = lch2labvalues(...getLCHComponents(token));
245
+ const [l, a, b, alpha] = lchvalues2labvalues(...getLCHComponents(token));
234
246
  if (l == null || a == null || b == null) {
235
247
  return null;
236
248
  }
@@ -261,7 +273,7 @@ function srgb2lsrgbvalues(r, g, b, a = null) {
261
273
  }
262
274
  return rgb;
263
275
  }
264
- function lsrgb2srgbvalues(r, g, b, alpha) {
276
+ function lsrgb2srgbvalues(r, g, b, alpha = null) {
265
277
  // convert an array of linear-light sRGB values in the range 0.0-1.0
266
278
  // to gamma corrected form
267
279
  // https://en.wikipedia.org/wiki/SRGB
@@ -281,4 +293,4 @@ function lsrgb2srgbvalues(r, g, b, alpha) {
281
293
  return rgb;
282
294
  }
283
295
 
284
- export { cmyk2srgb, hex2srgb, hsl2srgb, hsl2srgbvalues, hslvalues, hwb2srgb, lab2srgb, lch2srgb, lsrgb2srgbvalues, oklab2srgb, oklch2srgb, rgb2srgb, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
296
+ export { cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hslvalues, hslvalues2srgbvalues, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, oklab2srgbvalues, oklch2srgbvalues, rgb2srgb, rgb2srgbvalues, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
@@ -1,19 +1,19 @@
1
- import { EnumToken } from '../../../ast/types.js';
1
+ import { ColorType, EnumToken } from '../../../ast/types.js';
2
2
  import '../../../ast/minify.js';
3
3
  import '../../../ast/walk.js';
4
4
  import '../../../parser/parse.js';
5
5
  import '../../../parser/tokenize.js';
6
6
  import '../../../parser/utils/config.js';
7
- import { ColorKind, COLORS_NAMES } from './constants.js';
7
+ import { COLORS_NAMES } from './constants.js';
8
8
  import { expandHexValue } from '../hex.js';
9
- import '../../sourcemap/lib/encode.js';
9
+ import '../../../renderer/sourcemap/lib/encode.js';
10
10
 
11
11
  function getComponents(token) {
12
- if (token.kin == ColorKind.HEX || token.kin == ColorKind.LIT) {
13
- const value = expandHexValue(token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
12
+ if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
13
+ const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
14
14
  // @ts-ignore
15
- return value.slice(1).match(/([a-fA-F0-9]{2})/g).map((t) => {
16
- return { typ: EnumToken.Number, val: parseInt(t, 16).toString() };
15
+ return value.slice(1).match(/([a-fA-F0-9]{2})/g).map((t, index) => {
16
+ return { typ: EnumToken.Number, val: (index < 3 ? parseInt(t, 16) : parseInt(t, 16) / 255).toString() };
17
17
  });
18
18
  }
19
19
  const result = [];
@@ -4,8 +4,10 @@ import '../../../ast/walk.js';
4
4
  import '../../../parser/parse.js';
5
5
  import '../../../parser/tokenize.js';
6
6
  import '../../../parser/utils/config.js';
7
- import '../../sourcemap/lib/encode.js';
7
+ import '../../../renderer/sourcemap/lib/encode.js';
8
8
 
9
+ const colorPrecision = 6;
10
+ const anglePrecision = .001;
9
11
  const colorRange = {
10
12
  lab: {
11
13
  l: [0, 100],
@@ -28,38 +30,9 @@ const colorRange = {
28
30
  b: [0, 0.4]
29
31
  }
30
32
  };
31
- // xyz-d65 is an alias for xyz
32
- // display-p3 is an alias for srgb
33
- var ColorKind;
34
- (function (ColorKind) {
35
- ColorKind[ColorKind["SYS"] = 0] = "SYS";
36
- ColorKind[ColorKind["DPSYS"] = 1] = "DPSYS";
37
- ColorKind[ColorKind["LIT"] = 2] = "LIT";
38
- ColorKind[ColorKind["HEX"] = 3] = "HEX";
39
- ColorKind[ColorKind["RGB"] = 4] = "RGB";
40
- ColorKind[ColorKind["RGBA"] = 5] = "RGBA";
41
- ColorKind[ColorKind["HSL"] = 6] = "HSL";
42
- ColorKind[ColorKind["HSLA"] = 7] = "HSLA";
43
- ColorKind[ColorKind["HWB"] = 8] = "HWB";
44
- ColorKind[ColorKind["DEVICE_CMYK"] = 9] = "DEVICE_CMYK";
45
- ColorKind[ColorKind["OKLAB"] = 10] = "OKLAB";
46
- ColorKind[ColorKind["OKLCH"] = 11] = "OKLCH";
47
- ColorKind[ColorKind["LAB"] = 12] = "LAB";
48
- ColorKind[ColorKind["LCH"] = 13] = "LCH";
49
- ColorKind[ColorKind["COLOR"] = 14] = "COLOR";
50
- ColorKind[ColorKind["SRGB"] = 15] = "SRGB";
51
- ColorKind[ColorKind["PROPHOTO_RGB"] = 16] = "PROPHOTO_RGB";
52
- ColorKind[ColorKind["A98_RGB"] = 17] = "A98_RGB";
53
- ColorKind[ColorKind["REC2020"] = 18] = "REC2020";
54
- ColorKind[ColorKind["DISPLAY_P3"] = 19] = "DISPLAY_P3";
55
- ColorKind[ColorKind["SRGB_LINEAR"] = 20] = "SRGB_LINEAR";
56
- ColorKind[ColorKind["XYZ"] = 21] = "XYZ";
57
- ColorKind[ColorKind["XYZ_D50"] = 22] = "XYZ_D50";
58
- ColorKind[ColorKind["XYZ_D65"] = 23] = "XYZ_D65";
59
- ColorKind[ColorKind["LIGHT_DARK"] = 24] = "LIGHT_DARK";
60
- ColorKind[ColorKind["COLOR_MIX"] = 25] = "COLOR_MIX";
61
- })(ColorKind || (ColorKind = {}));
33
+ const timelineFunc = ['view', 'scroll'];
62
34
  const generalEnclosedFunc = ['selector', 'font-tech', 'font-format', 'media', 'supports'];
35
+ const timingFunc = ['ease', 'ease-in', 'ease-out', 'ease-in-out', 'linear', 'step-start', 'step-end', 'steps', 'cubic-bezier'];
63
36
  const funcLike = [
64
37
  EnumToken.ParensTokenType,
65
38
  EnumToken.FunctionTokenType,
@@ -77,7 +50,7 @@ const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
77
50
  const k = Math.pow(29, 3) / Math.pow(3, 3);
78
51
  const e = Math.pow(6, 3) / Math.pow(29, 3);
79
52
  // color module v4
80
- const systemColors = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText'].map(m => m.toLowerCase()));
53
+ const systemColors = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText', '-webkit-focus-ring-color'].map(m => m.toLowerCase()));
81
54
  // deprecated
82
55
  const deprecatedSystemColors = new Set(['ActiveBorder', 'ActiveCaption', 'AppWorkspace', 'Background', 'ButtonFace', 'ButtonHighlight', 'ButtonShadow', 'ButtonText', 'CaptionText', 'GrayText', 'Highlight', 'HighlightText', 'InactiveBorder', 'InactiveCaption', 'InactiveCaptionText', 'InfoBackground', 'InfoText', 'Menu', 'MenuText', 'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight', 'ThreeDLightShadow', 'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText'].map(t => t.toLowerCase()));
83
56
  // name to color
@@ -238,4 +211,4 @@ const NAMES_COLORS = Object.seal(Object.entries(COLORS_NAMES).reduce((acc, [key,
238
211
  return acc;
239
212
  }, Object.create(null)));
240
213
 
241
- export { COLORS_NAMES, ColorKind, D50, NAMES_COLORS, colorFuncColorSpace, colorRange, colorsFunc, deprecatedSystemColors, e, funcLike, generalEnclosedFunc, k, systemColors };
214
+ export { COLORS_NAMES, D50, NAMES_COLORS, anglePrecision, colorFuncColorSpace, colorPrecision, colorRange, colorsFunc, deprecatedSystemColors, e, funcLike, generalEnclosedFunc, k, systemColors, timelineFunc, timingFunc };
@@ -0,0 +1,30 @@
1
+ import { convertColor } from '../color.js';
2
+ import { getOKLABComponents } from '../oklab.js';
3
+ import { 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 './constants.js';
10
+ import '../../../renderer/sourcemap/lib/encode.js';
11
+
12
+ function okLabDistance(okLab1, okLab2) {
13
+ return Math.sqrt(Math.pow(okLab1[0] - okLab2[0], 2) + Math.pow(okLab1[1] - okLab2[1], 2) + Math.pow(okLab1[2] - okLab2[2], 2));
14
+ }
15
+ function isOkLabClose(color1, color2, threshold = .01) {
16
+ color1 = convertColor(color1, ColorType.OKLAB);
17
+ color2 = convertColor(color2, ColorType.OKLAB);
18
+ // console.error(JSON.stringify({color1, color2}, null, 1));
19
+ if (color1 == null || color2 == null) {
20
+ return false;
21
+ }
22
+ const okLab1 = getOKLABComponents(color1);
23
+ const okLab2 = getOKLABComponents(color2);
24
+ if (okLab1 == null || okLab2 == null) {
25
+ return false;
26
+ }
27
+ return okLabDistance(okLab1, okLab2) < threshold;
28
+ }
29
+
30
+ export { isOkLabClose, okLabDistance };
@@ -7,9 +7,10 @@ import '../../parser/parse.js';
7
7
  import '../../parser/tokenize.js';
8
8
  import '../../parser/utils/config.js';
9
9
  import { srgb2lsrgbvalues } from './srgb.js';
10
- import '../sourcemap/lib/encode.js';
10
+ import { XYZ_D65_to_D50 } from './xyzd50.js';
11
+ import '../../renderer/sourcemap/lib/encode.js';
11
12
 
12
- function XYZ_to_lin_sRGB(x, y, z) {
13
+ function XYZ_to_lin_sRGB(x, y, z, alpha = null) {
13
14
  // convert XYZ to linear-light sRGB
14
15
  const M = [
15
16
  [12831 / 3959, -329 / 214, -1974 / 3959],
@@ -17,7 +18,7 @@ function XYZ_to_lin_sRGB(x, y, z) {
17
18
  [705 / 12673, -2585 / 12673, 705 / 667],
18
19
  ];
19
20
  const XYZ = [x, y, z]; // convert to XYZ
20
- return multiplyMatrices(M, XYZ).map((v) => v);
21
+ return multiplyMatrices(M, XYZ).map((v) => v).concat(alpha == null || alpha == 1 ? [] : [alpha]);
21
22
  }
22
23
  function XYZ_D50_to_D65(x, y, z) {
23
24
  // Bradford chromatic adaptation from D50 to D65
@@ -29,23 +30,35 @@ function XYZ_D50_to_D65(x, y, z) {
29
30
  const XYZ = [x, y, z];
30
31
  return multiplyMatrices(M, XYZ); //.map((v: number) => v);
31
32
  }
33
+ // xyz d65
32
34
  function srgb2xyz(r, g, b, alpha) {
33
35
  [r, g, b] = srgb2lsrgbvalues(r, g, b);
34
- const rgb = [
35
- 0.436065742824811 * r +
36
- 0.3851514688337912 * g +
37
- 0.14307845442264197 * b,
38
- 0.22249319175623702 * r +
39
- 0.7168870538238823 * g +
40
- 0.06061979053616537 * b,
41
- 0.013923904500943465 * r +
42
- 0.09708128566574634 * g +
43
- 0.7140993584005155 * b
36
+ // xyx d65
37
+ let rgb = [
38
+ 0.4123907992659595 * r +
39
+ 0.35758433938387796 * g +
40
+ 0.1804807884018343 * b,
41
+ 0.21263900587151036 * r +
42
+ 0.7151686787677559 * g +
43
+ 0.07219231536073371 * b,
44
+ 0.01933081871559185 * r +
45
+ 0.11919477979462599 * g +
46
+ 0.9505321522496606 * b
44
47
  ];
45
48
  if (alpha != null && alpha != 1) {
46
49
  rgb.push(alpha);
47
50
  }
48
51
  return rgb;
49
52
  }
53
+ // xyz d50
54
+ function srgb2xyz_d50(r, g, b, alpha) {
55
+ // xyx d65
56
+ // @ts-ignore
57
+ let rgb = XYZ_D65_to_D50(...srgb2xyz(r, g, b));
58
+ if (alpha != null && alpha != 1) {
59
+ rgb.push(alpha);
60
+ }
61
+ return rgb;
62
+ }
50
63
 
51
- export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz };
64
+ export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz, srgb2xyz_d50 };
@@ -8,9 +8,9 @@ import '../../parser/parse.js';
8
8
  import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
10
  import { xyz2lab } from './lab.js';
11
- import { lab2lchvalues } from './lch.js';
11
+ import { labvalues2lchvalues } from './lch.js';
12
12
  import { XYZ_D50_to_D65 } from './xyz.js';
13
- import '../sourcemap/lib/encode.js';
13
+ import '../../renderer/sourcemap/lib/encode.js';
14
14
 
15
15
  /*
16
16
  */
@@ -18,23 +18,23 @@ function xyzd502lch(x, y, z, alpha) {
18
18
  // @ts-ignore
19
19
  const [l, a, b] = xyz2lab(...XYZ_D50_to_D65(x, y, z));
20
20
  // L in range [0,100]. For use in CSS, add a percent
21
- return lab2lchvalues(l, a, b, alpha);
21
+ return labvalues2lchvalues(l, a, b, alpha);
22
22
  }
23
- function XYZ_D65_to_D50(x, y, z) {
23
+ function XYZ_D65_to_D50(x, y, z, alpha = null) {
24
24
  // Bradford chromatic adaptation from D65 to D50
25
25
  // The matrix below is the result of three operations:
26
26
  // - convert from XYZ to retinal cone domain
27
27
  // - scale components from one reference white to another
28
28
  // - convert back to XYZ
29
29
  // see https://github.com/LeaVerou/color.js/pull/354/files
30
- var M = [
30
+ let M = [
31
31
  [1.0479297925449969, 0.022946870601609652, -0.05019226628920524],
32
32
  [0.02962780877005599, 0.9904344267538799, -0.017073799063418826],
33
33
  [-0.009243040646204504, 0.015055191490298152, 0.7518742814281371]
34
34
  ];
35
- return multiplyMatrices(M, [x, y, z]);
35
+ return multiplyMatrices(M, [x, y, z]).concat(alpha == null || alpha == 1 ? [] : [alpha]);
36
36
  }
37
- function xyzd502srgb(x, y, z) {
37
+ function xyzd502srgb(x, y, z, alpha = null) {
38
38
  // @ts-ignore
39
39
  return lsrgb2srgbvalues(
40
40
  /* r: */
@@ -48,7 +48,7 @@ function xyzd502srgb(x, y, z) {
48
48
  /* b: */
49
49
  x * 0.07195537988411677 -
50
50
  y * 0.2289768264158322 +
51
- 1.405386058324125 * z);
51
+ 1.405386058324125 * z, alpha);
52
52
  }
53
53
 
54
54
  export { XYZ_D65_to_D50, xyzd502lch, xyzd502srgb };
@@ -1,10 +1,10 @@
1
- import { EnumToken } from '../ast/types.js';
1
+ import { EnumToken, ColorType } from '../ast/types.js';
2
2
  import '../ast/minify.js';
3
3
  import { walkValues, WalkerOptionEnum } from '../ast/walk.js';
4
4
  import '../parser/parse.js';
5
5
  import '../parser/tokenize.js';
6
6
  import '../parser/utils/config.js';
7
- import { COLORS_NAMES, colorsFunc, funcLike, ColorKind } from '../renderer/color/utils/constants.js';
7
+ import { COLORS_NAMES, colorsFunc, funcLike } from './color/utils/constants.js';
8
8
  import { buildExpression } from '../ast/math/expression.js';
9
9
  import '../renderer/sourcemap/lib/encode.js';
10
10
 
@@ -498,15 +498,24 @@ function isPolarColorspace(token) {
498
498
  return ['hsl', 'hwb', 'lch', 'oklch'].includes(token.val);
499
499
  }
500
500
  function isHueInterpolationMethod(token) {
501
- if (token.typ != EnumToken.IdenTokenType) {
501
+ if (!Array.isArray(token)) {
502
+ return token.typ == EnumToken.IdenTokenType && 'hue'.localeCompare(token.val, undefined, { sensitivity: 'base' }) == 0;
503
+ }
504
+ if (token.length != 2 || token[0].typ != EnumToken.IdenTokenType || token[1].typ != EnumToken.IdenTokenType) {
502
505
  return false;
503
506
  }
504
- return ['shorter', 'longer', 'increasing', 'decreasing'].includes(token.val);
507
+ return ['shorter', 'longer', 'increasing', 'decreasing'].includes(token[0].val) && 'hue'.localeCompare(token[1].val, undefined, { sensitivity: 'base' }) == 0;
505
508
  }
506
509
  function isIdentColor(token) {
507
- return token.typ == EnumToken.ColorTokenType && [ColorKind.SYS, ColorKind.DPSYS, ColorKind.LIT].includes(token.kin) && isIdent(token.val);
510
+ return token.typ == EnumToken.ColorTokenType && [ColorType.SYS, ColorType.DPSYS, ColorType.LIT].includes(token.kin) && isIdent(token.val);
511
+ }
512
+ function isPercentageToken(token) {
513
+ return token.typ == EnumToken.PercentageTokenType || (token.typ == EnumToken.NumberTokenType && token.val == '0');
508
514
  }
509
515
  function isColor(token) {
516
+ if (token.typ == EnumToken.ColorTokenType) {
517
+ return true;
518
+ }
510
519
  if (token.typ == EnumToken.IdenTokenType) {
511
520
  // named color
512
521
  return token.val.toLowerCase() in COLORS_NAMES;
@@ -595,79 +604,80 @@ function isColor(token) {
595
604
  }
596
605
  return true;
597
606
  }
598
- else { // @ts-ignore
599
- if (token.val == 'color-mix') {
600
- // @ts-ignore
601
- const children = token.chi.reduce((acc, t) => {
602
- if (t.typ == EnumToken.CommaTokenType) {
603
- acc.push([]);
604
- }
605
- else {
606
- if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
607
- acc[acc.length - 1].push(t);
608
- }
607
+ // @ts-ignore
608
+ else if (token.val == 'color-mix') {
609
+ // @ts-ignore
610
+ const children = token.chi.reduce((acc, t) => {
611
+ if (t.typ == EnumToken.CommaTokenType) {
612
+ acc.push([]);
613
+ }
614
+ else {
615
+ if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
616
+ acc[acc.length - 1].push(t);
609
617
  }
610
- return acc;
611
- }, [[]]);
612
- if (children.length == 3) {
613
- if (children[0].length > 3 ||
614
- children[0][0].typ != EnumToken.IdenTokenType ||
615
- children[0][0].val != 'in' ||
616
- !isColorspace(children[0][1]) ||
617
- (children[0].length == 3 && !isHueInterpolationMethod(children[0][2])) ||
618
- children[1].length > 2 ||
619
- children[1][0].typ != EnumToken.ColorTokenType ||
620
- children[2].length > 2 ||
621
- children[2][0].typ != EnumToken.ColorTokenType) {
618
+ }
619
+ return acc;
620
+ }, [[]]);
621
+ if (children.length == 3) {
622
+ if (children[0].length > 4 ||
623
+ children[0][0].typ != EnumToken.IdenTokenType ||
624
+ 'in'.localeCompare(children[0][0].val, undefined, { sensitivity: 'base' }) != 0 ||
625
+ !isColorspace(children[0][1]) ||
626
+ (children[0].length >= 3 && !isHueInterpolationMethod(children[0].slice(2))) ||
627
+ children[1].length > 2 ||
628
+ !isColor(children[1][0]) ||
629
+ (children[1].length == 2 && !isPercentageToken(children[1][1])) ||
630
+ children[2].length > 2 ||
631
+ (children[2].length == 2 && !isPercentageToken(children[2][1])) ||
632
+ !isColor(children[2][0])) {
633
+ return false;
634
+ }
635
+ if (children[1].length == 2) {
636
+ if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
622
637
  return false;
623
638
  }
624
- if (children[1].length == 2) {
625
- if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
626
- return false;
627
- }
628
- }
629
- if (children[2].length == 2) {
630
- if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
631
- return false;
632
- }
639
+ }
640
+ if (children[2].length == 2) {
641
+ if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
642
+ return false;
633
643
  }
634
- return true;
635
644
  }
636
- return false;
645
+ return true;
637
646
  }
638
- else {
639
- const keywords = ['from', 'none'];
647
+ return false;
648
+ }
649
+ else {
650
+ const keywords = ['from', 'none'];
651
+ // @ts-ignore
652
+ if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
640
653
  // @ts-ignore
641
- if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
642
- // @ts-ignore
643
- keywords.push('alpha', ...token.val.slice(-3).split(''));
654
+ keywords.push('alpha', ...token.val.slice(-3).split(''));
655
+ }
656
+ // @ts-ignore
657
+ for (const v of token.chi) {
658
+ if (v.typ == EnumToken.CommaTokenType) {
659
+ isLegacySyntax = true;
644
660
  }
645
- // @ts-ignore
646
- for (const v of token.chi) {
647
- if (v.typ == EnumToken.CommaTokenType) {
648
- isLegacySyntax = true;
661
+ if (v.typ == EnumToken.IdenTokenType) {
662
+ if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
663
+ return false;
649
664
  }
650
- if (v.typ == EnumToken.IdenTokenType) {
651
- if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
665
+ if (keywords.includes(v.val)) {
666
+ if (isLegacySyntax) {
652
667
  return false;
653
668
  }
654
- if (keywords.includes(v.val)) {
655
- if (isLegacySyntax) {
656
- return false;
657
- }
658
- // @ts-ignore
659
- if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
660
- return false;
661
- }
669
+ // @ts-ignore
670
+ if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
671
+ return false;
662
672
  }
663
- continue;
664
- }
665
- if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
666
- continue;
667
- }
668
- if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
669
- return false;
670
673
  }
674
+ continue;
675
+ }
676
+ if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
677
+ continue;
678
+ }
679
+ if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
680
+ return false;
671
681
  }
672
682
  }
673
683
  }
@@ -680,7 +690,7 @@ function parseColor(token) {
680
690
  // @ts-ignore
681
691
  token.typ = EnumToken.ColorTokenType;
682
692
  // @ts-ignore
683
- token.kin = ColorKind[token.val.replaceAll('-', '_').toUpperCase()];
693
+ token.kin = ColorType[token.val.replaceAll('-', '_').toUpperCase()];
684
694
  // @ts-ignore
685
695
  if (token.chi[0].typ == EnumToken.IdenTokenType) {
686
696
  // @ts-ignore
@@ -947,4 +957,4 @@ function isWhiteSpace(codepoint) {
947
957
  codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
948
958
  }
949
959
 
950
- export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseColor, parseDimension, pseudoAliasMap, pseudoElements, transformFunctions, webkitExtensions, wildCardFuncs };
960
+ export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPercentageToken, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseColor, parseDimension, pseudoAliasMap, pseudoElements, transformFunctions, webkitExtensions, wildCardFuncs };