2017-02-07 02:33:24 -05:00
|
|
|
define([],function(){"use strict";var CanvasImage,Swatch,Vibrant,pv={map:function(array,f){var o={};return f?array.map(function(d,i){return o.index=i,f.call(o,d)}):array.slice()},naturalOrder:function(a,b){return a<b?-1:a>b?1:0},sum:function(array,f){var o={};return array.reduce(f?function(p,d,i){return o.index=i,p+f.call(o,d)}:function(p,d){return p+d},0)},max:function(array,f){return Math.max.apply(null,f?pv.map(array,f):array)}},MMCQ=function(){function getColorIndex(r,g,b){return(r<<2*sigbits)+(g<<sigbits)+b}function PQueue(comparator){function sort(){contents.sort(comparator),sorted=!0}var contents=[],sorted=!1;return{push:function(o){contents.push(o),sorted=!1},peek:function(index){return sorted||sort(),void 0===index&&(index=contents.length-1),contents[index]},pop:function(){return sorted||sort(),contents.pop()},size:function(){return contents.length},map:function(f){return contents.map(f)},debug:function(){return sorted||sort(),contents}}}function VBox(r1,r2,g1,g2,b1,b2,histo){var vbox=this;vbox.r1=r1,vbox.r2=r2,vbox.g1=g1,vbox.g2=g2,vbox.b1=b1,vbox.b2=b2,vbox.histo=histo}function CMap(){this.vboxes=new PQueue(function(a,b){return pv.naturalOrder(a.vbox.count()*a.vbox.volume(),b.vbox.count()*b.vbox.volume())})}function getHisto(pixels){var index,rval,gval,bval,histosize=1<<3*sigbits,histo=new Array(histosize);return pixels.forEach(function(pixel){rval=pixel[0]>>rshift,gval=pixel[1]>>rshift,bval=pixel[2]>>rshift,index=getColorIndex(rval,gval,bval),histo[index]=(histo[index]||0)+1}),histo}function vboxFromPixels(pixels,histo){var rval,gval,bval,rmin=1e6,rmax=0,gmin=1e6,gmax=0,bmin=1e6,bmax=0;return pixels.forEach(function(pixel){rval=pixel[0]>>rshift,gval=pixel[1]>>rshift,bval=pixel[2]>>rshift,rval<rmin?rmin=rval:rval>rmax&&(rmax=rval),gval<gmin?gmin=gval:gval>gmax&&(gmax=gval),bval<bmin?bmin=bval:bval>bmax&&(bmax=bval)}),new VBox(rmin,rmax,gmin,gmax,bmin,bmax,histo)}function medianCutApply(histo,vbox){function doCut(color){var left,right,vbox1,vbox2,d2,dim1=color+"1",dim2=color+"2",count2=0;for(i=vbox[dim1];i<=vbox[dim2];i++)if(partialsum[i]>total/2){for(vbox1=vbox.copy(),vbox2=vbox.copy(),left=i-vbox[dim1],right=vbox[dim2]-i,d2=left<=right?Math.min(vbox[dim2]-1,~~(i+right/2)):Math.max(vbox[dim1],~~(i-1-left/2));!partialsum[d2];)d2++;for(count2=lookaheadsum[d2];!count2&&partialsum[d2-1];)count2=lookaheadsum[--d2];return vbox1[dim2]=d2,vbox2[dim1]=vbox1[dim2]+1,[vbox1,vbox2]}}var vBoxCount=vbox.count();if(vBoxCount){var rw=vbox.r2-vbox.r1+1,gw=vbox.g2-vbox.g1+1,bw=vbox.b2-vbox.b1+1,maxw=pv.max([rw,gw,bw]);if(1===vBoxCount)return[vbox.copy()];var i,j,k,sum,index,total=0,partialsum=[],lookaheadsum=[];if(maxw===rw)for(i=vbox.r1;i<=vbox.r2;i++){for(sum=0,j=vbox.g1;j<=vbox.g2;j++)for(k=vbox.b1;k<=vbox.b2;k++)index=getColorIndex(i,j,k),sum+=histo[index]||0;total+=sum,partialsum[i]=total}else if(maxw===gw)for(i=vbox.g1;i<=vbox.g2;i++){for(sum=0,j=vbox.r1;j<=vbox.r2;j++)for(k=vbox.b1;k<=vbox.b2;k++)index=getColorIndex(j,i,k),sum+=histo[index]||0;total+=sum,partialsum[i]=total}else for(i=vbox.b1;i<=vbox.b2;i++){for(sum=0,j=vbox.r1;j<=vbox.r2;j++)for(k=vbox.g1;k<=vbox.g2;k++)index=getColorIndex(j,k,i),sum+=histo[index]||0;total+=sum,partialsum[i]=total}return partialsum.forEach(function(d,i){lookaheadsum[i]=total-d}),doCut(maxw===rw?"r":maxw===gw?"g":"b")}}function quantize(pixels,maxcolors){function iter(lh,target){for(var vbox,ncolors=1,niters=0;niters<maxIterations;)if(vbox=lh.pop(),vbox.count()){var vboxes=medianCutApply(histo,vbox),vbox1=vboxes[0],vbox2=vboxes[1];if(!vbox1)return;if(lh.push(vbox1),vbox2&&(lh.push(vbox2),ncolors++),ncolors>=target)return;if(niters++>maxIterations)return}else lh.push(vbox),niters++}if(!pixels.length||maxcolors<2||maxcolors>256)return!1;var histo=getHisto(pixels),nColors=0;histo.forEach(function(){nColors++});var vbox=vboxFromPixels(pixels,histo),pq=new PQueue(function(a,b){return pv.naturalOrder(a.count(),b.count())});pq.push(vbox),iter(pq,fractByPopulations*maxcolors);for(var pq2=new PQueue(function(a,b){return pv.naturalOrder(a.count()*a.volume(),b.count()*b.volume())});pq.size();)pq2.
|