@tbela99/css-parser 1.6.6 → 1.6.7
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 +50 -0
- package/README.md +1 -58
- package/architecture.png +0 -0
- package/dist/index-umd-web.js +605 -410
- package/dist/index.cjs +605 -410
- package/dist/index.d.ts +16 -8
- package/dist/lib/ast/expand.js +12 -10
- package/dist/lib/ast/features/prefix.js +1 -3
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/types.js +16 -8
- package/dist/lib/ast/walk.js +9 -0
- package/dist/lib/parser/declaration/list.js +17 -8
- package/dist/lib/parser/declaration/map.js +2 -1
- package/dist/lib/parser/parse.js +32 -28
- package/dist/lib/parser/utils/declaration.js +10 -0
- package/dist/lib/printer/render.js +11 -8
- package/dist/lib/syntax/color/color-mix.js +132 -48
- package/dist/lib/syntax/color/color.js +0 -1
- package/dist/lib/syntax/color/hsl.js +45 -38
- package/dist/lib/syntax/color/hsv.js +1 -15
- package/dist/lib/syntax/color/hwb.js +83 -53
- package/dist/lib/syntax/color/oklab.js +1 -1
- package/dist/lib/syntax/color/relative-color.js +29 -0
- package/dist/lib/syntax/color/srgb.js +81 -58
- package/package.json +4 -4
|
@@ -151,7 +151,36 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
|
|
|
151
151
|
: aExp),
|
|
152
152
|
};
|
|
153
153
|
const result = computeComponentValue(keys, values);
|
|
154
|
+
if (result != null) {
|
|
155
|
+
if ("hsl" == relativeKeys) {
|
|
156
|
+
Object.assign(result.s, {
|
|
157
|
+
typ: EnumToken.PercentageTokenType,
|
|
158
|
+
// @ts-ignore
|
|
159
|
+
val: result.s.val * 100,
|
|
160
|
+
});
|
|
161
|
+
Object.assign(result.l, {
|
|
162
|
+
typ: EnumToken.PercentageTokenType,
|
|
163
|
+
// @ts-ignore
|
|
164
|
+
val: result.l.val * 100,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
else if ("hwb" == relativeKeys) {
|
|
168
|
+
// @ts
|
|
169
|
+
Object.assign(result.w, {
|
|
170
|
+
typ: EnumToken.PercentageTokenType,
|
|
171
|
+
// @ts-ignore
|
|
172
|
+
val: result.w.val * 100,
|
|
173
|
+
});
|
|
174
|
+
Object.assign(result.b, {
|
|
175
|
+
typ: EnumToken.PercentageTokenType,
|
|
176
|
+
// @ts-ignore
|
|
177
|
+
val: result.b.val * 100,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// @ts-ignore
|
|
154
182
|
if (result?.alpha?.typ == EnumToken.NumberTokenType && result.alpha.val === 1) {
|
|
183
|
+
// @ts-ignore
|
|
155
184
|
const { alpha, ...components } = result;
|
|
156
185
|
return components;
|
|
157
186
|
}
|
|
@@ -9,6 +9,7 @@ import { getOKLCHComponents } from './oklch.js';
|
|
|
9
9
|
import { XYZ_to_lin_sRGB } from './xyz.js';
|
|
10
10
|
import { COLORS_NAMES } from '../constants.js';
|
|
11
11
|
import { parseColor } from '../syntax.js';
|
|
12
|
+
import { hwbvalues } from './hwb.js';
|
|
12
13
|
|
|
13
14
|
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
|
|
14
15
|
// srgb-linear -> srgb
|
|
@@ -83,25 +84,59 @@ function xyz2srgb(x, y, z, alpha = null) {
|
|
|
83
84
|
return lsrgb2srgbvalues(values[0], values[1], values[2], alpha);
|
|
84
85
|
}
|
|
85
86
|
function hwb2srgbvalues(token) {
|
|
86
|
-
const { h
|
|
87
|
-
if (
|
|
87
|
+
const { h, w, b, a } = hwbvalues(token) ?? {};
|
|
88
|
+
if (h == null || w == null || b == null) {
|
|
88
89
|
return [];
|
|
89
90
|
}
|
|
90
|
-
const rgb =
|
|
91
|
-
|
|
92
|
-
rgb
|
|
93
|
-
rgb[i] = rgb[i] + white;
|
|
94
|
-
}
|
|
95
|
-
if (alpha != null && alpha != 1) {
|
|
96
|
-
rgb.push(alpha);
|
|
91
|
+
const rgb = hwb2srgb(h, w, b);
|
|
92
|
+
if (a != null && a != 1) {
|
|
93
|
+
rgb.push(a);
|
|
97
94
|
}
|
|
98
95
|
return rgb;
|
|
99
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* convert hwb to sRGB
|
|
99
|
+
*/
|
|
100
|
+
function hwb2srgb(h, w, b, alpha) {
|
|
101
|
+
const wNorm = w / 100;
|
|
102
|
+
const bNorm = b / 100;
|
|
103
|
+
if (wNorm + bNorm >= 1) {
|
|
104
|
+
const gray = wNorm / (wNorm + bNorm);
|
|
105
|
+
return [Math.round(gray * 10000) / 10000, Math.round(gray * 10000) / 10000, Math.round(gray * 10000) / 10000];
|
|
106
|
+
}
|
|
107
|
+
const hPrime = (h % 360) / 60;
|
|
108
|
+
const x = 1 - Math.abs((hPrime % 2) - 1);
|
|
109
|
+
let rChroma = 0;
|
|
110
|
+
let gChroma = 0;
|
|
111
|
+
let bChroma = 0;
|
|
112
|
+
if (hPrime >= 0 && hPrime < 1) {
|
|
113
|
+
[rChroma, gChroma, bChroma] = [1, x, 0];
|
|
114
|
+
}
|
|
115
|
+
else if (hPrime >= 1 && hPrime < 2) {
|
|
116
|
+
[rChroma, gChroma, bChroma] = [x, 1, 0];
|
|
117
|
+
}
|
|
118
|
+
else if (hPrime >= 2 && hPrime < 3) {
|
|
119
|
+
[rChroma, gChroma, bChroma] = [0, 1, x];
|
|
120
|
+
}
|
|
121
|
+
else if (hPrime >= 3 && hPrime < 4) {
|
|
122
|
+
[rChroma, gChroma, bChroma] = [0, x, 1];
|
|
123
|
+
}
|
|
124
|
+
else if (hPrime >= 4 && hPrime < 5) {
|
|
125
|
+
[rChroma, gChroma, bChroma] = [x, 0, 1];
|
|
126
|
+
}
|
|
127
|
+
else if (hPrime >= 5 && hPrime <= 6) {
|
|
128
|
+
[rChroma, gChroma, bChroma] = [1, 0, x];
|
|
129
|
+
}
|
|
130
|
+
const factor = 1 - wNorm - bNorm;
|
|
131
|
+
const srgb = [rChroma * factor + wNorm, gChroma * factor + wNorm, bChroma * factor + wNorm];
|
|
132
|
+
return srgb;
|
|
133
|
+
}
|
|
100
134
|
function hsl2srgb(token) {
|
|
101
135
|
let { h, s, l, a } = hslvalues(token) ?? {};
|
|
102
136
|
if (h == null || s == null || l == null) {
|
|
103
137
|
return null;
|
|
104
138
|
}
|
|
139
|
+
// hue in degrees
|
|
105
140
|
return hslvalues2srgbvalues(h, s, l, a);
|
|
106
141
|
}
|
|
107
142
|
function cmyk2srgbvalues(token) {
|
|
@@ -162,7 +197,7 @@ function hslvalues(token) {
|
|
|
162
197
|
}
|
|
163
198
|
let t;
|
|
164
199
|
// @ts-ignore
|
|
165
|
-
let h = getAngle(components[0]);
|
|
200
|
+
let h = getAngle(components[0]) * 360;
|
|
166
201
|
// @ts-ignore
|
|
167
202
|
t = components[1];
|
|
168
203
|
// @ts-ignore
|
|
@@ -171,6 +206,12 @@ function hslvalues(token) {
|
|
|
171
206
|
t = components[2];
|
|
172
207
|
// @ts-ignore
|
|
173
208
|
let l = getNumber(t);
|
|
209
|
+
if (components[1].typ == EnumToken.PercentageTokenType) {
|
|
210
|
+
s *= 100;
|
|
211
|
+
}
|
|
212
|
+
if (components[2].typ == EnumToken.PercentageTokenType) {
|
|
213
|
+
l *= 100;
|
|
214
|
+
}
|
|
174
215
|
let a = null;
|
|
175
216
|
if (components.length == 4) {
|
|
176
217
|
// @ts-ignore
|
|
@@ -181,56 +222,38 @@ function hslvalues(token) {
|
|
|
181
222
|
return a == null ? { h, s, l } : { h, s, l, a };
|
|
182
223
|
}
|
|
183
224
|
function hslvalues2srgbvalues(h, s, l, a = null) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
let vsf = v * sv * fract;
|
|
195
|
-
let mid1 = m + vsf;
|
|
196
|
-
let mid2 = v - vsf;
|
|
197
|
-
switch (sextant) {
|
|
198
|
-
case 0:
|
|
199
|
-
r = v;
|
|
200
|
-
g = mid1;
|
|
201
|
-
b = m;
|
|
202
|
-
break;
|
|
203
|
-
case 1:
|
|
204
|
-
r = mid2;
|
|
205
|
-
g = v;
|
|
206
|
-
b = m;
|
|
207
|
-
break;
|
|
208
|
-
case 2:
|
|
209
|
-
r = m;
|
|
210
|
-
g = v;
|
|
211
|
-
b = mid1;
|
|
212
|
-
break;
|
|
213
|
-
case 3:
|
|
214
|
-
r = m;
|
|
215
|
-
g = mid2;
|
|
216
|
-
b = v;
|
|
217
|
-
break;
|
|
218
|
-
case 4:
|
|
219
|
-
r = mid1;
|
|
220
|
-
g = m;
|
|
221
|
-
b = v;
|
|
222
|
-
break;
|
|
223
|
-
case 5:
|
|
224
|
-
r = v;
|
|
225
|
-
g = m;
|
|
226
|
-
b = mid2;
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
225
|
+
const sNorm = s / 100;
|
|
226
|
+
const lNorm = l / 100;
|
|
227
|
+
const chroma = (1 - Math.abs(2 * lNorm - 1)) * sNorm;
|
|
228
|
+
const hPrime = h / 60;
|
|
229
|
+
const x = chroma * (1 - Math.abs((hPrime % 2) - 1));
|
|
230
|
+
let rTmp = 0;
|
|
231
|
+
let gTmp = 0;
|
|
232
|
+
let bTmp = 0;
|
|
233
|
+
if (hPrime >= 0 && hPrime < 1) {
|
|
234
|
+
[rTmp, gTmp, bTmp] = [chroma, x, 0];
|
|
229
235
|
}
|
|
230
|
-
|
|
231
|
-
|
|
236
|
+
else if (hPrime >= 1 && hPrime < 2) {
|
|
237
|
+
[rTmp, gTmp, bTmp] = [x, chroma, 0];
|
|
238
|
+
}
|
|
239
|
+
else if (hPrime >= 2 && hPrime < 3) {
|
|
240
|
+
[rTmp, gTmp, bTmp] = [0, chroma, x];
|
|
241
|
+
}
|
|
242
|
+
else if (hPrime >= 3 && hPrime < 4) {
|
|
243
|
+
[rTmp, gTmp, bTmp] = [0, x, chroma];
|
|
244
|
+
}
|
|
245
|
+
else if (hPrime >= 4 && hPrime < 5) {
|
|
246
|
+
[rTmp, gTmp, bTmp] = [x, 0, chroma];
|
|
247
|
+
}
|
|
248
|
+
else if (hPrime >= 5 && hPrime <= 6) {
|
|
249
|
+
[rTmp, gTmp, bTmp] = [chroma, 0, x];
|
|
250
|
+
}
|
|
251
|
+
const m = lNorm - chroma / 2;
|
|
252
|
+
const values = [rTmp + m, gTmp + m, bTmp + m];
|
|
253
|
+
if (a != null) {
|
|
232
254
|
values.push(a);
|
|
233
255
|
}
|
|
256
|
+
// Return exact float decimals instead of multiplying by 255
|
|
234
257
|
return values;
|
|
235
258
|
}
|
|
236
259
|
function lab2srgbvalues(token) {
|
|
@@ -301,4 +324,4 @@ function lsrgb2srgbvalues(r, g, b, alpha = null) {
|
|
|
301
324
|
return rgb;
|
|
302
325
|
}
|
|
303
326
|
|
|
304
|
-
export { cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hslvalues, hslvalues2srgbvalues, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, oklab2srgbvalues, oklch2srgbvalues, rgb2srgb, rgb2srgbvalues, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
|
|
327
|
+
export { cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hslvalues, hslvalues2srgbvalues, hwb2srgb, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, oklab2srgbvalues, oklch2srgbvalues, rgb2srgb, rgb2srgbvalues, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
3
|
"description": "CSS parser, minifier and validator for node and the browser",
|
|
4
|
-
"version": "1.6.
|
|
4
|
+
"version": "1.6.7",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node.js",
|
|
7
7
|
"./node": "./dist/node.js",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
"@types/chai": "^5.2.3",
|
|
69
69
|
"@types/chai-as-promised": "^8.0.2",
|
|
70
70
|
"@types/mocha": "^10.0.10",
|
|
71
|
-
"@types/node": "^26.6.
|
|
72
|
-
"@types/web": "^0.0.
|
|
71
|
+
"@types/node": "^26.6.5",
|
|
72
|
+
"@types/web": "^0.0.358",
|
|
73
73
|
"@web/test-runner": "^1.0.0",
|
|
74
74
|
"@web/test-runner-playwright": "^1.0.0",
|
|
75
75
|
"chai": "^6.3.0",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"esno": "^4.8.0",
|
|
78
78
|
"markdown-it-task-lists": "^2.1.1",
|
|
79
79
|
"mocha": "^12.0.3",
|
|
80
|
-
"playwright": "^1.
|
|
80
|
+
"playwright": "^1.64.0",
|
|
81
81
|
"rollup": "^4.63.6",
|
|
82
82
|
"rollup-plugin-dts": "^6.5.1",
|
|
83
83
|
"superc8": "^12.7.0",
|