@tbela99/css-parser 1.1.1 → 1.3.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 (107) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +5503 -5037
  4. package/dist/index.cjs +5510 -5044
  5. package/dist/index.d.ts +148 -41
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +14 -36
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -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 +74 -172
  13. package/dist/lib/ast/math/math.js +24 -22
  14. package/dist/lib/ast/minify.js +249 -199
  15. package/dist/lib/ast/transform/compute.js +18 -41
  16. package/dist/lib/ast/transform/matrix.js +36 -36
  17. package/dist/lib/ast/transform/minify.js +37 -59
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +80 -28
  24. package/dist/lib/ast/types.js +122 -2
  25. package/dist/lib/fs/resolve.js +1 -14
  26. package/dist/lib/parser/declaration/list.js +1 -1
  27. package/dist/lib/parser/declaration/map.js +1 -1
  28. package/dist/lib/parser/declaration/set.js +3 -3
  29. package/dist/lib/parser/parse.js +53 -107
  30. package/dist/lib/parser/tokenize.js +29 -53
  31. package/dist/lib/parser/utils/declaration.js +1 -1
  32. package/dist/lib/parser/utils/type.js +2 -2
  33. package/dist/lib/renderer/render.js +79 -194
  34. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  35. package/dist/lib/syntax/color/cmyk.js +104 -0
  36. package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
  37. package/dist/lib/syntax/color/color.js +581 -0
  38. package/dist/lib/syntax/color/hex.js +179 -0
  39. package/dist/lib/syntax/color/hsl.js +201 -0
  40. package/dist/lib/syntax/color/hwb.js +204 -0
  41. package/dist/lib/syntax/color/lab.js +262 -0
  42. package/dist/lib/syntax/color/lch.js +194 -0
  43. package/dist/lib/syntax/color/oklab.js +237 -0
  44. package/dist/lib/syntax/color/oklch.js +166 -0
  45. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  46. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  47. package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
  48. package/dist/lib/syntax/color/rgb.js +140 -0
  49. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  50. package/dist/lib/syntax/color/utils/components.js +46 -0
  51. package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
  52. package/dist/lib/syntax/color/utils/distance.js +40 -0
  53. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  54. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  55. package/dist/lib/syntax/syntax.js +78 -77
  56. package/dist/lib/syntax/utils.js +70 -0
  57. package/dist/lib/validation/at-rules/container.js +1 -1
  58. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  59. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  60. package/dist/lib/validation/at-rules/document.js +2 -2
  61. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  62. package/dist/lib/validation/at-rules/import.js +5 -5
  63. package/dist/lib/validation/at-rules/keyframes.js +3 -13
  64. package/dist/lib/validation/at-rules/layer.js +1 -1
  65. package/dist/lib/validation/at-rules/media.js +1 -1
  66. package/dist/lib/validation/at-rules/namespace.js +1 -1
  67. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  68. package/dist/lib/validation/at-rules/page.js +1 -1
  69. package/dist/lib/validation/at-rules/supports.js +7 -7
  70. package/dist/lib/validation/at-rules/when.js +1 -1
  71. package/dist/lib/validation/atrule.js +2 -2
  72. package/dist/lib/validation/config.js +0 -3
  73. package/dist/lib/validation/config.json.js +1 -1
  74. package/dist/lib/validation/parser/parse.js +8 -11
  75. package/dist/lib/validation/selector.js +1 -9
  76. package/dist/lib/validation/syntax.js +67 -137
  77. package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
  78. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  79. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  80. package/dist/lib/validation/syntaxes/family-name.js +5 -40
  81. package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
  82. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  83. package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
  84. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  85. package/dist/lib/validation/syntaxes/url.js +3 -34
  86. package/dist/lib/validation/utils/list.js +2 -9
  87. package/dist/lib/validation/utils/whitespace.js +1 -1
  88. package/dist/node/index.js +4 -2
  89. package/dist/web/index.js +4 -2
  90. package/package.json +4 -4
  91. package/.editorconfig +0 -484
  92. package/dist/lib/ast/transform/convert.js +0 -33
  93. package/dist/lib/ast/utils/utils.js +0 -104
  94. package/dist/lib/renderer/color/color.js +0 -654
  95. package/dist/lib/renderer/color/hex.js +0 -105
  96. package/dist/lib/renderer/color/hsl.js +0 -125
  97. package/dist/lib/renderer/color/hwb.js +0 -103
  98. package/dist/lib/renderer/color/lab.js +0 -148
  99. package/dist/lib/renderer/color/lch.js +0 -90
  100. package/dist/lib/renderer/color/oklab.js +0 -131
  101. package/dist/lib/renderer/color/oklch.js +0 -75
  102. package/dist/lib/renderer/color/rgb.js +0 -50
  103. package/dist/lib/renderer/color/utils/components.js +0 -34
  104. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  105. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  106. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  107. /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 };
