index.es.mjs 59 KB

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