var e=class{constructor(e,t){this.pixels=e,this.opts=t;let{sigBits:n}=t,r=(e,t,r)=>(e<<2*n)+(t<>=i,m>>=i,h>>=i;let n=r(p,m,h);o[n]===void 0&&(o[n]=0),o[n]+=1,p>s&&(s=p),pl&&(l=m),md&&(d=h),ht>0?e+1:e,0),this.hist=o,this.rmax=s,this.rmin=c,this.gmax=l,this.gmin=u,this.bmax=d,this.bmin=f}get colorCount(){return this._colorCount}},t=class{scaleDown(e){let t=this.getWidth(),n=this.getHeight(),r=1;if(e.maxDimension>0){let i=Math.max(t,n);i>e.maxDimension&&(r=e.maxDimension/i)}else r=1/e.quality;r<1&&this.resize(t*r,n*r,r)}};function n(e,t){var n;if(t.length>0){let r=e.data,i=r.length/4,a,o,s,c,l;for(let e=0;e{let i=()=>{this._initCanvas(),e(this)};t.complete?i():(t.onload=i,t.onerror=e=>r(Error(`Fail to load image: ${n}`)))})}clear(){this._getContext().clearRect(0,0,this._getWidth(),this._getHeight())}update(e){this._getContext().putImageData(e,0,0)}getWidth(){return this._getWidth()}getHeight(){return this._getHeight()}resize(e,t,n){if(!this.image)throw Error(`Image is not initialized`);this._width=this._getCanvas().width=e,this._height=this._getCanvas().height=t,this._getContext().scale(n,n),this._getContext().drawImage(this.image,0,0)}getPixelCount(){return this._getWidth()*this._getHeight()}getImageData(){return this._getContext().getImageData(0,0,this._getWidth(),this._getHeight())}remove(){this._canvas&&this._canvas.parentNode&&this._canvas.parentNode.removeChild(this._canvas)}};function o(e,...t){return t.forEach(t=>{if(t){for(let n in t)if(t.hasOwnProperty(n)){let r=t[n];Array.isArray(r)?e[n]=r.slice(0):typeof r==`object`?(e[n]||(e[n]={}),o(e[n],r)):e[n]=r}}}),e}function s(e,t){let{colorCount:n,quantizer:r,generators:i,filters:a}=e,s={colorCount:n},c=typeof r==`string`?{name:r,options:{}}:r;return c.options=o({},s,c.options),o({},{quantizer:c,generators:i,filters:a},t)}var c=class{constructor(e,t={}){this._src=e,this._opts=o({},g.DefaultOpts,t)}maxColorCount(e){return this._opts.colorCount=e,this}maxDimension(e){return this._opts.maxDimension=e,this}addFilter(e){return this._opts.filters?this._opts.filters.push(e):this._opts.filters=[e],this}removeFilter(e){if(this._opts.filters){let t=this._opts.filters.indexOf(e);t>0&&this._opts.filters.splice(t)}return this}clearFilters(){return this._opts.filters=[],this}quality(e){return this._opts.quality=e,this}useImageClass(e){return this._opts.ImageClass=e,this}useGenerator(e,t){return this._opts.generators||(this._opts.generators=[]),this._opts.generators.push(t?{name:e,options:t}:e),this}useQuantizer(e,t){return this._opts.quantizer=t?{name:e,options:t}:e,this}build(){return new g(this._src,this._opts)}getPalette(){return this.build().getPalette()}},l=class{constructor(e){this.pipeline=e,this._map={}}names(){return Object.keys(this._map)}has(e){return!!this._map[e]}get(e){return this._map[e]}register(e,t){return this._map[e]=t,this.pipeline}},u=class{constructor(){this.filter=new l(this),this.quantizer=new l(this),this.generator=new l(this)}_buildProcessTasks({filters:e,quantizer:t,generators:n}){return n.length===1&&n[0]===`*`&&(n=this.generator.names()),{filters:e.map(e=>r(this.filter,e)),quantizer:r(this.quantizer,t),generators:n.map(e=>r(this.generator,e))};function r(e,t){let n,r;return typeof t==`string`?n=t:(n=t.name,r=t.options),{name:n,fn:e.get(n),options:r}}}async process(e,t){let{filters:n,quantizer:r,generators:i}=this._buildProcessTasks(t),a=await this._filterColors(n,e),o=await this._generateColors(r,a),s=await this._generatePalettes(i,o);return{colors:o,palettes:s}}_filterColors(e,t){return Promise.resolve(n(t,e.map(({fn:e})=>e)))}_generateColors(e,t){return Promise.resolve(e.fn(t.data,e.options))}async _generatePalettes(e,t){let n=await Promise.all(e.map(({fn:e,options:n})=>Promise.resolve(e(t,n))));return Promise.resolve(n.reduce((t,n,r)=>(t[e[r].name]=n,t),{}))}};function d(e,t,n){return`#`+((1<<24)+(e<<16)+(t<<8)+n).toString(16).slice(1,7)}function f(e,t,n){e/=255,t/=255,n/=255;let r=Math.max(e,t,n),i=Math.min(e,t,n),a=0,o=0,s=(r+i)/2;if(r!==i){let c=r-i;switch(o=s>.5?c/(2-r-i):c/(r+i),r){case e:a=(t-n)/c+(t1&&--n,n<1/6?e+(t-e)*6*n:n<1/2?t:n<2/3?e+(t-e)*(2/3-n)*6:e}if(t===0)r=i=a=n;else{let s=n<.5?n*(1+t):n+t-n*t,c=2*n-s;r=o(c,s,e+1/3),i=o(c,s,e),a=o(c,s,e-1/3)}return[r*255,i*255,a*255]}var m=class e{static applyFilters(e,t){return t.length>0?e.filter(({r:e,g:n,b:r})=>{var i;for(let a=0;a>=v,n>>=v,r>>=v,t>=i&&t<=a&&n>=o&&n<=s&&r>=c&&r<=l}split(){let{hist:e,getColorIndex:t}=this.histogram,{r1:n,r2:r,g1:i,g2:a,b1:o,b2:s}=this.dimension,c=this.count();if(!c)return[];if(c===1)return[this.clone()];let l=r-n+1,u=a-i+1,d=s-o+1,f=Math.max(l,u,d),p=null,m,h;m=h=0;let g=null;if(f===l){g=`r`,p=new Uint32Array(r+1);for(let c=n;c<=r;c++){m=0;for(let n=i;n<=a;n++)for(let r=o;r<=s;r++){let i=t(c,n,r);e[i]&&(m+=e[i])}h+=m,p[c]=h}}else if(f===u){g=`g`,p=new Uint32Array(a+1);for(let c=i;c<=a;c++){m=0;for(let i=n;i<=r;i++)for(let n=o;n<=s;n++){let r=t(i,c,n);e[r]&&(m+=e[r])}h+=m,p[c]=h}}else{g=`b`,p=new Uint32Array(s+1);for(let c=o;c<=s;c++){m=0;for(let o=n;o<=r;o++)for(let n=i;n<=a;n++){let r=t(o,n,c);e[r]&&(m+=e[r])}h+=m,p[c]=h}}let _=-1,v=new Uint32Array(p.length);for(let e=0;eh/2&&(_=e),v[e]=h-t)}let y=this;function b(e){let t=e+`1`,n=e+`2`,r=y.dimension[t],i=y.dimension[n],a=y.clone(),o=y.clone(),s=_-r,c=i-_;for(s<=c?(i=Math.min(i-1,~~(_+c/2)),i=Math.max(0,i)):(i=Math.max(r,~~(_-1-s/2)),i=Math.min(y.dimension[n],i));!p[i];)i++;let l=v[i];for(;!l&&p[i-1];)l=v[--i];return a.dimension[n]=i,o.dimension[t]=i+1,[a,o]}return b(g)}},b=class{_sort(){this._sorted||(this.contents.sort(this._comparator),this._sorted=!0)}constructor(e){this._comparator=e,this.contents=[],this._sorted=!1}push(e){this.contents.push(e),this._sorted=!1}peek(e){return this._sort(),e=typeof e==`number`?e:this.contents.length-1,this.contents[e]}pop(){return this._sort(),this.contents.pop()}size(){return this.contents.length}map(e){return this._sort(),this.contents.map(e)}};const x=.75;function S(e,t){let n=e.size();for(;e.size()0){let[r,i]=t.split();if(!r||(e.push(r),i&&i.count()>0&&e.push(i),e.size()===n))break;n=e.size()}else break}}const C=(e,t)=>{if(e.length===0||t.colorCount<2||t.colorCount>256)throw Error(`Wrong MMCQ parameters`);let n=y.build(e);n.histogram.colorCount;let r=new b((e,t)=>e.count()-t.count());r.push(n),S(r,x*t.colorCount);let i=new b((e,t)=>e.count()*e.volume()-t.count()*t.volume());return i.contents=r.contents,S(i,t.colorCount-i.size()),w(i)};function w(e){let t=[];for(;e.size();){let n=e.pop(),r=n.avg();t.push(new m(r,n.count()))}return t}const T={targetDarkLuma:.26,maxDarkLuma:.45,minLightLuma:.55,targetLightLuma:.74,minNormalLuma:.3,targetNormalLuma:.5,maxNormalLuma:.7,targetMutesSaturation:.3,maxMutesSaturation:.4,targetVibrantSaturation:1,minVibrantSaturation:.35,weightSaturation:3,weightLuma:6.5,weightPopulation:.5};function E(e){let t=0;return e.forEach(e=>{t=Math.max(t,e.population)}),t}function D(e,t){return e.Vibrant===t||e.DarkVibrant===t||e.LightVibrant===t||e.Muted===t||e.DarkMuted===t||e.LightMuted===t}function O(e,t,n,r,i,a,o){function s(...e){let t=0,n=0;for(let r=0;r{let[,f,p]=t.hsl;if(f>=s&&f<=c&&p>=i&&p<=a&&!D(e,t)){let e=O(f,o,p,r,t.population,n,l);(u===null||e>d)&&(u=t,d=e)}}),u}function A(e,t,n){let r={Vibrant:null,DarkVibrant:null,LightVibrant:null,Muted:null,DarkMuted:null,LightMuted:null};return r.Vibrant=k(r,e,t,n.targetNormalLuma,n.minNormalLuma,n.maxNormalLuma,n.targetVibrantSaturation,n.minVibrantSaturation,1,n),r.LightVibrant=k(r,e,t,n.targetLightLuma,n.minLightLuma,1,n.targetVibrantSaturation,n.minVibrantSaturation,1,n),r.DarkVibrant=k(r,e,t,n.targetDarkLuma,0,n.maxDarkLuma,n.targetVibrantSaturation,n.minVibrantSaturation,1,n),r.Muted=k(r,e,t,n.targetNormalLuma,n.minNormalLuma,n.maxNormalLuma,n.targetMutesSaturation,0,n.maxMutesSaturation,n),r.LightMuted=k(r,e,t,n.targetLightLuma,n.minLightLuma,1,n.targetMutesSaturation,0,n.maxMutesSaturation,n),r.DarkMuted=k(r,e,t,n.targetDarkLuma,0,n.maxDarkLuma,n.targetMutesSaturation,0,n.maxMutesSaturation,n),r}function j(e,t,n){if(!e.Vibrant&&!e.DarkVibrant&&!e.LightVibrant){if(!e.DarkVibrant&&e.DarkMuted){let[t,r,i]=e.DarkMuted.hsl;i=n.targetDarkLuma,e.DarkVibrant=new m(p(t,r,i),0)}if(!e.LightVibrant&&e.LightMuted){let[t,r,i]=e.LightMuted.hsl;i=n.targetDarkLuma,e.DarkVibrant=new m(p(t,r,i),0)}}if(!e.Vibrant&&e.DarkVibrant){let[t,r,i]=e.DarkVibrant.hsl;i=n.targetNormalLuma,e.Vibrant=new m(p(t,r,i),0)}else if(!e.Vibrant&&e.LightVibrant){let[t,r,i]=e.LightVibrant.hsl;i=n.targetNormalLuma,e.Vibrant=new m(p(t,r,i),0)}if(!e.DarkVibrant&&e.Vibrant){let[t,r,i]=e.Vibrant.hsl;i=n.targetDarkLuma,e.DarkVibrant=new m(p(t,r,i),0)}if(!e.LightVibrant&&e.Vibrant){let[t,r,i]=e.Vibrant.hsl;i=n.targetLightLuma,e.LightVibrant=new m(p(t,r,i),0)}if(!e.Muted&&e.Vibrant){let[t,r,i]=e.Vibrant.hsl;i=n.targetMutesSaturation,e.Muted=new m(p(t,r,i),0)}if(!e.DarkMuted&&e.DarkVibrant){let[t,r,i]=e.DarkVibrant.hsl;i=n.targetMutesSaturation,e.DarkMuted=new m(p(t,r,i),0)}if(!e.LightMuted&&e.LightVibrant){let[t,r,i]=e.LightVibrant.hsl;i=n.targetMutesSaturation,e.LightMuted=new m(p(t,r,i),0)}}const M=(e,t)=>{t=Object.assign({},T,t);let n=E(e),r=A(e,n,t);return j(r,n,t),r},N=new u().filter.register(`default`,(e,t,n,r)=>r>=125&&!(e>250&&t>250&&n>250)).quantizer.register(`mmcq`,C).generator.register(`default`,M);g.use(N);export{g as Vibrant};