index.cjs.js 5.2 KB

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