withAlphaVariable.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. function _export(target, all) {
  6. for(var name in all)Object.defineProperty(target, name, {
  7. enumerable: true,
  8. get: all[name]
  9. });
  10. }
  11. _export(exports, {
  12. withAlphaValue: function() {
  13. return withAlphaValue;
  14. },
  15. default: function() {
  16. return withAlphaVariable;
  17. }
  18. });
  19. const _color = require("./color");
  20. function withAlphaValue(color, alphaValue, defaultValue) {
  21. if (typeof color === "function") {
  22. return color({
  23. opacityValue: alphaValue
  24. });
  25. }
  26. let parsed = (0, _color.parseColor)(color, {
  27. loose: true
  28. });
  29. if (parsed === null) {
  30. return defaultValue;
  31. }
  32. return (0, _color.formatColor)({
  33. ...parsed,
  34. alpha: alphaValue
  35. });
  36. }
  37. function withAlphaVariable({ color , property , variable }) {
  38. let properties = [].concat(property);
  39. if (typeof color === "function") {
  40. return {
  41. [variable]: "1",
  42. ...Object.fromEntries(properties.map((p)=>{
  43. return [
  44. p,
  45. color({
  46. opacityVariable: variable,
  47. opacityValue: `var(${variable})`
  48. })
  49. ];
  50. }))
  51. };
  52. }
  53. const parsed = (0, _color.parseColor)(color);
  54. if (parsed === null) {
  55. return Object.fromEntries(properties.map((p)=>[
  56. p,
  57. color
  58. ]));
  59. }
  60. if (parsed.alpha !== undefined) {
  61. // Has an alpha value, return color as-is
  62. return Object.fromEntries(properties.map((p)=>[
  63. p,
  64. color
  65. ]));
  66. }
  67. return {
  68. [variable]: "1",
  69. ...Object.fromEntries(properties.map((p)=>{
  70. return [
  71. p,
  72. (0, _color.formatColor)({
  73. ...parsed,
  74. alpha: `var(${variable})`
  75. })
  76. ];
  77. }))
  78. };
  79. }