mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-25 13:54:25 +00:00
2 lines
6.7 KiB
JavaScript
2 lines
6.7 KiB
JavaScript
if(!t)var t={map:function(t,r){var n={};return r?t.map(function(t,e){return n.index=e,r.call(n,t)}):t.slice()},naturalOrder:function(t,r){return t<r?-1:t>r?1:0},sum:function(t,r){var n={};return t.reduce(r?function(t,e,o){return n.index=o,t+r.call(n,e)}:function(t,r){return t+r},0)},max:function(r,n){return Math.max.apply(null,n?t.map(r,n):r)}};var r=function(){var r=5,n=8-r;function e(t,n,e){return(t<<2*r)+(n<<r)+e}function o(t){var r=[],n=!1;function e(){r.sort(t),n=!0}return{push:function(t){r.push(t),n=!1},peek:function(t){return n||e(),void 0===t&&(t=r.length-1),r[t]},pop:function(){return n||e(),r.pop()},size:function(){return r.length},map:function(t){return r.map(t)},debug:function(){return n||e(),r}}}function a(t,r,n,e,o,a,u){var i=this;i.r1=t,i.r2=r,i.g1=n,i.g2=e,i.b1=o,i.b2=a,i.histo=u}function u(){this.vboxes=new o(function(r,n){return t.naturalOrder(r.vbox.count()*r.vbox.volume(),n.vbox.count()*n.vbox.volume())})}function i(r,n){if(n.count()){var o=n.r2-n.r1+1,a=n.g2-n.g1+1,u=t.max([o,a,n.b2-n.b1+1]);if(1==n.count())return[n.copy()];var i,c,f,s,h=0,v=[],l=[];if(u==o)for(i=n.r1;i<=n.r2;i++){for(s=0,c=n.g1;c<=n.g2;c++)for(f=n.b1;f<=n.b2;f++)s+=r[e(i,c,f)]||0;v[i]=h+=s}else if(u==a)for(i=n.g1;i<=n.g2;i++){for(s=0,c=n.r1;c<=n.r2;c++)for(f=n.b1;f<=n.b2;f++)s+=r[e(c,i,f)]||0;v[i]=h+=s}else for(i=n.b1;i<=n.b2;i++){for(s=0,c=n.r1;c<=n.r2;c++)for(f=n.g1;f<=n.g2;f++)s+=r[e(c,f,i)]||0;v[i]=h+=s}return v.forEach(function(t,r){l[r]=h-t}),function(t){var r,e,o,a,u,c=t+"1",f=t+"2",s=0;for(i=n[c];i<=n[f];i++)if(v[i]>h/2){for(o=n.copy(),a=n.copy(),u=(r=i-n[c])<=(e=n[f]-i)?Math.min(n[f]-1,~~(i+e/2)):Math.max(n[c],~~(i-1-r/2));!v[u];)u++;for(s=l[u];!s&&v[u-1];)s=l[--u];return o[f]=u,a[c]=o[f]+1,[o,a]}}(u==o?"r":u==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,n=r.histo;if(!r._count_set||t){var o,a,u,i=0;for(o=r.r1;o<=r.r2;o++)for(a=r.g1;a<=r.g2;a++)for(u=r.b1;u<=r.b2;u++)i+=n[e(o,a,u)]||0;r._count=i,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 n=this,o=n.histo;if(!n._avg||t){var a,u,i,c,f=0,s=1<<8-r,h=0,v=0,l=0;for(u=n.r1;u<=n.r2;u++)for(i=n.g1;i<=n.g2;i++)for(c=n.b1;c<=n.b2;c++)f+=a=o[e(u,i,c)]||0,h+=a*(u+.5)*s,v+=a*(i+.5)*s,l+=a*(c+.5)*s;n._avg=f?[~~(h/f),~~(v/f),~~(l/f)]:[~~(s*(n.r1+n.r2+1)/2),~~(s*(n.g1+n.g2+1)/2),~~(s*(n.b1+n.b2+1)/2)]}return n._avg},contains:function(t){var r=this,e=t[0]>>n;return gval=t[1]>>n,bval=t[2]>>n,e>=r.r1&&e<=r.r2&&gval>=r.g1&&gval<=r.g2&&bval>=r.b1&&bval<=r.b2}},u.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,n=0;n<r.size();n++)if(r.peek(n).vbox.contains(t))return r.peek(n).color;return this.nearest(t)},nearest:function(t){for(var r,n,e,o=this.vboxes,a=0;a<o.size();a++)((n=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=n,e=o.peek(a).color);return e},forcebw:function(){var r=this.vboxes;r.sort(function(r,n){return t.naturalOrder(t.sum(r.color),t.sum(n.color))});var n=r[0].color;n[0]<5&&n[1]<5&&n[2]<5&&(r[0].color=[0,0,0]);var e=r.length-1,o=r[e].color;o[0]>251&&o[1]>251&&o[2]>251&&(r[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*r);return t.forEach(function(t){o=e(t[0]>>n,t[1]>>n,t[2]>>n),a[o]=(a[o]||0)+1}),a}(c);s.forEach(function(){});var h=function(t,r){var e,o,u,i=1e6,c=0,f=1e6,s=0,h=1e6,v=0;return t.forEach(function(t){(e=t[0]>>n)<i?i=e:e>c&&(c=e),(o=t[1]>>n)<f?f=o:o>s&&(s=o),(u=t[2]>>n)<h?h=u:u>v&&(v=u)}),new a(i,c,f,s,h,v,r)}(c,s),v=new o(function(r,n){return t.naturalOrder(r.count(),n.count())});function l(t,r){for(var n,e=t.size(),o=0;o<1e3;){if(e>=r)return;if(o++>1e3)return;if((n=t.pop()).count()){var a=i(s,n),u=a[0],c=a[1];if(!u)return;t.push(u),c&&(t.push(c),e++)}else t.push(n),o++}}v.push(h),l(v,.75*f);for(var g=new o(function(r,n){return t.naturalOrder(r.count()*r.volume(),n.count()*n.volume())});v.size();)g.push(v.pop());l(g,f);for(var m=new u;g.size();)m.push(g.pop());return m}}}().quantize,n=function(t){this.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)},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)},e=/*#__PURE__*/function(){function t(){var t=this;this.getColorAsync=function(r,n,e){var o=t;t.getImageDataFromURL(r).then(function(t){var r=document.createElement("img");r.addEventListener("load",function(){var t=o.getPalette(r,5,e);n(t[0],r)}),r.src=t})},this.getColorPromise=function(r,n){return new Promise(function(e,o){var a=t;t.getImageDataFromURL(r).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)})})}}var e=t.prototype;return e.getColor=function(t,r){return void 0===r&&(r=10),this.getPalette(t,5,r)[0]},e.getPalette=function(t,e,o){void 0===o&&(o=10);var a=function(t){var r=t.colorCount,n=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===n||!Number.isInteger(n)||n<1)&&(n=10),{colorCount:r,quality:n}}({colorCount:e,quality:o}),u=new n(t),i=function(t,r,n){for(var e=[],o=0;o<r;o+=n){var a=4*o,u=t[a+0],i=t[a+1],c=t[a+2],f=t[a+3];(void 0===f||f>=125)&&(u>250&&i>250&&c>250||e.push([u,i,c]))}return e}(u.getImageData().data,u.width*u.height,a.quality),c=r(i,a.colorCount);return c?c.palette():null},e.getColorFromUrl=function(t,r,n){var e=this;void 0===n&&(n=10);var o=document.createElement("img");o.addEventListener("load",function(){var a=e.getPalette(o,5,n);r(a[0],t)}),o.src=t},e.getImageData=function(t,r){fetch(t).then(function(t){200===t.status&&t.arrayBuffer().then(function(t){for(var n=new Uint8Array(t),e=new Array(n.length),o=0;o<n.length;o++)e[o]=String.fromCharCode(n[o]);var a=e.join("");r("data:image/png;base64,"+window.btoa(a))})})},e.getImageDataFromURL=function(t){return new Promise(function(r,n){fetch(t).then(function(t){200===t.status?t.arrayBuffer().then(function(t){for(var n=new Uint8Array(t),e=new Array(n.length),o=0;o<n.length;o++)e[o]=String.fromCharCode(n[o]);var a=e.join("");r("data:image/png;base64,"+window.btoa(a))}):n("ERR_LOADING")})})},t}();export{e as default};
|