Files
zoff/static/dist/lib/color-thief.js
2015-07-03 17:19:05 +02:00

1 line
5.6 KiB
JavaScript
Executable File

var CanvasImage=function(a){this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),document.body.appendChild(this.canvas),this.width=this.canvas.width=a.width,this.height=this.canvas.height=a.height,this.context.drawImage(a,0,0,this.width,this.height)};CanvasImage.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)},CanvasImage.prototype.update=function(a){this.context.putImageData(a,0,0)},CanvasImage.prototype.getPixelCount=function(){return this.width*this.height},CanvasImage.prototype.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)},CanvasImage.prototype.removeCanvas=function(){this.canvas.parentNode.removeChild(this.canvas)};var ColorThief=function(){};if(ColorThief.prototype.getColor=function(a,b){var c=this.getPalette(a,5,b),d=c[0];return d},ColorThief.prototype.getPalette=function(a,b,c){"undefined"==typeof b&&(b=10),"undefined"==typeof c&&(c=10);for(var j,k,l,m,n,d=new CanvasImage(a),e=d.getImageData(),f=e.data,g=d.getPixelCount(),h=[],i=0;g>i;i+=c)j=4*i,k=f[j+0],l=f[j+1],m=f[j+2],n=f[j+3],n>=125&&(k>250&&l>250&&m>250||h.push([k,l,m]));var o=MMCQ.quantize(h,b),p=o.palette();return d.removeCanvas(),p},!pv)var pv={map:function(a,b){var c={};return b?a.map(function(a,d){return c.index=d,b.call(c,a)}):a.slice()},naturalOrder:function(a,b){return b>a?-1:a>b?1:0},sum:function(a,b){var c={};return a.reduce(b?function(a,d,e){return c.index=e,a+b.call(c,d)}:function(a,b){return a+b},0)},max:function(a,b){return Math.max.apply(null,b?pv.map(a,b):a)}};var MMCQ=function(){function e(b,c,d){return(b<<2*a)+(c<<a)+d}function f(a){function d(){b.sort(a),c=!0}var b=[],c=!1;return{push:function(a){b.push(a),c=!1},peek:function(a){return c||d(),void 0===a&&(a=b.length-1),b[a]},pop:function(){return c||d(),b.pop()},size:function(){return b.length},map:function(a){return b.map(a)},debug:function(){return c||d(),b}}}function g(a,b,c,d,e,f,g){var h=this;h.r1=a,h.r2=b,h.g1=c,h.g2=d,h.b1=e,h.b2=f,h.histo=g}function h(){this.vboxes=new f(function(a,b){return pv.naturalOrder(a.vbox.count()*a.vbox.volume(),b.vbox.count()*b.vbox.volume())})}function i(c){var g,h,i,j,d=1<<3*a,f=new Array(d);return c.forEach(function(a){h=a[0]>>b,i=a[1]>>b,j=a[2]>>b,g=e(h,i,j),f[g]=(f[g]||0)+1}),f}function j(a,c){var k,l,m,d=1e6,e=0,f=1e6,h=0,i=1e6,j=0;return a.forEach(function(a){k=a[0]>>b,l=a[1]>>b,m=a[2]>>b,d>k?d=k:k>e&&(e=k),f>l?f=l:l>h&&(h=l),i>m?i=m:m>j&&(j=m)}),new g(d,e,f,h,i,j,c)}function k(a,b){function p(a){var e,f,g,l,m,c=a+"1",d=a+"2",n=0;for(k=b[c];k<=b[d];k++)if(i[k]>h/2){for(g=b.copy(),l=b.copy(),e=k-b[c],f=b[d]-k,m=f>=e?Math.min(b[d]-1,~~(k+f/2)):Math.max(b[c],~~(k-1-e/2));!i[m];)m++;for(n=j[m];!n&&i[m-1];)n=j[--m];return g[d]=m,l[c]=g[d]+1,[g,l]}}if(b.count()){var c=b.r2-b.r1+1,d=b.g2-b.g1+1,f=b.b2-b.b1+1,g=pv.max([c,d,f]);if(1==b.count())return[b.copy()];var k,l,m,n,o,h=0,i=[],j=[];if(g==c)for(k=b.r1;k<=b.r2;k++){for(n=0,l=b.g1;l<=b.g2;l++)for(m=b.b1;m<=b.b2;m++)o=e(k,l,m),n+=a[o]||0;h+=n,i[k]=h}else if(g==d)for(k=b.g1;k<=b.g2;k++){for(n=0,l=b.r1;l<=b.r2;l++)for(m=b.b1;m<=b.b2;m++)o=e(l,k,m),n+=a[o]||0;h+=n,i[k]=h}else for(k=b.b1;k<=b.b2;k++){for(n=0,l=b.r1;l<=b.r2;l++)for(m=b.g1;m<=b.g2;m++)o=e(l,m,k),n+=a[o]||0;h+=n,i[k]=h}return i.forEach(function(a,b){j[b]=h-a}),g==c?p("r"):g==d?p("g"):p("b")}}function l(b,e){function p(a,b){for(var f,d=1,e=0;c>e;)if(f=a.pop(),f.count()){var h=k(g,f),i=h[0],j=h[1];if(!i)return;if(a.push(i),j&&(a.push(j),d++),d>=b)return;if(e++>c)return}else a.push(f),e++}if(!b.length||2>e||e>256)return!1;var g=i(b),m=0;g.forEach(function(){m++});var n=j(b,g),o=new f(function(a,b){return pv.naturalOrder(a.count(),b.count())});o.push(n),p(o,d*e);for(var q=new f(function(a,b){return pv.naturalOrder(a.count()*a.volume(),b.count()*b.volume())});o.size();)q.push(o.pop());p(q,e-q.size());for(var r=new h;q.size();)r.push(q.pop());return r}var a=5,b=8-a,c=1e3,d=.75;return g.prototype={volume:function(a){var b=this;return(!b._volume||a)&&(b._volume=(b.r2-b.r1+1)*(b.g2-b.g1+1)*(b.b2-b.b1+1)),b._volume},count:function(a){var b=this,c=b.histo;if(!b._count_set||a){var f,g,h,d=0;for(f=b.r1;f<=b.r2;f++)for(g=b.g1;g<=b.g2;g++)for(h=b.b1;h<=b.b2;h++)index=e(f,g,h),d+=c[index]||0;b._count=d,b._count_set=!0}return b._count},copy:function(){var a=this;return new g(a.r1,a.r2,a.g1,a.g2,a.b1,a.b2,a.histo)},avg:function(b){var c=this,d=c.histo;if(!c._avg||b){var k,l,m,n,o,f=0,g=1<<8-a,h=0,i=0,j=0;for(l=c.r1;l<=c.r2;l++)for(m=c.g1;m<=c.g2;m++)for(n=c.b1;n<=c.b2;n++)o=e(l,m,n),k=d[o]||0,f+=k,h+=k*(l+.5)*g,i+=k*(m+.5)*g,j+=k*(n+.5)*g;c._avg=f?[~~(h/f),~~(i/f),~~(j/f)]:[~~(g*(c.r1+c.r2+1)/2),~~(g*(c.g1+c.g2+1)/2),~~(g*(c.b1+c.b2+1)/2)]}return c._avg},contains:function(a){var c=this,d=a[0]>>b;return gval=a[1]>>b,bval=a[2]>>b,d>=c.r1&&d<=c.r2&&gval>=c.g1&&gval<=c.g2&&bval>=c.b1&&bval<=c.b2}},h.prototype={push:function(a){this.vboxes.push({vbox:a,color:a.avg()})},palette:function(){return this.vboxes.map(function(a){return a.color})},size:function(){return this.vboxes.size()},map:function(a){for(var b=this.vboxes,c=0;c<b.size();c++)if(b.peek(c).vbox.contains(a))return b.peek(c).color;return this.nearest(a)},nearest:function(a){for(var c,d,e,b=this.vboxes,f=0;f<b.size();f++)d=Math.sqrt(Math.pow(a[0]-b.peek(f).color[0],2)+Math.pow(a[1]-b.peek(f).color[1],2)+Math.pow(a[2]-b.peek(f).color[2],2)),(c>d||void 0===c)&&(c=d,e=b.peek(f).color);return e},forcebw:function(){var a=this.vboxes;a.sort(function(a,b){return pv.naturalOrder(pv.sum(a.color),pv.sum(b.color))});var b=a[0].color;b[0]<5&&b[1]<5&&b[2]<5&&(a[0].color=[0,0,0]);var c=a.length-1,d=a[c].color;d[0]>251&&d[1]>251&&d[2]>251&&(a[c].color=[255,255,255])}},{quantize:l}}();