index.cjs.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. 'use strict';
  2. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  3. var valueParser = _interopDefault(require('postcss-values-parser'));
  4. var fs = _interopDefault(require('fs'));
  5. var path = _interopDefault(require('path'));
  6. var postcss = _interopDefault(require('postcss'));
  7. var convertColors = require('@csstools/convert-colors');
  8. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  9. try {
  10. var info = gen[key](arg);
  11. var value = info.value;
  12. } catch (error) {
  13. reject(error);
  14. return;
  15. }
  16. if (info.done) {
  17. resolve(value);
  18. } else {
  19. Promise.resolve(value).then(_next, _throw);
  20. }
  21. }
  22. function _asyncToGenerator(fn) {
  23. return function () {
  24. var self = this,
  25. args = arguments;
  26. return new Promise(function (resolve, reject) {
  27. var gen = fn.apply(self, args);
  28. function _next(value) {
  29. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
  30. }
  31. function _throw(err) {
  32. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
  33. }
  34. _next(undefined);
  35. });
  36. };
  37. }
  38. function _defineProperty(obj, key, value) {
  39. if (key in obj) {
  40. Object.defineProperty(obj, key, {
  41. value: value,
  42. enumerable: true,
  43. configurable: true,
  44. writable: true
  45. });
  46. } else {
  47. obj[key] = value;
  48. }
  49. return obj;
  50. }
  51. function _objectSpread(target) {
  52. for (var i = 1; i < arguments.length; i++) {
  53. var source = arguments[i] != null ? arguments[i] : {};
  54. var ownKeys = Object.keys(source);
  55. if (typeof Object.getOwnPropertySymbols === 'function') {
  56. ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
  57. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  58. }));
  59. }
  60. ownKeys.forEach(function (key) {
  61. _defineProperty(target, key, source[key]);
  62. });
  63. }
  64. return target;
  65. }
  66. function _slicedToArray(arr, i) {
  67. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
  68. }
  69. function _toArray(arr) {
  70. return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest();
  71. }
  72. function _arrayWithHoles(arr) {
  73. if (Array.isArray(arr)) return arr;
  74. }
  75. function _iterableToArray(iter) {
  76. if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
  77. }
  78. function _iterableToArrayLimit(arr, i) {
  79. var _arr = [];
  80. var _n = true;
  81. var _d = false;
  82. var _e = undefined;
  83. try {
  84. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  85. _arr.push(_s.value);
  86. if (i && _arr.length === i) break;
  87. }
  88. } catch (err) {
  89. _d = true;
  90. _e = err;
  91. } finally {
  92. try {
  93. if (!_n && _i["return"] != null) _i["return"]();
  94. } finally {
  95. if (_d) throw _e;
  96. }
  97. }
  98. return _arr;
  99. }
  100. function _nonIterableRest() {
  101. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  102. }
  103. function getCustomProperties(root, opts) {
  104. // initialize custom selectors
  105. const customPropertiesFromHtmlElement = {};
  106. const customPropertiesFromRootPsuedo = {}; // for each html or :root rule
  107. root.nodes.slice().forEach(rule => {
  108. const customPropertiesObject = isHtmlRule(rule) ? customPropertiesFromHtmlElement : isRootRule(rule) ? customPropertiesFromRootPsuedo : null; // for each custom property
  109. if (customPropertiesObject) {
  110. rule.nodes.slice().forEach(decl => {
  111. if (isCustomDecl(decl)) {
  112. const prop = decl.prop; // write the parsed value to the custom property
  113. customPropertiesObject[prop] = valueParser(decl.value).parse(); // conditionally remove the custom property declaration
  114. if (!opts.preserve) {
  115. decl.remove();
  116. }
  117. }
  118. }); // conditionally remove the empty html or :root rule
  119. if (!opts.preserve && isEmptyParent(rule)) {
  120. rule.remove();
  121. }
  122. }
  123. }); // return all custom properties, preferring :root properties over html properties
  124. return _objectSpread({}, customPropertiesFromHtmlElement, customPropertiesFromRootPsuedo);
  125. } // match html and :root rules
  126. const htmlSelectorRegExp = /^html$/i;
  127. const rootSelectorRegExp = /^:root$/i;
  128. const customPropertyRegExp = /^--[A-z][\w-]*$/; // whether the node is an html or :root rule
  129. const isHtmlRule = node => node.type === 'rule' && htmlSelectorRegExp.test(node.selector) && Object(node.nodes).length;
  130. const isRootRule = node => node.type === 'rule' && rootSelectorRegExp.test(node.selector) && Object(node.nodes).length; // whether the node is an custom property
  131. const isCustomDecl = node => node.type === 'decl' && customPropertyRegExp.test(node.prop); // whether the node is a parent without children
  132. const isEmptyParent = node => Object(node.nodes).length === 0;
  133. /* Import Custom Properties from CSS AST
  134. /* ========================================================================== */
  135. function importCustomPropertiesFromCSSAST(root) {
  136. return getCustomProperties(root, {
  137. preserve: true
  138. });
  139. }
  140. /* Import Custom Properties from CSS File
  141. /* ========================================================================== */
  142. function importCustomPropertiesFromCSSFile(_x) {
  143. return _importCustomPropertiesFromCSSFile.apply(this, arguments);
  144. }
  145. /* Import Custom Properties from Object
  146. /* ========================================================================== */
  147. function _importCustomPropertiesFromCSSFile() {
  148. _importCustomPropertiesFromCSSFile = _asyncToGenerator(function* (from) {
  149. const css = yield readFile(from);
  150. const root = postcss.parse(css, {
  151. from
  152. });
  153. return importCustomPropertiesFromCSSAST(root);
  154. });
  155. return _importCustomPropertiesFromCSSFile.apply(this, arguments);
  156. }
  157. function importCustomPropertiesFromObject(object) {
  158. const customProperties = Object.assign({}, Object(object).customProperties || Object(object)['custom-properties']);
  159. for (const prop in customProperties) {
  160. customProperties[prop] = valueParser(customProperties[prop]).parse();
  161. }
  162. return customProperties;
  163. }
  164. /* Import Custom Properties from JSON file
  165. /* ========================================================================== */
  166. function importCustomPropertiesFromJSONFile(_x2) {
  167. return _importCustomPropertiesFromJSONFile.apply(this, arguments);
  168. }
  169. /* Import Custom Properties from JS file
  170. /* ========================================================================== */
  171. function _importCustomPropertiesFromJSONFile() {
  172. _importCustomPropertiesFromJSONFile = _asyncToGenerator(function* (from) {
  173. const object = yield readJSON(from);
  174. return importCustomPropertiesFromObject(object);
  175. });
  176. return _importCustomPropertiesFromJSONFile.apply(this, arguments);
  177. }
  178. function importCustomPropertiesFromJSFile(_x3) {
  179. return _importCustomPropertiesFromJSFile.apply(this, arguments);
  180. }
  181. /* Import Custom Properties from Sources
  182. /* ========================================================================== */
  183. function _importCustomPropertiesFromJSFile() {
  184. _importCustomPropertiesFromJSFile = _asyncToGenerator(function* (from) {
  185. const object = yield Promise.resolve(require(from));
  186. return importCustomPropertiesFromObject(object);
  187. });
  188. return _importCustomPropertiesFromJSFile.apply(this, arguments);
  189. }
  190. function importCustomPropertiesFromSources(sources) {
  191. return sources.map(source => {
  192. if (source instanceof Promise) {
  193. return source;
  194. } else if (source instanceof Function) {
  195. return source();
  196. } // read the source as an object
  197. const opts = source === Object(source) ? source : {
  198. from: String(source)
  199. }; // skip objects with Custom Properties
  200. if (opts.customProperties || opts['custom-properties']) {
  201. return opts;
  202. } // source pathname
  203. const from = path.resolve(String(opts.from || '')); // type of file being read from
  204. const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
  205. return {
  206. type,
  207. from
  208. };
  209. }).reduce(
  210. /*#__PURE__*/
  211. function () {
  212. var _ref = _asyncToGenerator(function* (customProperties, source) {
  213. const _ref2 = yield source,
  214. type = _ref2.type,
  215. from = _ref2.from;
  216. if (type === 'ast') {
  217. return Object.assign((yield customProperties), importCustomPropertiesFromCSSAST(from));
  218. }
  219. if (type === 'css') {
  220. return Object.assign((yield customProperties), (yield importCustomPropertiesFromCSSFile(from)));
  221. }
  222. if (type === 'js') {
  223. return Object.assign((yield customProperties), (yield importCustomPropertiesFromJSFile(from)));
  224. }
  225. if (type === 'json') {
  226. return Object.assign((yield customProperties), (yield importCustomPropertiesFromJSONFile(from)));
  227. }
  228. return Object.assign((yield customProperties), (yield importCustomPropertiesFromObject((yield source))));
  229. });
  230. return function (_x4, _x5) {
  231. return _ref.apply(this, arguments);
  232. };
  233. }(), {});
  234. }
  235. /* Helper utilities
  236. /* ========================================================================== */
  237. const readFile = from => new Promise((resolve, reject) => {
  238. fs.readFile(from, 'utf8', (error, result) => {
  239. if (error) {
  240. reject(error);
  241. } else {
  242. resolve(result);
  243. }
  244. });
  245. });
  246. const readJSON =
  247. /*#__PURE__*/
  248. function () {
  249. var _ref3 = _asyncToGenerator(function* (from) {
  250. return JSON.parse((yield readFile(from)));
  251. });
  252. return function readJSON(_x6) {
  253. return _ref3.apply(this, arguments);
  254. };
  255. }();
  256. /* Convert Degree to Hue Degree
  257. /* ========================================================================== */
  258. function convertDtoD(deg) {
  259. return deg % 360;
  260. }
  261. /* Convert Gradian to Hue Degree
  262. /* ========================================================================== */
  263. function convertGtoD(grad) {
  264. return grad * 0.9 % 360;
  265. }
  266. /* Convert Radian to Hue Degree
  267. /* ========================================================================== */
  268. function convertRtoD(rad) {
  269. return rad * 180 / Math.PI % 360;
  270. }
  271. /* Convert Turn to Hue Degree
  272. /* ========================================================================== */
  273. function convertTtoD(turn) {
  274. return turn * 360 % 360;
  275. }
  276. /* Convert a Name to Red/Green/Blue
  277. /* ========================================================================== */
  278. function convertNtoRGB(name) {
  279. const names = {
  280. aliceblue: [240, 248, 255],
  281. antiquewhite: [250, 235, 215],
  282. aqua: [0, 255, 255],
  283. aquamarine: [127, 255, 212],
  284. azure: [240, 255, 255],
  285. beige: [245, 245, 220],
  286. bisque: [255, 228, 196],
  287. black: [0, 0, 0],
  288. blanchedalmond: [255, 235, 205],
  289. blue: [0, 0, 255],
  290. blueviolet: [138, 43, 226],
  291. brown: [165, 42, 42],
  292. burlywood: [222, 184, 135],
  293. cadetblue: [95, 158, 160],
  294. chartreuse: [127, 255, 0],
  295. chocolate: [210, 105, 30],
  296. coral: [255, 127, 80],
  297. cornflowerblue: [100, 149, 237],
  298. cornsilk: [255, 248, 220],
  299. crimson: [220, 20, 60],
  300. cyan: [0, 255, 255],
  301. darkblue: [0, 0, 139],
  302. darkcyan: [0, 139, 139],
  303. darkgoldenrod: [184, 134, 11],
  304. darkgray: [169, 169, 169],
  305. darkgreen: [0, 100, 0],
  306. darkgrey: [169, 169, 169],
  307. darkkhaki: [189, 183, 107],
  308. darkmagenta: [139, 0, 139],
  309. darkolivegreen: [85, 107, 47],
  310. darkorange: [255, 140, 0],
  311. darkorchid: [153, 50, 204],
  312. darkred: [139, 0, 0],
  313. darksalmon: [233, 150, 122],
  314. darkseagreen: [143, 188, 143],
  315. darkslateblue: [72, 61, 139],
  316. darkslategray: [47, 79, 79],
  317. darkslategrey: [47, 79, 79],
  318. darkturquoise: [0, 206, 209],
  319. darkviolet: [148, 0, 211],
  320. deeppink: [255, 20, 147],
  321. deepskyblue: [0, 191, 255],
  322. dimgray: [105, 105, 105],
  323. dimgrey: [105, 105, 105],
  324. dodgerblue: [30, 144, 255],
  325. firebrick: [178, 34, 34],
  326. floralwhite: [255, 250, 240],
  327. forestgreen: [34, 139, 34],
  328. fuchsia: [255, 0, 255],
  329. gainsboro: [220, 220, 220],
  330. ghostwhite: [248, 248, 255],
  331. gold: [255, 215, 0],
  332. goldenrod: [218, 165, 32],
  333. gray: [128, 128, 128],
  334. green: [0, 128, 0],
  335. greenyellow: [173, 255, 47],
  336. grey: [128, 128, 128],
  337. honeydew: [240, 255, 240],
  338. hotpink: [255, 105, 180],
  339. indianred: [205, 92, 92],
  340. indigo: [75, 0, 130],
  341. ivory: [255, 255, 240],
  342. khaki: [240, 230, 140],
  343. lavender: [230, 230, 250],
  344. lavenderblush: [255, 240, 245],
  345. lawngreen: [124, 252, 0],
  346. lemonchiffon: [255, 250, 205],
  347. lightblue: [173, 216, 230],
  348. lightcoral: [240, 128, 128],
  349. lightcyan: [224, 255, 255],
  350. lightgoldenrodyellow: [250, 250, 210],
  351. lightgray: [211, 211, 211],
  352. lightgreen: [144, 238, 144],
  353. lightgrey: [211, 211, 211],
  354. lightpink: [255, 182, 193],
  355. lightsalmon: [255, 160, 122],
  356. lightseagreen: [32, 178, 170],
  357. lightskyblue: [135, 206, 250],
  358. lightslategray: [119, 136, 153],
  359. lightslategrey: [119, 136, 153],
  360. lightsteelblue: [176, 196, 222],
  361. lightyellow: [255, 255, 224],
  362. lime: [0, 255, 0],
  363. limegreen: [50, 205, 50],
  364. linen: [250, 240, 230],
  365. magenta: [255, 0, 255],
  366. maroon: [128, 0, 0],
  367. mediumaquamarine: [102, 205, 170],
  368. mediumblue: [0, 0, 205],
  369. mediumorchid: [186, 85, 211],
  370. mediumpurple: [147, 112, 219],
  371. mediumseagreen: [60, 179, 113],
  372. mediumslateblue: [123, 104, 238],
  373. mediumspringgreen: [0, 250, 154],
  374. mediumturquoise: [72, 209, 204],
  375. mediumvioletred: [199, 21, 133],
  376. midnightblue: [25, 25, 112],
  377. mintcream: [245, 255, 250],
  378. mistyrose: [255, 228, 225],
  379. moccasin: [255, 228, 181],
  380. navajowhite: [255, 222, 173],
  381. navy: [0, 0, 128],
  382. oldlace: [253, 245, 230],
  383. olive: [128, 128, 0],
  384. olivedrab: [107, 142, 35],
  385. orange: [255, 165, 0],
  386. orangered: [255, 69, 0],
  387. orchid: [218, 112, 214],
  388. palegoldenrod: [238, 232, 170],
  389. palegreen: [152, 251, 152],
  390. paleturquoise: [175, 238, 238],
  391. palevioletred: [219, 112, 147],
  392. papayawhip: [255, 239, 213],
  393. peachpuff: [255, 218, 185],
  394. peru: [205, 133, 63],
  395. pink: [255, 192, 203],
  396. plum: [221, 160, 221],
  397. powderblue: [176, 224, 230],
  398. purple: [128, 0, 128],
  399. rebeccapurple: [102, 51, 153],
  400. red: [255, 0, 0],
  401. rosybrown: [188, 143, 143],
  402. royalblue: [65, 105, 225],
  403. saddlebrown: [139, 69, 19],
  404. salmon: [250, 128, 114],
  405. sandybrown: [244, 164, 96],
  406. seagreen: [46, 139, 87],
  407. seashell: [255, 245, 238],
  408. sienna: [160, 82, 45],
  409. silver: [192, 192, 192],
  410. skyblue: [135, 206, 235],
  411. slateblue: [106, 90, 205],
  412. slategray: [112, 128, 144],
  413. slategrey: [112, 128, 144],
  414. snow: [255, 250, 250],
  415. springgreen: [0, 255, 127],
  416. steelblue: [70, 130, 180],
  417. tan: [210, 180, 140],
  418. teal: [0, 128, 128],
  419. thistle: [216, 191, 216],
  420. tomato: [255, 99, 71],
  421. transparent: [0, 0, 0],
  422. turquoise: [64, 224, 208],
  423. violet: [238, 130, 238],
  424. wheat: [245, 222, 179],
  425. white: [255, 255, 255],
  426. whitesmoke: [245, 245, 245],
  427. yellow: [255, 255, 0],
  428. yellowgreen: [154, 205, 50]
  429. };
  430. return names[name] && names[name].map(c => c / 2.55);
  431. }
  432. /* Convert a Hex to Red/Green/Blue
  433. /* ========================================================================== */
  434. function convertHtoRGB(hex) {
  435. // #<hex-color>{3,4,6,8}
  436. const _slice = (hex.match(hexColorMatch) || []).slice(1),
  437. _slice2 = _slicedToArray(_slice, 8),
  438. r = _slice2[0],
  439. g = _slice2[1],
  440. b = _slice2[2],
  441. a = _slice2[3],
  442. rr = _slice2[4],
  443. gg = _slice2[5],
  444. bb = _slice2[6],
  445. aa = _slice2[7];
  446. if (rr !== undefined || r !== undefined) {
  447. const red = rr !== undefined ? parseInt(rr, 16) : r !== undefined ? parseInt(r + r, 16) : 0;
  448. const green = gg !== undefined ? parseInt(gg, 16) : g !== undefined ? parseInt(g + g, 16) : 0;
  449. const blue = bb !== undefined ? parseInt(bb, 16) : b !== undefined ? parseInt(b + b, 16) : 0;
  450. const alpha = aa !== undefined ? parseInt(aa, 16) : a !== undefined ? parseInt(a + a, 16) : 255;
  451. return [red, green, blue, alpha].map(c => c / 2.55);
  452. }
  453. return undefined;
  454. }
  455. const hexColorMatch = /^#(?:([a-f0-9])([a-f0-9])([a-f0-9])([a-f0-9])?|([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?)$/i;
  456. class Color {
  457. constructor(color) {
  458. this.color = Object(Object(color).color || color);
  459. this.color.colorspace = this.color.colorspace ? this.color.colorspace : 'red' in color && 'green' in color && 'blue' in color ? 'rgb' : 'hue' in color && 'saturation' in color && 'lightness' in color ? 'hsl' : 'hue' in color && 'whiteness' in color && 'blackness' in color ? 'hwb' : 'unknown';
  460. if (color.colorspace === 'rgb') {
  461. this.color.hue = convertColors.rgb2hue(color.red, color.green, color.blue, color.hue || 0);
  462. }
  463. }
  464. alpha(alpha) {
  465. const color = this.color;
  466. return alpha === undefined ? color.alpha : new Color(assign(color, {
  467. alpha
  468. }));
  469. }
  470. blackness(blackness) {
  471. const hwb = color2hwb(this.color);
  472. return blackness === undefined ? hwb.blackness : new Color(assign(hwb, {
  473. blackness
  474. }));
  475. }
  476. blend(color, percentage, colorspace = 'rgb') {
  477. const base = this.color;
  478. return new Color(blend(base, color, percentage, colorspace));
  479. }
  480. blenda(color, percentage, colorspace = 'rgb') {
  481. const base = this.color;
  482. return new Color(blend(base, color, percentage, colorspace, true));
  483. }
  484. blue(blue) {
  485. const rgb = color2rgb(this.color);
  486. return blue === undefined ? rgb.blue : new Color(assign(rgb, {
  487. blue
  488. }));
  489. }
  490. contrast(percentage) {
  491. const base = this.color;
  492. return new Color(contrast(base, percentage));
  493. }
  494. green(green) {
  495. const rgb = color2rgb(this.color);
  496. return green === undefined ? rgb.green : new Color(assign(rgb, {
  497. green
  498. }));
  499. }
  500. hue(hue) {
  501. const hsl = color2hsl(this.color);
  502. return hue === undefined ? hsl.hue : new Color(assign(hsl, {
  503. hue
  504. }));
  505. }
  506. lightness(lightness) {
  507. const hsl = color2hsl(this.color);
  508. return lightness === undefined ? hsl.lightness : new Color(assign(hsl, {
  509. lightness
  510. }));
  511. }
  512. red(red) {
  513. const rgb = color2rgb(this.color);
  514. return red === undefined ? rgb.red : new Color(assign(rgb, {
  515. red
  516. }));
  517. }
  518. rgb(red, green, blue) {
  519. const rgb = color2rgb(this.color);
  520. return new Color(assign(rgb, {
  521. red,
  522. green,
  523. blue
  524. }));
  525. }
  526. saturation(saturation) {
  527. const hsl = color2hsl(this.color);
  528. return saturation === undefined ? hsl.saturation : new Color(assign(hsl, {
  529. saturation
  530. }));
  531. }
  532. shade(percentage) {
  533. const hwb = color2hwb(this.color);
  534. const shade = {
  535. hue: 0,
  536. whiteness: 0,
  537. blackness: 100,
  538. colorspace: 'hwb'
  539. };
  540. const colorspace = 'rgb';
  541. return percentage === undefined ? hwb.blackness : new Color(blend(hwb, shade, percentage, colorspace));
  542. }
  543. tint(percentage) {
  544. const hwb = color2hwb(this.color);
  545. const tint = {
  546. hue: 0,
  547. whiteness: 100,
  548. blackness: 0,
  549. colorspace: 'hwb'
  550. };
  551. const colorspace = 'rgb';
  552. return percentage === undefined ? hwb.blackness : new Color(blend(hwb, tint, percentage, colorspace));
  553. }
  554. whiteness(whiteness) {
  555. const hwb = color2hwb(this.color);
  556. return whiteness === undefined ? hwb.whiteness : new Color(assign(hwb, {
  557. whiteness
  558. }));
  559. }
  560. toHSL() {
  561. return color2hslString(this.color);
  562. }
  563. toHWB() {
  564. return color2hwbString(this.color);
  565. }
  566. toLegacy() {
  567. return color2legacyString(this.color);
  568. }
  569. toRGB() {
  570. return color2rgbString(this.color);
  571. }
  572. toRGBLegacy() {
  573. return color2rgbLegacyString(this.color);
  574. }
  575. toString() {
  576. return color2string(this.color);
  577. }
  578. }
  579. /* Blending
  580. /* ========================================================================== */
  581. function blend(base, color, percentage, colorspace, isBlendingAlpha) {
  582. const addition = percentage / 100;
  583. const subtraction = 1 - addition;
  584. if (colorspace === 'hsl') {
  585. const _color2hsl = color2hsl(base),
  586. h1 = _color2hsl.hue,
  587. s1 = _color2hsl.saturation,
  588. l1 = _color2hsl.lightness,
  589. a1 = _color2hsl.alpha;
  590. const _color2hsl2 = color2hsl(color),
  591. h2 = _color2hsl2.hue,
  592. s2 = _color2hsl2.saturation,
  593. l2 = _color2hsl2.lightness,
  594. a2 = _color2hsl2.alpha;
  595. const hue = h1 * subtraction + h2 * addition,
  596. saturation = s1 * subtraction + s2 * addition,
  597. lightness = l1 * subtraction + l2 * addition,
  598. alpha = isBlendingAlpha ? a1 * subtraction + a2 * addition : a1;
  599. return {
  600. hue,
  601. saturation,
  602. lightness,
  603. alpha,
  604. colorspace: 'hsl'
  605. };
  606. } else if (colorspace === 'hwb') {
  607. const _color2hwb = color2hwb(base),
  608. h1 = _color2hwb.hue,
  609. w1 = _color2hwb.whiteness,
  610. b1 = _color2hwb.blackness,
  611. a1 = _color2hwb.alpha;
  612. const _color2hwb2 = color2hwb(color),
  613. h2 = _color2hwb2.hue,
  614. w2 = _color2hwb2.whiteness,
  615. b2 = _color2hwb2.blackness,
  616. a2 = _color2hwb2.alpha;
  617. const hue = h1 * subtraction + h2 * addition,
  618. whiteness = w1 * subtraction + w2 * addition,
  619. blackness = b1 * subtraction + b2 * addition,
  620. alpha = isBlendingAlpha ? a1 * subtraction + a2 * addition : a1;
  621. return {
  622. hue,
  623. whiteness,
  624. blackness,
  625. alpha,
  626. colorspace: 'hwb'
  627. };
  628. } else {
  629. const _color2rgb = color2rgb(base),
  630. r1 = _color2rgb.red,
  631. g1 = _color2rgb.green,
  632. b1 = _color2rgb.blue,
  633. a1 = _color2rgb.alpha;
  634. const _color2rgb2 = color2rgb(color),
  635. r2 = _color2rgb2.red,
  636. g2 = _color2rgb2.green,
  637. b2 = _color2rgb2.blue,
  638. a2 = _color2rgb2.alpha;
  639. const red = r1 * subtraction + r2 * addition,
  640. green = g1 * subtraction + g2 * addition,
  641. blue = b1 * subtraction + b2 * addition,
  642. alpha = isBlendingAlpha ? a1 * subtraction + a2 * addition : a1;
  643. return {
  644. red,
  645. green,
  646. blue,
  647. alpha,
  648. colorspace: 'rgb'
  649. };
  650. }
  651. }
  652. /* Assign channels to a new instance of a base color
  653. /* ========================================================================== */
  654. function assign(base, channels) {
  655. const color = Object.assign({}, base);
  656. Object.keys(channels).forEach(channel => {
  657. // detect channel
  658. const isHue = channel === 'hue';
  659. const isRGB = !isHue && blueGreenRedMatch.test(channel); // normalized value of the channel
  660. const value = normalize(channels[channel], channel); // assign channel to new object
  661. color[channel] = value;
  662. if (isRGB) {
  663. // conditionally preserve the hue
  664. color.hue = convertColors.rgb2hue(color.red, color.green, color.blue, base.hue || 0);
  665. }
  666. });
  667. return color;
  668. }
  669. function normalize(value, channel) {
  670. // detect channel
  671. const isHue = channel === 'hue'; // value limitations
  672. const min = 0;
  673. const max = isHue ? 360 : 100;
  674. const normalizedValue = Math.min(Math.max(isHue ? value % 360 : value, min), max);
  675. return normalizedValue;
  676. }
  677. /* Convert colors
  678. /* ========================================================================== */
  679. function color2rgb(color) {
  680. const _ref = color.colorspace === 'hsl' ? convertColors.hsl2rgb(color.hue, color.saturation, color.lightness) : color.colorspace === 'hwb' ? convertColors.hwb2rgb(color.hue, color.whiteness, color.blackness) : [color.red, color.green, color.blue],
  681. _ref2 = _slicedToArray(_ref, 3),
  682. red = _ref2[0],
  683. green = _ref2[1],
  684. blue = _ref2[2];
  685. return {
  686. red,
  687. green,
  688. blue,
  689. hue: color.hue,
  690. alpha: color.alpha,
  691. colorspace: 'rgb'
  692. };
  693. }
  694. function color2hsl(color) {
  695. const _ref3 = color.colorspace === 'rgb' ? convertColors.rgb2hsl(color.red, color.green, color.blue, color.hue) : color.colorspace === 'hwb' ? convertColors.hwb2hsl(color.hue, color.whiteness, color.blackness) : [color.hue, color.saturation, color.lightness],
  696. _ref4 = _slicedToArray(_ref3, 3),
  697. hue = _ref4[0],
  698. saturation = _ref4[1],
  699. lightness = _ref4[2];
  700. return {
  701. hue,
  702. saturation,
  703. lightness,
  704. alpha: color.alpha,
  705. colorspace: 'hsl'
  706. };
  707. }
  708. function color2hwb(color) {
  709. const _ref5 = color.colorspace === 'rgb' ? convertColors.rgb2hwb(color.red, color.green, color.blue, color.hue) : color.colorspace === 'hsl' ? convertColors.hsl2hwb(color.hue, color.saturation, color.lightness) : [color.hue, color.whiteness, color.blackness],
  710. _ref6 = _slicedToArray(_ref5, 3),
  711. hue = _ref6[0],
  712. whiteness = _ref6[1],
  713. blackness = _ref6[2];
  714. return {
  715. hue,
  716. whiteness,
  717. blackness,
  718. alpha: color.alpha,
  719. colorspace: 'hwb'
  720. };
  721. }
  722. /* Contrast functions
  723. /* ========================================================================== */
  724. function contrast(color, percentage) {
  725. // https://drafts.csswg.org/css-color/#contrast-adjuster
  726. const hwb = color2hwb(color);
  727. const rgb = color2rgb(color); // compute the luminance of the color.
  728. const luminance = rgb2luminance(rgb.red, rgb.green, rgb.blue); // the maximum-contrast color, if it is less than .5
  729. const maxContrastColor = luminance < 0.5 // hwb(X, 100%, 0%), where X is the hue angle of the color
  730. ? {
  731. hue: hwb.hue,
  732. whiteness: 100,
  733. blackness: 0,
  734. alpha: hwb.alpha,
  735. colorspace: 'hwb' // otherwise, hwb(X, 0%, 100%), where X is the hue angle of the color
  736. } : {
  737. hue: hwb.hue,
  738. whiteness: 0,
  739. blackness: 100,
  740. alpha: hwb.alpha,
  741. colorspace: 'hwb'
  742. }; // contrast ratio
  743. const contrastRatio = colors2contrast(color, maxContrastColor);
  744. const minContrastColor = contrastRatio > 4.5 // the color with the smallest contrast ratio with the base color that is greater than 4.5
  745. ? colors2contrastRatioColor(hwb, maxContrastColor) // otherwise, the maximum-contrast color
  746. : maxContrastColor; // color(maximum-contrast blend(minimum-contrast <percentage> hwb)));
  747. return blend(maxContrastColor, minContrastColor, percentage, 'hwb', false);
  748. }
  749. function colors2contrast(color1, color2) {
  750. // https://drafts.csswg.org/css-color/#contrast-ratio
  751. const rgb1 = color2rgb(color1);
  752. const rgb2 = color2rgb(color2);
  753. const l1 = rgb2luminance(rgb1.red, rgb1.green, rgb1.blue);
  754. const l2 = rgb2luminance(rgb2.red, rgb2.green, rgb2.blue);
  755. return l1 > l2 // if l1 is the relative luminance of the lighter of the colors
  756. ? (l1 + 0.05) / (l2 + 0.05) // otherwise, if l2 is the relative luminance of the lighter of the colors
  757. : (l2 + 0.05) / (l1 + 0.05);
  758. }
  759. function rgb2luminance(red, green, blue) {
  760. const _ref7 = [channel2luminance(red), channel2luminance(green), channel2luminance(blue)],
  761. redLuminance = _ref7[0],
  762. greenLuminance = _ref7[1],
  763. blueLuminance = _ref7[2]; // https://drafts.csswg.org/css-color/#luminance
  764. const luminance = 0.2126 * redLuminance + 0.7152 * greenLuminance + 0.0722 * blueLuminance;
  765. return luminance;
  766. }
  767. function channel2luminance(value) {
  768. // https://drafts.csswg.org/css-color/#luminance
  769. const luminance = value <= 0.03928 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
  770. return luminance;
  771. } // return the smallest contrast ratio from a color and a maximum contrast (credit: @thetalecrafter)
  772. function colors2contrastRatioColor(hwb, maxHWB) {
  773. const modifiedHWB = Object.assign({}, hwb); // values to be used for linear interpolations in HWB space
  774. let minW = hwb.whiteness;
  775. let minB = hwb.blackness;
  776. let maxW = maxHWB.whiteness;
  777. let maxB = maxHWB.blackness; // find the color with the smallest contrast ratio with the base color that is greater than 4.5
  778. while (Math.abs(minW - maxW) > 100 || Math.abs(minB - maxB) > 100) {
  779. const midW = Math.round((maxW + minW) / 2);
  780. const midB = Math.round((maxB + minB) / 2);
  781. modifiedHWB.whiteness = midW;
  782. modifiedHWB.blackness = midB;
  783. if (colors2contrast(modifiedHWB, hwb) > 4.5) {
  784. maxW = midW;
  785. maxB = midB;
  786. } else {
  787. minW = midW;
  788. minB = midB;
  789. }
  790. }
  791. return modifiedHWB;
  792. }
  793. /* Match
  794. /* ========================================================================== */
  795. const blueGreenRedMatch = /^(blue|green|red)$/i;
  796. /* Stringifiers
  797. /* ========================================================================== */
  798. function color2string(color) {
  799. return color.colorspace === 'hsl' ? color2hslString(color) : color.colorspace === 'hwb' ? color2hwbString(color) : color2rgbString(color);
  800. }
  801. function color2hslString(color) {
  802. const hsl = color2hsl(color);
  803. const isOpaque = hsl.alpha === 100;
  804. const hue = hsl.hue;
  805. const saturation = Math.round(hsl.saturation * 10000000000) / 10000000000;
  806. const lightness = Math.round(hsl.lightness * 10000000000) / 10000000000;
  807. const alpha = Math.round(hsl.alpha * 10000000000) / 10000000000;
  808. return `hsl(${hue} ${saturation}% ${lightness}%${isOpaque ? '' : ` / ${alpha}%`})`;
  809. }
  810. function color2hwbString(color) {
  811. const hwb = color2hwb(color);
  812. const isOpaque = hwb.alpha === 100;
  813. const hue = hwb.hue;
  814. const whiteness = Math.round(hwb.whiteness * 10000000000) / 10000000000;
  815. const blackness = Math.round(hwb.blackness * 10000000000) / 10000000000;
  816. const alpha = Math.round(hwb.alpha * 10000000000) / 10000000000;
  817. return `hwb(${hue} ${whiteness}% ${blackness}%${isOpaque ? '' : ` / ${alpha}%`})`;
  818. }
  819. function color2rgbString(color) {
  820. const rgb = color2rgb(color);
  821. const isOpaque = rgb.alpha === 100;
  822. const red = Math.round(rgb.red * 10000000000) / 10000000000;
  823. const green = Math.round(rgb.green * 10000000000) / 10000000000;
  824. const blue = Math.round(rgb.blue * 10000000000) / 10000000000;
  825. const alpha = Math.round(rgb.alpha * 10000000000) / 10000000000;
  826. return `rgb(${red}% ${green}% ${blue}%${isOpaque ? '' : ` / ${alpha}%`})`;
  827. }
  828. function color2legacyString(color) {
  829. return color.colorspace === 'hsl' ? color2hslLegacyString(color) : color2rgbLegacyString(color);
  830. }
  831. function color2rgbLegacyString(color) {
  832. const rgb = color2rgb(color);
  833. const isOpaque = rgb.alpha === 100;
  834. const name = isOpaque ? 'rgb' : 'rgba';
  835. const red = Math.round(rgb.red * 255 / 100);
  836. const green = Math.round(rgb.green * 255 / 100);
  837. const blue = Math.round(rgb.blue * 255 / 100);
  838. const alpha = Math.round(rgb.alpha / 100 * 10000000000) / 10000000000;
  839. return `${name}(${red}, ${green}, ${blue}${isOpaque ? '' : `, ${alpha}`})`;
  840. }
  841. function color2hslLegacyString(color) {
  842. const hsl = color2hsl(color);
  843. const isOpaque = hsl.alpha === 100;
  844. const name = isOpaque ? 'hsl' : 'hsla';
  845. const hue = hsl.hue;
  846. const saturation = Math.round(hsl.saturation * 10000000000) / 10000000000;
  847. const lightness = Math.round(hsl.lightness * 10000000000) / 10000000000;
  848. const alpha = Math.round(hsl.alpha / 100 * 10000000000) / 10000000000;
  849. return `${name}(${hue}, ${saturation}%, ${lightness}%${isOpaque ? '' : `, ${alpha}`})`;
  850. }
  851. function manageUnresolved(node, opts, word, message) {
  852. if ('warn' === opts.unresolved) {
  853. opts.decl.warn(opts.result, message, {
  854. word
  855. });
  856. } else if ('ignore' !== opts.unresolved) {
  857. throw opts.decl.error(message, {
  858. word
  859. });
  860. }
  861. }
  862. /* Transform AST
  863. /* ========================================================================== */
  864. function transformAST(node, opts) {
  865. node.nodes.slice(0).forEach(child => {
  866. if (isColorModFunction(child)) {
  867. // transform any variables within the color-mod() function
  868. if (opts.transformVars) {
  869. transformVariables(child, opts);
  870. } // transform any color-mod() functions
  871. const color = transformColorModFunction(child, opts);
  872. if (color) {
  873. // update the color-mod() function with the transformed value
  874. child.replaceWith(valueParser.word({
  875. raws: child.raws,
  876. value: opts.stringifier(color)
  877. }));
  878. }
  879. } else if (child.nodes && Object(child.nodes).length) {
  880. transformAST(child, opts);
  881. }
  882. });
  883. }
  884. /* Transform <var> functions
  885. /* ========================================================================== */
  886. function transformVariables(node, opts) {
  887. walk(node, child => {
  888. if (isVariable(child)) {
  889. // get the custom property and fallback value from var()
  890. const _transformArgsByParam = transformArgsByParams(child, [// <value> , [ <fallback> ]?
  891. [transformWord, isComma, transformNode]]),
  892. _transformArgsByParam2 = _slicedToArray(_transformArgsByParam, 2),
  893. prop = _transformArgsByParam2[0],
  894. fallbackNode = _transformArgsByParam2[1]; // if the custom property is known
  895. if (prop in opts.customProperties) {
  896. let customPropertyValue = opts.customProperties[prop]; // follow custom properties referencing custom properties
  897. if (looseVarMatch.test(customPropertyValue)) {
  898. const rootChildAST = customPropertyValue.clone();
  899. transformVariables(rootChildAST, opts);
  900. customPropertyValue = rootChildAST;
  901. } // replace var() with the custom property value
  902. if (customPropertyValue.nodes.length === 1 && customPropertyValue.nodes[0].nodes.length) {
  903. customPropertyValue.nodes[0].nodes.forEach(customPropertyChild => {
  904. child.parent.insertBefore(child, customPropertyChild);
  905. });
  906. }
  907. child.remove();
  908. } else if (fallbackNode && fallbackNode.nodes.length === 1 && fallbackNode.nodes[0].nodes.length) {
  909. // otherwise, replace var() with the fallback value
  910. transformVariables(fallbackNode, opts);
  911. child.replaceWith(...fallbackNode.nodes[0].nodes[0]);
  912. }
  913. }
  914. });
  915. }
  916. /* Transform <color> functions
  917. /* ========================================================================== */
  918. function transformColor(node, opts) {
  919. if (isRGBFunction(node)) {
  920. return transformRGBFunction(node, opts);
  921. } else if (isHSLFunction(node)) {
  922. return transformHSLFunction(node, opts);
  923. } else if (isHWBFunction(node)) {
  924. return transformHWBFunction(node, opts);
  925. } else if (isColorModFunction(node)) {
  926. return transformColorModFunction(node, opts);
  927. } else if (isHexColor(node)) {
  928. return transformHexColor(node, opts);
  929. } else if (isNamedColor(node)) {
  930. return transformNamedColor(node, opts);
  931. } else {
  932. return manageUnresolved(node, opts, node.value, `Expected a color`);
  933. }
  934. } // return a transformed rgb/rgba color function
  935. function transformRGBFunction(node, opts) {
  936. const _transformArgsByParam3 = transformArgsByParams(node, [// <percentage> <percentage> <percentage> [ , <alpha-value> ]?
  937. [transformPercentage, transformPercentage, transformPercentage, isSlash, transformAlpha], // <number> <number> <number> [ , <alpha-value> ]?
  938. [transformRGBNumber, transformRGBNumber, transformRGBNumber, isSlash, transformAlpha], // <percentage> , <percentage> , <percentage> [ , <alpha-value> ]?
  939. [transformPercentage, isComma, transformPercentage, isComma, transformPercentage, isComma, transformAlpha], // <number> , <number> , <number> [ , <alpha-value> ]?
  940. [transformRGBNumber, isComma, transformRGBNumber, isComma, transformRGBNumber, isComma, transformAlpha]]),
  941. _transformArgsByParam4 = _slicedToArray(_transformArgsByParam3, 4),
  942. red = _transformArgsByParam4[0],
  943. green = _transformArgsByParam4[1],
  944. blue = _transformArgsByParam4[2],
  945. _transformArgsByParam5 = _transformArgsByParam4[3],
  946. alpha = _transformArgsByParam5 === void 0 ? 100 : _transformArgsByParam5;
  947. if (red !== undefined) {
  948. const color = new Color({
  949. red,
  950. green,
  951. blue,
  952. alpha,
  953. colorspace: 'rgb'
  954. });
  955. return color;
  956. } else {
  957. return manageUnresolved(node, opts, node.value, `Expected a valid rgb() function`);
  958. }
  959. } // return a transformed hsl/hsla color function
  960. function transformHSLFunction(node, opts) {
  961. const _transformArgsByParam6 = transformArgsByParams(node, [// <hue> <percentage> <percentage> [ / <alpha-value> ]?
  962. [transformHue, transformPercentage, transformPercentage, isSlash, transformAlpha], // <hue> , <percentage> , <percentage> [ , <alpha-value> ]?
  963. [transformHue, isComma, transformPercentage, isComma, transformPercentage, isComma, transformAlpha]]),
  964. _transformArgsByParam7 = _slicedToArray(_transformArgsByParam6, 4),
  965. hue = _transformArgsByParam7[0],
  966. saturation = _transformArgsByParam7[1],
  967. lightness = _transformArgsByParam7[2],
  968. _transformArgsByParam8 = _transformArgsByParam7[3],
  969. alpha = _transformArgsByParam8 === void 0 ? 100 : _transformArgsByParam8;
  970. if (lightness !== undefined) {
  971. const color = new Color({
  972. hue,
  973. saturation,
  974. lightness,
  975. alpha,
  976. colorspace: 'hsl'
  977. });
  978. return color;
  979. } else {
  980. return manageUnresolved(node, opts, node.value, `Expected a valid hsl() function`);
  981. }
  982. } // return a transformed hwb color function
  983. function transformHWBFunction(node, opts) {
  984. const _transformArgsByParam9 = transformArgsByParams(node, [// <hue> <percentage> <percentage> [ / <alpha-value> ]?
  985. [transformHue, transformPercentage, transformPercentage, isSlash, transformAlpha]]),
  986. _transformArgsByParam10 = _slicedToArray(_transformArgsByParam9, 4),
  987. hue = _transformArgsByParam10[0],
  988. whiteness = _transformArgsByParam10[1],
  989. blackness = _transformArgsByParam10[2],
  990. _transformArgsByParam11 = _transformArgsByParam10[3],
  991. alpha = _transformArgsByParam11 === void 0 ? 100 : _transformArgsByParam11;
  992. if (blackness !== undefined) {
  993. const color = new Color({
  994. hue,
  995. whiteness,
  996. blackness,
  997. alpha,
  998. colorspace: 'hwb'
  999. });
  1000. return color;
  1001. } else {
  1002. return manageUnresolved(node, opts, node.value, `Expected a valid hwb() function`);
  1003. }
  1004. } // return a transformed color-mod color function
  1005. function transformColorModFunction(node, opts) {
  1006. // [ <color> | <hue> ] <color-adjuster>*
  1007. const _ref = (node.nodes || []).slice(1, -1) || [],
  1008. _ref2 = _toArray(_ref),
  1009. colorOrHueNode = _ref2[0],
  1010. adjusterNodes = _ref2.slice(1);
  1011. if (colorOrHueNode !== undefined) {
  1012. const color = isHue(colorOrHueNode) ? new Color({
  1013. hue: transformHue(colorOrHueNode, opts),
  1014. saturation: 100,
  1015. lightness: 50,
  1016. alpha: 100,
  1017. colorspace: 'hsl'
  1018. }) : transformColor(colorOrHueNode, opts);
  1019. if (color) {
  1020. const adjustedColor = transformColorByAdjusters(color, adjusterNodes, opts);
  1021. return adjustedColor;
  1022. } else {
  1023. return manageUnresolved(node, opts, node.value, `Expected a valid color`);
  1024. }
  1025. } else {
  1026. return manageUnresolved(node, opts, node.value, `Expected a valid color-mod() function`);
  1027. }
  1028. } // return a transformed hex color
  1029. function transformHexColor(node, opts) {
  1030. if (hexColorMatch$1.test(node.value)) {
  1031. // #<hex-color>{3,4,6,8}
  1032. const _convertHtoRGB = convertHtoRGB(node.value),
  1033. _convertHtoRGB2 = _slicedToArray(_convertHtoRGB, 4),
  1034. red = _convertHtoRGB2[0],
  1035. green = _convertHtoRGB2[1],
  1036. blue = _convertHtoRGB2[2],
  1037. alpha = _convertHtoRGB2[3];
  1038. const color = new Color({
  1039. red,
  1040. green,
  1041. blue,
  1042. alpha
  1043. });
  1044. return color;
  1045. } else {
  1046. return manageUnresolved(node, opts, node.value, `Expected a valid hex color`);
  1047. }
  1048. } // return a transformed named-color
  1049. function transformNamedColor(node, opts) {
  1050. if (isNamedColor(node)) {
  1051. // <named-color>
  1052. const _convertNtoRGB = convertNtoRGB(node.value),
  1053. _convertNtoRGB2 = _slicedToArray(_convertNtoRGB, 3),
  1054. red = _convertNtoRGB2[0],
  1055. green = _convertNtoRGB2[1],
  1056. blue = _convertNtoRGB2[2];
  1057. const color = new Color({
  1058. red,
  1059. green,
  1060. blue,
  1061. alpha: 100,
  1062. colorspace: 'rgb'
  1063. });
  1064. return color;
  1065. } else {
  1066. return manageUnresolved(node, opts, node.value, `Expected a valid named-color`);
  1067. }
  1068. }
  1069. /* Transform <color-adjuster> functions
  1070. /* ========================================================================== */
  1071. // return a transformed color using adjustments
  1072. function transformColorByAdjusters(color, adjusterNodes, opts) {
  1073. const adjustedColor = adjusterNodes.reduce((base, node) => {
  1074. if (isAlphaBlueGreenRedAdjuster(node)) {
  1075. return transformAlphaBlueGreenRedAdjuster(base, node, opts);
  1076. } else if (isRGBAdjuster(node)) {
  1077. return transformRGBAdjuster(base, node, opts);
  1078. } else if (isHueAdjuster(node)) {
  1079. return transformHueAdjuster(base, node, opts);
  1080. } else if (isBlacknessLightnessSaturationWhitenessAdjuster(node)) {
  1081. return transformBlacknessLightnessSaturationWhitenessAdjuster(base, node, opts);
  1082. } else if (isShadeTintAdjuster(node)) {
  1083. return transformShadeTintAdjuster(base, node, opts);
  1084. } else if (isBlendAdjuster(node)) {
  1085. return transformBlendAdjuster(base, node, node.value === 'blenda', opts);
  1086. } else if (isContrastAdjuster(node)) {
  1087. return transformContrastAdjuster(base, node, opts);
  1088. } else {
  1089. manageUnresolved(node, opts, node.value, `Expected a valid color adjuster`);
  1090. return base;
  1091. }
  1092. }, color);
  1093. return adjustedColor;
  1094. } // return a transformed color using a/alpha/blue/green/red adjustments
  1095. function transformAlphaBlueGreenRedAdjuster(base, node, opts) {
  1096. const _transformArgsByParam12 = transformArgsByParams(node, alphaMatch.test(node.value) // a/alpha adjustments
  1097. ? [// [ + | - ] <alpha-value>
  1098. [transformMinusPlusOperator, transformAlpha], // * <percentage>
  1099. [transformTimesOperator, transformPercentage], // <alpha-value>
  1100. [transformAlpha]] // blue/green/red adjustments
  1101. : [// [ + | - ] <percentage>
  1102. [transformMinusPlusOperator, transformPercentage], // [ + | - ] <number>
  1103. [transformMinusPlusOperator, transformRGBNumber], // * <percentage>
  1104. [transformTimesOperator, transformPercentage], // <percentage>
  1105. [transformPercentage], // <number>
  1106. [transformRGBNumber]]),
  1107. _transformArgsByParam13 = _slicedToArray(_transformArgsByParam12, 2),
  1108. operatorOrValue = _transformArgsByParam13[0],
  1109. adjustment = _transformArgsByParam13[1];
  1110. if (operatorOrValue !== undefined) {
  1111. // normalized channel name
  1112. const channel = node.value.toLowerCase().replace(alphaMatch, 'alpha');
  1113. const existingValue = base[channel]();
  1114. const modifiedValue = adjustment !== undefined ? operatorOrValue === '+' ? existingValue + Number(adjustment) : operatorOrValue === '-' ? existingValue - Number(adjustment) : operatorOrValue === '*' ? existingValue * Number(adjustment) : Number(adjustment) : Number(operatorOrValue);
  1115. const modifiedColor = base[channel](modifiedValue);
  1116. return modifiedColor;
  1117. } else {
  1118. return manageUnresolved(node, opts, node.value, `Expected a valid modifier()`);
  1119. }
  1120. } // return a transformed color using an rgb adjustment
  1121. function transformRGBAdjuster(base, node, opts) {
  1122. const _transformArgsByParam14 = transformArgsByParams(node, [// [ + | - ] <percentage> <percentage> <percentage>
  1123. [transformMinusPlusOperator, transformPercentage, transformPercentage, transformPercentage], // [ + | - ] <number> <number> <number>
  1124. [transformMinusPlusOperator, transformRGBNumber, transformRGBNumber, transformRGBNumber], // [ + | - ] <hash-token>
  1125. [transformMinusPlusOperator, transformHexColor], // [ * ] <percentage>
  1126. [transformTimesOperator, transformPercentage]]),
  1127. _transformArgsByParam15 = _slicedToArray(_transformArgsByParam14, 4),
  1128. arg1 = _transformArgsByParam15[0],
  1129. arg2 = _transformArgsByParam15[1],
  1130. arg3 = _transformArgsByParam15[2],
  1131. arg4 = _transformArgsByParam15[3];
  1132. if (arg2 !== undefined && arg2.color) {
  1133. const modifiedColor = base.rgb(arg1 === '+' ? base.red() + arg2.red() : base.red() - arg2.red(), arg1 === '+' ? base.green() + arg2.green() : base.green() - arg2.green(), arg1 === '+' ? base.blue() + arg2.blue() : base.blue() - arg2.blue());
  1134. return modifiedColor;
  1135. } else if (arg1 !== undefined && minusPlusMatch.test(arg1)) {
  1136. const modifiedColor = base.rgb(arg1 === '+' ? base.red() + arg2 : base.red() - arg2, arg1 === '+' ? base.green() + arg3 : base.green() - arg3, arg1 === '+' ? base.blue() + arg4 : base.blue() - arg4);
  1137. return modifiedColor;
  1138. } else if (arg1 !== undefined && arg2 !== undefined) {
  1139. const modifiedColor = base.rgb(base.red() * arg2, base.green() * arg2, base.blue() * arg2);
  1140. return modifiedColor;
  1141. } else {
  1142. return manageUnresolved(node, opts, node.value, `Expected a valid rgb() adjuster`);
  1143. }
  1144. } // return a transformed color using a blend/blenda adjustment
  1145. function transformBlendAdjuster(base, node, isAlphaBlend, opts) {
  1146. const _transformArgsByParam16 = transformArgsByParams(node, [[transformColor, transformPercentage, transformColorSpace]]),
  1147. _transformArgsByParam17 = _slicedToArray(_transformArgsByParam16, 3),
  1148. color = _transformArgsByParam17[0],
  1149. percentage = _transformArgsByParam17[1],
  1150. _transformArgsByParam18 = _transformArgsByParam17[2],
  1151. colorspace = _transformArgsByParam18 === void 0 ? 'rgb' : _transformArgsByParam18;
  1152. if (percentage !== undefined) {
  1153. const modifiedColor = isAlphaBlend ? base.blenda(color.color, percentage, colorspace) : base.blend(color.color, percentage, colorspace);
  1154. return modifiedColor;
  1155. } else {
  1156. return manageUnresolved(node, opts, node.value, `Expected a valid blend() adjuster)`);
  1157. }
  1158. } // return a transformed color using a contrast adjustment
  1159. function transformContrastAdjuster(base, node, opts) {
  1160. const _transformArgsByParam19 = transformArgsByParams(node, [// <percentage>
  1161. [transformPercentage]]),
  1162. _transformArgsByParam20 = _slicedToArray(_transformArgsByParam19, 1),
  1163. percentage = _transformArgsByParam20[0];
  1164. if (percentage !== undefined) {
  1165. const modifiedColor = base.contrast(percentage);
  1166. return modifiedColor;
  1167. } else {
  1168. return manageUnresolved(node, opts, node.value, `Expected a valid contrast() adjuster)`);
  1169. }
  1170. } // return a transformed color using a hue adjustment
  1171. function transformHueAdjuster(base, node, opts) {
  1172. const _transformArgsByParam21 = transformArgsByParams(node, [// [ + | - | * ] <angle>
  1173. [transformMinusPlusTimesOperator, transformHue], // <angle>
  1174. [transformHue]]),
  1175. _transformArgsByParam22 = _slicedToArray(_transformArgsByParam21, 2),
  1176. operatorOrHue = _transformArgsByParam22[0],
  1177. adjustment = _transformArgsByParam22[1];
  1178. if (operatorOrHue !== undefined) {
  1179. const existingHue = base.hue();
  1180. const modifiedValue = adjustment !== undefined ? operatorOrHue === '+' ? existingHue + Number(adjustment) : operatorOrHue === '-' ? existingHue - Number(adjustment) : operatorOrHue === '*' ? existingHue * Number(adjustment) : Number(adjustment) : Number(operatorOrHue);
  1181. return base.hue(modifiedValue);
  1182. } else {
  1183. return manageUnresolved(node, opts, node.value, `Expected a valid hue() function)`);
  1184. }
  1185. } // [ b | blackness | l | lightness | s | saturation | w | whiteness ]( [ + | - | * ]? <percentage> )
  1186. function transformBlacknessLightnessSaturationWhitenessAdjuster(base, node, opts) {
  1187. const channel = node.value.toLowerCase().replace(/^b$/, 'blackness').replace(/^l$/, 'lightness').replace(/^s$/, 'saturation').replace(/^w$/, 'whiteness');
  1188. const _transformArgsByParam23 = transformArgsByParams(node, [[transformMinusPlusTimesOperator, transformPercentage], [transformPercentage]]),
  1189. _transformArgsByParam24 = _slicedToArray(_transformArgsByParam23, 2),
  1190. operatorOrValue = _transformArgsByParam24[0],
  1191. adjustment = _transformArgsByParam24[1];
  1192. if (operatorOrValue !== undefined) {
  1193. const existingValue = base[channel]();
  1194. const modifiedValue = adjustment !== undefined ? operatorOrValue === '+' ? existingValue + Number(adjustment) : operatorOrValue === '-' ? existingValue - Number(adjustment) : operatorOrValue === '*' ? existingValue * Number(adjustment) : Number(adjustment) : Number(operatorOrValue);
  1195. return base[channel](modifiedValue);
  1196. } else {
  1197. return manageUnresolved(node, opts, node.value, `Expected a valid ${channel}() function)`);
  1198. }
  1199. } // return a transformed color using shade/tint adjustments
  1200. function transformShadeTintAdjuster(base, node, opts) {
  1201. const channel = node.value.toLowerCase();
  1202. const _transformArgsByParam25 = transformArgsByParams(node, [// [ shade | tint ]( <percentage> )
  1203. [transformPercentage]]),
  1204. _transformArgsByParam26 = _slicedToArray(_transformArgsByParam25, 1),
  1205. percentage = _transformArgsByParam26[0];
  1206. if (percentage !== undefined) {
  1207. const modifiedValue = Number(percentage);
  1208. return base[channel](modifiedValue);
  1209. } else {
  1210. return manageUnresolved(node, opts, node.value, `Expected valid ${channel}() arguments`);
  1211. }
  1212. }
  1213. /* Argument Transforms
  1214. /* ========================================================================== */
  1215. // return a transformed color space
  1216. function transformColorSpace(node, opts) {
  1217. if (isColorSpace(node)) {
  1218. // [ hsl | hwb | rgb ]
  1219. return node.value;
  1220. } else {
  1221. return manageUnresolved(node, opts, node.value, `Expected a valid color space)`);
  1222. }
  1223. } // return a transformed alpha value
  1224. function transformAlpha(node, opts) {
  1225. if (isNumber(node)) {
  1226. // <number>
  1227. return node.value * 100;
  1228. } else if (isPercentage(node)) {
  1229. // <percentage>
  1230. return transformPercentage(node, opts);
  1231. } else {
  1232. return manageUnresolved(node, opts, node.value, `Expected a valid alpha value)`);
  1233. }
  1234. } // return a transformed rgb number
  1235. function transformRGBNumber(node, opts) {
  1236. if (isNumber(node)) {
  1237. // <number>
  1238. return node.value / 2.55;
  1239. } else {
  1240. return manageUnresolved(node, opts, node.value, `Expected a valid RGB value)`);
  1241. }
  1242. } // return a transformed hue
  1243. function transformHue(node, opts) {
  1244. if (isHue(node)) {
  1245. // <hue> = <number> | <angle>
  1246. const unit = node.unit.toLowerCase();
  1247. if (unit === 'grad') {
  1248. // if <angle> = <gradian> (400 per circle)
  1249. return convertGtoD(node.value);
  1250. } else if (unit === 'rad') {
  1251. // if <angle> = <radian> (2π per circle)
  1252. return convertRtoD(node.value);
  1253. } else if (unit === 'turn') {
  1254. // if <angle> = <turn> (1 per circle)
  1255. return convertTtoD(node.value);
  1256. } else {
  1257. // if <angle> = [ <degree> | <number> ] (360 per circle)
  1258. return convertDtoD(node.value);
  1259. }
  1260. } else {
  1261. return manageUnresolved(node, opts, node.value, `Expected a valid hue`);
  1262. }
  1263. } // return a transformed percentage
  1264. function transformPercentage(node, opts) {
  1265. if (isPercentage(node)) {
  1266. // <percentage>
  1267. return Number(node.value);
  1268. } else {
  1269. return manageUnresolved(node, opts, node.value, `Expected a valid hue`);
  1270. }
  1271. } // return a transformed minus-plus operator
  1272. function transformMinusPlusOperator(node, opts) {
  1273. if (isMinusPlusOperator(node)) {
  1274. // [ - | + ]
  1275. return node.value;
  1276. } else {
  1277. return manageUnresolved(node, opts, node.value, `Expected a plus or minus operator`);
  1278. }
  1279. } // return a transformed times operator
  1280. function transformTimesOperator(node, opts) {
  1281. if (isTimesOperator(node)) {
  1282. // [ * ]
  1283. return node.value;
  1284. } else {
  1285. return manageUnresolved(node, opts, node.value, `Expected a times operator`);
  1286. }
  1287. } // return a transformed minus-plus-times operator
  1288. function transformMinusPlusTimesOperator(node, opts) {
  1289. if (isMinusPlusTimesOperator(node)) {
  1290. // [ - | + | * ]
  1291. return node.value;
  1292. } else {
  1293. return manageUnresolved(node, opts, node.value, `Expected a plus, minus, or times operator`);
  1294. }
  1295. }
  1296. /* Additional transforms
  1297. /* ========================================================================== */
  1298. function transformWord(node, opts) {
  1299. if (isWord(node)) {
  1300. return node.value;
  1301. } else {
  1302. return manageUnresolved(node, opts, node.value, `Expected a valid word`);
  1303. }
  1304. }
  1305. function transformNode(node) {
  1306. return Object(node);
  1307. }
  1308. /* Transform helper
  1309. /* ========================================================================== */
  1310. // return the first set of transformed arguments allowable by the parameters
  1311. function transformArgsByParams(node, params) {
  1312. const nodes = (node.nodes || []).slice(1, -1);
  1313. const opts = {
  1314. unresolved: 'ignore'
  1315. };
  1316. return params.map(param => nodes.map((childNode, index) => typeof param[index] === 'function' ? param[index](childNode, opts) : undefined).filter(child => typeof child !== 'boolean')).filter(param => param.every(result => result !== undefined))[0] || [];
  1317. }
  1318. /* Walk helper (required because the default walker is affected by mutations)
  1319. /* ========================================================================== */
  1320. // run a function over each node and hen walk each child node of that node
  1321. function walk(node, fn) {
  1322. fn(node);
  1323. if (Object(node.nodes).length) {
  1324. node.nodes.slice().forEach(childNode => {
  1325. walk(childNode, fn);
  1326. });
  1327. }
  1328. }
  1329. /* Variable validators
  1330. /* ========================================================================== */
  1331. // return whether the node is a var function
  1332. function isVariable(node) {
  1333. // var()
  1334. return Object(node).type === 'func' && varMatch.test(node.value);
  1335. }
  1336. /* Adjustment validators
  1337. /* ========================================================================== */
  1338. // return whether the node is an a/alpha/blue/green/red adjuster
  1339. function isAlphaBlueGreenRedAdjuster(node) {
  1340. // [ a(), alpha(), blue(), green(), red() ]
  1341. return Object(node).type === 'func' && alphaBlueGreenRedMatch.test(node.value);
  1342. } // return whether the node is an rgb adjuster
  1343. function isRGBAdjuster(node) {
  1344. return Object(node).type === 'func' && rgbMatch.test(node.value);
  1345. } // return whether the node is a hue adjuster
  1346. function isHueAdjuster(node) {
  1347. // [ h() | hue() ]
  1348. return Object(node).type === 'func' && hueMatch.test(node.value);
  1349. } // return whether the node is a blackness/lightness/saturation/whiteness adjuster
  1350. function isBlacknessLightnessSaturationWhitenessAdjuster(node) {
  1351. // [ b() | blackness() | l() | lightness() | s() | saturation() | w() | whiteness() ]
  1352. return Object(node).type === 'func' && blacknessLightnessSaturationWhitenessMatch.test(node.value);
  1353. } // return whether the node is a shade/tint adjuster
  1354. function isShadeTintAdjuster(node) {
  1355. // [ shade() | tint() ]
  1356. return Object(node).type === 'func' && shadeTintMatch.test(node.value);
  1357. } // return whether the node is a blend adjuster
  1358. function isBlendAdjuster(node) {
  1359. // [ blend(), blenda() ]
  1360. return Object(node).type === 'func' && blendMatch.test(node.value);
  1361. } // return whether the node is a contrast adjuster
  1362. function isContrastAdjuster(node) {
  1363. // [ contrast() ]
  1364. return Object(node).type === 'func' && contrastMatch.test(node.value);
  1365. }
  1366. /* Color validators
  1367. /* ========================================================================== */
  1368. // return whether the node is an rgb/rgba color function
  1369. function isRGBFunction(node) {
  1370. // [ rgb(), rgba() ]
  1371. return Object(node).type === 'func' && rgbaMatch.test(node.value);
  1372. } // return whether the node is an hsl color function
  1373. function isHSLFunction(node) {
  1374. // [ hsl(), hsla() ]
  1375. return Object(node).type === 'func' && hslaMatch.test(node.value);
  1376. } // return whether the node is an hwb color function
  1377. function isHWBFunction(node) {
  1378. // hwb()
  1379. return Object(node).type === 'func' && hwbMatch.test(node.value);
  1380. } // return whether the node is a color-mod function
  1381. function isColorModFunction(node) {
  1382. // color-mod()
  1383. return Object(node).type === 'func' && colorModMatch.test(node.value);
  1384. } // return whether the node is a valid named-color
  1385. function isNamedColor(node) {
  1386. return Object(node).type === 'word' && Boolean(convertNtoRGB(node.value));
  1387. } // return whether the node is a valid hex color
  1388. function isHexColor(node) {
  1389. // #<hex-color>{3,4,6,8}
  1390. return Object(node).type === 'word' && hexColorMatch$1.test(node.value);
  1391. } // return whether the node is a valid color space
  1392. function isColorSpace(node) {
  1393. // [ hsl | hwb | rgb ]
  1394. return Object(node).type === 'word' && colorSpaceMatch.test(node.value);
  1395. }
  1396. /* Additional validators
  1397. /* ========================================================================== */
  1398. // return whether the hue value is valid
  1399. function isHue(node) {
  1400. return Object(node).type === 'number' && hueUnitMatch.test(node.unit);
  1401. } // return whether the comma is valid
  1402. function isComma(node) {
  1403. return Object(node).type === 'comma';
  1404. } // return whether the slash operator is valid
  1405. function isSlash(node) {
  1406. return Object(node).type === 'operator' && node.value === '/';
  1407. } // return whether the number is valid
  1408. function isNumber(node) {
  1409. return Object(node).type === 'number' && node.unit === '';
  1410. } // return whether the mind-plus operator is valid
  1411. function isMinusPlusOperator(node) {
  1412. return Object(node).type === 'operator' && minusPlusMatch.test(node.value);
  1413. } // return whether the minus-plus-times operator is valid
  1414. function isMinusPlusTimesOperator(node) {
  1415. return Object(node).type === 'operator' && minusPlusTimesMatch.test(node.value);
  1416. } // return whether the times operator is valid
  1417. function isTimesOperator(node) {
  1418. return Object(node).type === 'operator' && timesMatch.test(node.value);
  1419. } // return whether the percentage is valid
  1420. function isPercentage(node) {
  1421. return Object(node).type === 'number' && (node.unit === '%' || node.value === '0');
  1422. } // return whether the node is a word
  1423. function isWord(node) {
  1424. // <word>
  1425. return Object(node).type === 'word';
  1426. }
  1427. /* Matchers
  1428. /* ========================================================================== */
  1429. const alphaMatch = /^a(lpha)?$/i;
  1430. const alphaBlueGreenRedMatch = /^(a(lpha)?|blue|green|red)$/i;
  1431. const blacknessLightnessSaturationWhitenessMatch = /^(b(lackness)?|l(ightness)?|s(aturation)?|w(hiteness)?)$/i;
  1432. const blendMatch = /^blenda?$/i;
  1433. const colorModMatch = /^color-mod$/i;
  1434. const colorSpaceMatch = /^(hsl|hwb|rgb)$/i;
  1435. const contrastMatch = /^contrast$/i;
  1436. const hexColorMatch$1 = /^#(?:([a-f0-9])([a-f0-9])([a-f0-9])([a-f0-9])?|([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?)$/i;
  1437. const hslaMatch = /^hsla?$/i;
  1438. const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
  1439. const hueMatch = /^h(ue)?$/i;
  1440. const hwbMatch = /^hwb$/i;
  1441. const minusPlusMatch = /^[+-]$/;
  1442. const minusPlusTimesMatch = /^[*+-]$/;
  1443. const rgbMatch = /^rgb$/i;
  1444. const rgbaMatch = /^rgba?$/i;
  1445. const shadeTintMatch = /^(shade|tint)$/i;
  1446. const varMatch = /^var$/i;
  1447. const looseVarMatch = /(^|[^\w-])var\(/i;
  1448. const timesMatch = /^[*]$/;
  1449. var index = postcss.plugin('postcss-color-mod-function', opts => {
  1450. // how unresolved functions and arguments should be handled (default: "throw")
  1451. const unresolvedOpt = String(Object(opts).unresolved || 'throw').toLowerCase(); // how transformed colors will be produced in CSS
  1452. const stringifierOpt = Object(opts).stringifier || (color => color.toLegacy()); // sources to import custom selectors from
  1453. const importFrom = [].concat(Object(opts).importFrom || []); // whether var() within color-mod() should use Custom Properties or var() fallback
  1454. const transformVarsOpt = 'transformVars' in Object(opts) ? opts.transformVars : true; // promise any custom selectors are imported
  1455. const customPropertiesPromise = importCustomPropertiesFromSources(importFrom);
  1456. return (
  1457. /*#__PURE__*/
  1458. function () {
  1459. var _ref = _asyncToGenerator(function* (root, result) {
  1460. const customProperties = Object.assign((yield customPropertiesPromise), getCustomProperties(root, {
  1461. preserve: true
  1462. }));
  1463. root.walkDecls(decl => {
  1464. const originalValue = decl.value;
  1465. if (colorModFunctionMatch.test(originalValue)) {
  1466. const ast = valueParser(originalValue, {
  1467. loose: true
  1468. }).parse();
  1469. transformAST(ast, {
  1470. unresolved: unresolvedOpt,
  1471. stringifier: stringifierOpt,
  1472. transformVars: transformVarsOpt,
  1473. decl,
  1474. result,
  1475. customProperties
  1476. });
  1477. const modifiedValue = ast.toString();
  1478. if (originalValue !== modifiedValue) {
  1479. decl.value = modifiedValue;
  1480. }
  1481. }
  1482. });
  1483. });
  1484. return function (_x, _x2) {
  1485. return _ref.apply(this, arguments);
  1486. };
  1487. }()
  1488. );
  1489. });
  1490. const colorModFunctionMatch = /(^|[^\w-])color-mod\(/i;
  1491. module.exports = index;
  1492. //# sourceMappingURL=index.cjs.js.map