Files
color-thief/dist/color-thief.cjs

2 lines
6.9 KiB
JavaScript

if(!t)var t={map:function(t,n){var r={};return n?t.map(function(t,e){return r.index=e,n.call(r,t)}):t.slice()},naturalOrder:function(t,n){return t<n?-1:t>n?1:0},sum:function(t,n){var r={};return t.reduce(n?function(t,e,o){return r.index=o,t+n.call(r,e)}:function(t,n){return t+n},0)},max:function(n,r){return Math.max.apply(null,r?t.map(n,r):n)}};var n=function(){var n=5,r=8-n;function e(t,r,e){return(t<<2*n)+(r<<n)+e}function o(t){var n=[],r=!1;function e(){n.sort(t),r=!0}return{push:function(t){n.push(t),r=!1},peek:function(t){return r||e(),void 0===t&&(t=n.length-1),n[t]},pop:function(){return r||e(),n.pop()},size:function(){return n.length},map:function(t){return n.map(t)},debug:function(){return r||e(),n}}}function a(t,n,r,e,o,a,i){var u=this;u.r1=t,u.r2=n,u.g1=r,u.g2=e,u.b1=o,u.b2=a,u.histo=i}function i(){this.vboxes=new o(function(n,r){return t.naturalOrder(n.vbox.count()*n.vbox.volume(),r.vbox.count()*r.vbox.volume())})}function u(n,r){if(r.count()){var o=r.r2-r.r1+1,a=r.g2-r.g1+1,i=t.max([o,a,r.b2-r.b1+1]);if(1==r.count())return[r.copy()];var u,c,f,s,h=0,v=[],l=[];if(i==o)for(u=r.r1;u<=r.r2;u++){for(s=0,c=r.g1;c<=r.g2;c++)for(f=r.b1;f<=r.b2;f++)s+=n[e(u,c,f)]||0;v[u]=h+=s}else if(i==a)for(u=r.g1;u<=r.g2;u++){for(s=0,c=r.r1;c<=r.r2;c++)for(f=r.b1;f<=r.b2;f++)s+=n[e(c,u,f)]||0;v[u]=h+=s}else for(u=r.b1;u<=r.b2;u++){for(s=0,c=r.r1;c<=r.r2;c++)for(f=r.g1;f<=r.g2;f++)s+=n[e(c,f,u)]||0;v[u]=h+=s}return v.forEach(function(t,n){l[n]=h-t}),function(t){var n,e,o,a,i,c=t+"1",f=t+"2",s=0;for(u=r[c];u<=r[f];u++)if(v[u]>h/2){for(o=r.copy(),a=r.copy(),i=(n=u-r[c])<=(e=r[f]-u)?Math.min(r[f]-1,~~(u+e/2)):Math.max(r[c],~~(u-1-n/2));!v[i];)i++;for(s=l[i];!s&&v[i-1];)s=l[--i];return o[f]=i,a[c]=o[f]+1,[o,a]}}(i==o?"r":i==a?"g":"b")}}return a.prototype={volume:function(t){var n=this;return n._volume&&!t||(n._volume=(n.r2-n.r1+1)*(n.g2-n.g1+1)*(n.b2-n.b1+1)),n._volume},count:function(t){var n=this,r=n.histo;if(!n._count_set||t){var o,a,i,u=0;for(o=n.r1;o<=n.r2;o++)for(a=n.g1;a<=n.g2;a++)for(i=n.b1;i<=n.b2;i++)u+=r[e(o,a,i)]||0;n._count=u,n._count_set=!0}return n._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 r=this,o=r.histo;if(!r._avg||t){var a,i,u,c,f=0,s=1<<8-n,h=0,v=0,l=0;for(i=r.r1;i<=r.r2;i++)for(u=r.g1;u<=r.g2;u++)for(c=r.b1;c<=r.b2;c++)f+=a=o[e(i,u,c)]||0,h+=a*(i+.5)*s,v+=a*(u+.5)*s,l+=a*(c+.5)*s;r._avg=f?[~~(h/f),~~(v/f),~~(l/f)]:[~~(s*(r.r1+r.r2+1)/2),~~(s*(r.g1+r.g2+1)/2),~~(s*(r.b1+r.b2+1)/2)]}return r._avg},contains:function(t){var n=this,e=t[0]>>r;return gval=t[1]>>r,bval=t[2]>>r,e>=n.r1&&e<=n.r2&&gval>=n.g1&&gval<=n.g2&&bval>=n.b1&&bval<=n.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 n=this.vboxes,r=0;r<n.size();r++)if(n.peek(r).vbox.contains(t))return n.peek(r).color;return this.nearest(t)},nearest:function(t){for(var n,r,e,o=this.vboxes,a=0;a<o.size();a++)((r=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)))<n||void 0===n)&&(n=r,e=o.peek(a).color);return e},forcebw:function(){var n=this.vboxes;n.sort(function(n,r){return t.naturalOrder(t.sum(n.color),t.sum(r.color))});var r=n[0].color;r[0]<5&&r[1]<5&&r[2]<5&&(n[0].color=[0,0,0]);var e=n.length-1,o=n[e].color;o[0]>251&&o[1]>251&&o[2]>251&&(n[e].color=[255,255,255])}},{quantize:function(c,f){if(!c.length||f<2||f>256)return!1;var s=function(t){var o,a=new Array(1<<3*n);return t.forEach(function(t){o=e(t[0]>>r,t[1]>>r,t[2]>>r),a[o]=(a[o]||0)+1}),a}(c);s.forEach(function(){});var h=function(t,n){var e,o,i,u=1e6,c=0,f=1e6,s=0,h=1e6,v=0;return t.forEach(function(t){(e=t[0]>>r)<u?u=e:e>c&&(c=e),(o=t[1]>>r)<f?f=o:o>s&&(s=o),(i=t[2]>>r)<h?h=i:i>v&&(v=i)}),new a(u,c,f,s,h,v,n)}(c,s),v=new o(function(n,r){return t.naturalOrder(n.count(),r.count())});function l(t,n){for(var r,e=t.size(),o=0;o<1e3;){if(e>=n)return;if(o++>1e3)return;if((r=t.pop()).count()){var a=u(s,r),i=a[0],c=a[1];if(!i)return;t.push(i),c&&(t.push(c),e++)}else t.push(r),o++}}v.push(h),l(v,.75*f);for(var g=new o(function(n,r){return t.naturalOrder(n.count()*n.volume(),r.count()*r.volume())});v.size();)g.push(v.pop());l(g,f);for(var m=new i;g.size();)m.push(g.pop());return m}}}().quantize,r=function(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}};module.exports=/*#__PURE__*/function(){function t(){}var e=t.prototype;return e.getColor=function(t,n){return void 0===n&&(n=10),this.getPalette(t,5,n)[0]},e.getPalette=function(t,e,o){void 0===o&&(o=10);var a=function(t){var n=t.colorCount,r=t.quality;if(void 0!==n&&Number.isInteger(n)){if(1===n)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");n=Math.max(n,2),n=Math.min(n,20)}else n=10;return(void 0===r||!Number.isInteger(r)||r<1)&&(r=10),{colorCount:n,quality:r}}({colorCount:e,quality:o}),i=new r(t),u=function(t,n,r){for(var e=[],o=0;o<n;o+=r){var a=4*o,i=t[a+0],u=t[a+1],c=t[a+2],f=t[a+3];(void 0===f||f>=125)&&(i>250&&u>250&&c>250||e.push([i,u,c]))}return e}(i.getImageData().data,i.width*i.height,a.quality),c=n(u,a.colorCount);return c?c.palette():null},e.getColorFromUrl=function(t,n,r){var e=this;void 0===r&&(r=10);var o=document.createElement("img");o.addEventListener("load",function(){var a=e.getPalette(o,5,r);n(a[0],t)}),o.src=t},e.getImageData=function(t,n){fetch(t).then(function(t){200===t.status&&t.arrayBuffer().then(function(t){for(var r=new Uint8Array(t),e=new Array(r.length),o=0;o<r.length;o++)e[o]=String.fromCharCode(r[o]);var a=e.join("");n("data:image/png;base64,"+window.btoa(a))})})},e.getImageDataFromURL=function(t){return new Promise(function(n,r){fetch(t).then(function(t){200===t.status?t.arrayBuffer().then(function(t){for(var r=new Uint8Array(t),e=new Array(r.length),o=0;o<r.length;o++)e[o]=String.fromCharCode(r[o]);var a=e.join("");n("data:image/png;base64,"+window.btoa(a))}):r("ERR_LOADING")})})},e.getColorAsync=function(t,n,r){var e=this;this.getImageDataFromURL(t).then(function(t){var o=document.createElement("img");o.addEventListener("load",function(){var t=e.getPalette(o,5,r);n(t[0],o)}),o.src=t})},e.getColorFromURLPromise=function(t,n){var r=this;return new Promise(function(e,o){var a=r;r.getImageDataFromURL(t).then(function(t){var r=document.createElement("img");r.addEventListener("load",function(){var t=a.getPalette(r,5,n);e({color:t[0],img:r})}),r.src=t}).catch(function(t){o(t)})})},t}();