utils.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. export function getBackgroundColor(color) {
  13. if (typeof color === 'string') {
  14. return color;
  15. }
  16. if (Array.isArray(color)) {
  17. if (color[0] && color[0][0] === '#') {
  18. color.unshift('90deg');
  19. }
  20. return `linear-gradient( ${color.join(',')} )`;
  21. }
  22. const { from, to, direction = 'to right' } = color, rest = __rest(color, ["from", "to", "direction"]);
  23. let keys = Object.keys(rest);
  24. if (keys.length) {
  25. keys = keys.sort((a, b) => parseFloat(a.substr(0, a.length - 1)) - parseFloat(b.substr(0, b.length - 1)));
  26. const tempArr = keys.map((key) => `${rest[key]} ${key}`);
  27. return `linear-gradient(${direction}, ${tempArr.join(',')})`;
  28. }
  29. return `linear-gradient(${direction}, ${from}, ${to})`;
  30. }