Files
color-thief/dist/color-thief.modern.js

2 lines
6.7 KiB
JavaScript

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 t<r?-1:t>r?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<<r)+n}function o(t){var r=[],e=!1;function n(){r.sort(t),e=!0}return{push:function(t){r.push(t),e=!1},peek:function(t){return e||n(),void 0===t&&(t=r.length-1),r[t]},pop:function(){return e||n(),r.pop()},size:function(){return r.length},map:function(t){return r.map(t)},debug:function(){return e||n(),r}}}function a(t,r,e,n,o,a,i){var u=this;u.r1=t,u.r2=r,u.g1=e,u.g2=n,u.b1=o,u.b2=a,u.histo=i}function i(){this.vboxes=new o(function(r,e){return t.naturalOrder(r.vbox.count()*r.vbox.volume(),e.vbox.count()*e.vbox.volume())})}function u(r,e){if(e.count()){var o=e.r2-e.r1+1,a=e.g2-e.g1+1,i=t.max([o,a,e.b2-e.b1+1]);if(1==e.count())return[e.copy()];var u,c,s,h,f=0,l=[],g=[];if(i==o)for(u=e.r1;u<=e.r2;u++){for(h=0,c=e.g1;c<=e.g2;c++)for(s=e.b1;s<=e.b2;s++)h+=r[n(u,c,s)]||0;l[u]=f+=h}else if(i==a)for(u=e.g1;u<=e.g2;u++){for(h=0,c=e.r1;c<=e.r2;c++)for(s=e.b1;s<=e.b2;s++)h+=r[n(c,u,s)]||0;l[u]=f+=h}else for(u=e.b1;u<=e.b2;u++){for(h=0,c=e.r1;c<=e.r2;c++)for(s=e.g1;s<=e.g2;s++)h+=r[n(c,s,u)]||0;l[u]=f+=h}return l.forEach(function(t,r){g[r]=f-t}),function(t){var r,n,o,a,i,c=t+"1",s=t+"2",h=0;for(u=e[c];u<=e[s];u++)if(l[u]>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;e<r.size();e++)if(r.peek(e).vbox.contains(t))return r.peek(e).color;return this.nearest(t)},nearest:function(t){for(var r,e,n,o=this.vboxes,a=0;a<o.size();a++)((e=Math.sqrt(Math.pow(t[0]-o.peek(a).color[0],2)+Math.pow(t[1]-o.peek(a).color[1],2)+Math.pow(t[2]-o.peek(a).color[2],2)))<r||void 0===r)&&(r=e,n=o.peek(a).color);return n},forcebw:function(){var r=this.vboxes;r.sort(function(r,e){return t.naturalOrder(t.sum(r.color),t.sum(e.color))});var e=r[0].color;e[0]<5&&e[1]<5&&e[2]<5&&(r[0].color=[0,0,0]);var n=r.length-1,o=r[n].color;o[0]>251&&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)<u?u=n:n>c&&(c=n),(o=t[1]>>e)<s?s=o:o>h&&(h=o),(i=t[2]>>e)<f?f=i:i>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<r;o+=e){const r=4*o,e=t[r+0],a=t[r+1],i=t[r+2],u=t[r+3];(void 0===u||u>=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<e.length;t++)n[t]=String.fromCharCode(e[t]);const o=n.join("");r("data:image/png;base64,"+window.btoa(o))})})}getImageDataFromURL(t){return new Promise((r,e)=>{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<e.length;t++)n[t]=String.fromCharCode(e[t]);const o=n.join("");r("data:image/png;base64,"+window.btoa(o))}):e("ERR_LOADING")})})}getColorAsync(t,r,e){const n=this;this.getImageDataFromURL(t).then(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};