@tbela99/css-parser 1.4.2 → 1.4.4
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 +354 -4
- package/README.md +69 -1077
- package/dist/config.json.js +76 -3
- package/dist/index-umd-web.js +26838 -18743
- package/dist/index.cjs +26819 -18731
- package/dist/index.d.ts +2471 -1884
- package/dist/lib/ast/clone.js +46 -0
- package/dist/lib/ast/expand.js +90 -65
- package/dist/lib/ast/features/calc.js +86 -43
- package/dist/lib/ast/features/if.js +221 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.js +33 -28
- package/dist/lib/ast/features/prefix.js +406 -44
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.js +14 -15
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.js +145 -99
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.js +551 -232
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.js +154 -150
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +3 -2
- package/dist/lib/ast/types.js +345 -30
- package/dist/lib/ast/walk.js +119 -64
- package/dist/lib/fs/resolve.js +94 -88
- package/dist/lib/parser/declaration/list.js +117 -26
- package/dist/lib/parser/declaration/map.js +141 -96
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/parse.js +1810 -1739
- package/dist/lib/parser/tokenize.js +662 -403
- package/dist/lib/parser/utils/angle.js +29 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-support.js +431 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.js +31 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration.js +672 -91
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.js +518 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.js +104 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.js +870 -269
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/alpha.js +64 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.js +311 -237
- package/dist/lib/syntax/color/color.js +98 -92
- package/dist/lib/syntax/color/hex.js +29 -33
- package/dist/lib/syntax/color/hsl.js +7 -14
- package/dist/lib/syntax/color/hwb.js +14 -27
- package/dist/lib/syntax/color/lab.js +23 -24
- package/dist/lib/syntax/color/lch.js +16 -18
- package/dist/lib/syntax/color/oklab.js +21 -40
- package/dist/lib/syntax/color/oklch.js +16 -18
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.js +160 -48
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.js +30 -29
- package/dist/lib/syntax/color/utils/color-type.js +18 -0
- package/dist/lib/syntax/color/utils/colorspace.js +46 -0
- package/dist/lib/syntax/color/utils/components.js +36 -16
- package/dist/lib/syntax/color/utils/distance.js +21 -11
- package/dist/lib/syntax/color/utils/make-color.js +56 -0
- package/dist/lib/syntax/color/xyz.js +10 -22
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.js +464 -0
- package/dist/lib/syntax/syntax.js +825 -592
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1185 -85
- package/dist/lib/validation/match.js +3120 -0
- package/dist/lib/validation/parser/parse.js +683 -935
- package/dist/lib/validation/parser/typedef.js +106 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.js +102 -57
- package/dist/web.js +100 -48
- package/package.json +31 -17
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { convertColor, getNumber } from './color.js';
|
|
2
|
-
import {
|
|
3
|
-
import '../../ast/minify.js';
|
|
2
|
+
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
4
3
|
import { walkValues } from '../../ast/walk.js';
|
|
5
|
-
import '../../parser/parse.js';
|
|
6
|
-
import '../../parser/tokenize.js';
|
|
7
|
-
import '../../parser/utils/config.js';
|
|
8
|
-
import { mathFuncs } from '../syntax.js';
|
|
9
|
-
import { colorRange } from './utils/constants.js';
|
|
10
4
|
import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
|
|
11
|
-
import '
|
|
5
|
+
import { colorsFunc, colorFuncColorSpace, colorRange, mathFuncs } from '../constants.js';
|
|
6
|
+
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
7
|
+
import { getColorComponents } from './utils/components.js';
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Parse relative color components
|
|
11
|
+
* @param relativeKeys
|
|
12
|
+
* @param original
|
|
13
|
+
* @param rExp
|
|
14
|
+
* @param gExp
|
|
15
|
+
* @param bExp
|
|
16
|
+
* @param aExp
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp, aExp) {
|
|
14
20
|
let r;
|
|
15
21
|
let g;
|
|
16
22
|
let b;
|
|
@@ -18,88 +24,194 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
|
|
|
18
24
|
let keys = {};
|
|
19
25
|
let values = {};
|
|
20
26
|
// colorFuncColorSpace x,y,z or r,g,b
|
|
21
|
-
const names = relativeKeys.startsWith(
|
|
22
|
-
|
|
27
|
+
const names = relativeKeys.startsWith("xyz")
|
|
28
|
+
? "xyz"
|
|
29
|
+
: ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, relativeKeys))
|
|
30
|
+
? "rgb"
|
|
31
|
+
: relativeKeys.slice(-3);
|
|
32
|
+
const allComponents = [rExp, gExp, bExp, aExp];
|
|
33
|
+
const components = getColorComponents(original);
|
|
34
|
+
const validKeys = names.split("");
|
|
35
|
+
let val = "";
|
|
36
|
+
if (components != null) {
|
|
37
|
+
allComponents.push(...components);
|
|
38
|
+
}
|
|
39
|
+
// ensure all components are valid for the color space
|
|
40
|
+
for (const component of allComponents) {
|
|
41
|
+
if (component == null) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (component.typ == EnumToken.IdenTokenType) {
|
|
45
|
+
val = component.val.toLowerCase();
|
|
46
|
+
if (
|
|
47
|
+
// @ts-expect-error
|
|
48
|
+
typeof Math[val.toUpperCase()] !== "number" &&
|
|
49
|
+
val != "in" &&
|
|
50
|
+
val != "hue" &&
|
|
51
|
+
val != "from" &&
|
|
52
|
+
val != "alpha" &&
|
|
53
|
+
val != "none" &&
|
|
54
|
+
val != "shorter" &&
|
|
55
|
+
val != "longer" &&
|
|
56
|
+
val != "increasing" &&
|
|
57
|
+
val != "decreasing" &&
|
|
58
|
+
!colorsFunc.includes(val) &&
|
|
59
|
+
!colorFuncColorSpace.includes(val) &&
|
|
60
|
+
!validKeys.includes(val)) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (component.typ === EnumToken.MathFunctionTokenType &&
|
|
66
|
+
equalsIgnoreCase("calc", component.val)) {
|
|
67
|
+
for (const { value } of walkValues(component.chi)) {
|
|
68
|
+
if (value.typ == EnumToken.IdenTokenType) {
|
|
69
|
+
val = value.val.toLowerCase();
|
|
70
|
+
if (
|
|
71
|
+
// @ts-expect-error
|
|
72
|
+
typeof Math[val.toUpperCase()] !== "number" &&
|
|
73
|
+
val != "in" &&
|
|
74
|
+
val != "hue" &&
|
|
75
|
+
val != "from" &&
|
|
76
|
+
val != "alpha" &&
|
|
77
|
+
val != "none" &&
|
|
78
|
+
val != "shorter" &&
|
|
79
|
+
val != "longer" &&
|
|
80
|
+
val != "increasing" &&
|
|
81
|
+
val != "decreasing" &&
|
|
82
|
+
!colorsFunc.includes(val) &&
|
|
83
|
+
!colorFuncColorSpace.includes(val) &&
|
|
84
|
+
!validKeys.includes(val)) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const converted = (convertColor(original, ColorType[relativeKeys.toUpperCase().replaceAll("-", "_")]));
|
|
23
92
|
if (converted == null) {
|
|
24
93
|
return null;
|
|
25
94
|
}
|
|
26
|
-
const children = converted.chi.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
95
|
+
const children = converted.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
27
96
|
[r, g, b, alpha] = converted.kin == ColorType.COLOR ? children.slice(1) : children;
|
|
28
97
|
values = {
|
|
29
98
|
[names[0]]: getValue(r, converted, names[0]),
|
|
30
99
|
[names[1]]: getValue(g, converted, names[1]), // string,
|
|
31
100
|
[names[2]]: getValue(b, converted, names[2]),
|
|
32
101
|
// @ts-ignore
|
|
33
|
-
alpha: alpha == null
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
102
|
+
alpha: alpha == null
|
|
103
|
+
? {
|
|
104
|
+
typ: EnumToken.NumberTokenType,
|
|
105
|
+
val: 1,
|
|
106
|
+
}
|
|
107
|
+
: alpha.typ == EnumToken.IdenTokenType && alpha.val == "none"
|
|
108
|
+
? {
|
|
109
|
+
typ: EnumToken.NumberTokenType,
|
|
110
|
+
val: 0,
|
|
111
|
+
}
|
|
112
|
+
: alpha.typ == EnumToken.PercentageTokenType
|
|
113
|
+
? {
|
|
114
|
+
typ: EnumToken.NumberTokenType,
|
|
115
|
+
val: getNumber(alpha),
|
|
116
|
+
}
|
|
117
|
+
: alpha,
|
|
43
118
|
};
|
|
44
119
|
keys = {
|
|
45
120
|
[names[0]]: getValue(rExp, converted, names[0]),
|
|
46
121
|
[names[1]]: getValue(gExp, converted, names[1]),
|
|
47
122
|
[names[2]]: getValue(bExp, converted, names[2]),
|
|
48
123
|
// @ts-ignore
|
|
49
|
-
alpha: getValue(aExp == null
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
124
|
+
alpha: getValue(aExp == null
|
|
125
|
+
? {
|
|
126
|
+
typ: EnumToken.NumberTokenType,
|
|
127
|
+
val: 1,
|
|
128
|
+
}
|
|
129
|
+
: aExp.typ == EnumToken.IdenTokenType && aExp.val == "none"
|
|
130
|
+
? {
|
|
131
|
+
typ: EnumToken.NumberTokenType,
|
|
132
|
+
val: 0,
|
|
133
|
+
}
|
|
134
|
+
: aExp),
|
|
56
135
|
};
|
|
57
|
-
|
|
136
|
+
const result = computeComponentValue(keys, values);
|
|
137
|
+
if (result?.alpha?.typ == EnumToken.NumberTokenType && result.alpha.val === 1) {
|
|
138
|
+
const { alpha, ...components } = result;
|
|
139
|
+
return components;
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
58
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Get token numeric value
|
|
145
|
+
* @param t
|
|
146
|
+
* @param converted
|
|
147
|
+
* @param component
|
|
148
|
+
* @returns
|
|
149
|
+
*/
|
|
59
150
|
function getValue(t, converted, component) {
|
|
60
151
|
if (t.typ == EnumToken.PercentageTokenType) {
|
|
61
152
|
let value = getNumber(t);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
153
|
+
if (converted != null) {
|
|
154
|
+
let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll("-", "_");
|
|
155
|
+
if (colorSpace in colorRange) {
|
|
156
|
+
// @ts-ignore
|
|
157
|
+
value *= colorRange[colorSpace][component].at(-1);
|
|
158
|
+
}
|
|
66
159
|
}
|
|
67
160
|
return {
|
|
68
161
|
typ: EnumToken.NumberTokenType,
|
|
69
|
-
val: value
|
|
162
|
+
val: value,
|
|
70
163
|
};
|
|
71
164
|
}
|
|
72
165
|
return t;
|
|
73
166
|
}
|
|
74
|
-
|
|
167
|
+
/**
|
|
168
|
+
* Compute component value
|
|
169
|
+
* @param expr
|
|
170
|
+
* @param values
|
|
171
|
+
* @returns
|
|
172
|
+
*/
|
|
173
|
+
function computeComponentValue(expr, values) {
|
|
75
174
|
for (const object of [values, expr]) {
|
|
76
|
-
if (
|
|
175
|
+
if ("h" in object) {
|
|
77
176
|
// normalize hue
|
|
78
177
|
for (const k of walkValues([object.h])) {
|
|
79
|
-
if (k.value.typ == EnumToken.AngleTokenType && k.value.unit ==
|
|
178
|
+
if (k.value.typ == EnumToken.AngleTokenType && k.value.unit == "deg") {
|
|
80
179
|
k.value.typ = EnumToken.NumberTokenType;
|
|
81
180
|
}
|
|
82
181
|
}
|
|
83
182
|
}
|
|
84
183
|
}
|
|
85
184
|
for (const [key, exp] of Object.entries(expr)) {
|
|
86
|
-
if ([
|
|
185
|
+
if ([
|
|
186
|
+
EnumToken.NumberTokenType,
|
|
187
|
+
EnumToken.PercentageTokenType,
|
|
188
|
+
EnumToken.AngleTokenType,
|
|
189
|
+
EnumToken.LengthTokenType,
|
|
190
|
+
].includes(exp.typ)) ;
|
|
87
191
|
else if (exp.typ == EnumToken.IdenTokenType && exp.val in values) {
|
|
88
192
|
expr[key] = values[exp.val];
|
|
89
193
|
}
|
|
90
|
-
else if (exp.typ == EnumToken.
|
|
194
|
+
else if (exp.typ == EnumToken.MathFunctionTokenType ||
|
|
195
|
+
(exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val))) {
|
|
91
196
|
for (let { value, parent } of walkValues(exp.chi, exp)) {
|
|
92
197
|
if (value.typ == EnumToken.IdenTokenType) {
|
|
93
198
|
// @ts-ignore
|
|
94
|
-
replaceValue(parent, value,
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
199
|
+
replaceValue(parent, value,
|
|
200
|
+
// @ts-expect-error
|
|
201
|
+
values[value.val] ??
|
|
202
|
+
{
|
|
203
|
+
typ: EnumToken.NumberTokenType,
|
|
204
|
+
// @ts-ignore
|
|
205
|
+
val: "" + Math[value.val.toUpperCase()],
|
|
206
|
+
// @ts-ignore
|
|
207
|
+
});
|
|
100
208
|
}
|
|
101
209
|
}
|
|
102
|
-
const result = exp.typ
|
|
210
|
+
const result = (exp.typ === EnumToken.MathFunctionTokenType ||
|
|
211
|
+
(exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val))) &&
|
|
212
|
+
exp.val !== "calc"
|
|
213
|
+
? evaluateFunc(exp)
|
|
214
|
+
: evaluate(exp.chi);
|
|
103
215
|
if (result.length == 1 && result[0].typ != EnumToken.BinaryExpressionTokenType) {
|
|
104
216
|
expr[key] = result[0];
|
|
105
217
|
}
|
|
@@ -128,4 +240,4 @@ function replaceValue(parent, value, newValue) {
|
|
|
128
240
|
}
|
|
129
241
|
}
|
|
130
242
|
|
|
131
|
-
export {
|
|
243
|
+
export { parseRelativeColorComponents, replaceValue };
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
import { color2srgbvalues, minmax } from './color.js';
|
|
2
|
-
import { COLORS_NAMES } from './utils/constants.js';
|
|
3
|
-
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
4
|
-
import '../../ast/minify.js';
|
|
5
|
-
import '../../ast/walk.js';
|
|
6
|
-
import '../../parser/parse.js';
|
|
7
|
-
import '../../parser/tokenize.js';
|
|
8
|
-
import '../../parser/utils/config.js';
|
|
9
2
|
import { expandHexValue } from './hex.js';
|
|
10
3
|
import { hslvalues, hslvalues2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues } from './srgb.js';
|
|
11
|
-
import '../../
|
|
4
|
+
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
5
|
+
import { COLORS_NAMES } from '../constants.js';
|
|
12
6
|
|
|
13
7
|
function srgb2rgb(value) {
|
|
14
8
|
return minmax(Math.round(value * 255), 0, 255);
|
|
@@ -70,7 +64,7 @@ function color2RgbToken(token) {
|
|
|
70
64
|
if (values == null) {
|
|
71
65
|
return null;
|
|
72
66
|
}
|
|
73
|
-
return rgb2RgbToken(values.map((t, index) => index == 3 ? t : srgb2rgb(t)));
|
|
67
|
+
return rgb2RgbToken(values.map((t, index) => (index == 3 ? t : srgb2rgb(t))));
|
|
74
68
|
}
|
|
75
69
|
function rgb2RgbToken(values) {
|
|
76
70
|
const chi = [
|
|
@@ -83,9 +77,9 @@ function rgb2RgbToken(values) {
|
|
|
83
77
|
}
|
|
84
78
|
return {
|
|
85
79
|
typ: EnumToken.ColorTokenType,
|
|
86
|
-
val:
|
|
80
|
+
val: "rgb",
|
|
87
81
|
chi,
|
|
88
|
-
kin: ColorType.RGB
|
|
82
|
+
kin: ColorType.RGB,
|
|
89
83
|
};
|
|
90
84
|
}
|
|
91
85
|
function hex2rgbvalues(token) {
|
|
@@ -105,14 +99,16 @@ function hex2rgbvalues(token) {
|
|
|
105
99
|
return rgb;
|
|
106
100
|
}
|
|
107
101
|
function hwb2rgbvalues(token) {
|
|
108
|
-
return hwb2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
|
|
102
|
+
return hwb2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
109
103
|
}
|
|
110
104
|
function hsl2rgbvalues(token) {
|
|
111
105
|
let { h, s, l, a } = hslvalues(token) ?? {};
|
|
112
106
|
if (h == null || s == null || l == null) {
|
|
113
107
|
return null;
|
|
114
108
|
}
|
|
115
|
-
return hslvalues2srgbvalues(h, s, l)
|
|
109
|
+
return hslvalues2srgbvalues(h, s, l)
|
|
110
|
+
.map((t) => minmax(Math.round(t * 255), 0, 255))
|
|
111
|
+
.concat(a == 1 || a == null ? [] : [a]);
|
|
116
112
|
}
|
|
117
113
|
function hsl2srgbvalues(token) {
|
|
118
114
|
let { h, s, l, a } = hslvalues(token) ?? {};
|
|
@@ -122,19 +118,19 @@ function hsl2srgbvalues(token) {
|
|
|
122
118
|
return hslvalues2srgbvalues(h, s, l).concat(a == 1 || a == null ? [] : [a]);
|
|
123
119
|
}
|
|
124
120
|
function cmyk2rgbvalues(token) {
|
|
125
|
-
return cmyk2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
|
|
121
|
+
return cmyk2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
126
122
|
}
|
|
127
123
|
function oklab2rgbvalues(token) {
|
|
128
|
-
return oklab2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
|
|
124
|
+
return oklab2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
129
125
|
}
|
|
130
126
|
function oklch2rgbvalues(token) {
|
|
131
|
-
return oklch2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
|
|
127
|
+
return oklch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
132
128
|
}
|
|
133
129
|
function lab2rgbvalues(token) {
|
|
134
|
-
return lab2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
|
|
130
|
+
return lab2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
135
131
|
}
|
|
136
132
|
function lch2rgbvalues(token) {
|
|
137
|
-
return lch2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
|
|
133
|
+
return lch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
138
134
|
}
|
|
139
135
|
|
|
140
136
|
export { cmyk2RgbToken, cmyk2rgbvalues, color2RgbToken, hex2RgbToken, hex2rgbvalues, hsl2RgbToken, hsl2rgbvalues, hsl2srgbvalues, hwb2RgbToken, hwb2rgbvalues, lab2RgbToken, lab2rgbvalues, lch2RgbToken, lch2rgbvalues, oklab2RgbToken, oklab2rgbvalues, oklch2RgbToken, oklch2rgbvalues, srgb2rgb };
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getComponents } from './utils/components.js';
|
|
1
|
+
import { getColorComponents } from './utils/components.js';
|
|
3
2
|
import { color2srgbvalues, getNumber, getAngle } from './color.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import '../../ast/minify.js';
|
|
7
|
-
import '../../ast/walk.js';
|
|
8
|
-
import '../../parser/parse.js';
|
|
9
|
-
import '../../parser/tokenize.js';
|
|
10
|
-
import '../../parser/utils/config.js';
|
|
3
|
+
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
4
|
+
import { Lab_to_sRGB, getLABComponents, lchvalues2labvalues } from './lab.js';
|
|
11
5
|
import { expandHexValue } from './hex.js';
|
|
12
|
-
import {
|
|
6
|
+
import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
|
|
13
7
|
import { getLCHComponents } from './lch.js';
|
|
14
8
|
import { getOKLCHComponents } from './oklch.js';
|
|
15
9
|
import { XYZ_to_lin_sRGB } from './xyz.js';
|
|
16
|
-
import '
|
|
10
|
+
import { COLORS_NAMES } from '../constants.js';
|
|
11
|
+
import { parseColor } from '../syntax.js';
|
|
17
12
|
|
|
18
13
|
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
|
|
19
14
|
// srgb-linear -> srgb
|
|
20
15
|
// 0 <= r, g, b <= 1
|
|
21
16
|
function srgbvalues(token) {
|
|
17
|
+
if (token.typ === EnumToken.IdenTokenType) {
|
|
18
|
+
token = parseColor(token);
|
|
19
|
+
}
|
|
22
20
|
switch (token.kin) {
|
|
23
21
|
case ColorType.LIT:
|
|
24
22
|
case ColorType.HEX:
|
|
@@ -45,10 +43,18 @@ function srgbvalues(token) {
|
|
|
45
43
|
return null;
|
|
46
44
|
}
|
|
47
45
|
function rgb2srgb(token) {
|
|
48
|
-
return
|
|
46
|
+
return (getColorComponents(token)?.map?.((t, index) => index == 3
|
|
47
|
+
? t.typ == EnumToken.IdenTokenType && t.val == "none"
|
|
48
|
+
? 1
|
|
49
|
+
: getNumber(t)
|
|
50
|
+
: ((t.typ == EnumToken.PercentageTokenType ? 255 : 1) *
|
|
51
|
+
getNumber(t)) /
|
|
52
|
+
255) ?? null);
|
|
49
53
|
}
|
|
50
54
|
function rgb2srgbvalues(token) {
|
|
51
|
-
return
|
|
55
|
+
return (getColorComponents(token)?.map?.((t, index) => index == 3
|
|
56
|
+
? getNumber(t)
|
|
57
|
+
: getNumber(t) / 255) ?? null);
|
|
52
58
|
}
|
|
53
59
|
function hex2srgbvalues(token) {
|
|
54
60
|
const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
|
|
@@ -71,9 +77,9 @@ function hwb2srgbvalues(token) {
|
|
|
71
77
|
if (hue == null || white == null || black == null) {
|
|
72
78
|
return [];
|
|
73
79
|
}
|
|
74
|
-
const rgb = hslvalues2srgbvalues(hue, 1, .5);
|
|
80
|
+
const rgb = hslvalues2srgbvalues(hue, 1, 0.5);
|
|
75
81
|
for (let i = 0; i < 3; i++) {
|
|
76
|
-
rgb[i] *=
|
|
82
|
+
rgb[i] *= 1 - white - black;
|
|
77
83
|
rgb[i] = rgb[i] + white;
|
|
78
84
|
}
|
|
79
85
|
if (alpha != null && alpha != 1) {
|
|
@@ -89,7 +95,7 @@ function hsl2srgb(token) {
|
|
|
89
95
|
return hslvalues2srgbvalues(h, s, l, a);
|
|
90
96
|
}
|
|
91
97
|
function cmyk2srgbvalues(token) {
|
|
92
|
-
const components =
|
|
98
|
+
const components = getColorComponents(token);
|
|
93
99
|
if (components == null) {
|
|
94
100
|
return null;
|
|
95
101
|
}
|
|
@@ -112,20 +118,11 @@ function cmyk2srgbvalues(token) {
|
|
|
112
118
|
const rgb = [
|
|
113
119
|
1 - Math.min(1, c * (1 - k) + k),
|
|
114
120
|
1 - Math.min(1, m * (1 - k) + k),
|
|
115
|
-
1 - Math.min(1, y * (1 - k) + k)
|
|
121
|
+
1 - Math.min(1, y * (1 - k) + k),
|
|
116
122
|
];
|
|
117
123
|
if (components.length == 5) {
|
|
118
124
|
rgb.push(getNumber(components[4]));
|
|
119
125
|
}
|
|
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
|
-
// }
|
|
129
126
|
return rgb;
|
|
130
127
|
}
|
|
131
128
|
function oklab2srgbvalues(token) {
|
|
@@ -152,7 +149,7 @@ function oklch2srgbvalues(token) {
|
|
|
152
149
|
return rgb;
|
|
153
150
|
}
|
|
154
151
|
function hslvalues(token) {
|
|
155
|
-
const components =
|
|
152
|
+
const components = getColorComponents(token);
|
|
156
153
|
if (components == null) {
|
|
157
154
|
return null;
|
|
158
155
|
}
|
|
@@ -177,7 +174,7 @@ function hslvalues(token) {
|
|
|
177
174
|
return a == null ? { h, s, l } : { h, s, l, a };
|
|
178
175
|
}
|
|
179
176
|
function hslvalues2srgbvalues(h, s, l, a = null) {
|
|
180
|
-
let v = l <= .5 ? l * (1.0 + s) : l + s - l * s;
|
|
177
|
+
let v = l <= 0.5 ? l * (1.0 + s) : l + s - l * s;
|
|
181
178
|
let r = l;
|
|
182
179
|
let g = l;
|
|
183
180
|
let b = l;
|
|
@@ -241,8 +238,12 @@ function lab2srgbvalues(token) {
|
|
|
241
238
|
return rgb;
|
|
242
239
|
}
|
|
243
240
|
function lch2srgbvalues(token) {
|
|
241
|
+
const components = getLCHComponents(token);
|
|
242
|
+
if (components == null) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
244
245
|
// @ts-ignore
|
|
245
|
-
const [l, a, b, alpha] = lchvalues2labvalues(...
|
|
246
|
+
const [l, a, b, alpha] = lchvalues2labvalues(...components);
|
|
246
247
|
if (l == null || a == null || b == null) {
|
|
247
248
|
return null;
|
|
248
249
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ColorType } from '../../../ast/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Return the color type of the color components
|
|
5
|
+
*/
|
|
6
|
+
function getColorType(color) {
|
|
7
|
+
if (color.kin === ColorType.COLOR_MIX || color.cal === "rel") {
|
|
8
|
+
return color.kin === ColorType.COLOR_MIX
|
|
9
|
+
? // @ts-expect-error
|
|
10
|
+
ColorType[
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
color.chi[2]?.val?.toUpperCase?.()?.replace?.("-", "_")]
|
|
13
|
+
: getColorType(color.chi[2]);
|
|
14
|
+
}
|
|
15
|
+
return color.kin;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { getColorType };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { equalsIgnoreCase } from '../../../parser/utils/text.js';
|
|
2
|
+
import { getColorComponents } from './components.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Return the color space of the color
|
|
6
|
+
* @param color
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
function getColorSpace(color) {
|
|
10
|
+
if (!("chi" in color)) {
|
|
11
|
+
return "rgb";
|
|
12
|
+
}
|
|
13
|
+
let name;
|
|
14
|
+
const components = getColorComponents(color);
|
|
15
|
+
if (components == null || components.length < 3) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
if (equalsIgnoreCase("color", color.val)) {
|
|
19
|
+
if (equalsIgnoreCase("from", components[0].val)) {
|
|
20
|
+
name = components[2].val;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
name = components[0].val;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (equalsIgnoreCase("color-mix", color.val)) {
|
|
27
|
+
if (equalsIgnoreCase("in", components[0].val)) {
|
|
28
|
+
name = components[1].val;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return "oklab";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
name = color.val;
|
|
36
|
+
}
|
|
37
|
+
return name == "device-cmyk"
|
|
38
|
+
? "cmyk"
|
|
39
|
+
: name.startsWith("xyz")
|
|
40
|
+
? "xyz"
|
|
41
|
+
: ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, name))
|
|
42
|
+
? "rgb"
|
|
43
|
+
: name.slice(-3);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { getColorSpace };
|
|
@@ -1,41 +1,61 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import '../../../ast/minify.js';
|
|
1
|
+
import { EnumToken, ColorType } from '../../../ast/types.js';
|
|
3
2
|
import { walkValues } from '../../../ast/walk.js';
|
|
4
|
-
import '
|
|
5
|
-
import '../../../parser/tokenize.js';
|
|
6
|
-
import '../../../parser/utils/config.js';
|
|
7
|
-
import { COLORS_NAMES } from './constants.js';
|
|
3
|
+
import { COLORS_NAMES } from '../../constants.js';
|
|
8
4
|
import { expandHexValue } from '../hex.js';
|
|
9
|
-
import '
|
|
5
|
+
import { isColor, parseColor } from '../../syntax.js';
|
|
6
|
+
import { equalsIgnoreCase } from '../../../parser/utils/text.js';
|
|
10
7
|
|
|
11
|
-
function
|
|
8
|
+
function getColorComponents(token) {
|
|
9
|
+
if (token.typ === EnumToken.IdenTokenType) {
|
|
10
|
+
if (isColor(token)) {
|
|
11
|
+
parseColor(token);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
12
17
|
if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
|
|
18
|
+
if (equalsIgnoreCase('currentcolor', token.val)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
13
21
|
const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
|
|
14
22
|
// @ts-ignore
|
|
15
|
-
return value
|
|
23
|
+
return value
|
|
24
|
+
.slice(1)
|
|
25
|
+
.match(/([a-fA-F0-9]{2})/g)
|
|
26
|
+
.map((t, index) => {
|
|
16
27
|
return { typ: EnumToken.Number, val: index < 3 ? parseInt(t, 16) : parseInt(t, 16) / 255 };
|
|
17
28
|
});
|
|
18
29
|
}
|
|
19
30
|
const result = [];
|
|
20
|
-
for (const child of
|
|
31
|
+
for (const child of token.chi) {
|
|
21
32
|
if ([
|
|
22
|
-
EnumToken.LiteralTokenType,
|
|
33
|
+
EnumToken.LiteralTokenType,
|
|
34
|
+
EnumToken.CommentTokenType,
|
|
35
|
+
EnumToken.CommaTokenType,
|
|
36
|
+
EnumToken.WhitespaceTokenType,
|
|
23
37
|
].includes(child.typ)) {
|
|
24
38
|
continue;
|
|
25
39
|
}
|
|
26
|
-
if (child.typ
|
|
27
|
-
|
|
40
|
+
if (child.typ === EnumToken.IdenTokenType && isColor(child)) {
|
|
41
|
+
parseColor(child);
|
|
42
|
+
}
|
|
43
|
+
if (child.typ === EnumToken.FunctionTokenType ||
|
|
44
|
+
child.typ === EnumToken.WildCardFunctionTokenType ||
|
|
45
|
+
child.typ === EnumToken.MathFunctionTokenType) {
|
|
46
|
+
if ("var" == child.val.toLowerCase()) {
|
|
28
47
|
return null;
|
|
29
48
|
}
|
|
30
49
|
else {
|
|
31
50
|
for (const { value } of walkValues(child.chi)) {
|
|
32
|
-
if (value.typ == EnumToken.
|
|
51
|
+
if (value.typ == EnumToken.WildCardFunctionTokenDefType &&
|
|
52
|
+
"var" === value.val.toLowerCase()) {
|
|
33
53
|
return null;
|
|
34
54
|
}
|
|
35
55
|
}
|
|
36
56
|
}
|
|
37
57
|
}
|
|
38
|
-
if (child.typ == EnumToken.ColorTokenType &&
|
|
58
|
+
if (child.typ == EnumToken.ColorTokenType && equalsIgnoreCase("currentcolor", child.val)) {
|
|
39
59
|
return null;
|
|
40
60
|
}
|
|
41
61
|
result.push(child);
|
|
@@ -43,4 +63,4 @@ function getComponents(token) {
|
|
|
43
63
|
return result;
|
|
44
64
|
}
|
|
45
65
|
|
|
46
|
-
export {
|
|
66
|
+
export { getColorComponents };
|