source: trip-planner-front/node_modules/postcss-lab-function/index.cjs.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.2 KB
Line 
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var convertColors = require('@csstools/convert-colors');
6var postcss = _interopDefault(require('postcss'));
7var parser = _interopDefault(require('postcss-values-parser'));
8
9var index = postcss.plugin('postcss-lab-function', opts => {
10 const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
11 return root => {
12 root.walkDecls(decl => {
13 const value = decl.value;
14
15 if (colorAnyRegExp.test(value)) {
16 const ast = parser(value).parse();
17 ast.walkType('func', node => {
18 if (colorRegExp.test(node.value)) {
19 const children = node.nodes.slice(1, -1);
20 const isLab = labRegExp.test(node.value);
21 const isGray = grayRegExp.test(node.value);
22 const isFunctionalLAB = !isGray && matchFunctionalLAB(children);
23 const isFunctionalLCH = !isGray && matchFunctionalLCH(children);
24 const isFunctionalGray = isGray && matchFunctionalGray(children);
25
26 if (isFunctionalLAB || isFunctionalLCH) {
27 node.value = 'rgb';
28 const slashNode = children[3];
29 const alphaNode = children[4];
30
31 if (alphaNode) {
32 if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
33 alphaNode.unit = '';
34 alphaNode.value = String(alphaNode.value / 100);
35 }
36
37 if (alphaNode.value === '1') {
38 slashNode.remove();
39 alphaNode.remove();
40 } else {
41 node.value += 'a';
42 }
43 }
44
45 if (slashNode && isSlash(slashNode)) {
46 slashNode.replaceWith(newComma());
47 }
48
49 const converter = isLab ? convertColors.lab2rgb : convertColors.lch2rgb;
50 const rgbValues = converter(...[children[0].value, children[1].value, children[2].value].map(number => parseFloat(number))).map(sourceValue => Math.max(Math.min(parseInt(sourceValue * 2.55), 255), 0));
51 children[0].value = String(rgbValues[0]);
52 children[1].value = String(rgbValues[1]);
53 children[2].value = String(rgbValues[2]);
54 node.nodes.splice(3, 0, [newComma()]);
55 node.nodes.splice(2, 0, [newComma()]);
56 } else if (isFunctionalGray) {
57 node.value = 'rgb';
58 const alphaNode = children[2];
59 const rgbValues = convertColors.lab2rgb(...[children[0].value, 0, 0].map(number => parseFloat(number))).map(sourceValue => Math.max(Math.min(parseInt(sourceValue * 2.55), 255), 0));
60 node.removeAll().append(newParen('(')).append(newNumber(rgbValues[0])).append(newComma()).append(newNumber(rgbValues[1])).append(newComma()).append(newNumber(rgbValues[2])).append(newParen(')'));
61
62 if (alphaNode) {
63 if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
64 alphaNode.unit = '';
65 alphaNode.value = String(alphaNode.value / 100);
66 }
67
68 if (alphaNode.value !== '1') {
69 node.value += 'a';
70 node.insertBefore(node.last, newComma()).insertBefore(node.last, alphaNode);
71 }
72 }
73 }
74 }
75 });
76 const newValue = String(ast);
77
78 if (preserve) {
79 decl.cloneBefore({
80 value: newValue
81 });
82 } else {
83 decl.value = newValue;
84 }
85 }
86 });
87 };
88});
89const colorAnyRegExp = /(^|[^\w-])(lab|lch|gray)\(/i;
90const colorRegExp = /^(lab|lch|gray)$/i;
91const labRegExp = /^lab$/i;
92const grayRegExp = /^gray$/i;
93const alphaUnitMatch = /^%?$/i;
94const calcFuncMatch = /^calc$/i;
95const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
96
97const isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);
98
99const isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);
100
101const isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);
102
103const isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';
104
105const isPercentage = node => isCalc(node) || node.type === 'number' && node.unit === '%';
106
107const isSlash = node => node.type === 'operator' && node.value === '/';
108
109const functionalLABMatch = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
110const functionalLCHMatch = [isNumber, isNumber, isHue, isSlash, isAlphaValue];
111const functionalGrayMatch = [isNumber, isSlash, isAlphaValue];
112
113const matchFunctionalLAB = children => children.every((child, index) => typeof functionalLABMatch[index] === 'function' && functionalLABMatch[index](child));
114
115const matchFunctionalLCH = children => children.every((child, index) => typeof functionalLCHMatch[index] === 'function' && functionalLCHMatch[index](child));
116
117const matchFunctionalGray = children => children.every((child, index) => typeof functionalGrayMatch[index] === 'function' && functionalGrayMatch[index](child));
118
119const newComma = () => parser.comma({
120 value: ','
121});
122
123const newNumber = value => parser.number({
124 value
125});
126
127const newParen = value => parser.paren({
128 value
129});
130
131module.exports = index;
132//# sourceMappingURL=index.cjs.js.map
Note: See TracBrowser for help on using the repository browser.