@@ -0,0 +1,46 @@
1
+ import { ColorType, EnumToken } from '../../../ast/types.js';
2
+ import '../../../ast/minify.js';
3
+ import { walkValues } from '../../../ast/walk.js';
4
+ import '../../../parser/parse.js';
5
+ import '../../../parser/tokenize.js';
6
+ import '../../../parser/utils/config.js';
7
+ import { COLORS_NAMES } from './constants.js';
8
+ import { expandHexValue } from '../hex.js';
9
+ import '../../../renderer/sourcemap/lib/encode.js';
10
+
11
+ function getComponents(token) {
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
+ // @ts-ignore
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 };
17
+ });
18
+ }
19
+ const result = [];
20
+ for (const child of (token.chi)) {
21
+ if ([
22
+ EnumToken.LiteralTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType
23
+ ].includes(child.typ)) {
24
+ continue;
25
+ }
26
+ if (child.typ == EnumToken.FunctionTokenType) {
27
+ if ('var' == child.val.toLowerCase()) {
28
+ return null;
29
+ }
30
+ else {
31
+ for (const { value } of walkValues(child.chi)) {
32
+ if (value.typ == EnumToken.FunctionTokenType && 'var' === value.val.toLowerCase()) {
33
+ return null;
34
+ }
35
+ }
36
+ }
37
+ }
38
+ if (child.typ == EnumToken.ColorTokenType && 'currentcolor' === child.val.toLowerCase()) {
39
+ return null;
40
+ }
41
+ result.push(child);
42
+ }
43
+ return result;
44
+ }
45
+
46
+ export { getComponents };
@@ -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,
@@ -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,40 @@
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
+ /**
13
+ * Calculate the distance between two okLab colors.
14
+ * @param okLab1
15
+ * @param okLab2
16
+ */
17
+ function okLabDistance(okLab1, okLab2) {
18
+ return Math.sqrt(Math.pow(okLab1[0] - okLab2[0], 2) + Math.pow(okLab1[1] - okLab2[1], 2) + Math.pow(okLab1[2] - okLab2[2], 2));
19
+ }
20
+ /**
21
+ * Check if two colors are close.
22
+ * @param color1
23
+ * @param color2
24
+ * @param threshold
25
+ */
26
+ function isOkLabClose(color1, color2, threshold = .01) {
27
+ color1 = convertColor(color1, ColorType.OKLAB);
28
+ color2 = convertColor(color2, ColorType.OKLAB);
29
+ if (color1 == null || color2 == null) {
30
+ return false;
31
+ }
32
+ const okLab1 = getOKLABComponents(color1);
33
+ const okLab2 = getOKLABComponents(color2);
34
+ if (okLab1 == null || okLab2 == null) {
35
+ return false;
36
+ }
37
+ return okLabDistance(okLab1, okLab2) < threshold;
38
+ }
39
+
40
+ 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 };