if(!t)var t={map:function(t,r){var e={};return r?t.map(function(t,n){return e.index=n,r.call(e,t)}):t.slice()},naturalOrder:function(t,r){return tr?1:0},sum:function(t,r){var e={};return t.reduce(r?function(t,n,o){return e.index=o,t+r.call(e,n)}:function(t,r){return t+r},0)},max:function(r,e){return Math.max.apply(null,e?t.map(r,e):r)}};var r=function(){var r=5,e=8-r;function n(t,e,n){return(t<<2*r)+(e<f/2){for(o=e.copy(),a=e.copy(),i=(r=u-e[c])<=(n=e[s]-u)?Math.min(e[s]-1,~~(u+n/2)):Math.max(e[c],~~(u-1-r/2));!l[i];)i++;for(h=g[i];!h&&l[i-1];)h=g[--i];return o[s]=i,a[c]=o[s]+1,[o,a]}}(i==o?"r":i==a?"g":"b")}}return a.prototype={volume:function(t){var r=this;return r._volume&&!t||(r._volume=(r.r2-r.r1+1)*(r.g2-r.g1+1)*(r.b2-r.b1+1)),r._volume},count:function(t){var r=this,e=r.histo;if(!r._count_set||t){var o,a,i,u=0;for(o=r.r1;o<=r.r2;o++)for(a=r.g1;a<=r.g2;a++)for(i=r.b1;i<=r.b2;i++)u+=e[n(o,a,i)]||0;r._count=u,r._count_set=!0}return r._count},copy:function(){var t=this;return new a(t.r1,t.r2,t.g1,t.g2,t.b1,t.b2,t.histo)},avg:function(t){var e=this,o=e.histo;if(!e._avg||t){var a,i,u,c,s=0,h=1<<8-r,f=0,l=0,g=0;for(i=e.r1;i<=e.r2;i++)for(u=e.g1;u<=e.g2;u++)for(c=e.b1;c<=e.b2;c++)s+=a=o[n(i,u,c)]||0,f+=a*(i+.5)*h,l+=a*(u+.5)*h,g+=a*(c+.5)*h;e._avg=s?[~~(f/s),~~(l/s),~~(g/s)]:[~~(h*(e.r1+e.r2+1)/2),~~(h*(e.g1+e.g2+1)/2),~~(h*(e.b1+e.b2+1)/2)]}return e._avg},contains:function(t){var r=this,n=t[0]>>e;return gval=t[1]>>e,bval=t[2]>>e,n>=r.r1&&n<=r.r2&&gval>=r.g1&&gval<=r.g2&&bval>=r.b1&&bval<=r.b2}},i.prototype={push:function(t){this.vboxes.push({vbox:t,color:t.avg()})},palette:function(){return this.vboxes.map(function(t){return t.color})},size:function(){return this.vboxes.size()},map:function(t){for(var r=this.vboxes,e=0;e251&&o[1]>251&&o[2]>251&&(r[n].color=[255,255,255])}},{quantize:function(c,s){if(!c.length||s<2||s>256)return!1;var h=function(t){var o,a=new Array(1<<3*r);return t.forEach(function(t){o=n(t[0]>>e,t[1]>>e,t[2]>>e),a[o]=(a[o]||0)+1}),a}(c);h.forEach(function(){});var f=function(t,r){var n,o,i,u=1e6,c=0,s=1e6,h=0,f=1e6,l=0;return t.forEach(function(t){(n=t[0]>>e)c&&(c=n),(o=t[1]>>e)h&&(h=o),(i=t[2]>>e)l&&(l=i)}),new a(u,c,s,h,f,l,r)}(c,h),l=new o(function(r,e){return t.naturalOrder(r.count(),e.count())});function g(t,r){for(var e,n=t.size(),o=0;o<1e3;){if(n>=r)return;if(o++>1e3)return;if((e=t.pop()).count()){var a=u(h,e),i=a[0],c=a[1];if(!i)return;t.push(i),c&&(t.push(c),n++)}else t.push(e),o++}}l.push(f),g(l,.75*s);for(var v=new o(function(r,e){return t.naturalOrder(r.count()*r.volume(),e.count()*e.volume())});l.size();)v.push(l.pop());g(v,s);for(var m=new i;v.size();)m.push(v.pop());return m}}}().quantize;class e{constructor(t){if(this.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)},t instanceof HTMLImageElement)this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),this.width=this.canvas.width=t.naturalWidth,this.height=this.canvas.height=t.naturalHeight,this.context.drawImage(t,0,0,this.width,this.height);else{if(!(t instanceof HTMLCanvasElement))throw new Error("One of the two constructor arguments needed");this.canvas=t,this.context=this.canvas.getContext("2d"),this.width=this.canvas.width,this.height=this.canvas.height}}}class n{getColor(t,r=10){return this.getPalette(t,5,r)[0]}getPalette(t,n,o=10){const a=(t=>{let r=t.colorCount,e=t.quality;if(void 0!==r&&Number.isInteger(r)){if(1===r)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");r=Math.max(r,2),r=Math.min(r,20)}else r=10;return(void 0===e||!Number.isInteger(e)||e<1)&&(e=10),{colorCount:r,quality:e}})({colorCount:n,quality:o}),i=new e(t),u=((t,r,e)=>{const n=[];for(let o=0;o=125)&&(e>250&&a>250&&i>250||n.push([e,a,i]))}return n})(i.getImageData().data,i.width*i.height,a.quality),c=r(u,a.colorCount);return c?c.palette():null}getColorFromUrl(t,r,e=10){const n=document.createElement("img");n.addEventListener("load",()=>{const o=this.getPalette(n,5,e);r(o[0],t)}),n.src=t}getImageData(t,r){fetch(t).then(t=>{200===t.status&&t.arrayBuffer().then(t=>{const e=new Uint8Array(t),n=new Array(e.length);for(let t=0;t{fetch(t).then(t=>{200===t.status?t.arrayBuffer().then(t=>{const e=new Uint8Array(t),n=new Array(e.length);for(let t=0;t{const o=document.createElement("img");o.addEventListener("load",()=>{const t=n.getPalette(o,5,e);r(t[0],o)}),o.src=t})}getColorFromURLPromise(t,r){return new Promise((e,n)=>{const o=this;this.getImageDataFromURL(t).then(t=>{const n=document.createElement("img");n.addEventListener("load",()=>{const t=o.getPalette(n,5,r);e({color:t[0],img:n})}),n.src=t}).catch(t=>{n(t)})})}}export{n as default};