UNPKG

@pioug/colorthief

Version:

Get the dominant color or color palette from an image.

2 lines (1 loc) 6.39 kB
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t||self).ColorThief=r()}(this,function(){if(!t)var t={map:function(t,r){var n={};return r?t.map(function(t,o){return n.index=o,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,o,e){return n.index=e,t+r.call(n,o)}: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(){function r(t,r,n){return(t<<10)+(r<<5)+n}function n(t){var r=[],n=!1;function o(){r.sort(t),n=!0}return{push:function(t){r.push(t),n=!1},peek:function(t){return n||o(),void 0===t&&(t=r.length-1),r[t]},pop:function(){return n||o(),r.pop()},size:function(){return r.length},map:function(t){return r.map(t)},debug:function(){return n||o(),r}}}function o(t,r,n,o,e,i,u){var a=this;a.r1=t,a.r2=r,a.g1=n,a.g2=o,a.b1=e,a.b2=i,a.histo=u}function e(){this.vboxes=new n(function(r,n){return t.naturalOrder(r.vbox.count()*r.vbox.volume(),n.vbox.count()*n.vbox.volume())})}function i(n,o){if(o.count()){var e=o.r2-o.r1+1,i=o.g2-o.g1+1,u=t.max([e,i,o.b2-o.b1+1]);if(1==o.count())return[o.copy()];var a,c,f,s,l=0,h=[],v=[];if(u==e)for(a=o.r1;a<=o.r2;a++){for(s=0,c=o.g1;c<=o.g2;c++)for(f=o.b1;f<=o.b2;f++)s+=n[r(a,c,f)]||0;h[a]=l+=s}else if(u==i)for(a=o.g1;a<=o.g2;a++){for(s=0,c=o.r1;c<=o.r2;c++)for(f=o.b1;f<=o.b2;f++)s+=n[r(c,a,f)]||0;h[a]=l+=s}else for(a=o.b1;a<=o.b2;a++){for(s=0,c=o.r1;c<=o.r2;c++)for(f=o.g1;f<=o.g2;f++)s+=n[r(c,f,a)]||0;h[a]=l+=s}return h.forEach(function(t,r){v[r]=l-t}),function(t){var r,n,e,i,u,c=t+"1",f=t+"2",s=0;for(a=o[c];a<=o[f];a++)if(h[a]>l/2){for(e=o.copy(),i=o.copy(),u=(r=a-o[c])<=(n=o[f]-a)?Math.min(o[f]-1,~~(a+n/2)):Math.max(o[c],~~(a-1-r/2));!h[u];)u++;for(s=v[u];!s&&h[u-1];)s=v[--u];return e[f]=u,i[c]=e[f]+1,[e,i]}}(u==e?"r":u==i?"g":"b")}}return o.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 n=this,o=n.histo;if(!n._count_set||t){var e,i,u,a=0;for(e=n.r1;e<=n.r2;e++)for(i=n.g1;i<=n.g2;i++)for(u=n.b1;u<=n.b2;u++)a+=o[r(e,i,u)]||0;n._count=a,n._count_set=!0}return n._count},copy:function(){var t=this;return new o(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 e,i,u,a,c=0,f=0,s=0,l=0;for(i=n.r1;i<=n.r2;i++)for(u=n.g1;u<=n.g2;u++)for(a=n.b1;a<=n.b2;a++)c+=e=o[r(i,u,a)]||0,f+=e*(i+.5)*8,s+=e*(u+.5)*8,l+=e*(a+.5)*8;n._avg=c?[~~(f/c),~~(s/c),~~(l/c)]:[~~(8*(n.r1+n.r2+1)/2),~~(8*(n.g1+n.g2+1)/2),~~(8*(n.b1+n.b2+1)/2)]}return n._avg},contains:function(t){var r=this,n=t[0]>>3;return gval=t[1]>>3,bval=t[2]>>3,n>=r.r1&&n<=r.r2&&gval>=r.g1&&gval<=r.g2&&bval>=r.b1&&bval<=r.b2}},e.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,o,e=this.vboxes,i=0;i<e.size();i++)((n=Math.sqrt(Math.pow(t[0]-e.peek(i).color[0],2)+Math.pow(t[1]-e.peek(i).color[1],2)+Math.pow(t[2]-e.peek(i).color[2],2)))<r||void 0===r)&&(r=n,o=e.peek(i).color);return o},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 o=r.length-1,e=r[o].color;e[0]>251&&e[1]>251&&e[2]>251&&(r[o].color=[255,255,255])}},{quantize:function(u,a){if(!u.length||a<2||a>256)return!1;var c=function(t){var n,o=new Array(32768);return t.forEach(function(t){n=r(t[0]>>3,t[1]>>3,t[2]>>3),o[n]=(o[n]||0)+1}),o}(u);c.forEach(function(){});var f=function(t,r){var n,e,i,u=1e6,a=0,c=1e6,f=0,s=1e6,l=0;return t.forEach(function(t){(n=t[0]>>3)<u?u=n:n>a&&(a=n),(e=t[1]>>3)<c?c=e:e>f&&(f=e),(i=t[2]>>3)<s?s=i:i>l&&(l=i)}),new o(u,a,c,f,s,l,r)}(u,c),s=new n(function(r,n){return t.naturalOrder(r.count(),n.count())});function l(t,r){for(var n,o=t.size(),e=0;e<1e3;){if(o>=r)return;if(e++>1e3)return;if((n=t.pop()).count()){var u=i(c,n),a=u[0],f=u[1];if(!a)return;t.push(a),f&&(t.push(f),o++)}else t.push(n),e++}}s.push(f),l(s,.75*a);for(var h=new n(function(r,n){return t.naturalOrder(r.count()*r.volume(),n.count()*n.volume())});s.size();)h.push(s.pop());l(h,a);for(var v=new e;h.size();)v.push(h.pop());return v}}}().quantize,n=function(t){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)};n.prototype.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)};var o=function(){};return o.prototype.getColor=function(t,r){return void 0===r&&(r=10),this.getPalette(t,5,r)[0]},o.prototype.getPalette=function(t,o,e){var i=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:o,quality:e}),u=new n(t),a=function(t,r,n){for(var o,e,i,u,a,c=t,f=[],s=0;s<r;s+=n)e=c[0+(o=4*s)],i=c[o+1],u=c[o+2],(void 0===(a=c[o+3])||a>=125)&&(e>250&&i>250&&u>250||f.push([e,i,u]));return f}(u.getImageData().data,u.width*u.height,i.quality),c=r(a,i.colorCount);return c?c.palette():null},o.prototype.getColorFromUrl=function(t,r,n){var o=this,e=document.createElement("img");e.addEventListener("load",function(){var i=o.getPalette(e,5,n);r(i[0],t)}),e.src=t},o.prototype.getImageData=function(t,r){var n=new XMLHttpRequest;n.open("GET",t,!0),n.responseType="arraybuffer",n.onload=function(){if(200==this.status){var t=new Uint8Array(this.response);i=t.length;for(var n=new Array(i),o=0;o<t.length;o++)n[o]=String.fromCharCode(t[o]);var e=n.join(""),u=window.btoa(e);r("data:image/png;base64,"+u)}},n.send()},o.prototype.getColorAsync=function(t,r,n){var o=this;this.getImageData(t,function(t){var e=document.createElement("img");e.addEventListener("load",function(){var t=o.getPalette(e,5,n);r(t[0],this)}),e.src=t})},o});