linear-gradient-parser
Version:
Parses a SVG linear gradient string / parsed JSON into css background image property
2 lines • 7.94 kB
JavaScript
!function(t,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var e,n=r();for(e in n)("object"==typeof exports?exports:t)[e]=n[e]}}("undefined"!=typeof self?self:this,function(){return n={},o.m=e=[function(t,r,e){"use strict";e.r(r);function n(t){return parseInt(t.repeat(3-t.length),16)}var o=new RegExp(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i),i=new RegExp(/^#?([a-f\d])([a-f\d])([a-f\d])$/i),e=function(t){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:1,t=o.exec(t)||i.exec(t);return t?{r:n(t[1]),g:n(t[2]),b:n(t[3]),a:r}:void 0},u=function(t){var r=t.r,e=t.g,n=t.b,t=t.a,t=void 0===t?1:t;return 1!==t?"rgba(".concat(r,", ").concat(e,", ").concat(n,", ").concat(t,")"):"rgb(".concat(r,", ").concat(e,", ").concat(n,")")};function c(t){return function(t){if(Array.isArray(t))return t}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,r){if(!t)return;if("string"==typeof t)return a(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);"Object"===e&&t.constructor&&(e=t.constructor.name);if("Map"===e||"Set"===e)return Array.from(t);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return a(t,r)}(t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function f(t){return Number(t)}var l=new RegExp(/\d+/g),s=[{regexps:[o,i],handler:e},{regexps:[l],handler:function(t){var r=c(t.match(l)),e=r[0],n=r[1],o=r[2],t=r.slice(3).join(".")||1;return r=t,[e,n,o].every(function(t){return 0<=t&&t<=255})&&(!r||0<=r&&r<=1)?{r:f(e),g:f(n),b:f(o),a:f(t)}:void 0}}],p=function(r,t){var e=s.find(function(t){return t.regexps.some(function(t){return t.test(r)})});if(!e)throw new Error("Stop color - ".concat(r," does not follow one of the accepted formats Hex / Rgb / Rgba "));t=e.handler(r,t);return u(t)},y=function(t){return null!=t},b=function(t){return t<(1<arguments.length&&void 0!==arguments[1]?arguments[1]:0)?360+t:(2<arguments.length&&void 0!==arguments[2]?arguments[2]:360)<t?t-360:t};function d(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var e=[],n=!0,o=!1,i=void 0;try{for(var u,c=t[Symbol.iterator]();!(n=(u=c.next()).done)&&(e.push(u.value),!r||e.length!==r);n=!0);}catch(t){o=!0,i=t}finally{try{n||null==c.return||c.return()}finally{if(o)throw i}}return e}(t,r)||function(t,r){if(!t)return;if("string"==typeof t)return g(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);"Object"===e&&t.constructor&&(e=t.constructor.name);if("Map"===e||"Set"===e)return Array.from(t);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return g(t,r)}(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function m(t){return Number("".concat(t).trim().endsWith("%")?t.trim().replace("%",""):t)}function v(t){var r=t.getAttribute(x);if(r){var e=t.getAttribute(A);return p(r,e)}return e=t.getAttribute(P),(t=document.createElement("div")).setAttribute("style",e),e=t.style,t=e[x],e=e[A],t?p(t,e):void 0}function O(t){return t<=0||Math.abs(t)<=I?0:t}function h(t){return{x:O(Math.cos(t)),y:O(Math.sin(t))}}function j(t){return t*Math.PI/180}var w,S=function(t){var r=t.x1,e=t.x2,n=t.y1,o=t.y2,i=d([r,e,n,o].map(m),4),r=i[0],e=i[1],n=i[2],t=e-r,i=(o=i[3])-n;if(0==i)return e<r?270:90;if(0==t)return o<n?0:180;t=Math.atan2(i,t);return b(180*t/Math.PI+90)},P="style",x="stop-color",A="stop-opacity",E="offset",M=["x1","x2","y1","y2"],D=function(t){return Array.from(t.querySelectorAll("stop")).map(function(t){var r=_(t.getAttribute(E)),t=v(t);return{offset:Number(r),color:t}})},I=Math.pow(2,-52),N=function(){var t=(360-(0<arguments.length&&void 0!==arguments[0]?arguments[0]:0))%360;return{startPoint:h(j(90-t)),endPoint:h(j(270-t))}},_=function(t){return(t=t.toString().trim()).endsWith("%")?Number(t.replace("%","")):100*Number(t)};function C(r,t){var e,n=Object.keys(r);return Object.getOwnPropertySymbols&&(e=Object.getOwnPropertySymbols(r),t&&(e=e.filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable})),n.push.apply(n,e)),n}function G(r){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?C(Object(e),!0).forEach(function(t){R(r,t,e[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(e)):C(Object(e)).forEach(function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(e,t))})}return r}function R(t,r,e){return r in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function k(t){return(k="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function T(r,t){var e,n=Object.keys(r);return Object.getOwnPropertySymbols&&(e=Object.getOwnPropertySymbols(r),t&&(e=e.filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable})),n.push.apply(n,e)),n}function $(n){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?T(Object(o),!0).forEach(function(t){var r,e;r=n,t=o[e=t],e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):T(Object(o)).forEach(function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(o,t))})}return n}var q={string:function(t){var e,t=(w=w||new DOMParser).parseFromString(t,"image/svg+xml").querySelector("linearGradient");if(!t)throw new Error("Couldn't parse svg string into linearGradient SVGElement");return G(G({},(e=t,M.reduce(function(t,r){return Object.assign(t,R({},r,e.getAttribute(r)))},{}))),{},{stops:D(t).filter(function(t){var r=t.offset,t=t.color;return y(r)&&y(t)})})},object:function(t){return $($({},t),{},{stops:t.stops.map(function(t){var r=t.offset,e=t.color,t=t.opacity;return{offset:_(r),color:p(e,t)}})})}},e={getBackground:function(t){var r=k(t),r=q[r];if(!r)throw new Error("Cannot parse non JSON / SVG String input");r=(t=r(t)).stops||t.children,t=S(t);return{angle:t,background:(r=(t={angle:t,stops:r}).angle,1===(t=t.stops).length?t[0].color:"linear-gradient(".concat(r,"deg, ").concat(t.map(function(t){return"".concat(t.color," ").concat(t.offset,"%")}).join(", "),")"))}},getGradientCords:function(t){var r=N(t),t=r.startPoint,r=r.endPoint;return{x1:t.x,y1:t.y,x2:r.x,y2:r.y}}};r.default=e}],o.c=n,o.d=function(t,r,e){o.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:e})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(r,t){if(1&t&&(r=o(r)),8&t)return r;if(4&t&&"object"==typeof r&&r&&r.__esModule)return r;var e=Object.create(null);if(o.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:r}),2&t&&"string"!=typeof r)for(var n in r)o.d(e,n,function(t){return r[t]}.bind(null,n));return e},o.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(r,"a",r),r},o.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},o.p="",o(o.s=0);function o(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,o),r.l=!0,r.exports}var e,n});
//# sourceMappingURL=index.js.map