vscode-css-languageservice 6.0.1 → 6.1.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.
- package/CHANGELOG.md +3 -1
- package/SECURITY.md +41 -0
- package/lib/esm/beautify/beautify-css.js +11 -4
- package/lib/esm/cssLanguageService.d.ts +38 -37
- package/lib/esm/cssLanguageService.js +73 -72
- package/lib/esm/cssLanguageTypes.d.ts +238 -238
- package/lib/esm/cssLanguageTypes.js +42 -42
- package/lib/esm/data/webCustomData.js +22089 -21959
- package/lib/esm/languageFacts/builtinData.js +142 -142
- package/lib/esm/languageFacts/colors.js +469 -469
- package/lib/esm/languageFacts/dataManager.js +88 -88
- package/lib/esm/languageFacts/dataProvider.js +73 -73
- package/lib/esm/languageFacts/entry.js +137 -137
- package/lib/esm/languageFacts/facts.js +8 -8
- package/lib/esm/parser/cssErrors.js +48 -48
- package/lib/esm/parser/cssNodes.js +1511 -1502
- package/lib/esm/parser/cssParser.js +1606 -1534
- package/lib/esm/parser/cssScanner.js +592 -592
- package/lib/esm/parser/cssSymbolScope.js +311 -311
- package/lib/esm/parser/lessParser.js +715 -714
- package/lib/esm/parser/lessScanner.js +57 -57
- package/lib/esm/parser/scssErrors.js +18 -18
- package/lib/esm/parser/scssParser.js +806 -796
- package/lib/esm/parser/scssScanner.js +95 -95
- package/lib/esm/services/cssCodeActions.js +77 -77
- package/lib/esm/services/cssCompletion.js +1054 -1054
- package/lib/esm/services/cssFolding.js +190 -190
- package/lib/esm/services/cssFormatter.js +136 -136
- package/lib/esm/services/cssHover.js +148 -148
- package/lib/esm/services/cssNavigation.js +441 -378
- package/lib/esm/services/cssSelectionRange.js +47 -47
- package/lib/esm/services/cssValidation.js +41 -41
- package/lib/esm/services/lessCompletion.js +378 -378
- package/lib/esm/services/lint.js +518 -518
- package/lib/esm/services/lintRules.js +76 -76
- package/lib/esm/services/lintUtil.js +196 -196
- package/lib/esm/services/pathCompletion.js +157 -157
- package/lib/esm/services/scssCompletion.js +354 -354
- package/lib/esm/services/scssNavigation.js +82 -82
- package/lib/esm/services/selectorPrinting.js +492 -492
- package/lib/esm/utils/arrays.js +40 -40
- package/lib/esm/utils/objects.js +11 -11
- package/lib/esm/utils/resources.js +11 -11
- package/lib/esm/utils/strings.js +102 -102
- package/lib/umd/beautify/beautify-css.js +11 -4
- package/lib/umd/cssLanguageService.d.ts +38 -37
- package/lib/umd/cssLanguageService.js +104 -99
- package/lib/umd/cssLanguageTypes.d.ts +238 -238
- package/lib/umd/cssLanguageTypes.js +89 -89
- package/lib/umd/data/webCustomData.js +22102 -21972
- package/lib/umd/languageFacts/builtinData.js +154 -154
- package/lib/umd/languageFacts/colors.js +492 -492
- package/lib/umd/languageFacts/dataManager.js +101 -101
- package/lib/umd/languageFacts/dataProvider.js +86 -86
- package/lib/umd/languageFacts/entry.js +152 -152
- package/lib/umd/languageFacts/facts.js +33 -29
- package/lib/umd/parser/cssErrors.js +61 -61
- package/lib/umd/parser/cssNodes.js +1597 -1587
- package/lib/umd/parser/cssParser.js +1619 -1547
- package/lib/umd/parser/cssScanner.js +606 -606
- package/lib/umd/parser/cssSymbolScope.js +328 -328
- package/lib/umd/parser/lessParser.js +728 -727
- package/lib/umd/parser/lessScanner.js +70 -70
- package/lib/umd/parser/scssErrors.js +31 -31
- package/lib/umd/parser/scssParser.js +819 -809
- package/lib/umd/parser/scssScanner.js +108 -108
- package/lib/umd/services/cssCodeActions.js +90 -90
- package/lib/umd/services/cssCompletion.js +1067 -1067
- package/lib/umd/services/cssFolding.js +203 -203
- package/lib/umd/services/cssFormatter.js +150 -150
- package/lib/umd/services/cssHover.js +161 -161
- package/lib/umd/services/cssNavigation.js +454 -391
- package/lib/umd/services/cssSelectionRange.js +60 -60
- package/lib/umd/services/cssValidation.js +54 -54
- package/lib/umd/services/lessCompletion.js +391 -391
- package/lib/umd/services/lint.js +531 -531
- package/lib/umd/services/lintRules.js +91 -91
- package/lib/umd/services/lintUtil.js +210 -210
- package/lib/umd/services/pathCompletion.js +171 -171
- package/lib/umd/services/scssCompletion.js +367 -367
- package/lib/umd/services/scssNavigation.js +95 -95
- package/lib/umd/services/selectorPrinting.js +510 -510
- package/lib/umd/utils/arrays.js +55 -55
- package/lib/umd/utils/objects.js +25 -25
- package/lib/umd/utils/resources.js +26 -26
- package/lib/umd/utils/strings.js +120 -120
- package/package.json +13 -12
|
@@ -1,469 +1,469 @@
|
|
|
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
|
-
import * as nodes from '../parser/cssNodes';
|
|
6
|
-
import * as nls from 'vscode-nls';
|
|
7
|
-
const localize = nls.loadMessageBundle();
|
|
8
|
-
export const colorFunctions = [
|
|
9
|
-
{ func: 'rgb($red, $green, $blue)', desc: localize('css.builtin.rgb', 'Creates a Color from red, green, and blue values.') },
|
|
10
|
-
{ func: 'rgba($red, $green, $blue, $alpha)', desc: localize('css.builtin.rgba', 'Creates a Color from red, green, blue, and alpha values.') },
|
|
11
|
-
{ func: 'hsl($hue, $saturation, $lightness)', desc: localize('css.builtin.hsl', 'Creates a Color from hue, saturation, and lightness values.') },
|
|
12
|
-
{ func: 'hsla($hue, $saturation, $lightness, $alpha)', desc: localize('css.builtin.hsla', 'Creates a Color from hue, saturation, lightness, and alpha values.') },
|
|
13
|
-
{ func: 'hwb($hue $white $black)', desc: localize('css.builtin.hwb', 'Creates a Color from hue, white and black.') }
|
|
14
|
-
];
|
|
15
|
-
export const colors = {
|
|
16
|
-
aliceblue: '#f0f8ff',
|
|
17
|
-
antiquewhite: '#faebd7',
|
|
18
|
-
aqua: '#00ffff',
|
|
19
|
-
aquamarine: '#7fffd4',
|
|
20
|
-
azure: '#f0ffff',
|
|
21
|
-
beige: '#f5f5dc',
|
|
22
|
-
bisque: '#ffe4c4',
|
|
23
|
-
black: '#000000',
|
|
24
|
-
blanchedalmond: '#ffebcd',
|
|
25
|
-
blue: '#0000ff',
|
|
26
|
-
blueviolet: '#8a2be2',
|
|
27
|
-
brown: '#a52a2a',
|
|
28
|
-
burlywood: '#deb887',
|
|
29
|
-
cadetblue: '#5f9ea0',
|
|
30
|
-
chartreuse: '#7fff00',
|
|
31
|
-
chocolate: '#d2691e',
|
|
32
|
-
coral: '#ff7f50',
|
|
33
|
-
cornflowerblue: '#6495ed',
|
|
34
|
-
cornsilk: '#fff8dc',
|
|
35
|
-
crimson: '#dc143c',
|
|
36
|
-
cyan: '#00ffff',
|
|
37
|
-
darkblue: '#00008b',
|
|
38
|
-
darkcyan: '#008b8b',
|
|
39
|
-
darkgoldenrod: '#b8860b',
|
|
40
|
-
darkgray: '#a9a9a9',
|
|
41
|
-
darkgrey: '#a9a9a9',
|
|
42
|
-
darkgreen: '#006400',
|
|
43
|
-
darkkhaki: '#bdb76b',
|
|
44
|
-
darkmagenta: '#8b008b',
|
|
45
|
-
darkolivegreen: '#556b2f',
|
|
46
|
-
darkorange: '#ff8c00',
|
|
47
|
-
darkorchid: '#9932cc',
|
|
48
|
-
darkred: '#8b0000',
|
|
49
|
-
darksalmon: '#e9967a',
|
|
50
|
-
darkseagreen: '#8fbc8f',
|
|
51
|
-
darkslateblue: '#483d8b',
|
|
52
|
-
darkslategray: '#2f4f4f',
|
|
53
|
-
darkslategrey: '#2f4f4f',
|
|
54
|
-
darkturquoise: '#00ced1',
|
|
55
|
-
darkviolet: '#9400d3',
|
|
56
|
-
deeppink: '#ff1493',
|
|
57
|
-
deepskyblue: '#00bfff',
|
|
58
|
-
dimgray: '#696969',
|
|
59
|
-
dimgrey: '#696969',
|
|
60
|
-
dodgerblue: '#1e90ff',
|
|
61
|
-
firebrick: '#b22222',
|
|
62
|
-
floralwhite: '#fffaf0',
|
|
63
|
-
forestgreen: '#228b22',
|
|
64
|
-
fuchsia: '#ff00ff',
|
|
65
|
-
gainsboro: '#dcdcdc',
|
|
66
|
-
ghostwhite: '#f8f8ff',
|
|
67
|
-
gold: '#ffd700',
|
|
68
|
-
goldenrod: '#daa520',
|
|
69
|
-
gray: '#808080',
|
|
70
|
-
grey: '#808080',
|
|
71
|
-
green: '#008000',
|
|
72
|
-
greenyellow: '#adff2f',
|
|
73
|
-
honeydew: '#f0fff0',
|
|
74
|
-
hotpink: '#ff69b4',
|
|
75
|
-
indianred: '#cd5c5c',
|
|
76
|
-
indigo: '#4b0082',
|
|
77
|
-
ivory: '#fffff0',
|
|
78
|
-
khaki: '#f0e68c',
|
|
79
|
-
lavender: '#e6e6fa',
|
|
80
|
-
lavenderblush: '#fff0f5',
|
|
81
|
-
lawngreen: '#7cfc00',
|
|
82
|
-
lemonchiffon: '#fffacd',
|
|
83
|
-
lightblue: '#add8e6',
|
|
84
|
-
lightcoral: '#f08080',
|
|
85
|
-
lightcyan: '#e0ffff',
|
|
86
|
-
lightgoldenrodyellow: '#fafad2',
|
|
87
|
-
lightgray: '#d3d3d3',
|
|
88
|
-
lightgrey: '#d3d3d3',
|
|
89
|
-
lightgreen: '#90ee90',
|
|
90
|
-
lightpink: '#ffb6c1',
|
|
91
|
-
lightsalmon: '#ffa07a',
|
|
92
|
-
lightseagreen: '#20b2aa',
|
|
93
|
-
lightskyblue: '#87cefa',
|
|
94
|
-
lightslategray: '#778899',
|
|
95
|
-
lightslategrey: '#778899',
|
|
96
|
-
lightsteelblue: '#b0c4de',
|
|
97
|
-
lightyellow: '#ffffe0',
|
|
98
|
-
lime: '#00ff00',
|
|
99
|
-
limegreen: '#32cd32',
|
|
100
|
-
linen: '#faf0e6',
|
|
101
|
-
magenta: '#ff00ff',
|
|
102
|
-
maroon: '#800000',
|
|
103
|
-
mediumaquamarine: '#66cdaa',
|
|
104
|
-
mediumblue: '#0000cd',
|
|
105
|
-
mediumorchid: '#ba55d3',
|
|
106
|
-
mediumpurple: '#9370d8',
|
|
107
|
-
mediumseagreen: '#3cb371',
|
|
108
|
-
mediumslateblue: '#7b68ee',
|
|
109
|
-
mediumspringgreen: '#00fa9a',
|
|
110
|
-
mediumturquoise: '#48d1cc',
|
|
111
|
-
mediumvioletred: '#c71585',
|
|
112
|
-
midnightblue: '#191970',
|
|
113
|
-
mintcream: '#f5fffa',
|
|
114
|
-
mistyrose: '#ffe4e1',
|
|
115
|
-
moccasin: '#ffe4b5',
|
|
116
|
-
navajowhite: '#ffdead',
|
|
117
|
-
navy: '#000080',
|
|
118
|
-
oldlace: '#fdf5e6',
|
|
119
|
-
olive: '#808000',
|
|
120
|
-
olivedrab: '#6b8e23',
|
|
121
|
-
orange: '#ffa500',
|
|
122
|
-
orangered: '#ff4500',
|
|
123
|
-
orchid: '#da70d6',
|
|
124
|
-
palegoldenrod: '#eee8aa',
|
|
125
|
-
palegreen: '#98fb98',
|
|
126
|
-
paleturquoise: '#afeeee',
|
|
127
|
-
palevioletred: '#d87093',
|
|
128
|
-
papayawhip: '#ffefd5',
|
|
129
|
-
peachpuff: '#ffdab9',
|
|
130
|
-
peru: '#cd853f',
|
|
131
|
-
pink: '#ffc0cb',
|
|
132
|
-
plum: '#dda0dd',
|
|
133
|
-
powderblue: '#b0e0e6',
|
|
134
|
-
purple: '#800080',
|
|
135
|
-
red: '#ff0000',
|
|
136
|
-
rebeccapurple: '#663399',
|
|
137
|
-
rosybrown: '#bc8f8f',
|
|
138
|
-
royalblue: '#4169e1',
|
|
139
|
-
saddlebrown: '#8b4513',
|
|
140
|
-
salmon: '#fa8072',
|
|
141
|
-
sandybrown: '#f4a460',
|
|
142
|
-
seagreen: '#2e8b57',
|
|
143
|
-
seashell: '#fff5ee',
|
|
144
|
-
sienna: '#a0522d',
|
|
145
|
-
silver: '#c0c0c0',
|
|
146
|
-
skyblue: '#87ceeb',
|
|
147
|
-
slateblue: '#6a5acd',
|
|
148
|
-
slategray: '#708090',
|
|
149
|
-
slategrey: '#708090',
|
|
150
|
-
snow: '#fffafa',
|
|
151
|
-
springgreen: '#00ff7f',
|
|
152
|
-
steelblue: '#4682b4',
|
|
153
|
-
tan: '#d2b48c',
|
|
154
|
-
teal: '#008080',
|
|
155
|
-
thistle: '#d8bfd8',
|
|
156
|
-
tomato: '#ff6347',
|
|
157
|
-
turquoise: '#40e0d0',
|
|
158
|
-
violet: '#ee82ee',
|
|
159
|
-
wheat: '#f5deb3',
|
|
160
|
-
white: '#ffffff',
|
|
161
|
-
whitesmoke: '#f5f5f5',
|
|
162
|
-
yellow: '#ffff00',
|
|
163
|
-
yellowgreen: '#9acd32'
|
|
164
|
-
};
|
|
165
|
-
export const colorKeywords = {
|
|
166
|
-
'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.',
|
|
167
|
-
'transparent': 'Fully transparent. This keyword can be considered a shorthand for rgba(0,0,0,0) which is its computed value.',
|
|
168
|
-
};
|
|
169
|
-
function getNumericValue(node, factor) {
|
|
170
|
-
const val = node.getText();
|
|
171
|
-
const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(%?)$/);
|
|
172
|
-
if (m) {
|
|
173
|
-
if (m[2]) {
|
|
174
|
-
factor = 100.0;
|
|
175
|
-
}
|
|
176
|
-
const result = parseFloat(m[1]) / factor;
|
|
177
|
-
if (result >= 0 && result <= 1) {
|
|
178
|
-
return result;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
throw new Error();
|
|
182
|
-
}
|
|
183
|
-
function getAngle(node) {
|
|
184
|
-
const val = node.getText();
|
|
185
|
-
const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(deg|rad|grad|turn)?$/);
|
|
186
|
-
if (m) {
|
|
187
|
-
switch (m[2]) {
|
|
188
|
-
case 'deg':
|
|
189
|
-
return parseFloat(val) % 360;
|
|
190
|
-
case 'rad':
|
|
191
|
-
return (parseFloat(val) * 180 / Math.PI) % 360;
|
|
192
|
-
case 'grad':
|
|
193
|
-
return (parseFloat(val) * 0.9) % 360;
|
|
194
|
-
case 'turn':
|
|
195
|
-
return (parseFloat(val) * 360) % 360;
|
|
196
|
-
default:
|
|
197
|
-
if ('undefined' === typeof m[2]) {
|
|
198
|
-
return parseFloat(val) % 360;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
throw new Error();
|
|
203
|
-
}
|
|
204
|
-
export function isColorConstructor(node) {
|
|
205
|
-
const name = node.getName();
|
|
206
|
-
if (!name) {
|
|
207
|
-
return false;
|
|
208
|
-
}
|
|
209
|
-
return /^(rgb|rgba|hsl|hsla|hwb)$/gi.test(name);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Returns true if the node is a color value - either
|
|
213
|
-
* defined a hex number, as rgb or rgba function, or
|
|
214
|
-
* as color name.
|
|
215
|
-
*/
|
|
216
|
-
export function isColorValue(node) {
|
|
217
|
-
if (node.type === nodes.NodeType.HexColorValue) {
|
|
218
|
-
return true;
|
|
219
|
-
}
|
|
220
|
-
else if (node.type === nodes.NodeType.Function) {
|
|
221
|
-
return isColorConstructor(node);
|
|
222
|
-
}
|
|
223
|
-
else if (node.type === nodes.NodeType.Identifier) {
|
|
224
|
-
if (node.parent && node.parent.type !== nodes.NodeType.Term) {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
const candidateColor = node.getText().toLowerCase();
|
|
228
|
-
if (candidateColor === 'none') {
|
|
229
|
-
return false;
|
|
230
|
-
}
|
|
231
|
-
if (colors[candidateColor]) {
|
|
232
|
-
return true;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return false;
|
|
236
|
-
}
|
|
237
|
-
const Digit0 = 48;
|
|
238
|
-
const Digit9 = 57;
|
|
239
|
-
const A = 65;
|
|
240
|
-
const F = 70;
|
|
241
|
-
const a = 97;
|
|
242
|
-
const f = 102;
|
|
243
|
-
export function hexDigit(charCode) {
|
|
244
|
-
if (charCode < Digit0) {
|
|
245
|
-
return 0;
|
|
246
|
-
}
|
|
247
|
-
if (charCode <= Digit9) {
|
|
248
|
-
return charCode - Digit0;
|
|
249
|
-
}
|
|
250
|
-
if (charCode < a) {
|
|
251
|
-
charCode += (a - A);
|
|
252
|
-
}
|
|
253
|
-
if (charCode >= a && charCode <= f) {
|
|
254
|
-
return charCode - a + 10;
|
|
255
|
-
}
|
|
256
|
-
return 0;
|
|
257
|
-
}
|
|
258
|
-
export function colorFromHex(text) {
|
|
259
|
-
if (text[0] !== '#') {
|
|
260
|
-
return null;
|
|
261
|
-
}
|
|
262
|
-
switch (text.length) {
|
|
263
|
-
case 4:
|
|
264
|
-
return {
|
|
265
|
-
red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
|
|
266
|
-
green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
|
|
267
|
-
blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
|
|
268
|
-
alpha: 1
|
|
269
|
-
};
|
|
270
|
-
case 5:
|
|
271
|
-
return {
|
|
272
|
-
red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
|
|
273
|
-
green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
|
|
274
|
-
blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
|
|
275
|
-
alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
|
|
276
|
-
};
|
|
277
|
-
case 7:
|
|
278
|
-
return {
|
|
279
|
-
red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
|
|
280
|
-
green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
|
|
281
|
-
blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
|
|
282
|
-
alpha: 1
|
|
283
|
-
};
|
|
284
|
-
case 9:
|
|
285
|
-
return {
|
|
286
|
-
red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
|
|
287
|
-
green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
|
|
288
|
-
blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
|
|
289
|
-
alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
return null;
|
|
293
|
-
}
|
|
294
|
-
export function colorFrom256RGB(red, green, blue, alpha = 1.0) {
|
|
295
|
-
return {
|
|
296
|
-
red: red / 255.0,
|
|
297
|
-
green: green / 255.0,
|
|
298
|
-
blue: blue / 255.0,
|
|
299
|
-
alpha
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
export function colorFromHSL(hue, sat, light, alpha = 1.0) {
|
|
303
|
-
hue = hue / 60.0;
|
|
304
|
-
if (sat === 0) {
|
|
305
|
-
return { red: light, green: light, blue: light, alpha };
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
const hueToRgb = (t1, t2, hue) => {
|
|
309
|
-
while (hue < 0) {
|
|
310
|
-
hue += 6;
|
|
311
|
-
}
|
|
312
|
-
while (hue >= 6) {
|
|
313
|
-
hue -= 6;
|
|
314
|
-
}
|
|
315
|
-
if (hue < 1) {
|
|
316
|
-
return (t2 - t1) * hue + t1;
|
|
317
|
-
}
|
|
318
|
-
if (hue < 3) {
|
|
319
|
-
return t2;
|
|
320
|
-
}
|
|
321
|
-
if (hue < 4) {
|
|
322
|
-
return (t2 - t1) * (4 - hue) + t1;
|
|
323
|
-
}
|
|
324
|
-
return t1;
|
|
325
|
-
};
|
|
326
|
-
const t2 = light <= 0.5 ? (light * (sat + 1)) : (light + sat - (light * sat));
|
|
327
|
-
const t1 = light * 2 - t2;
|
|
328
|
-
return { red: hueToRgb(t1, t2, hue + 2), green: hueToRgb(t1, t2, hue), blue: hueToRgb(t1, t2, hue - 2), alpha };
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
export function hslFromColor(rgba) {
|
|
332
|
-
const r = rgba.red;
|
|
333
|
-
const g = rgba.green;
|
|
334
|
-
const b = rgba.blue;
|
|
335
|
-
const a = rgba.alpha;
|
|
336
|
-
const max = Math.max(r, g, b);
|
|
337
|
-
const min = Math.min(r, g, b);
|
|
338
|
-
let h = 0;
|
|
339
|
-
let s = 0;
|
|
340
|
-
const l = (min + max) / 2;
|
|
341
|
-
const chroma = max - min;
|
|
342
|
-
if (chroma > 0) {
|
|
343
|
-
s = Math.min((l <= 0.5 ? chroma / (2 * l) : chroma / (2 - (2 * l))), 1);
|
|
344
|
-
switch (max) {
|
|
345
|
-
case r:
|
|
346
|
-
h = (g - b) / chroma + (g < b ? 6 : 0);
|
|
347
|
-
break;
|
|
348
|
-
case g:
|
|
349
|
-
h = (b - r) / chroma + 2;
|
|
350
|
-
break;
|
|
351
|
-
case b:
|
|
352
|
-
h = (r - g) / chroma + 4;
|
|
353
|
-
break;
|
|
354
|
-
}
|
|
355
|
-
h *= 60;
|
|
356
|
-
h = Math.round(h);
|
|
357
|
-
}
|
|
358
|
-
return { h, s, l, a };
|
|
359
|
-
}
|
|
360
|
-
export function colorFromHWB(hue, white, black, alpha = 1.0) {
|
|
361
|
-
if (white + black >= 1) {
|
|
362
|
-
const gray = white / (white + black);
|
|
363
|
-
return { red: gray, green: gray, blue: gray, alpha };
|
|
364
|
-
}
|
|
365
|
-
const rgb = colorFromHSL(hue, 1, 0.5, alpha);
|
|
366
|
-
let red = rgb.red;
|
|
367
|
-
red *= (1 - white - black);
|
|
368
|
-
red += white;
|
|
369
|
-
let green = rgb.green;
|
|
370
|
-
green *= (1 - white - black);
|
|
371
|
-
green += white;
|
|
372
|
-
let blue = rgb.blue;
|
|
373
|
-
blue *= (1 - white - black);
|
|
374
|
-
blue += white;
|
|
375
|
-
return {
|
|
376
|
-
red: red,
|
|
377
|
-
green: green,
|
|
378
|
-
blue: blue,
|
|
379
|
-
alpha
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
export function hwbFromColor(rgba) {
|
|
383
|
-
const hsl = hslFromColor(rgba);
|
|
384
|
-
const white = Math.min(rgba.red, rgba.green, rgba.blue);
|
|
385
|
-
const black = 1 - Math.max(rgba.red, rgba.green, rgba.blue);
|
|
386
|
-
return {
|
|
387
|
-
h: hsl.h,
|
|
388
|
-
w: white,
|
|
389
|
-
b: black,
|
|
390
|
-
a: hsl.a
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
export function getColorValue(node) {
|
|
394
|
-
if (node.type === nodes.NodeType.HexColorValue) {
|
|
395
|
-
const text = node.getText();
|
|
396
|
-
return colorFromHex(text);
|
|
397
|
-
}
|
|
398
|
-
else if (node.type === nodes.NodeType.Function) {
|
|
399
|
-
const functionNode = node;
|
|
400
|
-
const name = functionNode.getName();
|
|
401
|
-
let colorValues = functionNode.getArguments().getChildren();
|
|
402
|
-
if (colorValues.length === 1) {
|
|
403
|
-
const functionArg = colorValues[0].getChildren();
|
|
404
|
-
if (functionArg.length === 1 && functionArg[0].type === nodes.NodeType.Expression) {
|
|
405
|
-
colorValues = functionArg[0].getChildren();
|
|
406
|
-
if (colorValues.length === 3) {
|
|
407
|
-
const lastValue = colorValues[2];
|
|
408
|
-
if (lastValue instanceof nodes.BinaryExpression) {
|
|
409
|
-
const left = lastValue.getLeft(), right = lastValue.getRight(), operator = lastValue.getOperator();
|
|
410
|
-
if (left && right && operator && operator.matches('/')) {
|
|
411
|
-
colorValues = [colorValues[0], colorValues[1], left, right];
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
if (!name || colorValues.length < 3 || colorValues.length > 4) {
|
|
418
|
-
return null;
|
|
419
|
-
}
|
|
420
|
-
try {
|
|
421
|
-
const alpha = colorValues.length === 4 ? getNumericValue(colorValues[3], 1) : 1;
|
|
422
|
-
if (name === 'rgb' || name === 'rgba') {
|
|
423
|
-
return {
|
|
424
|
-
red: getNumericValue(colorValues[0], 255.0),
|
|
425
|
-
green: getNumericValue(colorValues[1], 255.0),
|
|
426
|
-
blue: getNumericValue(colorValues[2], 255.0),
|
|
427
|
-
alpha
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
else if (name === 'hsl' || name === 'hsla') {
|
|
431
|
-
const h = getAngle(colorValues[0]);
|
|
432
|
-
const s = getNumericValue(colorValues[1], 100.0);
|
|
433
|
-
const l = getNumericValue(colorValues[2], 100.0);
|
|
434
|
-
return colorFromHSL(h, s, l, alpha);
|
|
435
|
-
}
|
|
436
|
-
else if (name === 'hwb') {
|
|
437
|
-
const h = getAngle(colorValues[0]);
|
|
438
|
-
const w = getNumericValue(colorValues[1], 100.0);
|
|
439
|
-
const b = getNumericValue(colorValues[2], 100.0);
|
|
440
|
-
return colorFromHWB(h, w, b, alpha);
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
catch (e) {
|
|
444
|
-
// parse error on numeric value
|
|
445
|
-
return null;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
else if (node.type === nodes.NodeType.Identifier) {
|
|
449
|
-
if (node.parent && node.parent.type !== nodes.NodeType.Term) {
|
|
450
|
-
return null;
|
|
451
|
-
}
|
|
452
|
-
const term = node.parent;
|
|
453
|
-
if (term && term.parent && term.parent.type === nodes.NodeType.BinaryExpression) {
|
|
454
|
-
const expression = term.parent;
|
|
455
|
-
if (expression.parent && expression.parent.type === nodes.NodeType.ListEntry && expression.parent.key === expression) {
|
|
456
|
-
return null;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
const candidateColor = node.getText().toLowerCase();
|
|
460
|
-
if (candidateColor === 'none') {
|
|
461
|
-
return null;
|
|
462
|
-
}
|
|
463
|
-
const colorHex = colors[candidateColor];
|
|
464
|
-
if (colorHex) {
|
|
465
|
-
return colorFromHex(colorHex);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
return null;
|
|
469
|
-
}
|
|
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
|
+
import * as nodes from '../parser/cssNodes';
|
|
6
|
+
import * as nls from 'vscode-nls';
|
|
7
|
+
const localize = nls.loadMessageBundle();
|
|
8
|
+
export const colorFunctions = [
|
|
9
|
+
{ func: 'rgb($red, $green, $blue)', desc: localize('css.builtin.rgb', 'Creates a Color from red, green, and blue values.') },
|
|
10
|
+
{ func: 'rgba($red, $green, $blue, $alpha)', desc: localize('css.builtin.rgba', 'Creates a Color from red, green, blue, and alpha values.') },
|
|
11
|
+
{ func: 'hsl($hue, $saturation, $lightness)', desc: localize('css.builtin.hsl', 'Creates a Color from hue, saturation, and lightness values.') },
|
|
12
|
+
{ func: 'hsla($hue, $saturation, $lightness, $alpha)', desc: localize('css.builtin.hsla', 'Creates a Color from hue, saturation, lightness, and alpha values.') },
|
|
13
|
+
{ func: 'hwb($hue $white $black)', desc: localize('css.builtin.hwb', 'Creates a Color from hue, white and black.') }
|
|
14
|
+
];
|
|
15
|
+
export const colors = {
|
|
16
|
+
aliceblue: '#f0f8ff',
|
|
17
|
+
antiquewhite: '#faebd7',
|
|
18
|
+
aqua: '#00ffff',
|
|
19
|
+
aquamarine: '#7fffd4',
|
|
20
|
+
azure: '#f0ffff',
|
|
21
|
+
beige: '#f5f5dc',
|
|
22
|
+
bisque: '#ffe4c4',
|
|
23
|
+
black: '#000000',
|
|
24
|
+
blanchedalmond: '#ffebcd',
|
|
25
|
+
blue: '#0000ff',
|
|
26
|
+
blueviolet: '#8a2be2',
|
|
27
|
+
brown: '#a52a2a',
|
|
28
|
+
burlywood: '#deb887',
|
|
29
|
+
cadetblue: '#5f9ea0',
|
|
30
|
+
chartreuse: '#7fff00',
|
|
31
|
+
chocolate: '#d2691e',
|
|
32
|
+
coral: '#ff7f50',
|
|
33
|
+
cornflowerblue: '#6495ed',
|
|
34
|
+
cornsilk: '#fff8dc',
|
|
35
|
+
crimson: '#dc143c',
|
|
36
|
+
cyan: '#00ffff',
|
|
37
|
+
darkblue: '#00008b',
|
|
38
|
+
darkcyan: '#008b8b',
|
|
39
|
+
darkgoldenrod: '#b8860b',
|
|
40
|
+
darkgray: '#a9a9a9',
|
|
41
|
+
darkgrey: '#a9a9a9',
|
|
42
|
+
darkgreen: '#006400',
|
|
43
|
+
darkkhaki: '#bdb76b',
|
|
44
|
+
darkmagenta: '#8b008b',
|
|
45
|
+
darkolivegreen: '#556b2f',
|
|
46
|
+
darkorange: '#ff8c00',
|
|
47
|
+
darkorchid: '#9932cc',
|
|
48
|
+
darkred: '#8b0000',
|
|
49
|
+
darksalmon: '#e9967a',
|
|
50
|
+
darkseagreen: '#8fbc8f',
|
|
51
|
+
darkslateblue: '#483d8b',
|
|
52
|
+
darkslategray: '#2f4f4f',
|
|
53
|
+
darkslategrey: '#2f4f4f',
|
|
54
|
+
darkturquoise: '#00ced1',
|
|
55
|
+
darkviolet: '#9400d3',
|
|
56
|
+
deeppink: '#ff1493',
|
|
57
|
+
deepskyblue: '#00bfff',
|
|
58
|
+
dimgray: '#696969',
|
|
59
|
+
dimgrey: '#696969',
|
|
60
|
+
dodgerblue: '#1e90ff',
|
|
61
|
+
firebrick: '#b22222',
|
|
62
|
+
floralwhite: '#fffaf0',
|
|
63
|
+
forestgreen: '#228b22',
|
|
64
|
+
fuchsia: '#ff00ff',
|
|
65
|
+
gainsboro: '#dcdcdc',
|
|
66
|
+
ghostwhite: '#f8f8ff',
|
|
67
|
+
gold: '#ffd700',
|
|
68
|
+
goldenrod: '#daa520',
|
|
69
|
+
gray: '#808080',
|
|
70
|
+
grey: '#808080',
|
|
71
|
+
green: '#008000',
|
|
72
|
+
greenyellow: '#adff2f',
|
|
73
|
+
honeydew: '#f0fff0',
|
|
74
|
+
hotpink: '#ff69b4',
|
|
75
|
+
indianred: '#cd5c5c',
|
|
76
|
+
indigo: '#4b0082',
|
|
77
|
+
ivory: '#fffff0',
|
|
78
|
+
khaki: '#f0e68c',
|
|
79
|
+
lavender: '#e6e6fa',
|
|
80
|
+
lavenderblush: '#fff0f5',
|
|
81
|
+
lawngreen: '#7cfc00',
|
|
82
|
+
lemonchiffon: '#fffacd',
|
|
83
|
+
lightblue: '#add8e6',
|
|
84
|
+
lightcoral: '#f08080',
|
|
85
|
+
lightcyan: '#e0ffff',
|
|
86
|
+
lightgoldenrodyellow: '#fafad2',
|
|
87
|
+
lightgray: '#d3d3d3',
|
|
88
|
+
lightgrey: '#d3d3d3',
|
|
89
|
+
lightgreen: '#90ee90',
|
|
90
|
+
lightpink: '#ffb6c1',
|
|
91
|
+
lightsalmon: '#ffa07a',
|
|
92
|
+
lightseagreen: '#20b2aa',
|
|
93
|
+
lightskyblue: '#87cefa',
|
|
94
|
+
lightslategray: '#778899',
|
|
95
|
+
lightslategrey: '#778899',
|
|
96
|
+
lightsteelblue: '#b0c4de',
|
|
97
|
+
lightyellow: '#ffffe0',
|
|
98
|
+
lime: '#00ff00',
|
|
99
|
+
limegreen: '#32cd32',
|
|
100
|
+
linen: '#faf0e6',
|
|
101
|
+
magenta: '#ff00ff',
|
|
102
|
+
maroon: '#800000',
|
|
103
|
+
mediumaquamarine: '#66cdaa',
|
|
104
|
+
mediumblue: '#0000cd',
|
|
105
|
+
mediumorchid: '#ba55d3',
|
|
106
|
+
mediumpurple: '#9370d8',
|
|
107
|
+
mediumseagreen: '#3cb371',
|
|
108
|
+
mediumslateblue: '#7b68ee',
|
|
109
|
+
mediumspringgreen: '#00fa9a',
|
|
110
|
+
mediumturquoise: '#48d1cc',
|
|
111
|
+
mediumvioletred: '#c71585',
|
|
112
|
+
midnightblue: '#191970',
|
|
113
|
+
mintcream: '#f5fffa',
|
|
114
|
+
mistyrose: '#ffe4e1',
|
|
115
|
+
moccasin: '#ffe4b5',
|
|
116
|
+
navajowhite: '#ffdead',
|
|
117
|
+
navy: '#000080',
|
|
118
|
+
oldlace: '#fdf5e6',
|
|
119
|
+
olive: '#808000',
|
|
120
|
+
olivedrab: '#6b8e23',
|
|
121
|
+
orange: '#ffa500',
|
|
122
|
+
orangered: '#ff4500',
|
|
123
|
+
orchid: '#da70d6',
|
|
124
|
+
palegoldenrod: '#eee8aa',
|
|
125
|
+
palegreen: '#98fb98',
|
|
126
|
+
paleturquoise: '#afeeee',
|
|
127
|
+
palevioletred: '#d87093',
|
|
128
|
+
papayawhip: '#ffefd5',
|
|
129
|
+
peachpuff: '#ffdab9',
|
|
130
|
+
peru: '#cd853f',
|
|
131
|
+
pink: '#ffc0cb',
|
|
132
|
+
plum: '#dda0dd',
|
|
133
|
+
powderblue: '#b0e0e6',
|
|
134
|
+
purple: '#800080',
|
|
135
|
+
red: '#ff0000',
|
|
136
|
+
rebeccapurple: '#663399',
|
|
137
|
+
rosybrown: '#bc8f8f',
|
|
138
|
+
royalblue: '#4169e1',
|
|
139
|
+
saddlebrown: '#8b4513',
|
|
140
|
+
salmon: '#fa8072',
|
|
141
|
+
sandybrown: '#f4a460',
|
|
142
|
+
seagreen: '#2e8b57',
|
|
143
|
+
seashell: '#fff5ee',
|
|
144
|
+
sienna: '#a0522d',
|
|
145
|
+
silver: '#c0c0c0',
|
|
146
|
+
skyblue: '#87ceeb',
|
|
147
|
+
slateblue: '#6a5acd',
|
|
148
|
+
slategray: '#708090',
|
|
149
|
+
slategrey: '#708090',
|
|
150
|
+
snow: '#fffafa',
|
|
151
|
+
springgreen: '#00ff7f',
|
|
152
|
+
steelblue: '#4682b4',
|
|
153
|
+
tan: '#d2b48c',
|
|
154
|
+
teal: '#008080',
|
|
155
|
+
thistle: '#d8bfd8',
|
|
156
|
+
tomato: '#ff6347',
|
|
157
|
+
turquoise: '#40e0d0',
|
|
158
|
+
violet: '#ee82ee',
|
|
159
|
+
wheat: '#f5deb3',
|
|
160
|
+
white: '#ffffff',
|
|
161
|
+
whitesmoke: '#f5f5f5',
|
|
162
|
+
yellow: '#ffff00',
|
|
163
|
+
yellowgreen: '#9acd32'
|
|
164
|
+
};
|
|
165
|
+
export const colorKeywords = {
|
|
166
|
+
'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.',
|
|
167
|
+
'transparent': 'Fully transparent. This keyword can be considered a shorthand for rgba(0,0,0,0) which is its computed value.',
|
|
168
|
+
};
|
|
169
|
+
function getNumericValue(node, factor) {
|
|
170
|
+
const val = node.getText();
|
|
171
|
+
const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(%?)$/);
|
|
172
|
+
if (m) {
|
|
173
|
+
if (m[2]) {
|
|
174
|
+
factor = 100.0;
|
|
175
|
+
}
|
|
176
|
+
const result = parseFloat(m[1]) / factor;
|
|
177
|
+
if (result >= 0 && result <= 1) {
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
throw new Error();
|
|
182
|
+
}
|
|
183
|
+
function getAngle(node) {
|
|
184
|
+
const val = node.getText();
|
|
185
|
+
const m = val.match(/^([-+]?[0-9]*\.?[0-9]+)(deg|rad|grad|turn)?$/);
|
|
186
|
+
if (m) {
|
|
187
|
+
switch (m[2]) {
|
|
188
|
+
case 'deg':
|
|
189
|
+
return parseFloat(val) % 360;
|
|
190
|
+
case 'rad':
|
|
191
|
+
return (parseFloat(val) * 180 / Math.PI) % 360;
|
|
192
|
+
case 'grad':
|
|
193
|
+
return (parseFloat(val) * 0.9) % 360;
|
|
194
|
+
case 'turn':
|
|
195
|
+
return (parseFloat(val) * 360) % 360;
|
|
196
|
+
default:
|
|
197
|
+
if ('undefined' === typeof m[2]) {
|
|
198
|
+
return parseFloat(val) % 360;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
throw new Error();
|
|
203
|
+
}
|
|
204
|
+
export function isColorConstructor(node) {
|
|
205
|
+
const name = node.getName();
|
|
206
|
+
if (!name) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
return /^(rgb|rgba|hsl|hsla|hwb)$/gi.test(name);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Returns true if the node is a color value - either
|
|
213
|
+
* defined a hex number, as rgb or rgba function, or
|
|
214
|
+
* as color name.
|
|
215
|
+
*/
|
|
216
|
+
export function isColorValue(node) {
|
|
217
|
+
if (node.type === nodes.NodeType.HexColorValue) {
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
else if (node.type === nodes.NodeType.Function) {
|
|
221
|
+
return isColorConstructor(node);
|
|
222
|
+
}
|
|
223
|
+
else if (node.type === nodes.NodeType.Identifier) {
|
|
224
|
+
if (node.parent && node.parent.type !== nodes.NodeType.Term) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
const candidateColor = node.getText().toLowerCase();
|
|
228
|
+
if (candidateColor === 'none') {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
if (colors[candidateColor]) {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
const Digit0 = 48;
|
|
238
|
+
const Digit9 = 57;
|
|
239
|
+
const A = 65;
|
|
240
|
+
const F = 70;
|
|
241
|
+
const a = 97;
|
|
242
|
+
const f = 102;
|
|
243
|
+
export function hexDigit(charCode) {
|
|
244
|
+
if (charCode < Digit0) {
|
|
245
|
+
return 0;
|
|
246
|
+
}
|
|
247
|
+
if (charCode <= Digit9) {
|
|
248
|
+
return charCode - Digit0;
|
|
249
|
+
}
|
|
250
|
+
if (charCode < a) {
|
|
251
|
+
charCode += (a - A);
|
|
252
|
+
}
|
|
253
|
+
if (charCode >= a && charCode <= f) {
|
|
254
|
+
return charCode - a + 10;
|
|
255
|
+
}
|
|
256
|
+
return 0;
|
|
257
|
+
}
|
|
258
|
+
export function colorFromHex(text) {
|
|
259
|
+
if (text[0] !== '#') {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
switch (text.length) {
|
|
263
|
+
case 4:
|
|
264
|
+
return {
|
|
265
|
+
red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
|
|
266
|
+
green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
|
|
267
|
+
blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
|
|
268
|
+
alpha: 1
|
|
269
|
+
};
|
|
270
|
+
case 5:
|
|
271
|
+
return {
|
|
272
|
+
red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
|
|
273
|
+
green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
|
|
274
|
+
blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
|
|
275
|
+
alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
|
|
276
|
+
};
|
|
277
|
+
case 7:
|
|
278
|
+
return {
|
|
279
|
+
red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
|
|
280
|
+
green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
|
|
281
|
+
blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
|
|
282
|
+
alpha: 1
|
|
283
|
+
};
|
|
284
|
+
case 9:
|
|
285
|
+
return {
|
|
286
|
+
red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
|
|
287
|
+
green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
|
|
288
|
+
blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
|
|
289
|
+
alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
export function colorFrom256RGB(red, green, blue, alpha = 1.0) {
|
|
295
|
+
return {
|
|
296
|
+
red: red / 255.0,
|
|
297
|
+
green: green / 255.0,
|
|
298
|
+
blue: blue / 255.0,
|
|
299
|
+
alpha
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
export function colorFromHSL(hue, sat, light, alpha = 1.0) {
|
|
303
|
+
hue = hue / 60.0;
|
|
304
|
+
if (sat === 0) {
|
|
305
|
+
return { red: light, green: light, blue: light, alpha };
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const hueToRgb = (t1, t2, hue) => {
|
|
309
|
+
while (hue < 0) {
|
|
310
|
+
hue += 6;
|
|
311
|
+
}
|
|
312
|
+
while (hue >= 6) {
|
|
313
|
+
hue -= 6;
|
|
314
|
+
}
|
|
315
|
+
if (hue < 1) {
|
|
316
|
+
return (t2 - t1) * hue + t1;
|
|
317
|
+
}
|
|
318
|
+
if (hue < 3) {
|
|
319
|
+
return t2;
|
|
320
|
+
}
|
|
321
|
+
if (hue < 4) {
|
|
322
|
+
return (t2 - t1) * (4 - hue) + t1;
|
|
323
|
+
}
|
|
324
|
+
return t1;
|
|
325
|
+
};
|
|
326
|
+
const t2 = light <= 0.5 ? (light * (sat + 1)) : (light + sat - (light * sat));
|
|
327
|
+
const t1 = light * 2 - t2;
|
|
328
|
+
return { red: hueToRgb(t1, t2, hue + 2), green: hueToRgb(t1, t2, hue), blue: hueToRgb(t1, t2, hue - 2), alpha };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
export function hslFromColor(rgba) {
|
|
332
|
+
const r = rgba.red;
|
|
333
|
+
const g = rgba.green;
|
|
334
|
+
const b = rgba.blue;
|
|
335
|
+
const a = rgba.alpha;
|
|
336
|
+
const max = Math.max(r, g, b);
|
|
337
|
+
const min = Math.min(r, g, b);
|
|
338
|
+
let h = 0;
|
|
339
|
+
let s = 0;
|
|
340
|
+
const l = (min + max) / 2;
|
|
341
|
+
const chroma = max - min;
|
|
342
|
+
if (chroma > 0) {
|
|
343
|
+
s = Math.min((l <= 0.5 ? chroma / (2 * l) : chroma / (2 - (2 * l))), 1);
|
|
344
|
+
switch (max) {
|
|
345
|
+
case r:
|
|
346
|
+
h = (g - b) / chroma + (g < b ? 6 : 0);
|
|
347
|
+
break;
|
|
348
|
+
case g:
|
|
349
|
+
h = (b - r) / chroma + 2;
|
|
350
|
+
break;
|
|
351
|
+
case b:
|
|
352
|
+
h = (r - g) / chroma + 4;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
h *= 60;
|
|
356
|
+
h = Math.round(h);
|
|
357
|
+
}
|
|
358
|
+
return { h, s, l, a };
|
|
359
|
+
}
|
|
360
|
+
export function colorFromHWB(hue, white, black, alpha = 1.0) {
|
|
361
|
+
if (white + black >= 1) {
|
|
362
|
+
const gray = white / (white + black);
|
|
363
|
+
return { red: gray, green: gray, blue: gray, alpha };
|
|
364
|
+
}
|
|
365
|
+
const rgb = colorFromHSL(hue, 1, 0.5, alpha);
|
|
366
|
+
let red = rgb.red;
|
|
367
|
+
red *= (1 - white - black);
|
|
368
|
+
red += white;
|
|
369
|
+
let green = rgb.green;
|
|
370
|
+
green *= (1 - white - black);
|
|
371
|
+
green += white;
|
|
372
|
+
let blue = rgb.blue;
|
|
373
|
+
blue *= (1 - white - black);
|
|
374
|
+
blue += white;
|
|
375
|
+
return {
|
|
376
|
+
red: red,
|
|
377
|
+
green: green,
|
|
378
|
+
blue: blue,
|
|
379
|
+
alpha
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
export function hwbFromColor(rgba) {
|
|
383
|
+
const hsl = hslFromColor(rgba);
|
|
384
|
+
const white = Math.min(rgba.red, rgba.green, rgba.blue);
|
|
385
|
+
const black = 1 - Math.max(rgba.red, rgba.green, rgba.blue);
|
|
386
|
+
return {
|
|
387
|
+
h: hsl.h,
|
|
388
|
+
w: white,
|
|
389
|
+
b: black,
|
|
390
|
+
a: hsl.a
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
export function getColorValue(node) {
|
|
394
|
+
if (node.type === nodes.NodeType.HexColorValue) {
|
|
395
|
+
const text = node.getText();
|
|
396
|
+
return colorFromHex(text);
|
|
397
|
+
}
|
|
398
|
+
else if (node.type === nodes.NodeType.Function) {
|
|
399
|
+
const functionNode = node;
|
|
400
|
+
const name = functionNode.getName();
|
|
401
|
+
let colorValues = functionNode.getArguments().getChildren();
|
|
402
|
+
if (colorValues.length === 1) {
|
|
403
|
+
const functionArg = colorValues[0].getChildren();
|
|
404
|
+
if (functionArg.length === 1 && functionArg[0].type === nodes.NodeType.Expression) {
|
|
405
|
+
colorValues = functionArg[0].getChildren();
|
|
406
|
+
if (colorValues.length === 3) {
|
|
407
|
+
const lastValue = colorValues[2];
|
|
408
|
+
if (lastValue instanceof nodes.BinaryExpression) {
|
|
409
|
+
const left = lastValue.getLeft(), right = lastValue.getRight(), operator = lastValue.getOperator();
|
|
410
|
+
if (left && right && operator && operator.matches('/')) {
|
|
411
|
+
colorValues = [colorValues[0], colorValues[1], left, right];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (!name || colorValues.length < 3 || colorValues.length > 4) {
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
try {
|
|
421
|
+
const alpha = colorValues.length === 4 ? getNumericValue(colorValues[3], 1) : 1;
|
|
422
|
+
if (name === 'rgb' || name === 'rgba') {
|
|
423
|
+
return {
|
|
424
|
+
red: getNumericValue(colorValues[0], 255.0),
|
|
425
|
+
green: getNumericValue(colorValues[1], 255.0),
|
|
426
|
+
blue: getNumericValue(colorValues[2], 255.0),
|
|
427
|
+
alpha
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
else if (name === 'hsl' || name === 'hsla') {
|
|
431
|
+
const h = getAngle(colorValues[0]);
|
|
432
|
+
const s = getNumericValue(colorValues[1], 100.0);
|
|
433
|
+
const l = getNumericValue(colorValues[2], 100.0);
|
|
434
|
+
return colorFromHSL(h, s, l, alpha);
|
|
435
|
+
}
|
|
436
|
+
else if (name === 'hwb') {
|
|
437
|
+
const h = getAngle(colorValues[0]);
|
|
438
|
+
const w = getNumericValue(colorValues[1], 100.0);
|
|
439
|
+
const b = getNumericValue(colorValues[2], 100.0);
|
|
440
|
+
return colorFromHWB(h, w, b, alpha);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
catch (e) {
|
|
444
|
+
// parse error on numeric value
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
else if (node.type === nodes.NodeType.Identifier) {
|
|
449
|
+
if (node.parent && node.parent.type !== nodes.NodeType.Term) {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
const term = node.parent;
|
|
453
|
+
if (term && term.parent && term.parent.type === nodes.NodeType.BinaryExpression) {
|
|
454
|
+
const expression = term.parent;
|
|
455
|
+
if (expression.parent && expression.parent.type === nodes.NodeType.ListEntry && expression.parent.key === expression) {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const candidateColor = node.getText().toLowerCase();
|
|
460
|
+
if (candidateColor === 'none') {
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
const colorHex = colors[candidateColor];
|
|
464
|
+
if (colorHex) {
|
|
465
|
+
return colorFromHex(colorHex);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return null;
|
|
469
|
+
}
|