vscode-css-languageservice 6.3.10 → 7.0.0-next.1

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 (74) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/lib/esm/beautify/beautify-css.js +1437 -1606
  3. package/lib/esm/cssLanguageService.d.ts +2 -2
  4. package/lib/esm/cssLanguageService.js +18 -18
  5. package/lib/esm/languageFacts/colors.js +1 -1
  6. package/lib/esm/languageFacts/dataManager.js +3 -3
  7. package/lib/esm/languageFacts/entry.js +1 -1
  8. package/lib/esm/languageFacts/facts.js +3 -3
  9. package/lib/esm/parser/cssNodes.js +1 -1
  10. package/lib/esm/parser/cssParser.js +5 -5
  11. package/lib/esm/parser/cssSymbolScope.js +2 -2
  12. package/lib/esm/parser/lessParser.js +5 -5
  13. package/lib/esm/parser/lessScanner.js +1 -1
  14. package/lib/esm/parser/scssParser.js +6 -6
  15. package/lib/esm/parser/scssScanner.js +1 -1
  16. package/lib/esm/services/cssCodeActions.js +4 -4
  17. package/lib/esm/services/cssCompletion.js +7 -7
  18. package/lib/esm/services/cssFolding.js +3 -3
  19. package/lib/esm/services/cssFormatter.js +3 -3
  20. package/lib/esm/services/cssHover.js +6 -6
  21. package/lib/esm/services/cssNavigation.js +6 -6
  22. package/lib/esm/services/cssSelectionRange.js +2 -2
  23. package/lib/esm/services/cssValidation.js +4 -4
  24. package/lib/esm/services/lessCompletion.js +2 -2
  25. package/lib/esm/services/lint.js +5 -5
  26. package/lib/esm/services/lintRules.js +1 -1
  27. package/lib/esm/services/lintUtil.js +1 -1
  28. package/lib/esm/services/pathCompletion.js +3 -3
  29. package/lib/esm/services/scssCompletion.js +3 -3
  30. package/lib/esm/services/scssNavigation.js +4 -4
  31. package/lib/esm/services/selectorPrinting.js +3 -3
  32. package/package.json +15 -15
  33. package/lib/umd/beautify/beautify-css.js +0 -1695
  34. package/lib/umd/cssLanguageService.d.ts +0 -39
  35. package/lib/umd/cssLanguageService.js +0 -105
  36. package/lib/umd/cssLanguageTypes.d.ts +0 -267
  37. package/lib/umd/cssLanguageTypes.js +0 -89
  38. package/lib/umd/data/webCustomData.js +0 -44613
  39. package/lib/umd/languageFacts/builtinData.js +0 -155
  40. package/lib/umd/languageFacts/colors.js +0 -949
  41. package/lib/umd/languageFacts/dataManager.js +0 -101
  42. package/lib/umd/languageFacts/dataProvider.js +0 -86
  43. package/lib/umd/languageFacts/entry.js +0 -217
  44. package/lib/umd/languageFacts/facts.js +0 -33
  45. package/lib/umd/parser/cssErrors.js +0 -61
  46. package/lib/umd/parser/cssNodes.js +0 -1676
  47. package/lib/umd/parser/cssParser.js +0 -2035
  48. package/lib/umd/parser/cssScanner.js +0 -619
  49. package/lib/umd/parser/cssSymbolScope.js +0 -328
  50. package/lib/umd/parser/lessParser.js +0 -732
  51. package/lib/umd/parser/lessScanner.js +0 -70
  52. package/lib/umd/parser/scssErrors.js +0 -30
  53. package/lib/umd/parser/scssParser.js +0 -874
  54. package/lib/umd/parser/scssScanner.js +0 -108
  55. package/lib/umd/services/cssCodeActions.js +0 -89
  56. package/lib/umd/services/cssCompletion.js +0 -1109
  57. package/lib/umd/services/cssFolding.js +0 -202
  58. package/lib/umd/services/cssFormatter.js +0 -149
  59. package/lib/umd/services/cssHover.js +0 -174
  60. package/lib/umd/services/cssNavigation.js +0 -539
  61. package/lib/umd/services/cssSelectionRange.js +0 -59
  62. package/lib/umd/services/cssValidation.js +0 -54
  63. package/lib/umd/services/lessCompletion.js +0 -390
  64. package/lib/umd/services/lint.js +0 -577
  65. package/lib/umd/services/lintRules.js +0 -90
  66. package/lib/umd/services/lintUtil.js +0 -210
  67. package/lib/umd/services/pathCompletion.js +0 -171
  68. package/lib/umd/services/scssCompletion.js +0 -367
  69. package/lib/umd/services/scssNavigation.js +0 -169
  70. package/lib/umd/services/selectorPrinting.js +0 -575
  71. package/lib/umd/utils/arrays.js +0 -54
  72. package/lib/umd/utils/objects.js +0 -24
  73. package/lib/umd/utils/resources.js +0 -25
  74. package/lib/umd/utils/strings.js +0 -123
