@tbela99/css-parser 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,16 @@
1
- import { getAngle, COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';
1
+ import { getAngle, clamp, COLORS_NAMES, NAMES_COLORS } from './utils/color.js';
2
+ import { rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex } from './utils/hex.js';
2
3
  import { EnumToken } from '../ast/types.js';
3
4
  import '../ast/minify.js';
4
5
  import { expand } from '../ast/expand.js';
5
6
  import { SourceMap } from './sourcemap/sourcemap.js';
6
7
  import '../parser/parse.js';
7
- import { isNewLine } from '../parser/utils/syntax.js';
8
+ import { isColor, isNewLine } from '../parser/utils/syntax.js';
9
+ import { parseRelativeColor } from './utils/calccolor.js';
8
10
 
9
11
  const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
10
12
  function reduceNumber(val) {
11
- val = (+val).toString();
13
+ val = String(+val);
12
14
  if (val === '0') {
13
15
  return '0';
14
16
  }
@@ -47,7 +49,7 @@ function doRender(data, options = {}) {
47
49
  newLine: '\n',
48
50
  compress: false,
49
51
  removeComments: false,
50
- }), sourcemap: false, colorConvert: true, expandNestingRules: false, preserveLicense: false, ...options
52
+ }), sourcemap: false, convertColor: true, expandNestingRules: false, preserveLicense: false, ...options
51
53
  };
52
54
  const startTime = performance.now();
53
55
  const errors = [];
@@ -199,6 +201,19 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
199
201
  return acc + renderToken(curr, options, cache, reducer, errors);
200
202
  };
201
203
  }
204
+ if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes(token.val)) {
205
+ if (isColor(token)) {
206
+ // @ts-ignore
207
+ token.typ = EnumToken.ColorTokenType;
208
+ if (token.chi[0].typ == EnumToken.IdenTokenType && token.chi[0].val == 'from') {
209
+ // @ts-ignore
210
+ token.cal = 'rel';
211
+ }
212
+ else {
213
+ token.chi = token.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommaTokenType, EnumToken.CommentTokenType].includes(t.typ));
214
+ }
215
+ }
216
+ }
202
217
  switch (token.typ) {
203
218
  case EnumToken.ListToken:
204
219
  return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
@@ -241,10 +256,46 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
241
256
  case EnumToken.Div:
242
257
  return '/';
243
258
  case EnumToken.ColorTokenType:
244
- if (options.colorConvert) {
245
- if (token.kin == 'lit' && token.val.toLowerCase() == 'currentcolor') {
259
+ if (options.convertColor) {
260
+ if (token.cal == 'rel' && ['rgb', 'hsl', 'hwb'].includes(token.val)) {
261
+ const chi = token.chi.filter(x => ![
262
+ EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType
263
+ ].includes(x.typ));
264
+ const components = parseRelativeColor(token.val.split(''), chi[1], chi[2], chi[3], chi[4], chi[5]);
265
+ if (components != null) {
266
+ token.chi = Object.values(components);
267
+ delete token.cal;
268
+ }
269
+ }
270
+ if (token.cal) {
271
+ let slice = false;
272
+ if (token.cal == 'rel') {
273
+ const last = token.chi.at(-1);
274
+ if ((last.typ == EnumToken.NumberTokenType && last.val == '1') || (last.typ == EnumToken.IdenTokenType && last.val == 'none')) {
275
+ const prev = token.chi.at(-2);
276
+ if (prev.typ == EnumToken.LiteralTokenType && prev.val == '/') {
277
+ slice = true;
278
+ }
279
+ }
280
+ }
281
+ return clamp(token).val + '(' + (slice ? token.chi.slice(0, -2) : token.chi).reduce((acc, curr) => {
282
+ const val = renderToken(curr, options, cache);
283
+ if ([EnumToken.LiteralTokenType, EnumToken.CommaTokenType].includes(curr.typ)) {
284
+ return acc + val;
285
+ }
286
+ if (acc.length > 0) {
287
+ return acc + (['/', ','].includes(acc.at(-1)) ? '' : ' ') + val;
288
+ }
289
+ return val;
290
+ }, '') + ')';
291
+ }
292
+ if (token.kin == 'lit' && token.val.localeCompare('currentcolor', undefined, { sensitivity: 'base' }) == 0) {
246
293
  return 'currentcolor';
247
294
  }
295
+ clamp(token);
296
+ if (Array.isArray(token.chi) && token.chi.some((t) => t.typ == EnumToken.FunctionTokenType || (t.typ == EnumToken.ColorTokenType && Array.isArray(t.chi)))) {
297
+ return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr) => acc + (acc.length > 0 && !(acc.endsWith('/') || curr.typ == EnumToken.LiteralTokenType) ? ' ' : '') + renderToken(curr, options, cache), '') + ')';
298
+ }
248
299
  let value = token.kin == 'hex' ? token.val.toLowerCase() : (token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : '');
249
300
  if (token.val == 'rgb' || token.val == 'rgba') {
250
301
  value = rgb2Hex(token);
@@ -281,13 +332,17 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
281
332
  if (token.kin == 'hex' || token.kin == 'lit') {
282
333
  return token.val;
283
334
  }
335
+ if (Array.isArray(token.chi)) {
336
+ return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr) => acc + (acc.length > 0 && !(acc.endsWith('/') || curr.typ == EnumToken.LiteralTokenType) ? ' ' : '') + renderToken(curr, options, cache), '') + ')';
337
+ }
284
338
  case EnumToken.ParensTokenType:
285
339
  case EnumToken.FunctionTokenType:
286
340
  case EnumToken.UrlFunctionTokenType:
287
341
  case EnumToken.ImageFunctionTokenType:
288
- case EnumToken.PseudoClassFuncTokenType:
289
342
  case EnumToken.TimingFunctionTokenType:
343
+ case EnumToken.PseudoClassFuncTokenType:
290
344
  case EnumToken.TimelineFunctionTokenType:
345
+ case EnumToken.GridTemplateFuncTokenType:
291
346
  if (token.typ == EnumToken.FunctionTokenType &&
292
347
  token.val == 'calc' &&
293
348
  token.chi.length == 1 &&
@@ -350,6 +405,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
350
405
  case EnumToken.ImportantTokenType:
351
406
  return '!important';
352
407
  case EnumToken.AttrTokenType:
408
+ case EnumToken.IdenListTokenType:
353
409
  return '[' + token.chi.reduce(reducer, '') + ']';
354
410
  case EnumToken.TimeTokenType:
355
411
  case EnumToken.AngleTokenType:
@@ -416,10 +472,23 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
416
472
  }
417
473
  return '0';
418
474
  }
