/**
 * Skipped minification because the original files appears to be already minified.
 * Original file: /npm/modern-palette@2.0.0/dist/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
(function(w,C){typeof exports=="object"&&typeof module<"u"?C(exports):typeof define=="function"&&define.amd?define(["exports"],C):(w=typeof globalThis<"u"?globalThis:w||self,C(w.modernPalette={}))})(this,function(w){"use strict";const C=typeof window<"u",f=(1<<16)-1,x=f*f,U=(1<<9)-1,S=[0,20,40,60,80,99,119,139,159,179,199,219,241,264,288,313,340,367,396,427,458,491,526,562,599,637,677,718,761,805,851,898,947,997,1048,1101,1156,1212,1270,1330,1391,1453,1517,1583,1651,1720,1790,1863,1937,2013,2090,2170,2250,2333,2418,2504,2592,2681,2773,2866,2961,3058,3157,3258,3360,3464,3570,3678,3788,3900,4014,4129,4247,4366,4488,4611,4736,4864,4993,5124,5257,5392,5530,5669,5810,5953,6099,6246,6395,6547,6700,6856,7014,7174,7335,7500,7666,7834,8004,8177,8352,8528,8708,8889,9072,9258,9445,9635,9828,10022,10219,10417,10619,10822,11028,11235,11446,11658,11873,12090,12309,12530,12754,12980,13209,13440,13673,13909,14146,14387,14629,14874,15122,15371,15623,15878,16135,16394,16656,16920,17187,17456,17727,18001,18277,18556,18837,19121,19407,19696,19987,20281,20577,20876,21177,21481,21787,22096,22407,22721,23038,23357,23678,24002,24329,24658,24990,25325,25662,26001,26344,26688,27036,27386,27739,28094,28452,28813,29176,29542,29911,30282,30656,31033,31412,31794,32179,32567,32957,33350,33745,34143,34544,34948,35355,35764,36176,36591,37008,37429,37852,38278,38706,39138,39572,40009,40449,40891,41337,41785,42236,42690,43147,43606,44069,44534,45002,45473,45947,46423,46903,47385,47871,48359,48850,49344,49841,50341,50844,51349,51858,52369,52884,53401,53921,54445,54971,55500,56032,56567,57105,57646,58190,58737,59287,59840,60396,60955,61517,62082,62650,63221,63795,64372,64952,65535],R=[0,6,13,18,22,25,28,31,34,36,38,40,42,44,46,48,50,51,53,54,56,57,59,60,61,62,64,65,66,67,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,86,87,88,89,90,91,91,92,93,94,95,95,96,97,98,98,99,100,101,101,102,103,103,104,105,106,106,107,108,108,109,110,110,111,111,112,113,113,114,115,115,116,116,117,118,118,119,119,120,121,121,122,122,123,123,124,125,125,126,126,127,127,128,128,129,129,130,130,131,132,132,133,133,134,134,135,135,136,136,137,137,138,138,139,139,140,140,140,141,141,142,142,143,143,144,144,145,145,146,146,147,147,147,148,148,149,149,150,150,151,151,151,152,152,153,153,154,154,154,155,155,156,156,156,157,157,158,158,159,159,159,160,160,161,161,161,162,162,163,163,163,164,164,165,165,165,166,166,166,167,167,168,168,168,169,169,169,170,170,171,171,171,172,172,172,173,173,174,174,174,175,175,175,176,176,176,177,177,177,178,178,179,179,179,180,180,180,181,181,181,182,182,182,183,183,183,184,184,184,185,185,185,186,186,186,187,187,187,188,188,188,189,189,189,190,190,190,191,191,191,192,192,192,193,193,193,193,194,194,194,195,195,195,196,196,196,197,197,197,198,198,198,198,199,199,199,200,200,200,201,201,201,201,202,202,202,203,203,203,204,204,204,204,205,205,205,206,206,206,206,207,207,207,208,208,208,208,209,209,209,210,210,210,210,211,211,211,212,212,212,212,213,213,213,214,214,214,214,215,215,215,215,216,216,216,217,217,217,217,218,218,218,218,219,219,219,220,220,220,220,221,221,221,221,222,222,222,222,223,223,223,224,224,224,224,225,225,225,225,226,226,226,226,227,227,227,227,228,228,228,228,229,229,229,229,230,230,230,230,231,231,231,231,232,232,232,232,233,233,233,233,234,234,234,234,235,235,235,235,236,236,236,236,237,237,237,237,238,238,238,238,239,239,239,239,239,240,240,240,240,241,241,241,241,242,242,242,242,243,243,243,243,243,244,244,244,244,245,245,245,245,246,246,246,246,246,247,247,247,247,248,248,248,248,249,249,249,249,249,250,250,250,250,251,251,251,251,251,252,252,252,252,253,253,253,253,253,254,254,254,254,255,255,255];function A(l){let t;if(l<=0)return 0;if(l>=f)return f;t=l*(l*(l+-144107)/f+132114)/f+14379;for(let e=0;e<2;e++){const n=t*t*t,s=l+(2*n+x/2)/x;t=(t*(2*l+(n+x/2)/x)+s/2)/s}return t}function _(l,t){return(l^t)<0?(l-t/2)/t:(l+t/2)/t}function M(l,t,e,n,s=!0,r=[255,255,255]){if(!s){n/=255;const i=1-n;l=l*n+i*r[0]&255,t=t*n+i*r[1]&255,e=e*n+i*r[2]&255}return{r:l,g:t,b:e}}function T(l,t,e){return l<<16|t<<8|e}function N(l,t,e){l=S[l],t=S[t],e=S[e];const n=(27015*l+35149*t+3372*e+f/2)/f,s=(13887*l+44610*t+7038*e+f/2)/f,r=(5787*l+18462*t+41286*e+f/2)/f,i=A(n),o=A(s),h=A(r);return{l:_(13792*i+52010*o-267*h,f),a:_(129628*i-159158*o+29530*h,f),b:_(1698*i+51299*o-52997*h,f)}}function P(l){if(l<=0)return 0;if(l>=f)return 255;{const t=l*U,e=~~(t/f),n=t%f,s=R[e],r=R[e+1];return(n*(r-s)+f/2)/f+s}}function B(l){const t=l.l+_(25974*l.a,f)+_(14143*l.b,f),e=l.l+_(-6918*l.a,f)+_(-4185*l.b,f),n=l.l+_(-5864*l.a,f)+_(-84638*l.b,f),s=t**3/x,r=e**3/x,i=n**3/x,o=~~P((267169*s+-216771*r+15137*i+f/2)/f),h=~~P((-83127*s+171030*r+-22368*i+f/2)/f),a=~~P((-275*s+-46099*r+111909*i+f/2)/f);return{r:o,g:h,b:a}}function F(l){return new Promise(t=>{const e=new Image;e.decoding="sync",e.loading="eager",e.crossOrigin="anonymous",e.onload=()=>t(e),e.onerror=()=>t(e),e.src=l})}class v{constructor(){this.readable=new ReadableStream({start:t=>this._rsControler=t}),this.writable=new WritableStream({write:async t=>{let e;switch(typeof t){case"string":{const n=await F(t),s=v.ctx2d,r=s.canvas;s.clearRect(0,0,r.width,r.height),r.width=n.width,r.height=n.height,s.drawImage(n,0,0,r.width,r.height),e=s.getImageData(0,0,r.width,r.height).data;break}default:if(ArrayBuffer.isView(t))e=new Uint8ClampedArray(t.buffer);else if(t instanceof ArrayBuffer)e=new Uint8ClampedArray(t);else if(Array.isArray(t))if(Array.isArray(t[0])){const n=[];for(let s=t.length,r=0;r<s;r++)n.push(t[r][0]??0,t[r][1]??0,t[r][2]??0,t[r][3]??255);e=new Uint8ClampedArray(n)}else e=new Uint8ClampedArray(t);else{const n=v.ctx2d,s=n.canvas;n.clearRect(0,0,s.width,s.height),s.width=typeof t.width=="number"?t.width:t.width.baseVal.value,s.height=typeof t.height=="number"?t.height:t.height.baseVal.value,n.drawImage(t,0,0,s.width,s.height),e=n.getImageData(0,0,s.width,s.height).data}break}this._rsControler.enqueue(e)},close:()=>{this._rsControler.close()}})}static get ctx2d(){if(!this._ctx2d){if(!C)throw new Error("Failed to get ImageToPixels.ctx2d, not in browser.");const t=document.createElement("canvas").getContext("2d",{willReadFrequently:!0});if(!t)throw new Error("Failed to get ImageToPixels.ctx2d, getContext('2d') return null.");this._ctx2d=t}return this._ctx2d}}class y{constructor(t){this.maxColors=t,this.readable=new ReadableStream({start:e=>this._rsControler=e}),this.writable=new WritableStream({write:e=>{this._rsControler.enqueue(this._boxesToQuantizedColors(this._colorsToBoxes(e)))},close:()=>{this._rsControler.close()}})}static createSorter(t){const e=t[0],n=t[1],s=t[2];return(r,i)=>r.lab[e]-i.lab[e]||r.lab[n]-i.lab[n]||r.lab[s]-i.lab[s]}_colorsToBoxes(t){let e={start:0,end:t.length-1,sorted:null,count:0,score:0,weight:0,sort:"lab",avg:{l:0,a:0,b:0}};const n=[e];let s=1;const r=(a,u,c)=>a>=u?u>=c?"lab":a>=c?"lba":"bla":a>=c?"alb":u>=c?"abl":"bal",i=a=>{const{start:u,end:c}=a;a.count=c-u+1,a.weight=0;const g={l:0,a:0,b:0};for(let p=u;p<=c;p++){const d=t[p];g.l+=d.lab.l*d.count,g.a+=d.lab.a*d.count,g.b+=d.lab.b*d.count,a.weight+=d.count}a.avg.l=g.l/a.weight,a.avg.a=g.a/a.weight,a.avg.b=g.b/a.weight;const b={l:0,a:0,b:0};for(let p=u;p<=c;p++){const d=t[p];b.l+=(d.lab.l-a.avg.l)**2*d.count,b.a+=(d.lab.a-a.avg.a)**2*d.count,b.b+=(d.lab.b-a.avg.b)**2*d.count}a.sort=r(b.l,b.a,b.b),a.score=Math.max(b.l,b.a,b.b)},o=(a,u)=>{const c={start:u+1,end:a.end,sorted:a.sorted,count:0,score:0,weight:0,sort:"lab",avg:{l:0,a:0,b:0}};i(c),a.end-=c.count,i(a),n.push(c),s++},h=()=>{let a=-1,u=-1;if(s===this.maxColors)return-1;for(let c=0;c<s;c++){const g=n[c];g.count>=2&&g.score>u&&(a=c,u=g.score)}return a};for(i(e);e&&e.count>1;){const{start:a,end:u,sort:c,sorted:g}=e;if(c!==g){const q=t.slice(a,u+1).sort(y.createSorter(c));for(let L=q.length,I=0;I<L;I++)t[a+I]=q[I];e.sorted=c}const p=e.weight+1>>1;let d=a,m=0;for(;d<u-1&&(m+=t[d].count,!(m>p));d++);o(e,d);const $=h();e=$>=0?n[$]:null}return n}_boxesToQuantizedColors(t){const e=t.reduce((n,s)=>n+s.weight,0);return t.map(n=>{const{r:s,g:r,b:i}=B(n.avg);return{rgbInt:T(s,r,i),rgb:{r:s,g:r,b:i},hex:`#${s.toString(16).padStart(2,"0")}${r.toString(16).padStart(2,"0")}${i.toString(16).padStart(2,"0")}`,lab:n.avg,count:n.weight,percentage:n.weight/e}}).sort((n,s)=>n.rgbInt-s.rgbInt)}}class E{constructor(t,e,n){this.statsMode=t,this.premultipliedAlpha=e,this.tint=n,this._colors=[],this._cache=new Map,this.readable=new ReadableStream({start:s=>this._rsControler=s}),this.writable=new WritableStream({write:s=>{for(let r=s.length,i=0;i<r;i+=4){let o=s[i],h=s[i+1],a=s[i+2];const u=s[i+3];if(this.statsMode==="diff"&&this._previousPixels&&o===this._previousPixels[i]&&h===this._previousPixels[i+1]&&a===this._previousPixels[i+2]&&u===this._previousPixels[i+3])continue;({r:o,g:h,b:a}=M(o,h,a,u,this.premultipliedAlpha,this.tint));const c=T(o,h,a),g={rgbInt:c,lab:N(o,h,a),count:1},b=c%32768;let p=this._cache.get(b);p||this._cache.set(b,p=new Map);let d=p.get(c);if(d!==void 0){this._colors[d].count++;continue}d=this._colors.push(g)-1,p.set(c,d)}this.statsMode==="diff"&&(this._previousPixels=s)},close:()=>{this._rsControler.enqueue(this._colors.slice()),this._rsControler.close(),this._colors.length=0,this._cache.clear(),this._previousPixels=void 0}})}}class W{constructor(t=[],e=!1,n=[255,255,255]){this._premultipliedAlpha=e,this._tint=n,this._cache=new Map,this._colorMap=[],t.length&&this.setup(t)}setup(t){t=t.sort((i,o)=>i.rgbInt-o.rgbInt),this._cache.clear();const e=[],n=new Map;for(let i=-1,o=t.length,h=0;h<o;h++){const{rgbInt:a}=t[h];if(a===i){n.set(h,!0);continue}i=a}r({min:[-65535,-65535,-65535],max:[65535,65535,65535]}),this._colorMap=e;function s(i){const o={min:[65535,65535,65535],max:[-65535,-65535,-65535]},h=[];for(let p=t.length,d=0;d<p;d++){const{lab:m}=t[d];n.has(d)||m.l<i.min[0]||m.a<i.min[1]||m.b<i.min[2]||m.l>i.max[0]||m.a>i.max[1]||m.b>i.max[2]||(m.l<o.min[0]&&(o.min[0]=m.l),m.a<o.min[1]&&(o.min[1]=m.a),m.b<o.min[2]&&(o.min[2]=m.b),m.l>o.max[0]&&(o.max[0]=m.l),m.a>o.max[1]&&(o.max[1]=m.a),m.b>o.max[2]&&(o.max[2]=m.b),h.push({lab:m,index:d}))}let a="l",u=0;if(!h.length)return{index:-1,longest:a,longestIndex:u};const c=o.max[0]-o.min[0],g=o.max[1]-o.min[1],b=o.max[2]-o.min[2];return b>=c&&b>=g&&(a="b",u=2),g>=c&&g>=b&&(a="a",u=1),c>=g&&c>=b&&(a="l",u=0),{index:h.sort((p,d)=>p.lab[a]-d.lab[a])[h.length>>1].index,longest:a,longestIndex:u}}function r(i){const{index:o,longest:h,longestIndex:a}=s(i);if(o<0)return-1;n.set(o,!0);const{lab:u}=t[o],c={left:0,right:0,longest:h,lab:u,index:o},g=e.push(c)-1,b={max:[...i.max],min:[...i.min]},p={max:[...i.max],min:[...i.min]};b.max[a]=u[h],p.min[a]=Math.min(u[h]+1,65535);const d=r(b);let m=-1;return p.min[a]<=p.max[a]&&(m=r(p)),c.left=d,c.right=m,g}}_colormapNearestNode(t,e,n){const{left:s,right:r,longest:i,lab:o,index:h}=this._colorMap[t],a=Math.min((e.l-o.l)**2+(e.a-o.a)**2+(e.b-o.b)**2,4294967295-1);a<n.dist&&(n.index=h,n.dist=a);let u,c;if(s!==-1||r!==-1){const g=e[i]-o[i];g<=0?(u=s,c=r):(u=r,c=s),u!==-1&&this._colormapNearestNode(u,e,n),c!==-1&&g**2<n.dist&&this._colormapNearestNode(c,e,n)}}findNearestIndex(t,e,n,s=255){({r:t,g:e,b:n}=M(t,e,n,s,this._premultipliedAlpha,this._tint));const r=T(t,e,n),i=r%32768;let o=this._cache.get(i);o||this._cache.set(i,o=new Map);let h=o.get(r);if(h!==void 0)return h;const a={dist:Number.MAX_SAFE_INTEGER,index:-1};return this._colormapNearestNode(0,N(t,e,n),a),h=a.index,o.set(r,h),h}}class O{constructor(t={}){this.colors=[],this.config=this._resolveOptions(t),this._stream=this._createStream()}_resolveOptions(t){const{maxColors:e=256,statsMode:n="full",algorithm:s="median-cut",premultipliedAlpha:r=!1,tint:i=[255,255,255],samples:o=[]}=t;return{maxColors:e,statsMode:n,algorithm:s,premultipliedAlpha:r,tint:i,samples:o}}_createStream(){let t;switch(this.config.algorithm){case"median-cut":default:t=new y(this.config.maxColors);break}return new ReadableStream({start:e=>{this._streamControler=e,this.config.samples.forEach(n=>e.enqueue(n))}}).pipeThrough(new v).pipeThrough(new E(this.config.statsMode,this.config.premultipliedAlpha,this.config.tint)).pipeThrough(t)}addSample(t){this._streamControler.enqueue(t)}generate(){return new Promise(t=>{this._streamControler.close(),this._stream.pipeTo(new WritableStream({write:e=>{this.colors=e,this.finder=new W(e,this.config.premultipliedAlpha,this.config.tint),this._stream=this._createStream(),t(e)}}))})}match(t){var r;let e;if(typeof t=="number")e=[t>>24&255,t>>16&255,t>>8&255,t&255];else if(typeof t=="string"){const i=t.replace(/^#/,"");e=[`${i[0]}${i[1]}`,`${i[2]}${i[3]}`,`${i[4]}${i[5]}`].map(o=>parseInt(o,16))}else if(Array.isArray(t))e=t;else throw new TypeError("Unsupported color format");const n=(r=this.finder)==null?void 0:r.findNearestIndex(e[0],e[1],e[2],e[3]);if(n===void 0||n<0)return;const s=this.colors[n];if(s)return{color:s,index:n}}toColors(){return this.colors.slice()}toHexColors(){return this.colors.map(t=>t.hex)}toRgbColors(){return this.colors.map(t=>t.rgb)}toRgbIntColors(){return this.colors.map(t=>t.rgbInt)}toLabColors(){return this.colors.map(t=>t.lab)}toUint8Array(t=this.colors.length*4){var s;let e;const n=new Uint8ClampedArray(t);for(let r=0;r<t;r++){const i=r*4,o=((s=this.colors[r])==null?void 0:s.rgb)??e;o&&(n[i]=o.r,n[i+1]=o.g,n[i+2]=o.b,n[i+3]=255,e=o)}return n}clear(){this.colors.length=0,this._stream=this._createStream()}}w.Finder=W,w.ImageToPixels=v,w.MedianCut=y,w.Palette=O,w.PixelsToColors=E,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