@@ -1,949 +0,0 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for license information.
4
- *--------------------------------------------------------------------------------------------*/
5
- (function (factory) {
6
- if (typeof module === "object" && typeof module.exports === "object") {
7
- var v = factory(require, exports);
8
- if (v !== undefined) module.exports = v;
9
- }
10
- else if (typeof define === "function" && define.amd) {
11
- define(["require", "exports", "../parser/cssNodes", "@vscode/l10n"], factory);
12
- }
13
- })(function (require, exports) {
14
- "use strict";
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.colorKeywords = exports.colors = exports.colorFunctions = void 0;
17
- exports.isColorConstructor = isColorConstructor;
18
- exports.isColorString = isColorString;
19
- exports.isColorValue = isColorValue;
20
- exports.hexDigit = hexDigit;
21
- exports.colorFromHex = colorFromHex;
22
- exports.colorFrom256RGB = colorFrom256RGB;
23
- exports.colorFromHSL = colorFromHSL;
24
- exports.hslFromColor = hslFromColor;
25
- exports.colorFromHWB = colorFromHWB;
26
- exports.hwbFromColor = hwbFromColor;
27
- exports.xyzFromLAB = xyzFromLAB;
28
- exports.xyzFromOKLAB = xyzFromOKLAB;
29
- exports.xyzToRGB = xyzToRGB;
30
- exports.RGBtoXYZ = RGBtoXYZ;
31
- exports.XYZtoLAB = XYZtoLAB;
32
- exports.XYZtoOKLAB = XYZtoOKLAB;
33
- exports.labFromColor = labFromColor;
34
- exports.oklabFromColor = oklabFromColor;
35
- exports.lchFromColor = lchFromColor;
36
- exports.oklchFromColor = oklchFromColor;
37
- exports.colorFromLAB = colorFromLAB;
38
- exports.colorFromOKLAB = colorFromOKLAB;
39
- exports.labFromLCH = labFromLCH;
40
- exports.colorFromLCH = colorFromLCH;
41
- exports.colorFromOKLCH = colorFromOKLCH;
42
- exports.getColorValue = getColorValue;
43
- const nodes = require("../parser/cssNodes");
44
- const l10n = require("@vscode/l10n");
45
- const hexColorRegExp = /(^#([0-9A-F]{3}){1,2}$)|(^#([0-9A-F]{4}){1,2}$)/i;
46
- exports.colorFunctions = [
47
- {
48
- label: 'rgb',
49
- func: 'rgb($red, $green, $blue)',
50
- insertText: 'rgb(${1:red}, ${2:green}, ${3:blue})',
51
- desc: l10n.t('Creates a Color from red, green, and blue values.')
52
- },
53
- {
54
- label: 'rgba',
55
- func: 'rgba($red, $green, $blue, $alpha)',
56
- insertText: 'rgba(${1:red}, ${2:green}, ${3:blue}, ${4:alpha})',
57
- desc: l10n.t('Creates a Color from red, green, blue, and alpha values.')
58
- },
59
- {
60
- label: 'rgb relative',
61
- func: 'rgb(from $color $red $green $blue)',
62
- insertText: 'rgb(from ${1:color} ${2:r} ${3:g} ${4:b})',
63
- desc: l10n.t('Creates a Color from the red, green, and blue values of another Color.')
64
- },
65
- {
66
- label: 'hsl',
67
- func: 'hsl($hue, $saturation, $lightness)',
68
- insertText: 'hsl(${1:hue}, ${2:saturation}, ${3:lightness})',
69
- desc: l10n.t('Creates a Color from hue, saturation, and lightness values.')
70
- },
71
- {
72
- label: 'hsla',
73
- func: 'hsla($hue, $saturation, $lightness, $alpha)',
74
- insertText: 'hsla(${1:hue}, ${2:saturation}, ${3:lightness}, ${4:alpha})',
75
- desc: l10n.t('Creates a Color from hue, saturation, lightness, and alpha values.')
76
- },
77
- {
78
- label: 'hsl relative',
79
- func: 'hsl(from $color $hue $saturation $lightness)',
80
- insertText: 'hsl(from ${1:color} ${2:h} ${3:s} ${4:l})',
81
- desc: l10n.t('Creates a Color from the hue, saturation, and lightness values of another Color.')
82
- },
83
- {
84
- label: 'hwb',
85
- func: 'hwb($hue $white $black)',
86
- insertText: 'hwb(${1:hue} ${2:white} ${3:black})',
87
- desc: l10n.t('Creates a Color from hue, white, and black values.')
88
- },
89
- {
90
- label: 'hwb relative',
91
- func: 'hwb(from $color $hue $white $black)',
92
- insertText: 'hwb(from ${1:color} ${2:h} ${3:w} ${4:b})',
93
- desc: l10n.t('Creates a Color from the hue, white, and black values of another Color.')
94
- },
95
- {
96
- label: 'lab',
97
- func: 'lab($lightness $a $b)',
98
- insertText: 'lab(${1:lightness} ${2:a} ${3:b})',
99
- desc: l10n.t('Creates a Color from lightness, a, and b values.')
100
- },
101
- {
102
- label: 'lab relative',
103
- func: 'lab(from $color $lightness $a $b)',
104
- insertText: 'lab(from ${1:color} ${2:l} ${3:a} ${4:b})',
105
- desc: l10n.t('Creates a Color from the lightness, a, and b values of another Color.')
106
- },
107
- {
108
- label: 'oklab',
109
- func: 'oklab($lightness $a $b)',
110
- insertText: 'oklab(${1:lightness} ${2:a} ${3:b})',
111
- desc: l10n.t('Creates a Color from lightness, a, and b values.')
112
- },
113
- {
114
- label: 'oklab relative',
115
- func: 'oklab(from $color $lightness $a $b)',
116
- insertText: 'oklab(from ${1:color} ${2:l} ${3:a} ${4:b})',
117
- desc: l10n.t('Creates a Color from the lightness, a, and b values of another Color.')
118
- },
119
- {
120
- label: 'lch',
121
- func: 'lch($lightness $chroma $hue)',
122
- insertText: 'lch(${1:lightness} ${2:chroma} ${3:hue})',
123
- desc: l10n.t('Creates a Color from lightness, chroma, and hue values.')
124
- },
125
- {
126
- label: 'lch relative',
127
- func: 'lch(from $color $lightness $chroma $hue)',
128
- insertText: 'lch(from ${1:color} ${2:l} ${3:c} ${4:h})',
129
- desc: l10n.t('Creates a Color from the lightness, chroma, and hue values of another Color.')
130
- },
131
- {
132
- label: 'oklch',
133
- func: 'oklch($lightness $chroma $hue)',
134
- insertText: 'oklch(${1:lightness} ${2:chroma} ${3:hue})',
135
- desc: l10n.t('Creates a Color from lightness, chroma, and hue values.')
136
- },
137
- {
138
- label: 'oklch relative',
139
- func: 'oklch(from $color $lightness $chroma $hue)',
140
- insertText: 'oklch(from ${1:color} ${2:l} ${3:c} ${4:h})',
141
- desc: l10n.t('Creates a Color from the lightness, chroma, and hue values of another Color.')
142
- },
143
- {
144
- label: 'color',
145
- func: 'color($color-space $red $green $blue)',
146
- insertText: 'color(${1|srgb,srgb-linear,display-p3,a98-rgb,prophoto-rgb,rec2020,xyx,xyz-d50,xyz-d65|} ${2:red} ${3:green} ${4:blue})',
147
- desc: l10n.t('Creates a Color in a specific color space from red, green, and blue values.')
148
- },
149
- {
150
- label: 'color relative',
151
- func: 'color(from $color $color-space $red $green $blue)',
152
- insertText: 'color(from ${1:color} ${2|srgb,srgb-linear,display-p3,a98-rgb,prophoto-rgb,rec2020,xyx,xyz-d50,xyz-d65|} ${3:r} ${4:g} ${5:b})',
153
- desc: l10n.t('Creates a Color in a specific color space from the red, green, and blue values of another Color.')
154
- },
155
- {
156
- label: 'color-mix',
157
- func: 'color-mix(in $color-space, $color $percentage, $color $percentage)',
158
- insertText: 'color-mix(in ${1|srgb,srgb-linear,lab,oklab,xyz,xyz-d50,xyz-d65|}, ${3:color} ${4:percentage}, ${5:color} ${6:percentage})',
159
- desc: l10n.t('Mix two colors together in a rectangular color space.')
160
- },
161
- {
162
- label: 'color-mix hue',
163
- func: 'color-mix(in $color-space $interpolation-method hue, $color $percentage, $color $percentage)',
164
- insertText: 'color-mix(in ${1|hsl,hwb,lch,oklch|} ${2|shorter hue,longer hue,increasing hue,decreasing hue|}, ${3:color} ${4:percentage}, ${5:color} ${6:percentage})',
165
- desc: l10n.t('Mix two colors together in a polar color space.')
166
- },
167
- {
168
- label: 'lab',
169
- func: 'lab($lightness $channel_a $channel_b $alpha)',
170
- insertText: 'lab(${1:lightness} ${2:a} ${3:b} ${4:alpha})',
171
- desc: l10n.t('css.builtin.lab', 'Creates a Color from Lightness, Channel a, Channel b and alpha values.')
172
- },
173
- {
174
- label: 'lab relative',
175
- func: 'lab(from $color $lightness $channel_a $channel_b $alpha)',
176
- insertText: 'lab(from ${1:color} ${2:lightness} ${3:channel_a} ${4:channel_b} ${5:alpha})',
177
- desc: l10n.t('css.builtin.lab', 'Creates a Color from Lightness, Channel a, Channel b and alpha values of another Color.')
178
- },
179
- {
180
- label: 'lch',
181
- func: 'lch($lightness $chrome $hue $alpha)',
182
- insertText: 'lch(${1:lightness} ${2:chrome} ${3:hue} ${4:alpha})',
183
- desc: l10n.t('css.builtin.lab', 'Creates a Color from Lightness, Chroma, Hue and alpha values.')
184
- },
185
- {
186
- label: 'lch relative',
187
- func: 'lch(from $color $lightness $chrome $hue $alpha)',
188
- insertText: 'lch(from ${1:color} ${2:lightness} ${3:chrome} ${4:hue} ${5:alpha})',
189
- desc: l10n.t('css.builtin.lab', 'Creates a Color from Lightness, Chroma, Hue and alpha values of another Color.')
190
- }
191
- ];
192
- const colorFunctionNameRegExp = /^(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)$/iu;
193
- exports.colors = {
194
- aliceblue: '#f0f8ff',
195
- antiquewhite: '#faebd7',
196
- aqua: '#00ffff',
197
- aquamarine: '#7fffd4',
198
- azure: '#f0ffff',
199
- beige: '#f5f5dc',
200
- bisque: '#ffe4c4',
201
- black: '#000000',
202
- blanchedalmond: '#ffebcd',
203
- blue: '#0000ff',
204
- blueviolet: '#8a2be2',
205
- brown: '#a52a2a',
206
- burlywood: '#deb887',
207
- cadetblue: '#5f9ea0',
208
- chartreuse: '#7fff00',
209
- chocolate: '#d2691e',
210
- coral: '#ff7f50',
211
- cornflowerblue: '#6495ed',
212
- cornsilk: '#fff8dc',
213
- crimson: '#dc143c',
214
- cyan: '#00ffff',
215
- darkblue: '#00008b',
216
- darkcyan: '#008b8b',
217
- darkgoldenrod: '#b8860b',
218
- darkgray: '#a9a9a9',
219
- darkgrey: '#a9a9a9',
220
- darkgreen: '#006400',
221
- darkkhaki: '#bdb76b',
222
- darkmagenta: '#8b008b',
223
- darkolivegreen: '#556b2f',
224
- darkorange: '#ff8c00',
225
- darkorchid: '#9932cc',
226
- darkred: '#8b0000',
227
- darksalmon: '#e9967a',
228
- darkseagreen: '#8fbc8f',
229
- darkslateblue: '#483d8b',
230
- darkslategray: '#2f4f4f',
231
- darkslategrey: '#2f4f4f',
232
- darkturquoise: '#00ced1',
233
- darkviolet: '#9400d3',
234
- deeppink: '#ff1493',
235
- deepskyblue: '#00bfff',
236
- dimgray: '#696969',
237
- dimgrey: '#696969',
238
- dodgerblue: '#1e90ff',
239
- firebrick: '#b22222',
240
- floralwhite: '#fffaf0',
241
- forestgreen: '#228b22',
242
- fuchsia: '#ff00ff',
243
- gainsboro: '#dcdcdc',
244
- ghostwhite: '#f8f8ff',
245
- gold: '#ffd700',
246
- goldenrod: '#daa520',
247
- gray: '#808080',
248
- grey: '#808080',
249
- green: '#008000',
250
- greenyellow: '#adff2f',
251
- honeydew: '#f0fff0',
252
- hotpink: '#ff69b4',
253
- indianred: '#cd5c5c',
254
- indigo: '#4b0082',
255
- ivory: '#fffff0',
256
- khaki: '#f0e68c',
257
- lavender: '#e6e6fa',
258
- lavenderblush: '#fff0f5',
259
- lawngreen: '#7cfc00',
260
- lemonchiffon: '#fffacd',
261
- lightblue: '#add8e6',
262
- lightcoral: '#f08080',
263
- lightcyan: '#e0ffff',
264
- lightgoldenrodyellow: '#fafad2',
265
- lightgray: '#d3d3d3',
266
- lightgrey: '#d3d3d3',
267
- lightgreen: '#90ee90',
268
- lightpink: '#ffb6c1',
269
- lightsalmon: '#ffa07a',
270
- lightseagreen: '#20b2aa',
271
- lightskyblue: '#87cefa',
272
- lightslategray: '#778899',
273
- lightslategrey: '#778899',
274
- lightsteelblue: '#b0c4de',
275
- lightyellow: '#ffffe0',
276
- lime: '#00ff00',
277
- limegreen: '#32cd32',
278
- linen: '#faf0e6',
279
- magenta: '#ff00ff',
280
- maroon: '#800000',
281
- mediumaquamarine: '#66cdaa',
282
- mediumblue: '#0000cd',
283
- mediumorchid: '#ba55d3',
284
- mediumpurple: '#9370d8',
285
- mediumseagreen: '#3cb371',
286
- mediumslateblue: '#7b68ee',
287
- mediumspringgreen: '#00fa9a',
288
- mediumturquoise: '#48d1cc',
289
- mediumvioletred: '#c71585',
290
- midnightblue: '#191970',
291
- mintcream: '#f5fffa',
292
- mistyrose: '#ffe4e1',
293
- moccasin: '#ffe4b5',
294
- navajowhite: '#ffdead',
295
- navy: '#000080',
296
- oldlace: '#fdf5e6',
297
- olive: '#808000',
298
- olivedrab: '#6b8e23',
299
- orange: '#ffa500',
300
- orangered: '#ff4500',
301
- orchid: '#da70d6',
302
- palegoldenrod: '#eee8aa',
303
- palegreen: '#98fb98',
304
- paleturquoise: '#afeeee',
305
- palevioletred: '#d87093',
306
- papayawhip: '#ffefd5',
307
- peachpuff: '#ffdab9',
308
- peru: '#cd853f',
309
- pink: '#ffc0cb',
310
- plum: '#dda0dd',
311
- powderblue: '#b0e0e6',
312
- purple: '#800080',
313
- red: '#ff0000',
314
- rebeccapurple: '#663399',
315
- rosybrown: '#bc8f8f',
316
- royalblue: '#4169e1',
317
- saddlebrown: '#8b4513',
318
- salmon: '#fa8072',
319
- sandybrown: '#f4a460',
320
- seagreen: '#2e8b57',
321
- seashell: '#fff5ee',
322
- sienna: '#a0522d',
323
- silver: '#c0c0c0',
324
- skyblue: '#87ceeb',
325
- slateblue: '#6a5acd',
326
- slategray: '#708090',
327
- slategrey: '#708090',
328
- snow: '#fffafa',
329
- springgreen: '#00ff7f',
330
- steelblue: '#4682b4',
331
- tan: '#d2b48c',
332
- teal: '#008080',
333
- thistle: '#d8bfd8',
334
- tomato: '#ff6347',
335
- turquoise: '#40e0d0',
336
- violet: '#ee82ee',
337
- wheat: '#f5deb3',
338
- white: '#ffffff',
339
- whitesmoke: '#f5f5f5',
340
- yellow: '#ffff00',
341
- yellowgreen: '#9acd32'
342
- };
343
- const colorsRegExp = new RegExp(`^(${Object.keys(exports.colors).join('|')})$`, "i");
344
- exports.colorKeywords = {
345
- 'currentColor': 'The value of the \'color\' property. The computed value of the \'currentColor\' keyword is the computed value of the \'color\' property. If the \'currentColor\' keyword is set on the \'color\' property itself, it is treated as \'color:inherit\' at parse time.',
346
- 'transparent': 'Fully transparent. This keyword can be considered a shorthand for rgba(0,0,0,0) which is its computed value.',
347
- };
348
- const colorKeywordsRegExp = new RegExp(`^(${Object.keys(exports.colorKeywords).join('|')})$`, "i");
349
- function getNumericValue(node, factor, lowerLimit = 0, upperLimit = 1) {
350
- const val = node.getText();
351
- const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(%?)$/);
352
- if (m) {
353
- if (m[2]) {
354
- factor = 100.0;
355
- }
356
- const result = parseFloat(m[1]) / factor;
357
- if (result >= lowerLimit && result <= upperLimit) {
358
- return result;
359
- }
360
- }
361
- throw new Error();
362
- }
363
- const DEGREES_PER_CIRCLE = 360; // Number of degrees in a full circle
364
- const GRAD_TO_DEGREE_FACTOR = 0.9; // Conversion factor: grads to degrees
365
- const RADIANS_TO_DEGREES_FACTOR = DEGREES_PER_CIRCLE / 2 / Math.PI; // Conversion factor: radians to degrees
366
- function getAngle(node) {
367
- const textValue = node.getText();
368
- // Hue angle keyword `none` is the equivilient of `0deg`
369
- if (textValue === 'none') {
370
- return 0;
371
- }
372
- const m = /^(?<numberString>[-+]?[0-9]*\.?[0-9]+)(?<unit>deg|rad|grad|turn)?$/iu.exec(textValue);
373
- if (m?.groups?.['numberString']) {
374
- const value = Number.parseFloat(m.groups['numberString']);
375
- if (!Number.isNaN(value)) {
376
- switch (m.groups['unit']) {
377
- case 'deg': {
378
- return value % DEGREES_PER_CIRCLE;
379
- }
380
- case 'grad': {
381
- return (value * GRAD_TO_DEGREE_FACTOR) % DEGREES_PER_CIRCLE;
382
- }
383
- case 'rad': {
384
- return (value * RADIANS_TO_DEGREES_FACTOR) % DEGREES_PER_CIRCLE;
385
- }
386
- case 'turn': {
387
- return (value * DEGREES_PER_CIRCLE) % DEGREES_PER_CIRCLE;
388
- }
389
- default: {
390
- // Unitless angles are treated as degrees
391
- return value % DEGREES_PER_CIRCLE;
392
- }
393
- }
394
- }
395
- }
396
- throw new Error(`Failed to parse '${textValue}' as angle`);
397
- }
398
- function isColorConstructor(node) {
399
- const name = node.getName();
400
- if (!name) {
401
- return false;
402
- }
403
- return colorFunctionNameRegExp.test(name);
404
- }
405
- function isColorString(s) {
406
- return hexColorRegExp.test(s) || colorsRegExp.test(s) || colorKeywordsRegExp.test(s);
407
- }
408
- /**
409
- * Returns true if the node is a color value - either
410
- * defined a hex number, as rgb or rgba function, or
411
- * as color name.
412
- */
413
- function isColorValue(node) {
414
- if (node.type === nodes.NodeType.HexColorValue) {
415
- return true;
416
- }
417
- else if (node.type === nodes.NodeType.Function) {
418
- return isColorConstructor(node);
419
- }
420
- else if (node.type === nodes.NodeType.Identifier) {
421
- if (node.parent && node.parent.type !== nodes.NodeType.Term) {
422
- return false;
423
- }
424
- const candidateColor = node.getText().toLowerCase();
425
- if (candidateColor === 'none') {
426
- return false;
427
- }
428
- if (exports.colors[candidateColor]) {
429
- return true;
430
- }
431
- }
432
- return false;
433
- }
434
- const Digit0 = 48;
435
- const Digit9 = 57;
436
- const A = 65;
437
- const F = 70;
438
- const a = 97;
439
- const f = 102;
440
- function hexDigit(charCode) {
441
- if (charCode < Digit0) {
442
- return 0;
443
- }
444
- if (charCode <= Digit9) {
445
- return charCode - Digit0;
446
- }
447
- if (charCode < a) {
448
- charCode += (a - A);
449
- }
450
- if (charCode >= a && charCode <= f) {
451
- return charCode - a + 10;
452
- }
453
- return 0;
454
- }
455
- function colorFromHex(text) {
456
- if (text[0] !== '#') {
457
- return null;
458
- }
459
- switch (text.length) {
460
- case 4:
461
- return {
462
- red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
463
- green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
464
- blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
465
- alpha: 1
466
- };
467
- case 5:
468
- return {
469
- red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
470
- green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
471
- blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
472
- alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
473
- };
474
- case 7:
475
- return {
476
- red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
477
- green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
478
- blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
479
- alpha: 1
480
- };
481
- case 9:
482
- return {
483
- red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
484
- green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
485
- blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
486
- alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
487
- };
488
- }
489
- return null;
490
- }
491
- function colorFrom256RGB(red, green, blue, alpha = 1.0) {
492
- return {
493
- red: red / 255.0,
494
- green: green / 255.0,
495
- blue: blue / 255.0,
496
- alpha
497
- };
498
- }
499
- function colorFromHSL(hue, sat, light, alpha = 1.0) {
500
- hue = hue / 60.0;
501
- if (sat === 0) {
502
- return { red: light, green: light, blue: light, alpha };
503
- }
504
- else {
505
- const hueToRgb = (t1, t2, hue) => {
506
- while (hue < 0) {
507
- hue += 6;
508
- }
509
- while (hue >= 6) {
510
- hue -= 6;
511
- }
512
- if (hue < 1) {
513
- return (t2 - t1) * hue + t1;
514
- }
515
- if (hue < 3) {
516
- return t2;
517
- }
518
- if (hue < 4) {
519
- return (t2 - t1) * (4 - hue) + t1;
520
- }
521
- return t1;
522
- };
523
- const t2 = light <= 0.5 ? (light * (sat + 1)) : (light + sat - (light * sat));
524
- const t1 = light * 2 - t2;
525
- return { red: hueToRgb(t1, t2, hue + 2), green: hueToRgb(t1, t2, hue), blue: hueToRgb(t1, t2, hue - 2), alpha };
526
- }
527
- }
528
- function hslFromColor(rgba) {
529
- const r = rgba.red;
530
- const g = rgba.green;
531
- const b = rgba.blue;
532
- const a = rgba.alpha;
533
- const max = Math.max(r, g, b);
534
- const min = Math.min(r, g, b);
535
- let h = 0;
536
- let s = 0;
537
- const l = (min + max) / 2;
538
- const chroma = max - min;
539
- if (chroma > 0) {
540
- s = Math.min((l <= 0.5 ? chroma / (2 * l) : chroma / (2 - (2 * l))), 1);
541
- switch (max) {
542
- case r:
543
- h = (g - b) / chroma + (g < b ? 6 : 0);
544
- break;
545
- case g:
546
- h = (b - r) / chroma + 2;
547
- break;
548
- case b:
549
- h = (r - g) / chroma + 4;
550
- break;
551
- }
552
- h *= 60;
553
- h = Math.round(h);
554
- }
555
- return { h, s, l, a };
556
- }
557
- function colorFromHWB(hue, white, black, alpha = 1.0) {
558
- if (white + black >= 1) {
559
- const gray = white / (white + black);
560
- return { red: gray, green: gray, blue: gray, alpha };
561
- }
562
- const rgb = colorFromHSL(hue, 1, 0.5, alpha);
563
- let red = rgb.red;
564
- red *= (1 - white - black);
565
- red += white;
566
- let green = rgb.green;
567
- green *= (1 - white - black);
568
- green += white;
569
- let blue = rgb.blue;
570
- blue *= (1 - white - black);
571
- blue += white;
572
- return {
573
- red: red,
574
- green: green,
575
- blue: blue,
576
- alpha
577
- };
578
- }
579
- function hwbFromColor(rgba) {
580
- const hsl = hslFromColor(rgba);
581
- const white = Math.min(rgba.red, rgba.green, rgba.blue);
582
- const black = 1 - Math.max(rgba.red, rgba.green, rgba.blue);
583
- return {
584
- h: hsl.h,
585
- w: white,
586
- b: black,
587
- a: hsl.a
588
- };
589
- }
590
- function xyzFromLAB(lab) {
591
- const xyz = {
592
- x: 0,
593
- y: 0,
594
- z: 0,
595
- alpha: lab.alpha ?? 1
596
- };
597
- xyz.y = (lab.l + 16.0) / 116.0;
598
- xyz.x = (lab.a / 500.0) + xyz.y;
599
- xyz.z = xyz.y - (lab.b / 200.0);
600
- let key;
601
- for (key in xyz) {
602
- let pow = xyz[key] * xyz[key] * xyz[key];
603
- if (pow > 0.008856) {
604
- xyz[key] = pow;
605
- }
606
- else {
607
- xyz[key] = (xyz[key] - 16.0 / 116.0) / 7.787;
608
- }
609
- }
610
- xyz.x = xyz.x * 95.047;
611
- xyz.y = xyz.y * 100.0;
612
- xyz.z = xyz.z * 108.883;
613
- return xyz;
614
- }
615
- function xyzFromOKLAB(lab) {
616
- // Convert from OKLab to XYZ
617
- // References: https://bottosson.github.io/posts/oklab/
618
- // lab.l is in 0-1 range
619
- // lab.a and lab.b are in -0.4 to 0.4 range
620
- const l = lab.l + 0.3963377774 * lab.a + 0.2158037573 * lab.b;
621
- const m = lab.l - 0.1055613458 * lab.a - 0.0638541728 * lab.b;
622
- const s = lab.l - 0.0894841775 * lab.a - 1.291485548 * lab.b;
623
- // Apply non-linearity using exponentiation
624
- const l3 = l ** 3;
625
- const m3 = m ** 3;
626
- const s3 = s ** 3;
627
- // Convert to XYZ
628
- const x = 1.2270138511 * l3 - 0.5577999807 * m3 + 0.281256149 * s3;
629
- const y = -0.0405801784 * l3 + 1.1122568696 * m3 - 0.0716766787 * s3;
630
- const z = -0.0763812845 * l3 - 0.4214819784 * m3 + 1.5861632204 * s3;
631
- return {
632
- x: x * 100,
633
- y: y * 100,
634
- z: z * 100,
635
- alpha: lab.alpha ?? 1,
636
- };
637
- }
638
- function xyzToRGB(xyz) {
639
- const x = xyz.x / 100;
640
- const y = xyz.y / 100;
641
- const z = xyz.z / 100;
642
- const r = 3.2406254773200533 * x - 1.5372079722103187 * y - 0.4986285986982479 * z;
643
- const g = -0.9689307147293197 * x + 1.8757560608852415 * y + 0.041517523842953964 * z;
644
- const b = 0.055710120445510616 * x + -0.2040210505984867 * y + 1.0569959422543882 * z;
645
- const compand = (c) => {
646
- return c <= 0.0031308 ?
647
- 12.92 * c :
648
- Math.min(1.055 * Math.pow(c, 1 / 2.4) - 0.055, 1);
649
- };
650
- return {
651
- red: Math.round(compand(r) * 255.0),
652
- blue: Math.round(compand(b) * 255.0),
653
- green: Math.round(compand(g) * 255.0),
654
- alpha: xyz.alpha
655
- };
656
- }
657
- function RGBtoXYZ(rgba) {
658
- let r = rgba.red, g = rgba.green, b = rgba.blue;
659
- if (r > 0.04045) {
660
- r = Math.pow((r + 0.055) / 1.055, 2.4);
661
- }
662
- else {
663
- r = r / 12.92;
664
- }
665
- if (g > 0.04045) {
666
- g = Math.pow((g + 0.055) / 1.055, 2.4);
667
- }
668
- else {
669
- g = g / 12.92;
670
- }
671
- if (b > 0.04045) {
672
- b = Math.pow((b + 0.055) / 1.055, 2.4);
673
- }
674
- else {
675
- b = b / 12.92;
676
- }
677
- r = r * 100;
678
- g = g * 100;
679
- b = b * 100;
680
- //Observer = 2°, Illuminant = D65
681
- const x = r * 0.4124 + g * 0.3576 + b * 0.1805;
682
- const y = r * 0.2126 + g * 0.7152 + b * 0.0722;
683
- const z = r * 0.0193 + g * 0.1192 + b * 0.9505;
684
- return { x, y, z, alpha: rgba.alpha };
685
- }
686
- function XYZtoLAB(xyz, round = true) {
687
- const ref_X = 95.047, ref_Y = 100.000, ref_Z = 108.883;
688
- let x = xyz.x / ref_X, y = xyz.y / ref_Y, z = xyz.z / ref_Z;
689
- if (x > 0.008856) {
690
- x = Math.pow(x, 1 / 3);
691
- }
692
- else {
693
- x = (7.787 * x) + (16 / 116);
694
- }
695
- if (y > 0.008856) {
696
- y = Math.pow(y, 1 / 3);
697
- }
698
- else {
699
- y = (7.787 * y) + (16 / 116);
700
- }
701
- if (z > 0.008856) {
702
- z = Math.pow(z, 1 / 3);
703
- }
704
- else {
705
- z = (7.787 * z) + (16 / 116);
706
- }
707
- const l = (116 * y) - 16, a = 500 * (x - y), b = 200 * (y - z);
708
- if (round) {
709
- return {
710
- l: Math.round((l + Number.EPSILON) * 100) / 100,
711
- a: Math.round((a + Number.EPSILON) * 100) / 100,
712
- b: Math.round((b + Number.EPSILON) * 100) / 100,
713
- alpha: xyz.alpha
714
- };
715
- }
716
- else {
717
- return {
718
- l, a, b,
719
- alpha: xyz.alpha
720
- };
721
- }
722
- }
723
- function XYZtoOKLAB(xyz, round = true) {
724
- // Convert XYZ to OKLab
725
- // References: https://bottosson.github.io/posts/oklab/
726
- // Normalize XYZ values
727
- const x = xyz.x / 100;
728
- const y = xyz.y / 100;
729
- const z = xyz.z / 100;
730
- // Convert to LMS
731
- const l = 0.8189330101 * x + 0.3618667424 * y - 0.1288597137 * z;
732
- const m = 0.0329845436 * x + 0.9293118715 * y + 0.0361456387 * z;
733
- const s = 0.0482003018 * x + 0.2643662691 * y + 0.633851707 * z;
734
- // Apply non-linearity
735
- const l_ = Math.cbrt(l);
736
- const m_ = Math.cbrt(m);
737
- const s_ = Math.cbrt(s);
738
- // Convert to OKLab
739
- const L = 0.2104542553 * l_ + 0.793617785 * m_ - 0.0040720468 * s_;
740
- const a = 1.9779984951 * l_ - 2.428592205 * m_ + 0.4505937099 * s_;
741
- const b = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.808675766 * s_;
742
- return round
743
- // 5 decimal places for precision
744
- ? {
745
- l: Number(L.toFixed(5)),
746
- a: Number(a.toFixed(5)),
747
- b: Number(b.toFixed(5)),
748
- alpha: xyz.alpha,
749
- }
750
- : {
751
- l: L,
752
- a,
753
- b,
754
- alpha: xyz.alpha,
755
- };
756
- }
757
- function labFromColor(rgba, round = true) {
758
- const xyz = RGBtoXYZ(rgba);
759
- const lab = XYZtoLAB(xyz, round);
760
- return lab;
761
- }
762
- function oklabFromColor(rgba, round = true) {
763
- const xyz = RGBtoXYZ(rgba);
764
- const lab = XYZtoOKLAB(xyz, round);
765
- // Convert lightness to a percentage of oklab
766
- return { ...lab, l: lab.l * 100 };
767
- }
768
- /**
769
- * Calculate chroma and hue from Lab values
770
- * Returns LCH values without formatting/rounding
771
- */
772
- function labToLCH(lab) {
773
- const c = Math.sqrt(Math.pow(lab.a, 2) + Math.pow(lab.b, 2));
774
- let h = Math.atan2(lab.b, lab.a) * RADIANS_TO_DEGREES_FACTOR;
775
- while (h < 0) {
776
- h = h + 360;
777
- }
778
- return {
779
- l: lab.l,
780
- c: c,
781
- h: h,
782
- alpha: lab.alpha,
783
- };
784
- }
785
- function lchFromColor(rgba) {
786
- const lab = labFromColor(rgba, false);
787
- const lch = labToLCH(lab);
788
- return {
789
- l: Math.round((lch.l + Number.EPSILON) * 100) / 100,
790
- c: Math.round((lch.c + Number.EPSILON) * 100) / 100,
791
- h: Math.round((lch.h + Number.EPSILON) * 100) / 100,
792
- alpha: lch.alpha,
793
- };
794
- }
795
- function oklchFromColor(rgba) {
796
- const lab = oklabFromColor(rgba, false);
797
- const lch = labToLCH(lab);
798
- return {
799
- l: Number((lch.l).toFixed(3)),
800
- c: Number(lch.c.toFixed(5)),
801
- h: Number(lch.h.toFixed(3)),
802
- alpha: lch.alpha,
803
- };
804
- }
805
- /**
806
- * Generic function to convert LAB/OKLAB to Color
807
- */
808
- function labToColor(lab, xyzConverter) {
809
- const xyz = xyzConverter(lab);
810
- const rgb = xyzToRGB(xyz);
811
- return {
812
- red: (rgb.red >= 0 ? Math.min(rgb.red, 255) : 0) / 255,
813
- green: (rgb.green >= 0 ? Math.min(rgb.green, 255) : 0) / 255,
814
- blue: (rgb.blue >= 0 ? Math.min(rgb.blue, 255) : 0) / 255,
815
- alpha: lab.alpha ?? 1,
816
- };
817
- }
818
- function colorFromLAB(l, a, b, alpha = 1) {
819
- return labToColor({ l, a, b, alpha }, xyzFromLAB);
820
- }
821
- function colorFromOKLAB(l, a, b, alpha = 1) {
822
- return labToColor({ l, a, b, alpha }, xyzFromOKLAB);
823
- }
824
- const DEGREES_TO_RADIANS_FACTOR = Math.PI / 180;
825
- function labFromLCH(l, c, h, alpha = 1) {
826
- return {
827
- l: l,
828
- a: c * Math.cos(h * DEGREES_TO_RADIANS_FACTOR),
829
- b: c * Math.sin(h * DEGREES_TO_RADIANS_FACTOR),
830
- alpha: alpha,
831
- };
832
- }
833
- function colorFromLCH(l, c, h, alpha = 1) {
834
- const lab = labFromLCH(l, c, h, alpha);
835
- return colorFromLAB(lab.l, lab.a, lab.b, alpha);
836
- }
837
- function colorFromOKLCH(l, c, h, alpha = 1) {
838
- const lab = labFromLCH(l, c, h, alpha); // Conversion is the same as LCH->LAB for OKLCH-OKLAB
839
- return colorFromOKLAB(lab.l, lab.a, lab.b, alpha);
840
- }
841
- function getColorValue(node) {
842
- if (node.type === nodes.NodeType.HexColorValue) {
843
- const text = node.getText();
844
- return colorFromHex(text);
845
- }
846
- else if (node.type === nodes.NodeType.Function) {
847
- const functionNode = node;
848
- const name = functionNode.getName();
849
- let colorValues = functionNode.getArguments().getChildren();
850
- if (colorValues.length === 1) {
851
- const functionArg = colorValues[0].getChildren();
852
- if (functionArg.length === 1 && functionArg[0].type === nodes.NodeType.Expression) {
853
- colorValues = functionArg[0].getChildren();
854
- if (colorValues.length === 3) {
855
- const lastValue = colorValues[2];
856
- if (lastValue instanceof nodes.BinaryExpression) {
857
- const left = lastValue.getLeft(), right = lastValue.getRight(), operator = lastValue.getOperator();
858
- if (left && right && operator && operator.matches('/')) {
859
- colorValues = [colorValues[0], colorValues[1], left, right];
860
- }
861
- }
862
- }
863
- }
864
- }
865
- if (!name || colorValues.length < 3 || colorValues.length > 4) {
866
- return null;
867
- }
868
- try {
869
- const alpha = colorValues.length === 4 ? getNumericValue(colorValues[3], 1) : 1;
870
- switch (name) {
871
- case 'rgb':
872
- case 'rgba': {
873
- return {
874
- red: getNumericValue(colorValues[0], 255),
875
- green: getNumericValue(colorValues[1], 255),
876
- blue: getNumericValue(colorValues[2], 255),
877
- alpha,
878
- };
879
- }
880
- case 'hsl':
881
- case 'hsla': {
882
- const h = getAngle(colorValues[0]);
883
- const s = getNumericValue(colorValues[1], 100);
884
- const l = getNumericValue(colorValues[2], 100);
885
- return colorFromHSL(h, s, l, alpha);
886
- }
887
- case 'hwb': {
888
- const h = getAngle(colorValues[0]);
889
- const w = getNumericValue(colorValues[1], 100);
890
- const b = getNumericValue(colorValues[2], 100);
891
- return colorFromHWB(h, w, b, alpha);
892
- }
893
- case 'lab': {
894
- // Reference: https://mina86.com/2021/srgb-lab-lchab-conversions/
895
- const l = getNumericValue(colorValues[0], 100);
896
- // Since these two values can be negative, a lower limit of -1 has been added
897
- const a = getNumericValue(colorValues[1], 125, -1);
898
- const b = getNumericValue(colorValues[2], 125, -1);
899
- return colorFromLAB(l * 100, a * 125, b * 125, alpha);
900
- }
901
- case 'lch': {
902
- const l = getNumericValue(colorValues[0], 100);
903
- const c = getNumericValue(colorValues[1], 230);
904
- const h = getAngle(colorValues[2]);
905
- return colorFromLCH(l * 100, c * 230, h, alpha);
906
- }
907
- case 'oklab': {
908
- const l = getNumericValue(colorValues[0], 1);
909
- // Since these two values can be negative, a lower limit of -1 has been added
910
- const a = getNumericValue(colorValues[1], 0.4, -1);
911
- const b = getNumericValue(colorValues[2], 0.4, -1);
912
- return colorFromOKLAB(l, a * 0.4, b * 0.4, alpha);
913
- }
914
- case 'oklch': {
915
- const l = getNumericValue(colorValues[0], 1);
916
- const c = getNumericValue(colorValues[1], 0.4);
917
- const h = getAngle(colorValues[2]);
918
- return colorFromOKLCH(l, c * 0.4, h, alpha);
919
- }
920
- }
921
- }
922
- catch {
923
- // parse error on numeric value
924
- return null;
925
- }
926
- }
927
- else if (node.type === nodes.NodeType.Identifier) {
928
- if (node.parent && node.parent.type !== nodes.NodeType.Term) {
929
- return null;
930
- }
931
- const term = node.parent;
932
- if (term && term.parent && term.parent.type === nodes.NodeType.BinaryExpression) {
933
- const expression = term.parent;
934
- if (expression.parent && expression.parent.type === nodes.NodeType.ListEntry && expression.parent.key === expression) {
935
- return null;
936
- }
937
- }
938
- const candidateColor = node.getText().toLowerCase();
939
- if (candidateColor === 'none') {
940
- return null;
941
- }
942
- const colorHex = exports.colors[candidateColor];
943
- if (colorHex) {
944
- return colorFromHex(colorHex);
945
- }
946
- }
947
- return null;
948
- }
949
- });