@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.
@@ -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
- colorSpace = "srgb";
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
- // case "hwb":
163
- // values = srgb2hwb(values[0], values[1], values[2], values[3]);
164
- // break;
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 colorSpace1 = ColorType[
245
- // @ts-expect-error
246
- colorComponents.at(-1).kin
247
- // @ts-expect-error
248
- ]?.toLowerCase?.();
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
- // powerless hue for lch spaces
257
- if (lchSpaces.includes(colorSpace1) || lchSpaces.includes(colorSpace)) {
258
- if ((stack.length > 1 &&
259
- colorComponents[0][2].typ == EnumToken.IdenTokenType &&
260
- colorComponents[0][2].val == "none") ||
261
- stack[stack.length - 1].color[2] == 0) {
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[++currentIndex][3] != null &&
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 (lchSpaces.includes(colorSpace1) || lchSpaces.includes(colorSpace)) {
279
- if ((colorComponents[currentIndex][2].typ == EnumToken.IdenTokenType &&
280
- colorComponents[currentIndex][2].val == "none") ||
281
- r2.color[2] == 0) {
282
- r2.color[2] = r1.color[2];
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
- // case "hwb":
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] += 1;
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 * 360 };
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 * 100 };
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 * 100 };
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 };
@@ -582,7 +582,6 @@ function getAngle(token) {
582
582
  return +token.val;
583
583
  }
584
584
  }
585
- // @ts-ignore
586
585
  return token.val / 360;
587
586
  }
588
587
 
@@ -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, hslvalues } from './srgb.js';
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[0] = values[0] * 360;
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] * 100 },
76
- { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
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 hsla = hslvalues(token);
143
- const hwba = hwb2hsv(hsla.h, hsla.s, hsla.l, hsla.a);
144
- return hsv2hsl(hwba[0], hwba[1], hwba[2], hwba[3]);
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
- let max = Math.max(r, g, b);
176
- let min = Math.min(r, g, b);
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
- let l = (max + min) / 2;
180
- if (max != min) {
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) / d + (g < b ? 6 : 0);
192
+ h = 60 * (((g - b) / delta) % 6);
186
193
  break;
187
194
  case g:
188
- h = (b - r) / d + 2;
195
+ h = 60 * ((b - r) / delta + 2);
189
196
  break;
190
197
  case b:
191
- h = (r - g) / d + 4;
198
+ h = 60 * ((r - g) / delta + 4);
192
199
  break;
193
200
  }
194
- h /= 6;
195
201
  }
196
- const hsl = [h, s, l];
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, hsv2hsl, hwb2HslToken, hwb2hslvalues, lab2HslToken, lab2hslvalues, lch2HslToken, lch2hslvalues, oklab2HslToken, oklab2hslvalues, oklch2HslToken, oklch2hslvalues, rgb2HslToken, rgb2hslvalues, rgbvalues2hslvalues, srgb2hslvalues };
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 { hsl2hsv, hwb2hsv };
6
+ export { hwb2hsv };
@@ -1,8 +1,7 @@
1
- import { hsl2hsv } from './hsv.js';
2
1
  import { getColorComponents } from './utils/components.js';
3
- import { color2srgbvalues, getAngle, getNumber } from './color.js';
2
+ import { color2srgbvalues, getNumber, getAngle } from './color.js';
4
3
  import { EnumToken, ColorType } from '../../ast/types.js';
5
- import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
4
+ import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hslvalues } from './srgb.js';
6
5
 
