(function(a,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(a=typeof globalThis<"u"?globalThis:a||self,s(a.colourspace={}))})(this,function(a){"use strict";var L=Object.defineProperty;var y=(a,s,g)=>s in a?L(a,s,{enumerable:!0,configurable:!0,writable:!0,value:g}):a[s]=g;var b=(a,s,g)=>(y(a,typeof s!="symbol"?s+"":s,g),g);class s{constructor(t,o){b(this,"colour");b(this,"mapping",{hex:{from:this.fromHex,to:this.toHex},rgb:{from:this.fromRgb,to:this.toRgb},hsl:{from:this.fromHsl,to:this.toHsl}});this.colour=this.processColour(t,o)}toHex(t=!1){const{red:o,green:n,blue:e}=this.colour,l=`#${this.padLeft(o.toString(16))}${this.padLeft(n.toString(16))}${this.padLeft(e.toString(16))}`;return t?l.slice(1):l}toHexa(t=!1){const{red:o,green:n,blue:e,alpha:l}=this.colour,h=`#${this.padLeft(o.toString(16))}${this.padLeft(n.toString(16))}${this.padLeft(e.toString(16))}${this.padLeft(Math.round(l*255).toString(16))}`;return t?h.slice(1):h}toRgb(t=!1){const{red:o,green:n,blue:e}=this.colour,l=`${o}, ${n}, ${e}`;return t?l:`rgb(${l})`}toRgba(t=!1){const{red:o,green:n,blue:e,alpha:l}=this.colour,h=`${o}, ${n}, ${e}, ${l}`;return t?h:`rgba(${h})`}toHsl(t=!1){let{red:o,green:n,blue:e}=this.colour;o/=255,n/=255,e/=255;let l=Math.min(o,n,e),h=Math.max(o,n,e),u=h-l,c=0,i=0,f=0;u==0?c=0:h==o?c=(n-e)/u%6:h==n?c=(e-o)/u+2:c=(o-n)/u+4,c=Math.round(c*60),f=(h+l)/2,i=u==0?0:u/(1-Math.abs(2*f-1)),i=+(i*100).toFixed(1),f=+(f*100).toFixed(1);const d=`${Math.round(c)}, ${Math.round(i)}%, ${Math.round(f)}%`;return t?d:`hsl(${d})`}toHsla(t=!1){const{alpha:o}=this.colour,e=`${this.toHsl(!0)}, ${o}`;return t?e:`hsla(${e})`}toContrast(t){const{red:o,green:n,blue:e}=this.colour,l=Math.round((o*299+n*587+e*114)/1e3);return new s(l>125?(t==null?void 0:t.dark)??"#000000":(t==null?void 0:t.light)??"#FFFFFF")}getRaw(){return this.colour}fromHex(t){t=t.startsWith("#")?t:`#${t}`;const n=t.length===9?parseFloat((parseInt(t.slice(7,9),16)/255).toPrecision(2)):1,e=parseInt(t.slice(1,3),16),l=parseInt(t.slice(3,5),16),h=parseInt(t.slice(5,7),16);return{red:e,green:l,blue:h,alpha:n}}fromRgb(t){let[o,n,e,l]=t.split(t.includes(",")?",":" ").filter(h=>h!=="/").map(h=>h.replace(/[^0-9.]/g,"")).map(h=>h.includes(".")?parseFloat(h):parseInt(h));return t.includes("/")&&(l=l/100),{red:o,green:n,blue:e,alpha:l??1}}fromHsl(t){let[o,n,e,l]=t.split(t.includes(",")?",":" ").filter(i=>i!=="/").map(i=>i.replace(/[^0-9.]/g,"")).map(i=>i.includes(".")?parseFloat(i):parseInt(i));t.includes("/")&&(l=l/100),n/=100,e/=100;const h=i=>(i+o/30)%12,u=n*Math.min(e,1-e),c=i=>e-u*Math.max(-1,Math.min(h(i)-3,Math.min(9-h(i),1)));return{red:Math.round(255*c(0)),green:Math.round(255*c(8)),blue:Math.round(255*c(4)),alpha:l??1}}processColour(t,o){const n=o??this.guessFormat(t),e=this.mapping[n];if(!e)throw new Error("Invalid colour format type given.");return e.from(t)}guessFormat(t){if(t.startsWith("#"))return"hex";if(t.startsWith("rgb"))return"rgb";if(t.startsWith("hsl"))return"hsl";throw new Error("Unable to guess colour format, please define it manually.")}padLeft(t){return t.length===1?`0${t}`:t}}const g=(r,t=!1)=>new s(r).toRgb(t),p=(r,t=!1)=>new s(r).toRgba(t),H=(r,t=!1)=>new s(r).toHsl(t),m=(r,t=!1)=>new s(r).toHsla(t),T=(r,t=!1)=>new s(r,"rgb").toHex(t),$=(r,t=!1)=>new s(r,"rgb").toHexa(t),w=(r,t=!1)=>new s(r,"rgb").toHsl(t),x=(r,t=!1)=>new s(r,"rgb").toHsla(t),M=(r,t=!1)=>new s(r,"hsl").toHex(t),R=(r,t=!1)=>new s(r,"hsl").toHexa(t),F=(r,t=!1)=>new s(r,"hsl").toRgb(t),S=(r,t=!1)=>new s(r,"hsl").toRgba(t);a.default=s,a.hexToHsl=H,a.hexToHsla=m,a.hexToRgb=g,a.hexToRgba=p,a.hslToHex=M,a.hslToHexa=R,a.hslToRgb=F,a.hslToRgba=S,a.rgbToHex=T,a.rgbToHexa=$,a.rgbToHsl=w,a.rgbToHsla=x,Object.defineProperties(a,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});