var CanvasImage=function(t){this.canvas=document.createElement("canvas");this.canvas.classList.add("bct-canvas-image");this.context=this.canvas.getContext("2d");document.body.appendChild(this.canvas);this.width=this.canvas.width=t.width;this.height=this.canvas.height=t.height;this.context.drawImage(t,0,0,this.width,this.height)};CanvasImage.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)};CanvasImage.prototype.update=function(t){this.context.putImageData(t,0,0)};CanvasImage.prototype.getPixelCount=function(){return this.width*this.height};CanvasImage.prototype.getWidth=function(){return this.width};CanvasImage.prototype.getHeight=function(){return 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)};CanvasImage.prototype.markPixels=function(t){var e=this.context.getImageData(0,0,this.width,this.height);for(var r=0;r<t.length;r++){var n=t[r];e.data[n++]=255;e.data[n++]=0;e.data[n++]=0;e.data[n++]=255}this.context.putImageData(e,0,0)};var BackgroundColorTheif=function(){};BackgroundColorTheif.prototype.getBackGroundColor=function(t,e){var r=this.getPalette(t,5,e);var n=r[0];return n};BackgroundColorTheif.prototype.getPalette=function(t,e,r){if(typeof e==="undefined"){e=10}if(typeof r==="undefined"){r=10}var n=new CanvasImage(t);var o=n.getImageData();var i=o.data;var a;var u=Math.round(n.getWidth()*.1);var h=Math.round(n.getHeight()*.1);var f=[];var s=[];a=h*n.getWidth();for(var c=0,v,g,p,l,d;c<a;c=c+r){v=c*4;g=i[v+0];p=i[v+1];l=i[v+2];d=i[v+3];if(d>=125){f.push([g,p,l]);s.push(v)}}a=n.getHeight()*n.getWidth();for(var c=(n.getHeight()-h)*n.getWidth(),v,g,p,l,d;c<a;c=c+r){v=c*4;g=i[v+0];p=i[v+1];l=i[v+2];d=i[v+3];if(d>=125){f.push([g,p,l]);s.push(v)}}r=4;a=(n.getHeight()-h)*n.getWidth();for(var c=h*n.getWidth(),v,g,p,l,d,m=0,b=0;c<a;c=c+r){v=c*4;g=i[v+0];p=i[v+1];l=i[v+2];d=i[v+3];if(d>=125){f.push([g,p,l]);s.push(v)}m=m+r;if(m>=u){b=b+r;c=(h+b)*n.getWidth();m=0}}a=(n.getHeight()-h)*n.getWidth();for(var c=h*n.getWidth()+(n.getWidth()-u-r),v,g,p,l,d,m=0,b=0;c<a;c=c+r){v=c*4;g=i[v+0];p=i[v+1];l=i[v+2];d=i[v+3];if(d>=125){f.push([g,p,l]);s.push(v)}m=m+r;if(m>=u){b=b+r;c=(h+b)*n.getWidth()+(n.getWidth()-u-r);m=0}}var x=MMCQ.quantize(f,e);var w=x.palette();n.removeCanvas();return w};if(!pv){var pv={map:function(t,r){var n={};return r?t.map(function(t,e){n.index=e;return r.call(n,t)}):t.slice()},naturalOrder:function(t,e){return t<e?-1:t>e?1:0},sum:function(t,n){var o={};return t.reduce(n?function(t,e,r){o.index=r;return t+n.call(o,e)}:function(t,e){return t+e},0)},max:function(t,e){return Math.max.apply(null,e?pv.map(t,e):t)}}}var MMCQ=function(){var g=5,c=8-g,s=1e3,v=.75;function l(t,e,r){return(t<<2*g)+(e<<g)+r}function p(t){var e=[],r=false;function n(){e.sort(t);r=true}return{push:function(t){e.push(t);r=false},peek:function(t){if(!r)n();if(t===undefined)t=e.length-1;return e[t]},pop:function(){if(!r)n();return e.pop()},size:function(){return e.length},map:function(t){return e.map(t)},debug:function(){if(!r)n();return e}}}function d(t,e,r,n,o,i,a){var u=this;u.r1=t;u.r2=e;u.g1=r;u.g2=n;u.b1=o;u.b2=i;u.histo=a}d.prototype={volume:function(t){var e=this;if(!e._volume||t){e._volume=(e.r2-e.r1+1)*(e.g2-e.g1+1)*(e.b2-e.b1+1)}return e._volume},count:function(t){var e=this,r=e.histo;if(!e._count_set||t){var n=0,o,i,a;for(o=e.r1;o<=e.r2;o++){for(i=e.g1;i<=e.g2;i++){for(a=e.b1;a<=e.b2;a++){index=l(o,i,a);n+=r[index]||0}}}e._count=n;e._count_set=true}return e._count},copy:function(){var t=this;return new d(t.r1,t.r2,t.g1,t.g2,t.b1,t.b2,t.histo)},avg:function(t){var e=this,r=e.histo;if(!e._avg||t){var n=0,o=1<<8-g,i=0,a=0,u=0,h,f,s,c,v;for(f=e.r1;f<=e.r2;f++){for(s=e.g1;s<=e.g2;s++){for(c=e.b1;c<=e.b2;c++){v=l(f,s,c);h=r[v]||0;n+=h;i+=h*(f+.5)*o;a+=h*(s+.5)*o;u+=h*(c+.5)*o}}}if(n){e._avg=[~~(i/n),~~(a/n),~~(u/n)]}else{e._avg=[~~(o*(e.r1+e.r2+1)/2),~~(o*(e.g1+e.g2+1)/2),~~(o*(e.b1+e.b2+1)/2)]}}return e._avg},contains:function(t){var e=this,r=t[0]>>c;gval=t[1]>>c;bval=t[2]>>c;return r>=e.r1&&r<=e.r2&&gval>=e.g1&&gval<=e.g2&&bval>=e.b1&&bval<=e.b2}};function m(){this.vboxes=new p(function(t,e){return pv.naturalOrder(t.vbox.count()*t.vbox.volume(),e.vbox.count()*e.vbox.volume())})}m.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){var e=this.vboxes;for(var r=0;r<e.size();r++){if(e.peek(r).vbox.contains(t)){return e.peek(r).color}}return this.nearest(t)},nearest:function(t){var e=this.vboxes,r,n,o;for(var 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));if(n<r||r===undefined){r=n;o=e.peek(i).color}}return o},forcebw:function(){var t=this.vboxes;t.sort(function(t,e){return pv.naturalOrder(pv.sum(t.color),pv.sum(e.color))});var e=t[0].color;if(e[0]<5&&e[1]<5&&e[2]<5)t[0].color=[0,0,0];var r=t.length-1,n=t[r].color;if(n[0]>251&&n[1]>251&&n[2]>251)t[r].color=[255,255,255]}};function b(t){var e=1<<3*g,r=new Array(e),n,o,i,a;t.forEach(function(t){o=t[0]>>c;i=t[1]>>c;a=t[2]>>c;n=l(o,i,a);r[n]=(r[n]||0)+1});return r}function x(t,e){var r=1e6,n=0,o=1e6,i=0,a=1e6,u=0,h,f,s;t.forEach(function(t){h=t[0]>>c;f=t[1]>>c;s=t[2]>>c;if(h<r)r=h;else if(h>n)n=h;if(f<o)o=f;else if(f>i)i=f;if(s<a)a=s;else if(s>u)u=s});return new d(r,n,o,i,a,u,e)}function w(t,f){if(!f.count())return;var e=f.r2-f.r1+1,r=f.g2-f.g1+1,n=f.b2-f.b1+1,o=pv.max([e,r,n]);if(f.count()==1){return[f.copy()]}var s=0,c=[],v=[],g,i,a,u,h;if(o==e){for(g=f.r1;g<=f.r2;g++){u=0;for(i=f.g1;i<=f.g2;i++){for(a=f.b1;a<=f.b2;a++){h=l(g,i,a);u+=t[h]||0}}s+=u;c[g]=s}}else if(o==r){for(g=f.g1;g<=f.g2;g++){u=0;for(i=f.r1;i<=f.r2;i++){for(a=f.b1;a<=f.b2;a++){h=l(i,g,a);u+=t[h]||0}}s+=u;c[g]=s}}else{for(g=f.b1;g<=f.b2;g++){u=0;for(i=f.r1;i<=f.r2;i++){for(a=f.g1;a<=f.g2;a++){h=l(i,a,g);u+=t[h]||0}}s+=u;c[g]=s}}c.forEach(function(t,e){v[e]=s-t});function p(t){var e=t+"1",r=t+"2",n,o,i,a,u,h=0;for(g=f[e];g<=f[r];g++){if(c[g]>s/2){i=f.copy();a=f.copy();n=g-f[e];o=f[r]-g;if(n<=o)u=Math.min(f[r]-1,~~(g+o/2));else u=Math.max(f[e],~~(g-1-n/2));while(!c[u])u++;h=v[u];while(!h&&c[u-1])h=v[--u];i[r]=u;a[e]=i[r]+1;return[i,a]}}}return o==e?p("r"):o==r?p("g"):p("b")}function t(t,e){if(!t.length||e<2||e>256){return false}var h=b(t),r=1<<3*g;var n=0;h.forEach(function(){n++});if(n<=e){}var o=x(t,h),i=new p(function(t,e){return pv.naturalOrder(t.count(),e.count())});i.push(o);function a(t,e){var r=1,n=0,o;while(n<s){o=t.pop();if(!o.count()){t.push(o);n++;continue}var i=w(h,o),a=i[0],u=i[1];if(!a){return}t.push(a);if(u){t.push(u);r++}if(r>=e)return;if(n++>s){return}}}a(i,v*e);var u=new p(function(t,e){return pv.naturalOrder(t.count()*t.volume(),e.count()*e.volume())});while(i.size()){u.push(i.pop())}a(u,e-u.size());var f=new m;while(u.size()){f.push(u.pop())}return f}return{quantize:t}}();