7
6
  function rgb2hwbToken(token) {
8
7
  const values = rgb2hwbvalues(token);
@@ -16,6 +15,7 @@ function hsl2hwbToken(token) {
16
15
  if (values == null) {
17
16
  return null;
18
17
  }
18
+ // console.error(new Error("hsl2hwbToken"));
19
19
  return hwbToken(values);
20
20
  }
21
21
  function cmyk2hwbToken(token) {
@@ -61,11 +61,10 @@ function color2hwbToken(token) {
61
61
  return hwbToken(values);
62
62
  }
63
63
  function hwbToken(values) {
64
- values[0] = values[0] * 360;
65
64
  const chi = [
66
65
  { typ: EnumToken.NumberTokenType, val: values[0] },
67
- { typ: EnumToken.PercentageTokenType, val: values[1] * 100 },
68
- { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
66
+ { typ: EnumToken.PercentageTokenType, val: values[1] },
67
+ { typ: EnumToken.PercentageTokenType, val: values[2] },
69
68
  ];
70
69
  if (values.length == 4) {
71
70
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
@@ -95,17 +94,12 @@ function cmyk2hwbvalues(token) {
95
94
  return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
96
95
  }
97
96
  function hsl2hwbvalues(token) {
98
- const values = getColorComponents(token).map((t, index) => {
99
- if (index == 3 && t.typ == EnumToken.IdenTokenType && t.val == "none") {
100
- return 1;
101
- }
102
- if (index == 0) {
103
- return getAngle(t);
104
- }
105
- return getNumber(t);
106
- });
97
+ const values = hslvalues(token);
98
+ if (values == null) {
99
+ return null;
100
+ }
107
101
  // @ts-ignore
108
- return hslvalues2hwbvalues(values[0], values[1], values[2], values[3]);
102
+ return hslvalues2hwbvalues(values.h, values.s, values.l, values.a);
109
103
  }
110
104
  function lab2hwbvalues(token) {
111
105
  const values = lab2srgbvalues(token);
@@ -134,26 +128,6 @@ function oklch2hwbvalues(token) {
134
128
  // @ts-ignore
135
129
  return values == null ? null : srgb2hwb(values[0], values[1], values[2], values[3]);
136
130
  }
137
- function rgb2hue(r, g, b, fallback = 0) {
138
- let value = rgb2value(r, g, b);
139
- let whiteness = rgb2whiteness(r, g, b);
140
- let delta = value - whiteness;
141
- if (delta > 0) {
142
- // calculate segment
143
- let segment = value === r ? (g - b) / delta : value === g ? (b - r) / delta : (r - g) / delta;
144
- // calculate shift
145
- let shift = value === r ? (segment < 0 ? 360 / 60 : 0 / 60) : value === g ? 120 / 60 : 240 / 60;
146
- // calculate hue
147
- return (segment + shift) * 60;
148
- }
149
- return fallback;
150
- }
151
- function rgb2value(r, g, b) {
152
- return Math.max(r, g, b);
153
- }
154
- function rgb2whiteness(r, g, b) {
155
- return Math.min(r, g, b);
156
- }
157
131
  function color2hwbvalues(token) {
158
132
  const values = color2srgbvalues(token);
159
133
  if (values == null) {
@@ -162,31 +136,87 @@ function color2hwbvalues(token) {
162
136
  // @ts-ignore
163
137
  return srgb2hwb(values[0], values[1], values[2], values[3]);
164
138
  }
165
- function srgb2hwb(r, g, b, a = null, fallback = 0) {
166
- r *= 100;
167
- g *= 100;
139
+ function hwbvalues(token) {
140
+ const components = getColorComponents(token);
141
+ if (components == null) {
142
+ return null;
143
+ }
144
+ let t;
145
+ // @ts-ignore
146
+ let h = getAngle(components[0]) * 360;
147
+ // @ts-ignore
148
+ t = components[1];
149
+ // @ts-ignore
150
+ let w = getNumber(t);
151
+ // @ts-ignore
152
+ t = components[2];
153
+ // @ts-ignore
154
+ let b = getNumber(t);
155
+ // if (components[1].typ == EnumToken.NumberTokenType) {
156
+ w *= 100;
157
+ // }
158
+ // if (components[2].typ == EnumToken.NumberTokenType) {
168
159
  b *= 100;
169
- let hue = rgb2hue(r, g, b, fallback);
170
- let whiteness = rgb2whiteness(r, g, b);
171
- let value = Math.round(rgb2value(r, g, b));
172
- let blackness = 100 - value;
173
- const result = [hue / 360, whiteness / 100, blackness / 100];
174
- if (a != null) {
175
- result.push(a);
160
+ // }
161
+ let a = null;
162
+ if (components.length == 4) {
163
+ // @ts-ignore
164
+ t = components[3];
165
+ // @ts-ignore
166
+ a = getNumber(t);
176
167
  }
177
- return result;
168
+ return a == null ? { h, w, b } : { h, w, b, a };
178
169
  }
179
- function hsv2hwb(h, s, v, a = null) {
180
- const result = [h, (1 - s) * v, 1 - v];
170
+ function srgb2hwb(r, g, b, a = null, fallback = 0) {
171
+ // 1. Clamp inputs to the standard web-safe sRGB gamut [0.0, 1.0]
172
+ const rClamp = Math.min(1, Math.max(0, r));
173
+ const gClamp = Math.min(1, Math.max(0, g));
174
+ const bClamp = Math.min(1, Math.max(0, b));
175
+ const cMax = Math.max(rClamp, gClamp, bClamp);
176
+ const cMin = Math.min(rClamp, gClamp, bClamp);
177
+ const delta = cMax - cMin;
178
+ // 2. Whiteness and Blackness calculations
179
+ const w = cMin;
180
+ const bl = 1 - cMax;
181
+ // 3. Calculate Hue
182
+ let h = 0;
183
+ if (delta !== 0) {
184
+ if (cMax === rClamp) {
185
+ h = 60 * (((gClamp - bClamp) / delta) % 6);
186
+ }
187
+ else if (cMax === gClamp) {
188
+ h = 60 * ((bClamp - rClamp) / delta + 2);
189
+ }
190
+ else if (cMax === bClamp) {
191
+ h = 60 * ((rClamp - gClamp) / delta + 4);
192
+ }
193
+ if (h < 0) {
194
+ h += 360;
195
+ }
196
+ }
197
+ const result = [h, w * 100, bl * 100];
181
198
  if (a != null) {
182
199
  result.push(a);
183
200
  }
184
201
  return result;
185
202
  }
186
203
  function hslvalues2hwbvalues(h, s, l, a = null) {
187
- let values = hsl2hsv(h, s, l);
188
- // @ts-ignore
189
- return hsv2hwb(values[0], values[1], values[2], a);
204
+ const sNorm = s / 100;
205
+ const lNorm = l / 100;
206
+ // Keep Hue cleanly bound to standard circle loops
207
+ const hue = ((h % 360) + 360) % 360;
208
+ // 1. Compute the intermediate HSV Value component (brightness peak)
209
+ const v = lNorm + sNorm * Math.min(lNorm, 1 - lNorm);
210
+ // 2. Compute Chroma
211
+ const chroma = 2 * (v - lNorm);
212
+ // 3. Extract Whiteness and Blackness
213
+ const w = v - chroma;
214
+ const b = 1 - v;
215
+ const hwb = [hue, w * 100, b * 100];
216
+ if (a != null && a != 1) {
217
+ hwb.push(a);
218
+ }
219
+ return hwb;
190
220
  }
191
221
 
192
- export { cmyk2hwbToken, cmyk2hwbvalues, color2hwbToken, color2hwbvalues, hsl2hwbToken, hsl2hwbvalues, hslvalues2hwbvalues, hsv2hwb, hwbToken, lab2hwbToken, lab2hwbvalues, lch2hwbToken, lch2hwbvalues, oklab2hwbToken, oklab2hwbvalues, oklch2hwbToken, oklch2hwbvalues, rgb2hwbToken, rgb2hwbvalues, srgb2hwb };
222
+ export { cmyk2hwbToken, cmyk2hwbvalues, color2hwbToken, color2hwbvalues, hsl2hwbToken, hsl2hwbvalues, hslvalues2hwbvalues, hwbToken, hwbvalues, lab2hwbToken, lab2hwbvalues, lch2hwbToken, lch2hwbvalues, oklab2hwbToken, oklab2hwbvalues, oklch2hwbToken, oklch2hwbvalues, rgb2hwbToken, rgb2hwbvalues, srgb2hwb };
@@ -75,7 +75,7 @@ function oklabToken(values) {
75
75
  { typ: EnumToken.NumberTokenType, val: values[1] },
76
76
  { typ: EnumToken.NumberTokenType, val: values[2] },
77
77
  ];
78
- if (values.length == 4) {
78
+ if (values.length == 4 && values[3] != 1) {
79
79
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
80
80
  typ: EnumToken.PercentageTokenType,
81
81
  val: values[3] * 100,