[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
---|
| 4 |
|
---|
| 5 | var convertColors = require('@csstools/convert-colors');
|
---|
| 6 | var postcss = _interopDefault(require('postcss'));
|
---|
| 7 | var parser = _interopDefault(require('postcss-values-parser'));
|
---|
| 8 |
|
---|
| 9 | var 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 | });
|
---|
| 89 | const colorAnyRegExp = /(^|[^\w-])(lab|lch|gray)\(/i;
|
---|
| 90 | const colorRegExp = /^(lab|lch|gray)$/i;
|
---|
| 91 | const labRegExp = /^lab$/i;
|
---|
| 92 | const grayRegExp = /^gray$/i;
|
---|
| 93 | const alphaUnitMatch = /^%?$/i;
|
---|
| 94 | const calcFuncMatch = /^calc$/i;
|
---|
| 95 | const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
|
---|
| 96 |
|
---|
| 97 | const isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);
|
---|
| 98 |
|
---|
| 99 | const isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);
|
---|
| 100 |
|
---|
| 101 | const isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);
|
---|
| 102 |
|
---|
| 103 | const isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';
|
---|
| 104 |
|
---|
| 105 | const isPercentage = node => isCalc(node) || node.type === 'number' && node.unit === '%';
|
---|
| 106 |
|
---|
| 107 | const isSlash = node => node.type === 'operator' && node.value === '/';
|
---|
| 108 |
|
---|
| 109 | const functionalLABMatch = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
|
---|
| 110 | const functionalLCHMatch = [isNumber, isNumber, isHue, isSlash, isAlphaValue];
|
---|
| 111 | const functionalGrayMatch = [isNumber, isSlash, isAlphaValue];
|
---|
| 112 |
|
---|
| 113 | const matchFunctionalLAB = children => children.every((child, index) => typeof functionalLABMatch[index] === 'function' && functionalLABMatch[index](child));
|
---|
| 114 |
|
---|
| 115 | const matchFunctionalLCH = children => children.every((child, index) => typeof functionalLCHMatch[index] === 'function' && functionalLCHMatch[index](child));
|
---|
| 116 |
|
---|
| 117 | const matchFunctionalGray = children => children.every((child, index) => typeof functionalGrayMatch[index] === 'function' && functionalGrayMatch[index](child));
|
---|
| 118 |
|
---|
| 119 | const newComma = () => parser.comma({
|
---|
| 120 | value: ','
|
---|
| 121 | });
|
---|
| 122 |
|
---|
| 123 | const newNumber = value => parser.number({
|
---|
| 124 | value
|
---|
| 125 | });
|
---|
| 126 |
|
---|
| 127 | const newParen = value => parser.paren({
|
---|
| 128 | value
|
---|
| 129 | });
|
---|
| 130 |
|
---|
| 131 | module.exports = index;
|
---|
| 132 | //# sourceMappingURL=index.cjs.js.map
|
---|