475
+ if (token.typ == EnumToken.TimeTokenType) {
476
+ if (unit == 'ms') {
477
+ // @ts-ignore
478
+ const v = reduceNumber(val / 1000);
479
+ if (v.length + 1 <= val.length) {
480
+ return v + 's';
481
+ }
482
+ return val + 'ms';
483
+ }
484
+ return val + 's';
485
+ }
419
486
  return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
487
+ case EnumToken.FlexTokenType:
420
488
  case EnumToken.PercentageTokenType:
489
+ const uni = token.typ == EnumToken.PercentageTokenType ? '%' : 'fr';
421
490
  const perc = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
422
- return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', '%/') : perc + '%');
491
+ return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', uni + '/') : perc + uni);
423
492
  case EnumToken.NumberTokenType:
424
493
  return token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
425
494
  case EnumToken.CommentTokenType:
@@ -0,0 +1,238 @@
1
+ import { COLORS_NAMES, getNumber, getAngle } from './color.js';
2
+ import { EnumToken } from '../../ast/types.js';
3
+ import '../../ast/minify.js';
4
+ import { walkValues } from '../../ast/walk.js';
5
+ import '../../parser/parse.js';
6
+ import { reduceNumber } from '../render.js';
7
+ import { hwb2rgb, hsl2rgb } from './rgb.js';
8
+ import { rgb2hwb, hsl2hwb } from './hwb.js';
9
+ import { rgb2hsl, hwb2hsl } from './hsl.js';
10
+ import { evaluate } from '../../ast/math/expression.js';
11
+
12
+ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
13
+ const type = relativeKeys.join('');
14
+ let r;
15
+ let g;
16
+ let b;
17
+ let alpha = null;
18
+ let keys = {};
19
+ let values = {};
20
+ let children;
21
+ switch (original.kin) {
22
+ case 'lit':
23
+ case 'hex':
24
+ let value = original.val.toLowerCase();
25
+ if (original.kin == 'lit') {
26
+ if (original.val.toLowerCase() in COLORS_NAMES) {
27
+ value = COLORS_NAMES[original.val.toLowerCase()];
28
+ }
29
+ else {
30
+ return null;
31
+ }
32
+ }
33
+ if (value.length == 4) {
34
+ value = '#' + value[1] + value[1] + value[2] + value[2] + value[3] + value[3];
35
+ }
36
+ else if (value.length == 5) {
37
+ value = '#' + value[1] + value[1] + value[2] + value[2] + value[3] + value[3] + value[4] + value[4];
38
+ }
39
+ r = parseInt(value.slice(1, 3), 16);
40
+ g = parseInt(value.slice(3, 5), 16);
41
+ b = parseInt(value.slice(5, 7), 16);
42
+ alpha = value.length == 9 ? parseInt(value.slice(7, 9), 16) : null;
43
+ break;
44
+ case 'rgb':
45
+ case 'rgba':
46
+ children = original.chi.filter((t) => t.typ == EnumToken.NumberTokenType || t.typ == EnumToken.IdenTokenType || t.typ == EnumToken.PercentageTokenType);
47
+ if (children.every((t) => (t.typ == EnumToken.IdenTokenType && t.val == 'none') || t.typ == EnumToken.NumberTokenType)) {
48
+ r = children[0].typ == EnumToken.IdenTokenType && children[0].val == 'none' ? 0 : +children[0].val;
49
+ g = children[1].typ == EnumToken.IdenTokenType && children[1].val == 'none' ? 0 : +children[1].val;
50
+ b = children[2].typ == EnumToken.IdenTokenType && children[2].val == 'none' ? 0 : +children[2].val;
51
+ alpha = children.length < 4 ? null : children[3].typ == EnumToken.IdenTokenType && children[3].val == 'none' ? 0 : +children[3].val;
52
+ }
53
+ else if (children.every((t) => t.typ == EnumToken.PercentageTokenType || (t.typ == EnumToken.IdenTokenType && t.val == 'none') || (t.typ == EnumToken.NumberTokenType && t.val == '0'))) {
54
+ // @ts-ignore
55
+ r = children[0].typ == EnumToken.IdenTokenType && children[0].val == 'none' ? 0 : children[0].val * 255 / 100;
56
+ // @ts-ignore
57
+ g = children[1].typ == EnumToken.IdenTokenType && children[1].val == 'none' ? 0 : children[1].val * 255 / 100;
58
+ // @ts-ignore
59
+ b = children[2].typ == EnumToken.IdenTokenType && children[2].val == 'none' ? 0 : children[2].val * 255 / 100;
60
+ alpha = children.length < 4 ? null : children[3].typ == EnumToken.IdenTokenType && children[3].val == 'none' ? 0 : +children[3].val / 100;
61
+ }
62
+ else {
63
+ return null;
64
+ }
65
+ break;
66
+ case 'hsl':
67
+ case 'hsla':
68
+ case 'hwb':
69
+ children = original.chi.filter((t) => t.typ == EnumToken.AngleTokenType || t.typ == EnumToken.NumberTokenType || t.typ == EnumToken.IdenTokenType || t.typ == EnumToken.PercentageTokenType);
70
+ if (children.length == 3 || children.length == 4) {
71
+ [r, g, b, alpha] = children;
72
+ }
73
+ else {
74
+ return null;
75
+ }
76
+ break;
77
+ default:
78
+ return null;
79
+ }
80
+ const from = ['rgb', 'rgba', 'hex', 'lit'].includes(original.kin) ? 'rgb' : original.kin;
81
+ if (from != type) {
82
+ if (type == 'hsl' || type == 'hwb') {
83
+ if (from == 'rgb') {
84
+ [r, g, b] = (type == 'hwb' ? rgb2hwb : rgb2hsl)(r, g, b);
85
+ // @ts-ignore
86
+ r *= 360;
87
+ // @ts-ignore
88
+ g *= 100;
89
+ // @ts-ignore
90
+ b *= 100;
91
+ values = {
92
+ [relativeKeys[0]]: { typ: EnumToken.AngleTokenType, val: r, unit: 'deg' },
93
+ [relativeKeys[1]]: { typ: EnumToken.PercentageTokenType, val: g },
94
+ [relativeKeys[2]]: { typ: EnumToken.PercentageTokenType, val: b }
95
+ };
96
+ }
97
+ else if (from == 'hwb' || from == 'hsl') {
98
+ if (type == 'hsl') {
99
+ if (from == 'hwb') {
100
+ [r, g, b] = hwb2hsl(getAngle(r), getNumber(g), getNumber(b));
101
+ // @ts-ignore
102
+ r *= 360;
103
+ // @ts-ignore
104
+ g *= 100;
105
+ // @ts-ignore
106
+ b *= 100;
107
+ // @ts-ignore
108
+ values = {
109
+ [relativeKeys[0]]: { typ: EnumToken.AngleTokenType, val: r, unit: 'deg' },
110
+ [relativeKeys[1]]: { typ: EnumToken.PercentageTokenType, val: g },
111
+ [relativeKeys[2]]: { typ: EnumToken.PercentageTokenType, val: b }
112
+ };
113
+ }
114
+ }
115
+ else if (type == 'hwb') {
116
+ if (from == 'hsl') {
117
+ [r, g, b] = hsl2hwb(getAngle(r), getNumber(g), getNumber(b));
118
+ // @ts-ignore
119
+ r *= 360;
120
+ // @ts-ignore
121
+ g *= 100;
122
+ // @ts-ignore
123
+ b *= 100;
124
+ // @ts-ignore
125
+ values = {
126
+ [relativeKeys[0]]: { typ: EnumToken.AngleTokenType, val: r, unit: 'deg' },
127
+ [relativeKeys[1]]: { typ: EnumToken.PercentageTokenType, val: g },
128
+ [relativeKeys[2]]: { typ: EnumToken.PercentageTokenType, val: b }
129
+ };
130
+ }
131
+ }
132
+ }
133
+ else {
134
+ return null;
135
+ }
136
+ }
137
+ else if (type == 'rgb') {
138
+ if (from == 'hsl' || from == 'hwb') {
139
+ [r, g, b] = (from == 'hwb' ? hwb2rgb : hsl2rgb)(getAngle(r), getNumber(g), getNumber(b));
140
+ // @ts-ignore
141
+ values = {
142
+ [relativeKeys[0]]: { typ: EnumToken.NumberTokenType, val: r },
143
+ [relativeKeys[1]]: { typ: EnumToken.NumberTokenType, val: g },
144
+ [relativeKeys[2]]: { typ: EnumToken.NumberTokenType, val: b }
145
+ };
146
+ }
147
+ else {
148
+ return null;
149
+ }
150
+ }
151
+ }
152
+ else {
153
+ values = {
154
+ [relativeKeys[0]]: r,
155
+ [relativeKeys[1]]: g,
156
+ [relativeKeys[2]]: b
157
+ };
158
+ }
159
+ if (aExp != null && aExp.typ == EnumToken.IdenTokenType && aExp.val == 'none') {
160
+ aExp = null;
161
+ }
162
+ keys = {
163
+ [relativeKeys[0]]: rExp,
164
+ [relativeKeys[1]]: gExp,
165
+ [relativeKeys[2]]: bExp,
166
+ alpha: aExp ?? { typ: EnumToken.IdenTokenType, val: 'alpha' }
167
+ };
168
+ // @ts-ignore
169
+ values.alpha = alpha != null && typeof alpha == 'object' ? alpha : b.typ == EnumToken.PercentageTokenType ? { typ: EnumToken.PercentageTokenType, val: String(alpha ?? 100) } : { typ: EnumToken.NumberTokenType, val: String(alpha ?? 1) };
170
+ return computeComponentValue(keys, values);
171
+ }
172
+ function computeComponentValue(expr, values) {
173
+ for (const [key, exp] of Object.entries(expr)) {
174
+ if (exp == null) {
175
+ if (key in values) {
176
+ if (typeof values[key] == 'number') {
177
+ expr[key] = {
178
+ typ: EnumToken.NumberTokenType,
179
+ val: reduceNumber(values[key])
180
+ };
181
+ }
182
+ else {
183
+ expr[key] = values[key];
184
+ }
185
+ }
186
+ }
187
+ else if ([EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.LengthTokenType].includes(exp.typ)) ;
188
+ else if (exp.typ == EnumToken.IdenTokenType && exp.val in values) {
189
+ if (typeof values[exp.val] == 'number') {
190
+ expr[key] = {
191
+ typ: EnumToken.NumberTokenType,
192
+ val: reduceNumber(values[exp.val])
193
+ };
194
+ }
195
+ else {
196
+ expr[key] = values[exp.val];
197
+ }
198
+ }
199
+ else if (exp.typ == EnumToken.FunctionTokenType && exp.val == 'calc') {
200
+ for (let { value, parent } of walkValues(exp.chi)) {
201
+ if (value.typ == EnumToken.IdenTokenType) {
202
+ if (!(value.val in values)) {
203
+ return null;
204
+ }
205
+ if (parent == null) {
206
+ parent = exp;
207
+ }
208
+ if (parent.typ == EnumToken.BinaryExpressionTokenType) {
209
+ if (parent.l == value) {
210
+ parent.l = values[value.val];
211
+ }
212
+ else {
213
+ parent.r = values[value.val];
214
+ }
215
+ }
216
+ else {
217
+ for (let i = 0; i < parent.chi.length; i++) {
218
+ if (parent.chi[i] == value) {
219
+ parent.chi.splice(i, 1, values[value.val]);
220
+ break;
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
226
+ const result = evaluate(exp.chi);
227
+ if (result.length == 1 && result[0].typ != EnumToken.BinaryExpressionTokenType) {
228
+ expr[key] = result[0];
229
+ }
230
+ else {
231
+ return null;
232
+ }
233
+ }
234
+ }
235
+ return expr;
236
+ }
237
+
238
+ export { parseRelativeColor };
@@ -307,122 +307,47 @@ const NAMES_COLORS = Object.seal({
307
307
  '#663399': 'rebeccapurple',
308
308
  '#00000000': 'transparent'
309
309
  });
310
- function rgb2Hex(token) {
311
- let value = '#';
312
- let t;
313
- // @ts-ignore
314
- for (let i = 0; i < 6; i += 2) {
315
- // @ts-ignore
316
- t = token.chi[i];
317
- // @ts-ignore
318
- value += Math.round(t.typ == EnumToken.PercentageTokenType ? 255 * t.val / 100 : t.val).toString(16).padStart(2, '0');
310
+ /**
311
+ * clamp color values
312
+ * @param token
313
+ */
314
+ function clamp(token) {
315
+ if (token.kin == 'rgb' || token.kin == 'rgba') {
316
+ token.chi.filter((token) => ![EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType].includes(token.typ)).
317
+ forEach((token, index) => {
318
+ if (index <= 2) {
319
+ if (token.typ == EnumToken.NumberTokenType) {
320
+ token.val = String(Math.min(255, Math.max(0, +token.val)));
321
+ }
322
+ else if (token.typ == EnumToken.PercentageTokenType) {
323
+ token.val = String(Math.min(100, Math.max(0, +token.val)));
324
+ }
325
+ }
326
+ else {
327
+ if (token.typ == EnumToken.NumberTokenType) {
328
+ token.val = String(Math.min(1, Math.max(0, +token.val)));
329
+ }
330
+ else if (token.typ == EnumToken.PercentageTokenType) {
331
+ token.val = String(Math.min(100, Math.max(0, +token.val)));
332
+ }
333
+ }
334
+ });
319
335
  }
320
- // @ts-ignore
321
- if (token.chi.length == 7) {
322
- // @ts-ignore
323
- t = token.chi[6];
324
- // @ts-ignore
325
- if ((t.typ == EnumToken.NumberTokenType && t.val < 1) ||
326
- // @ts-ignore
327
- (t.typ == EnumToken.PercentageTokenType && t.val < 100)) {
328
- // @ts-ignore
329
- value += Math.round(255 * (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val)).toString(16).padStart(2, '0');
330
- }
331
- }
332
- return value;
336
+ return token;
333
337
  }
334
- function hsl2Hex(token) {
335
- let t;
336
- // @ts-ignore
337
- let h = getAngle(token.chi[0]);
338
- // @ts-ignore
339
- t = token.chi[2];
340
- // @ts-ignore
341
- let s = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
342
- // @ts-ignore
343
- t = token.chi[4];
344
- // @ts-ignore
345
- let l = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
346
- let a = null;
347
- if (token.chi?.length == 7) {
348
- // @ts-ignore
349
- t = token.chi[6];
350
- // @ts-ignore
351
- if ((t.typ == EnumToken.PercentageTokenType && t.val < 100) ||
352
- // @ts-ignore
353
- (t.typ == EnumToken.NumberTokenType && t.val < 1)) {
354
- // @ts-ignore
355
- a = (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val);
356
- }
338
+ function getNumber(token) {
339
+ if (token.typ == EnumToken.IdenTokenType && token.val == 'none') {
340
+ return 0;
357
341
  }
358
- return `#${hsl2rgb(h, s, l, a).reduce((acc, curr) => acc + curr.toString(16).padStart(2, '0'), '')}`;
359
- }
360
- function hwb2hex(token) {
361
- let t;
362
- // @ts-ignore
363
- let h = getAngle(token.chi[0]);
364
- // @ts-ignore
365
- t = token.chi[2];
366
- // @ts-ignore
367
- let white = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
368
342
  // @ts-ignore
369
- t = token.chi[4];
370
- // @ts-ignore
371
- let black = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
372
- let a = null;
373
- if (token.chi?.length == 7) {
374
- // @ts-ignore
375
- t = token.chi[6];
376
- // @ts-ignore
377
- if ((t.typ == EnumToken.PercentageTokenType && t.val < 100) ||
378
- // @ts-ignore
379
- (t.typ == EnumToken.NumberTokenType && t.val < 1)) {
380
- // @ts-ignore
381
- a = (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val);
382
- }
383
- }
384
- const rgb = hsl2rgb(h, 1, .5, a);
385
- let value;
386
- for (let i = 0; i < 3; i++) {
387
- value = rgb[i] / 255;
388
- value *= (1 - white - black);
389
- value += white;
390
- rgb[i] = Math.round(value * 255);
391
- }
392
- return `#${rgb.reduce((acc, curr) => acc + curr.toString(16).padStart(2, '0'), '')}`;
393
- }
394
- function cmyk2hex(token) {
395
- // @ts-ignore
396
- let t = token.chi[0];
397
- // @ts-ignore
398
- const c = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
399
- // @ts-ignore
400
- t = token.chi[2];
401
- // @ts-ignore
402
- const m = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
403
- // @ts-ignore
404
- t = token.chi[4];
405
- // @ts-ignore
406
- const y = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
407
- // @ts-ignore
408
- t = token.chi[6];
409
- // @ts-ignore
410
- const k = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
411
- const rgb = [
412
- Math.round(255 * (1 - Math.min(1, c * (1 - k) + k))),
413
- Math.round(255 * (1 - Math.min(1, m * (1 - k) + k))),
414
- Math.round(255 * (1 - Math.min(1, y * (1 - k) + k)))
415
- ];
416
- // @ts-ignore
417
- if (token.chi.length >= 9) {
418
- // @ts-ignore
419
- t = token.chi[8];
420
- // @ts-ignore
421
- rgb.push(Math.round(255 * (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val)));
422
- }
423
- return `#${rgb.reduce((acc, curr) => acc + curr.toString(16).padStart(2, '0'), '')}`;
343
+ return token.typ == EnumToken.PercentageTokenType ? token.val / 100 : +token.val;
424
344
  }
425
345
  function getAngle(token) {
346
+ if (token.typ == EnumToken.IdenTokenType) {
347
+ if (token.val == 'none') {
348
+ return 0;
349
+ }
350
+ }
426
351
  if (token.typ == EnumToken.AngleTokenType) {
427
352
  switch (token.unit) {
428
353
  case 'deg':
@@ -442,58 +367,5 @@ function getAngle(token) {
442
367
  // @ts-ignore
443
368
  return token.val / 360;
444
369
  }
445
- function hsl2rgb(h, s, l, a = null) {
446
- let v = l <= .5 ? l * (1.0 + s) : l + s - l * s;
447
- let r = l;
448
- let g = l;
449
- let b = l;
450
- if (v > 0) {
451
- let m = l + l - v;
452
- let sv = (v - m) / v;
453
- h *= 6.0;
454
- let sextant = Math.floor(h);
455
- let fract = h - sextant;
456
- let vsf = v * sv * fract;
457
- let mid1 = m + vsf;
458
- let mid2 = v - vsf;
459
- switch (sextant) {
460
- case 0:
461
- r = v;
462
- g = mid1;
463
- b = m;
464
- break;
465
- case 1:
466
- r = mid2;
467
- g = v;
468
- b = m;
469
- break;
470
- case 2:
471
- r = m;
472
- g = v;
473
- b = mid1;
474
- break;
475
- case 3:
476
- r = m;
477
- g = mid2;
478
- b = v;
479
- break;
480
- case 4:
481
- r = mid1;
482
- g = m;
483
- b = v;
484
- break;
485
- case 5:
486
- r = v;
487
- g = m;
488
- b = mid2;
489
- break;
490
- }
491
- }
492
- const values = [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
493
- if (a != null && a != 1) {
494
- values.push(Math.round(a * 255));
495
- }
496
- return values;
497
- }
498
370
 
499
- export { COLORS_NAMES, NAMES_COLORS, cmyk2hex, getAngle, hsl2Hex, hwb2hex, rgb2Hex };
371
+ export { COLORS_NAMES, NAMES_COLORS, clamp, getAngle, getNumber };