@tbela99/css-parser 1.6.5 → 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 +153 -0
- package/CONTRIBUTING.md +119 -0
- package/README.md +1 -58
- package/architecture.png +0 -0
- package/dist/data/validation.json.js +4 -1
- package/dist/index-umd-web.js +730 -463
- package/dist/index.cjs +730 -463
- package/dist/index.d.ts +16 -8
- package/dist/lib/ast/expand.js +12 -10
- package/dist/lib/ast/features/calc.js +3 -10
- package/dist/lib/ast/features/prefix.js +1 -3
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/math/expression.js +46 -35
- 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 -29
- package/dist/lib/parser/utils/at-rule-container.js +70 -1
- package/dist/lib/parser/utils/at-rule-media.js +0 -2
- 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/dist/lib/validation/match.js +3 -3
- package/package.json +8 -8
|
@@ -55,6 +55,7 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
55
55
|
let rules = null;
|
|
56
56
|
let syntaxRules = null;
|
|
57
57
|
let success = true;
|
|
58
|
+
let hasImportantFlag = false;
|
|
58
59
|
let validate = typeof options.validation === "boolean" ? options.validation : !!options.validation;
|
|
59
60
|
if (renamedStandardProperties.has(name.val)) {
|
|
60
61
|
name.val = renamedStandardProperties.get(name.val);
|
|
@@ -356,6 +357,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
356
357
|
}
|
|
357
358
|
}
|
|
358
359
|
break;
|
|
360
|
+
case EnumToken.ImportantTokenType:
|
|
361
|
+
hasImportantFlag = true;
|
|
362
|
+
break;
|
|
359
363
|
}
|
|
360
364
|
if (trimTokenSpace.has(token.typ)) {
|
|
361
365
|
if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
@@ -422,6 +426,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
422
426
|
}
|
|
423
427
|
name[STATE] = EnumAstNodeStatus.Unknown;
|
|
424
428
|
name[ERRORS] = result?.errors ?? [];
|
|
429
|
+
if (hasImportantFlag) {
|
|
430
|
+
name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
|
|
431
|
+
}
|
|
425
432
|
// @ts-expect-error
|
|
426
433
|
return Object.assign(name, {
|
|
427
434
|
typ: EnumToken.DeclarationNodeType,
|
|
@@ -459,6 +466,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
459
466
|
: EnumAstNodeStatus.Validated
|
|
460
467
|
: EnumAstNodeStatus.ValidationFailed;
|
|
461
468
|
name[ERRORS] = result?.errors ?? [];
|
|
469
|
+
if (hasImportantFlag) {
|
|
470
|
+
name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
|
|
471
|
+
}
|
|
462
472
|
// @ts-expect-error
|
|
463
473
|
return Object.assign(name, {
|
|
464
474
|
typ: EnumToken.DeclarationNodeType,
|
|
@@ -1284,17 +1284,20 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1284
1284
|
? ""
|
|
1285
1285
|
: token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
|
|
1286
1286
|
":" +
|
|
1287
|
+
(options.indent ?? "") +
|
|
1287
1288
|
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "");
|
|
1288
1289
|
case EnumToken.IfElseConditionTokenType:
|
|
1289
|
-
return renderValue(token.l) +
|
|
1290
|
+
return (renderValue(token.l, options, cache, reducer) +
|
|
1291
|
+
(options.indent ?? "") +
|
|
1292
|
+
renderValue(token.r, options, cache, reducer));
|
|
1290
1293
|
case EnumToken.DeclarationNodeType:
|
|
1291
1294
|
return (token.nam +
|
|
1292
1295
|
":" +
|
|
1293
|
-
(options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1296
|
+
(options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1294
1297
|
case EnumToken.MediaQueryUnaryFeatureTokenType:
|
|
1295
1298
|
return (renderValue(token.l, options, cache, reducer) +
|
|
1296
1299
|
" " +
|
|
1297
|
-
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1300
|
+
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1298
1301
|
case EnumToken.MediaQueryConditionTokenType: {
|
|
1299
1302
|
const indent = token.op.typ == EnumToken.LtTokenType ||
|
|
1300
1303
|
token.op.typ == EnumToken.GtTokenType ||
|
|
@@ -1308,14 +1311,14 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1308
1311
|
indent +
|
|
1309
1312
|
renderValue(token.op, options, cache, reducer) +
|
|
1310
1313
|
indent +
|
|
1311
|
-
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1314
|
+
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1312
1315
|
}
|
|
1313
1316
|
case EnumToken.MediaRangeQueryTokenType:
|
|
1314
|
-
return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
|
|
1315
|
-
renderValue(token.op1) +
|
|
1317
|
+
return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
|
|
1318
|
+
renderValue(token.op1, options, cache, reducer) +
|
|
1316
1319
|
token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
|
|
1317
|
-
renderValue(token.op2) +
|
|
1318
|
-
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1320
|
+
renderValue(token.op2, options, cache, reducer) +
|
|
1321
|
+
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1319
1322
|
case EnumToken.MediaFeatureTokenType:
|
|
1320
1323
|
return token.val;
|
|
1321
1324
|
case EnumToken.NotTokenType:
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
1
|
+
import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js';
|
|
2
2
|
import { minmax, getNumber } from './color.js';
|
|
3
|
-
import { srgbvalues, srgb2lsrgbvalues } from './srgb.js';
|
|
4
|
-
import { srgb2lch, xyz2lchvalues } from './lch.js';
|
|
3
|
+
import { srgbvalues, srgb2lsrgbvalues, hslvalues } from './srgb.js';
|
|
4
|
+
import { srgb2lch, xyz2lchvalues, getLCHComponents } from './lch.js';
|
|
5
5
|
import { srgb2hslvalues } from './hsl.js';
|
|
6
|
+
import { srgb2hwb, hwbvalues } from './hwb.js';
|
|
6
7
|
import { srgb2labvalues } from './lab.js';
|
|
7
8
|
import { srgb2lp3values, srgb2p3values } from './p3.js';
|
|
8
9
|
import { getColorComponents } from './utils/components.js';
|
|
9
|
-
import { srgb2oklch } from './oklch.js';
|
|
10
|
+
import { srgb2oklch, getOKLCHComponents } from './oklch.js';
|
|
10
11
|
import { srgb2oklab } from './oklab.js';
|
|
11
12
|
import { srgb2prophotorgbvalues } from './prophotorgb.js';
|
|
12
13
|
import { srgb2xyz_d65 } from './xyz.js';
|
|
@@ -15,6 +16,7 @@ import { srgb2rec2020values } from './rec2020.js';
|
|
|
15
16
|
import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../syntax.js';
|
|
16
17
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
17
18
|
import { srgb2a98values } from './a98rgb.js';
|
|
19
|
+
import { STATE, colorRange } from '../constants.js';
|
|
18
20
|
|
|
19
21
|
function interpolateHue(interpolationMethod, h1, h2) {
|
|
20
22
|
switch (interpolationMethod) {
|
|
@@ -70,6 +72,7 @@ function colorMix(...args) {
|
|
|
70
72
|
const percentages = [];
|
|
71
73
|
const srgbComponentValues = [];
|
|
72
74
|
const colorComponents = [];
|
|
75
|
+
const colors = [];
|
|
73
76
|
if (args[i]?.typ === EnumToken.IdenTokenType) {
|
|
74
77
|
if (!equalsIgnoreCase(args[i++].val, "in")) {
|
|
75
78
|
return null;
|
|
@@ -119,9 +122,10 @@ function colorMix(...args) {
|
|
|
119
122
|
if (values == null) {
|
|
120
123
|
return null;
|
|
121
124
|
}
|
|
122
|
-
if (colorSpace == "hwb") {
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
+
// if (colorSpace == "hwb") {
|
|
126
|
+
// colorSpace = "srgb";
|
|
127
|
+
// }
|
|
128
|
+
colors.push(processPowerlessComponents(args[i]));
|
|
125
129
|
switch (colorSpace) {
|
|
126
130
|
case "srgb":
|
|
127
131
|
break;
|
|
@@ -151,17 +155,12 @@ function colorMix(...args) {
|
|
|
151
155
|
values = srgb2xyz_d65(values[0], values[1], values[2], values[3]);
|
|
152
156
|
values = XYZ_D65_to_D50(values[0], values[1], values[2], values[3]);
|
|
153
157
|
break;
|
|
154
|
-
// case "rgb":
|
|
155
|
-
// for (let j = 0; j < values.length; j++) {
|
|
156
|
-
// values[j] = j == 3 ? values[j] : srgb2rgb(values[j]);
|
|
157
|
-
// }
|
|
158
|
-
// break;
|
|
159
158
|
case "hsl":
|
|
160
159
|
values = srgb2hslvalues(values[0], values[1], values[2], values[3]);
|
|
161
160
|
break;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
161
|
+
case "hwb":
|
|
162
|
+
values = srgb2hwb(values[0], values[1], values[2], values[3]);
|
|
163
|
+
break;
|
|
165
164
|
case "lab":
|
|
166
165
|
values = srgb2labvalues(values[0], values[1], values[2], values[3]);
|
|
167
166
|
break;
|
|
@@ -223,7 +222,8 @@ function colorMix(...args) {
|
|
|
223
222
|
}
|
|
224
223
|
}
|
|
225
224
|
const stack = [];
|
|
226
|
-
const lchSpaces = ["lch", "oklch"];
|
|
225
|
+
// const lchSpaces: string[] = ["lch", "oklch"];
|
|
226
|
+
const isLCHOrOKLCH = colorSpace === "lch" || colorSpace == "oklch";
|
|
227
227
|
i = srgbComponentValues.length;
|
|
228
228
|
while (i--) {
|
|
229
229
|
stack.push({
|
|
@@ -241,46 +241,73 @@ function colorMix(...args) {
|
|
|
241
241
|
let premult1;
|
|
242
242
|
let premult2;
|
|
243
243
|
let mixedPremult;
|
|
244
|
-
let
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (colorComponents[0][3] != null &&
|
|
250
|
-
colorComponents[0][3].typ == EnumToken.IdenTokenType &&
|
|
251
|
-
colorComponents[0][3].val == "none" &&
|
|
252
|
-
colorComponents[1].length == 4) {
|
|
244
|
+
let offset1 = colors[0].kin == ColorType.COLOR ? 1 : 0;
|
|
245
|
+
if (colorComponents[0][offset1 + 3] != null &&
|
|
246
|
+
colorComponents[0][offset1 + 3].typ == EnumToken.IdenTokenType &&
|
|
247
|
+
colorComponents[0][offset1 + 3].val == "none" &&
|
|
248
|
+
colorComponents[1].length == offset1 + 4) {
|
|
253
249
|
// fix powerless alpha for last color if previous color has alpha
|
|
254
250
|
stack[stack.length - 1].color[3] = stack[stack.length - 2].color[3];
|
|
255
251
|
}
|
|
256
|
-
//
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
252
|
+
// console.error(stack);
|
|
253
|
+
if (stack.length > 1) {
|
|
254
|
+
// powerless hue for lch spaces
|
|
255
|
+
// if (lchSpaces.includes(colorSpace)) {
|
|
256
|
+
// console.error(JSON.stringify({colorSpace1, colorSpace, stack, colorComponents}, null, 1));
|
|
257
|
+
if ((stack[stack.length - 1].color[0] == 0 && colorComponents[0][offset1].typ == EnumToken.IdenTokenType &&
|
|
258
|
+
colorComponents[0][offset1].val == "none") ||
|
|
259
|
+
colorComponents[0][0][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
260
|
+
stack[stack.length - 1].color[0] = stack[stack.length - 2].color[0];
|
|
261
|
+
}
|
|
262
|
+
if ((stack[stack.length - 1].color[1] == 0 && colorComponents[0][offset1 + 1].typ == EnumToken.IdenTokenType &&
|
|
263
|
+
colorComponents[0][offset1 + 1].val == "none") ||
|
|
264
|
+
colorComponents[0][1][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
265
|
+
stack[stack.length - 1].color[1] = stack[stack.length - 2].color[1];
|
|
266
|
+
}
|
|
267
|
+
if ((stack[stack.length - 1].color[2] == 0 && colorComponents[0][offset1 + 2].typ == EnumToken.IdenTokenType &&
|
|
268
|
+
colorComponents[0][offset1 + 2].val == "none") ||
|
|
269
|
+
colorComponents[0][2][STATE] & EnumAstNodeStatus.PowerlessColorComponent ||
|
|
270
|
+
(isLCHOrOKLCH &&
|
|
271
|
+
(stack[stack.length - 1].color[1] == 0 ||
|
|
272
|
+
stack[stack.length - 1].color[0] == 0 ||
|
|
273
|
+
stack[stack.length - 1].color[0] ==
|
|
274
|
+
colorRange[colors[0].kin == ColorType.LCH ? "lch" : "oklch"].l[1]))) {
|
|
262
275
|
stack[stack.length - 1].color[2] = stack[stack.length - 2].color[2];
|
|
263
276
|
}
|
|
277
|
+
// }
|
|
264
278
|
}
|
|
279
|
+
// console.error([colorComponents[0][offset1].typ == EnumToken.IdenTokenType, (colorComponents[0][offset1] as IdentToken).val == "none"]);
|
|
280
|
+
// console.error(isLCHOrOKLCH, colorComponents, stack);
|
|
265
281
|
while (stack.length > 1) {
|
|
266
282
|
r1 = stack.pop();
|
|
267
283
|
r2 = stack.pop();
|
|
284
|
+
offset1 = colors[++currentIndex].kin == ColorType.COLOR ? 1 : 0;
|
|
268
285
|
// r2 powerless alpha
|
|
269
|
-
if (colorComponents[
|
|
270
|
-
colorComponents[currentIndex][3].typ == EnumToken.IdenTokenType &&
|
|
271
|
-
colorComponents[currentIndex][3].val == "none") {
|
|
286
|
+
if (colorComponents[currentIndex][offset1 + 3] != null &&
|
|
287
|
+
colorComponents[currentIndex][offset1 + 3].typ == EnumToken.IdenTokenType &&
|
|
288
|
+
colorComponents[currentIndex][offset1 + 3].val == "none") {
|
|
272
289
|
// fix powerless alpha for last color if previous color has alpha
|
|
273
290
|
r2.color[3] = r1.color[3];
|
|
274
291
|
}
|
|
275
|
-
// @ts-expect-error
|
|
276
|
-
colorSpace1 = ColorType[colorComponents[currentIndex].kin]?.toLowerCase?.();
|
|
277
292
|
// powerless hue for lch spaces
|
|
278
|
-
if (
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
293
|
+
if ((r2.color[0] == 0 && colorComponents[currentIndex][offset1].typ == EnumToken.IdenTokenType &&
|
|
294
|
+
colorComponents[currentIndex][offset1].val == "none") ||
|
|
295
|
+
colorComponents[currentIndex][offset1][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
296
|
+
r2.color[0] = r1.color[0];
|
|
297
|
+
}
|
|
298
|
+
if ((r2.color[1] == 0 && colorComponents[currentIndex][offset1 + 1].typ == EnumToken.IdenTokenType &&
|
|
299
|
+
colorComponents[currentIndex][offset1 + 1].val == "none") ||
|
|
300
|
+
colorComponents[currentIndex][offset1 + 1][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
301
|
+
r2.color[1] = r1.color[1];
|
|
302
|
+
}
|
|
303
|
+
if ((r2.color[2] == 0 && colorComponents[currentIndex][offset1 + 2].typ == EnumToken.IdenTokenType &&
|
|
304
|
+
colorComponents[currentIndex][offset1 + 2].val == "none") ||
|
|
305
|
+
colorComponents[currentIndex][offset1 + 2][STATE] & EnumAstNodeStatus.PowerlessColorComponent ||
|
|
306
|
+
(isLCHOrOKLCH &&
|
|
307
|
+
(r2.color[1] == 0 ||
|
|
308
|
+
r2.color[0] == 0 ||
|
|
309
|
+
r2.color[0] == colorRange[colors[currentIndex].kin == ColorType.LCH ? "lch" : "oklch"].l[1]))) {
|
|
310
|
+
r2.color[2] = r1.color[2];
|
|
284
311
|
}
|
|
285
312
|
if (hueInterpolationMethod != null) {
|
|
286
313
|
let hueIndex = 2;
|
|
@@ -308,6 +335,7 @@ function colorMix(...args) {
|
|
|
308
335
|
alpha: mult,
|
|
309
336
|
};
|
|
310
337
|
}
|
|
338
|
+
// console.error({ r1, r2, r, premult1, mult1, premult2, mult2, mixedPremult, mult });
|
|
311
339
|
stack.push(r);
|
|
312
340
|
}
|
|
313
341
|
const result = stack.pop();
|
|
@@ -362,16 +390,16 @@ function colorMix(...args) {
|
|
|
362
390
|
};
|
|
363
391
|
// case "rgb":
|
|
364
392
|
case "hsl":
|
|
365
|
-
|
|
393
|
+
case "hwb":
|
|
366
394
|
case "lab":
|
|
367
395
|
case "lch":
|
|
368
396
|
case "oklab":
|
|
369
397
|
case "oklch":
|
|
370
|
-
if (colorSpace == "hsl") {
|
|
398
|
+
if (colorSpace == "hsl" || colorSpace == "hwb") {
|
|
371
399
|
// @ts-ignore
|
|
372
400
|
if (values[2] < 0) {
|
|
373
401
|
// @ts-ignore
|
|
374
|
-
values[2] +=
|
|
402
|
+
values[2] += 360;
|
|
375
403
|
}
|
|
376
404
|
}
|
|
377
405
|
else if (colorSpace == "lch" || colorSpace == "oklch") {
|
|
@@ -399,15 +427,71 @@ function colorMix(...args) {
|
|
|
399
427
|
};
|
|
400
428
|
if (colorSpace == "hsl") {
|
|
401
429
|
// @ts-ignore
|
|
402
|
-
result.chi[0] = { typ: EnumToken.AngleTokenType, val: result.chi[0].val
|
|
430
|
+
result.chi[0] = { typ: EnumToken.AngleTokenType, val: result.chi[0].val };
|
|
403
431
|
// @ts-ignore
|
|
404
|
-
result.chi[1] = { typ: EnumToken.PercentageTokenType, val: result.chi[1].val
|
|
432
|
+
result.chi[1] = { typ: EnumToken.PercentageTokenType, val: result.chi[1].val };
|
|
405
433
|
// @ts-ignore
|
|
406
|
-
result.chi[2] = { typ: EnumToken.PercentageTokenType, val: result.chi[2].val
|
|
434
|
+
result.chi[2] = { typ: EnumToken.PercentageTokenType, val: result.chi[2].val };
|
|
407
435
|
}
|
|
408
436
|
return result;
|
|
409
437
|
}
|
|
410
438
|
return null;
|
|
411
439
|
}
|
|
440
|
+
function processPowerlessComponents(color) {
|
|
441
|
+
let components;
|
|
442
|
+
let values;
|
|
443
|
+
switch (color.kin) {
|
|
444
|
+
case ColorType.LCH:
|
|
445
|
+
case ColorType.OKLCH:
|
|
446
|
+
values = color.kin == ColorType.LCH ? getLCHComponents(color) : getOKLCHComponents(color);
|
|
447
|
+
components = getColorComponents(color);
|
|
448
|
+
if (values == null || values.length < 3 || components == null) {
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
const max = color.kin == ColorType.LCH ? colorRange.lch.l[1] : colorRange.oklch.l[1];
|
|
452
|
+
// https://www.w3.org/TR/css-color-4/#specifying-lab-lch
|
|
453
|
+
// https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch
|
|
454
|
+
const epsilon = color.kin == ColorType.LCH ? 0.0015 : 0.000004;
|
|
455
|
+
if (values[0] < epsilon || Math.abs(values[0] - max) < epsilon) {
|
|
456
|
+
Object.assign(components[0], { val: values[0] < epsilon ? 0 : max, typ: EnumToken.NumberTokenType });
|
|
457
|
+
Object.assign(components[1], { val: "none", typ: EnumToken.IdenTokenType });
|
|
458
|
+
Object.assign(components[2], { val: "none", typ: EnumToken.IdenTokenType });
|
|
459
|
+
setColorFlag(components[1], EnumAstNodeStatus.PowerlessColorComponent);
|
|
460
|
+
setColorFlag(components[2], EnumAstNodeStatus.PowerlessColorComponent);
|
|
461
|
+
}
|
|
462
|
+
else if (values[1] < epsilon) {
|
|
463
|
+
values[1] = 0;
|
|
464
|
+
Object.assign(components[1], { val: 0, typ: EnumToken.NumberTokenType });
|
|
465
|
+
Object.assign(components[2], { val: "none", typ: EnumToken.IdenTokenType });
|
|
466
|
+
setColorFlag(components[2], EnumAstNodeStatus.PowerlessColorComponent);
|
|
467
|
+
}
|
|
468
|
+
break;
|
|
469
|
+
case ColorType.HSL:
|
|
470
|
+
case ColorType.HWB:
|
|
471
|
+
values = Object.values((color.kin == ColorType.HSL ? hslvalues(color) : hwbvalues(color)) ?? {});
|
|
472
|
+
components = getColorComponents(color);
|
|
473
|
+
if (values == null || values.length < 3 || components == null) {
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
// https://www.w3.org/TR/css-color-4/#the-hsl-notation
|
|
477
|
+
// https://www.w3.org/TR/css-color-4/#the-hwb-notation
|
|
478
|
+
if ((color.kin == ColorType.HSL && values[1] <= 0.001) ||
|
|
479
|
+
(color.kin == ColorType.HWB && values[1] + values[2] >= 99.999)) {
|
|
480
|
+
Object.assign(components[0], { val: "none", typ: EnumToken.IdenTokenType });
|
|
481
|
+
setColorFlag(components[0], EnumAstNodeStatus.PowerlessColorComponent);
|
|
482
|
+
}
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
// console.error({ color: color.chi ?? color, values });
|
|
486
|
+
return color;
|
|
487
|
+
}
|
|
488
|
+
function setColorFlag(token, flag) {
|
|
489
|
+
if (token[STATE] == null) {
|
|
490
|
+
token[STATE] = flag;
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
token[STATE] |= flag;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
412
496
|
|
|
413
497
|
export { colorMix };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { hwb2hsv } from './hsv.js';
|
|
2
1
|
import { color2srgbvalues, getNumber } from './color.js';
|
|
3
2
|
import { lch2rgbvalues, lab2rgbvalues, cmyk2rgbvalues } from './rgb.js';
|
|
4
3
|
import { getColorComponents } from './utils/components.js';
|
|
5
|
-
import { hex2srgbvalues, oklch2srgbvalues, oklab2srgbvalues
|
|
4
|
+
import { hex2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
|
|
6
5
|
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
6
|
+
import { hwbvalues } from './hwb.js';
|
|
7
7
|
|
|
8
8
|
function hex2HslToken(token) {
|
|
9
9
|
let values = hex2srgbvalues(token);
|
|
@@ -69,11 +69,17 @@ function color2HslToken(token) {
|
|
|
69
69
|
return hslToken(srgb2hslvalues(values[0], values[1], values[2], values[3]));
|
|
70
70
|
}
|
|
71
71
|
function hslToken(values) {
|
|
72
|
-
values[
|
|
72
|
+
if (values[1] == 0) {
|
|
73
|
+
values[0] = 0;
|
|
74
|
+
}
|
|
75
|
+
if (values[2] == 1 || values[2] == 0) {
|
|
76
|
+
values[0] = 0;
|
|
77
|
+
values[1] = 0;
|
|
78
|
+
}
|
|
73
79
|
const chi = [
|
|
74
80
|
{ typ: EnumToken.NumberTokenType, val: values[0] },
|
|
75
|
-
{ typ: EnumToken.PercentageTokenType, val: values[1]
|
|
76
|
-
{ typ: EnumToken.PercentageTokenType, val: values[2]
|
|
81
|
+
{ typ: EnumToken.PercentageTokenType, val: values[1] },
|
|
82
|
+
{ typ: EnumToken.PercentageTokenType, val: values[2] },
|
|
77
83
|
];
|
|
78
84
|
if (values.length == 4 && values[3] != 1) {
|
|
79
85
|
chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
|
|
@@ -116,32 +122,33 @@ function rgb2hslvalues(token) {
|
|
|
116
122
|
}
|
|
117
123
|
return rgbvalues2hslvalues(values[0], values[1], values[2], values[3]);
|
|
118
124
|
}
|
|
119
|
-
// https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
|
|
120
|
-
function hsv2hsl(h, s, v, a) {
|
|
121
|
-
const result = [
|
|
122
|
-
//[hue, saturation, lightness]
|
|
123
|
-
//Range should be between 0 - 1
|
|
124
|
-
h, //Hue stays the same
|
|
125
|
-
//Saturation is very different between the two color spaces
|
|
126
|
-
//If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
|
|
127
|
-
//Otherwise sat*val/(2-(2-sat)*val)
|
|
128
|
-
//Conditional is not operating with hue, it is reassigned!
|
|
129
|
-
(s * v) / ((h = (2 - s) * v) < 1 ? h : 2 - h),
|
|
130
|
-
h / 2, //Lightness is (2-sat)*val/2
|
|
131
|
-
];
|
|
132
|
-
if (a != null) {
|
|
133
|
-
result.push(a);
|
|
134
|
-
}
|
|
135
|
-
return result;
|
|
136
|
-
}
|
|
137
125
|
function cmyk2hslvalues(token) {
|
|
138
126
|
const values = cmyk2rgbvalues(token);
|
|
139
127
|
return values == null ? null : rgbvalues2hslvalues(values[0], values[1], values[2], values[3]);
|
|
140
128
|
}
|
|
141
129
|
function hwb2hslvalues(token) {
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
130
|
+
const { h, w, b, a } = hwbvalues(token) ?? {};
|
|
131
|
+
if (h == null || w == null || b == null) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const wNorm = w / 100;
|
|
135
|
+
const bNorm = b / 100;
|
|
136
|
+
const hue = ((h % 360) + 360) % 360;
|
|
137
|
+
if (wNorm + bNorm >= 1) {
|
|
138
|
+
const grayLightness = wNorm / (wNorm + bNorm);
|
|
139
|
+
return [hue, 0, grayLightness * 100];
|
|
140
|
+
}
|
|
141
|
+
const lightness = (1 - bNorm + wNorm) / 2;
|
|
142
|
+
let saturation = 0;
|
|
143
|
+
if (lightness > 0 && lightness < 1) {
|
|
144
|
+
const chroma = 1 - wNorm - bNorm;
|
|
145
|
+
saturation = chroma / (1 - Math.abs(2 * lightness - 1));
|
|
146
|
+
}
|
|
147
|
+
const hsl = [hue, saturation * 100, lightness * 100];
|
|
148
|
+
if (a != null && a != 1) {
|
|
149
|
+
hsl.push(a);
|
|
150
|
+
}
|
|
151
|
+
return hsl;
|
|
145
152
|
}
|
|
146
153
|
function lab2hslvalues(token) {
|
|
147
154
|
const values = lab2rgbvalues(token);
|
|
@@ -172,28 +179,28 @@ function rgbvalues2hslvalues(r, g, b, a = null) {
|
|
|
172
179
|
return srgb2hslvalues(r / 255, g / 255, b / 255, a);
|
|
173
180
|
}
|
|
174
181
|
function srgb2hslvalues(r, g, b, a = null) {
|
|
175
|
-
|
|
176
|
-
|
|
182
|
+
const max = Math.max(r, g, b);
|
|
183
|
+
const min = Math.min(r, g, b);
|
|
184
|
+
const delta = max - min;
|
|
185
|
+
const l = (max + min) / 2;
|
|
177
186
|
let h = 0;
|
|
178
187
|
let s = 0;
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
let d = max - min;
|
|
182
|
-
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
188
|
+
if (delta !== 0) {
|
|
189
|
+
s = delta / (1 - Math.abs(2 * l - 1));
|
|
183
190
|
switch (max) {
|
|
184
191
|
case r:
|
|
185
|
-
h = (g - b) /
|
|
192
|
+
h = 60 * (((g - b) / delta) % 6);
|
|
186
193
|
break;
|
|
187
194
|
case g:
|
|
188
|
-
h = (b - r) /
|
|
195
|
+
h = 60 * ((b - r) / delta + 2);
|
|
189
196
|
break;
|
|
190
197
|
case b:
|
|
191
|
-
h = (r - g) /
|
|
198
|
+
h = 60 * ((r - g) / delta + 4);
|
|
192
199
|
break;
|
|
193
200
|
}
|
|
194
|
-
h /= 6;
|
|
195
201
|
}
|
|
196
|
-
|
|
202
|
+
h = ((h % 360) + 360) % 360;
|
|
203
|
+
const hsl = [h, s * 100, l * 100];
|
|
197
204
|
if (a != null && a < 1) {
|
|
198
205
|
// @ts-ignore
|
|
199
206
|
return hsl.concat([a]);
|
|
@@ -202,4 +209,4 @@ function srgb2hslvalues(r, g, b, a = null) {
|
|
|
202
209
|
return hsl;
|
|
203
210
|
}
|
|
204
211
|
|
|
205
|
-
export { cmyk2HslToken, cmyk2hslvalues, color2HslToken, hex2HslToken,
|
|
212
|
+
export { cmyk2HslToken, cmyk2hslvalues, color2HslToken, hex2HslToken, hwb2HslToken, hwb2hslvalues, lab2HslToken, lab2hslvalues, lch2HslToken, lch2hslvalues, oklab2HslToken, oklab2hslvalues, oklch2HslToken, oklch2hslvalues, rgb2HslToken, rgb2hslvalues, rgbvalues2hslvalues, srgb2hslvalues };
|
|
@@ -2,19 +2,5 @@ function hwb2hsv(h, w, b, a) {
|
|
|
2
2
|
// @ts-ignore
|
|
3
3
|
return [h, 1 - w / (1 - b), 1 - b, a];
|
|
4
4
|
}
|
|
5
|
-
// https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
|
|
6
|
-
function hsl2hsv(h, s, l, a = null) {
|
|
7
|
-
s *= l < .5 ? l : 1 - l;
|
|
8
|
-
const result = [
|
|
9
|
-
//Range should be between 0 - 1
|
|
10
|
-
h, //Hue stays the same
|
|
11
|
-
2 * s / (l + s), //Saturation
|
|
12
|
-
l + s //Value
|
|
13
|
-
];
|
|
14
|
-
if (a != null) {
|
|
15
|
-
result.push(a);
|
|
16
|
-
}
|
|
17
|
-
return result;
|
|
18
|
-
}
|
|
19
5
|
|
|
20
|
-
export {
|
|
6
|
+
export { hwb2hsv };
|