@nauverse/color-to-hsla
Version:
🧪 A tiny URL builder library for TypeScript.
3 lines (2 loc) • 6.25 kB
JavaScript
(function(g,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(g=typeof globalThis<"u"?globalThis:g||self,b(g["color-to-hsla"]={}))})(this,function(g){"use strict";const b=/^#([0-9a-f]{3})$/,m=/^#([0-9a-f]{6})$/,F=/^rgb\(\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*\)$/,R=/^rgb\(\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*\)$/,M=/^rgba\(\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?[\d\.]+)\s*\)$/,I=/^rgba\(\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?[\d\.]+)\s*\)$/,$=/^hsl\(\s*([-+]?\d+(?:\.\d+)?)\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*\)$/,H=/^hsla\(\s*([-+]?\d+(?:\.\d+)?)\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?[\d\.]+)\s*\)$/,v={aliceblue:15792383,antiquewhite:16444375,aqua:65535,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,black:0,blanchedalmond:16772045,blue:255,blueviolet:9055202,brown:10824234,burlywood:14596231,cadetblue:6266528,chartreuse:8388352,chocolate:13789470,coral:16744272,cornflowerblue:6591981,cornsilk:16775388,crimson:14423100,cyan:65535,darkblue:139,darkcyan:35723,darkgoldenrod:12092939,darkgray:11119017,darkgreen:25600,darkgrey:11119017,darkkhaki:12433259,darkmagenta:9109643,darkolivegreen:5597999,darkorange:16747520,darkorchid:10040012,darkred:9109504,darksalmon:15308410,darkseagreen:9419919,darkslateblue:4734347,darkslategray:3100495,darkslategrey:3100495,darkturquoise:52945,darkviolet:9699539,deeppink:16716947,deepskyblue:49151,dimgray:6908265,dimgrey:6908265,dodgerblue:2003199,firebrick:11674146,floralwhite:16775920,forestgreen:2263842,fuchsia:16711935,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,gray:8421504,green:32768,greenyellow:11403055,grey:8421504,honeydew:15794160,hotpink:16738740,indianred:13458524,indigo:4915330,ivory:16777200,khaki:15787660,lavender:15132410,lavenderblush:16773365,lawngreen:8190976,lemonchiffon:16775885,lightblue:11393254,lightcoral:15761536,lightcyan:14745599,lightgoldenrodyellow:16448210,lightgray:13882323,lightgreen:9498256,lightgrey:13882323,lightpink:16758465,lightsalmon:16752762,lightseagreen:2142890,lightskyblue:8900346,lightslategray:7833753,lightslategrey:7833753,lightsteelblue:11584734,lightyellow:16777184,lime:65280,limegreen:3329330,linen:16445670,magenta:16711935,maroon:8388608,mediumaquamarine:6737322,mediumblue:205,mediumorchid:12211667,mediumpurple:9662683,mediumseagreen:3978097,mediumslateblue:8087790,mediumspringgreen:64154,mediumturquoise:4772300,mediumvioletred:13047173,midnightblue:1644912,mintcream:16121850,mistyrose:16770273,moccasin:16770229,navajowhite:16768685,navy:128,oldlace:16643558,olive:8421376,olivedrab:7048739,orange:16753920,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,purple:8388736,rebeccapurple:6697881,red:16711680,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,silver:12632256,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,teal:32896,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,white:16777215,whitesmoke:16119285,yellow:16776960,yellowgreen:10145074},k={h:0,s:0,l:0,a:0};function T(e){if(typeof e=="string")return q(e);if(typeof e!="object")throw new Error("Must pass string or object");const s=e.a==null?1:e.a;if(w(e,"hsl"))return y((e==null?void 0:e.h)??0,(e==null?void 0:e.s)??0,(e==null?void 0:e.l)??0,s);if(w(e,"rgb"))return h((e==null?void 0:e.r)??0,(e==null?void 0:e.g)??0,(e==null?void 0:e.b)??0,s);throw new Error("Could not parse argument")}function w(e,s){const t=s.split("");let a=!1;for(let i=0;i<t.length;i++){const o=t[i];if(typeof o<"u"&&o in e)a=!0;else if(typeof o>"u"&&a)throw new Error('Missing key "'+o+'" in color type '+s)}return a}function q(e){try{e=`${e}`.trim().toLowerCase();const s=$.exec(e);if(s){const n=s.length>=2?Math.round(parseInt(s[1]??"0",10)):0,l=s.length>=3?parseFloat(s[2]??"0"):0,r=s.length>=4?parseFloat(s[3]??"0"):0;return y(n,l/100,r/100,1)}const t=H.exec(e);if(t){const n=t.length>=2?Math.round(parseInt(t[1]??"0",10)):0,l=t.length>=3?parseFloat(t[2]??"0"):0,r=t.length>=4?parseFloat(t[3]??"0"):0,d=t.length>=5?parseFloat(t[4]??"0"):0;return y(n,l/100,r/100,d)}const a=b.exec(e);if(a){const n=parseInt(a[1]??"0",16);return h(n>>8&15|n>>4&240,n>>4&15|n&240,(n&15)<<4|n&15,1)}const i=m.exec(e);if(i){const n=parseInt(i[1]??"0",16);return f(n)}const o=F.exec(e);if(o){const n=parseInt(o[1]??"0",10),l=parseInt(o[2]??"0",10),r=parseInt(o[3]??"0",10);return h(n,l,r,1)}const c=R.exec(e);if(c){const n=parseFloat(c[1]??"0")*255/100,l=parseFloat(c[2]??"0")*255/100,r=parseFloat(c[3]??"0")*255/100;return h(n,l,r,1)}const p=M.exec(e);if(p){const n=parseInt(p[1]??"0",10),l=parseInt(p[2]??"0",10),r=parseInt(p[3]??"0",10),d=parseFloat(p[4]??"0");return x(n,l,r,d)}const u=I.exec(e);if(u){const n=parseFloat(u[1]??"0")*255/100,l=parseFloat(u[2]??"0")*255/100,r=parseFloat(u[3]??"0")*255/100,d=parseFloat(u[4]??"0");return x(n,l,r,d)}return v[e]?f(v[e]):e==="transparent"?h(0,0,0,0):k}catch{return k}}function f(e){return h(e>>16&255,e>>8&255,e&255,1)}function x(e,s,t,a){return h(e,s,t,a)}function h(e,s,t,a){const i=+e/255,o=+s/255,c=+t/255,p=+a,u=Math.min(i,o,c),n=Math.max(i,o,c);let l=0,r=n-u;const d=(n+u)/2;return r?(i===n?l=(o-c)/r+(o<c?1:0)*6:o===n?l=(c-i)/r+2:l=(i-o)/r+4,r/=d<.5?n+u:2-n-u,l*=60):r=d>0&&d<1?0:l,{h:Math.round(l),s:isNaN(r)?0:r,l:isNaN(d)?0:d,a:p}}function y(e,s,t,a){return{h:+e,s:+s,l:+t,a:+a}}function P(e){const s=Math.round(Math.max(0,Math.min(360,e.h))).toFixed(0),t=(Math.max(0,Math.min(1,e.s))*100).toFixed(0),a=(Math.max(0,Math.min(1,e.l))*100).toFixed(0),i=parseFloat(e.a.toFixed(1));return`hsla(${s}, ${t}%, ${a}%, ${i})`}g.colorToHSLA=T,g.hslaToString=P,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=color-to-hsla.umd.cjs.map