You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RnQ/Distro/Template/libs/vibrant.min.js

1 line
9.7 KiB
JavaScript

if(!pv)var pv={map:function(t,r){var o={};return r?t.map(function(t,i){return o.index=i,r.call(o,t)}):t.slice()},naturalOrder:function(t,r){return t<r?-1:t>r?1:0},sum:function(t,r){var o={};return t.reduce(r?function(t,i,n){return o.index=n,t+r.call(o,i)}:function(t,r){return t+r},0)},max:function(t,r){return Math.max.apply(null,r?pv.map(t,r):t)}};var MMCQ=function(){function t(t,r,o){return(t<<10)+(r<<5)+o}function r(t){var r=[],o=!1;function i(){r.sort(t),o=!0}return{push:function(t){r.push(t),o=!1},peek:function(t){return o||i(),void 0===t&&(t=r.length-1),r[t]},pop:function(){return o||i(),r.pop()},size:function(){return r.length},map:function(t){return r.map(t)},debug:function(){return o||i(),r}}}function o(t,r,o,i,n,e,s){var h=this;h.r1=t,h.r2=r,h.g1=o,h.g2=i,h.b1=n,h.b2=e,h.histo=s}function i(){this.vboxes=new r(function(t,r){return pv.naturalOrder(t.vbox.count()*t.vbox.volume(),r.vbox.count()*r.vbox.volume())})}function n(r,o){if(o.count()){var i=o.r2-o.r1+1,n=o.g2-o.g1+1,e=o.b2-o.b1+1,s=pv.max([i,n,e]);if(1==o.count())return[o.copy()];var h,a,u,c,p,$=0,f=[],A=[];if(s==i)for(h=o.r1;h<=o.r2;h++){for(c=0,a=o.g1;a<=o.g2;a++)for(u=o.b1;u<=o.b2;u++)c+=r[p=t(h,a,u)]||0;$+=c,f[h]=$}else if(s==n)for(h=o.g1;h<=o.g2;h++){for(c=0,a=o.r1;a<=o.r2;a++)for(u=o.b1;u<=o.b2;u++)c+=r[p=t(a,h,u)]||0;$+=c,f[h]=$}else for(h=o.b1;h<=o.b2;h++){for(c=0,a=o.r1;a<=o.r2;a++)for(u=o.g1;u<=o.g2;u++)c+=r[p=t(a,u,h)]||0;$+=c,f[h]=$}return f.forEach(function(t,r){A[r]=$-t}),function t(r){var i,n,e,s,a,u=r+"1",c=r+"2",p=0;for(h=o[u];h<=o[c];h++)if(f[h]>$/2){for(e=o.copy(),s=o.copy(),a=(i=h-o[u])<=(n=o[c]-h)?Math.min(o[c]-1,~~(h+n/2)):Math.max(o[u],~~(h-1-i/2));!f[a];)a++;for(p=A[a];!p&&f[a-1];)p=A[--a];return e[c]=a,s[u]=e[c]+1,[e,s]}}(s==i?"r":s==n?"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(r){var o=this,i=o.histo;if(!o._count_set||r){var n,e,s,h,a=0;for(n=o.r1;n<=o.r2;n++)for(e=o.g1;e<=o.g2;e++)for(s=o.b1;s<=o.b2;s++)a+=i[h=t(n,e,s)]||0;o._count=a,o._count_set=!0}return o._count},copy:function(){return new o(this.r1,this.r2,this.g1,this.g2,this.b1,this.b2,this.histo)},avg:function(r){var o=this,i=o.histo;if(!o._avg||r){var n,e,s,h,a,u=0,c=0,p=0,$=0;for(e=o.r1;e<=o.r2;e++)for(s=o.g1;s<=o.g2;s++)for(h=o.b1;h<=o.b2;h++)u+=n=i[a=t(e,s,h)]||0,c+=n*(e+.5)*8,p+=n*(s+.5)*8,$+=n*(h+.5)*8;u?o._avg=[~~(c/u),~~(p/u),~~($/u)]:o._avg=[~~(8*(o.r1+o.r2+1)/2),~~(8*(o.g1+o.g2+1)/2),~~(8*(o.b1+o.b2+1)/2)]}return o._avg},contains:function(t){var r=t[0]>>3;return gval=t[1]>>3,bval=t[2]>>3,r>=this.r1&&r<=this.r2&&gval>=this.g1&&gval<=this.g2&&bval>=this.b1&&bval<=this.b2}},i.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,o=0;o<r.size();o++)if(r.peek(o).vbox.contains(t))return r.peek(o).color;return this.nearest(t)},nearest:function(t){for(var r,o,i,n=this.vboxes,e=0;e<n.size();e++)((o=Math.sqrt(Math.pow(t[0]-n.peek(e).color[0],2)+Math.pow(t[1]-n.peek(e).color[1],2)+Math.pow(t[2]-n.peek(e).color[2],2)))<r||void 0===r)&&(r=o,i=n.peek(e).color);return i},forcebw:function(){var t=this.vboxes;t.sort(function(t,r){return pv.naturalOrder(pv.sum(t.color),pv.sum(r.color))});var r=t[0].color;r[0]<5&&r[1]<5&&r[2]<5&&(t[0].color=[0,0,0]);var o=t.length-1,i=t[o].color;i[0]>251&&i[1]>251&&i[2]>251&&(t[o].color=[255,255,255])}},{quantize:function e(s,h){if(!s.length||h<2||h>256)return!1;var a,u,c,p,$,f,A,T,_,l,v,g,b,M,d,R,w,S=(a=s,f=Array(32768),a.forEach(function(r){c=r[0]>>3,f[u=t(c,p=r[1]>>3,$=r[2]>>3)]=(f[u]||0)+1}),f),L=0;S.forEach(function(){L++});var U=(A=s,T=S,g=1e6,b=0,M=1e6,d=0,R=1e6,w=0,A.forEach(function(t){_=t[0]>>3,l=t[1]>>3,v=t[2]>>3,_<g?g=_:_>b&&(b=_),l<M?M=l:l>d&&(d=l),v<R?R=v:v>w&&(w=v)}),new o(g,b,M,d,R,w,T)),I=new r(function(t,r){return pv.naturalOrder(t.count(),r.count())});function N(t,r){for(var o,i=1,e=0;e<1e3;){if(!(o=t.pop()).count()){t.push(o),e++;continue}var s=n(S,o),h=s[0],a=s[1];if(!h||(t.push(h),a&&(t.push(a),i++),i>=r||e++>1e3))return}}I.push(U),N(I,.75*h);for(var V=new r(function(t,r){return pv.naturalOrder(t.count()*t.volume(),r.count()*r.volume())});I.size();)V.push(I.pop());N(V,h-V.size());for(var E=new i;V.size();)E.push(V.pop());return E}}}();(function(){var t,r,o=[].slice;window.Swatch=t=function(){function t(t,r){this.rgb=t,this.population=r}return t.prototype.hsl=void 0,t.prototype.rgb=void 0,t.prototype.population=1,t.yiq=0,t.prototype.getHsl=function(){return this.hsl?this.hsl:this.hsl=r.rgbToHsl(this.rgb[0],this.rgb[1],this.rgb[2])},t.prototype.getPopulation=function(){return this.population},t.prototype.getRgb=function(){return this.rgb},t.prototype.getHex=function(){return"#"+(16777216+(this.rgb[0]<<16)+(this.rgb[1]<<8)+this.rgb[2]).toString(16).slice(1,7)},t.prototype.getTitleTextColor=function(){return(this._ensureTextColors(),this.yiq<200)?"#fff":"#000"},t.prototype.getBodyTextColor=function(){return(this._ensureTextColors(),this.yiq<150)?"#fff":"#000"},t.prototype._ensureTextColors=function(){if(!this.yiq)return this.yiq=(299*this.rgb[0]+587*this.rgb[1]+114*this.rgb[2])/1e3},t}(),window.Vibrant=r=function(){function r(r,o,i){if(this.swatches=(e=this.swatches,s=this,function(){return e.apply(s,arguments)}),void 0===o&&(o=64),void 0===i){var n,e,s,h,a,r,i=Math.round(r.width/64);i<1&&(i=1)}for(h=[],y=0,x=0;y<r.height;){for(;x<r.width;)if((clr=r.colorAt(x,y))&&clr.A>=125&&!(clr.R>250&&clr.G>250&&clr.B>250)&&h.push([clr.R,clr.G,clr.B]),(x+=i)>=r.width){x-=r.width;break}y++}a=MMCQ.quantize(h,o),this._swatches=a.vboxes.map(function(r){return new t(r.color,r.vbox.count())}),this.maxPopulation=this.findMaxPopulation,this.generateVarationColors(),this.generateEmptySwatches()}return r.prototype._swatches=[],r.prototype.TARGET_DARK_LUMA=.26,r.prototype.MAX_DARK_LUMA=.45,r.prototype.MIN_LIGHT_LUMA=.55,r.prototype.TARGET_LIGHT_LUMA=.74,r.prototype.MIN_NORMAL_LUMA=.3,r.prototype.TARGET_NORMAL_LUMA=.5,r.prototype.MAX_NORMAL_LUMA=.7,r.prototype.TARGET_MUTED_SATURATION=.3,r.prototype.MAX_MUTED_SATURATION=.4,r.prototype.TARGET_VIBRANT_SATURATION=1,r.prototype.MIN_VIBRANT_SATURATION=.35,r.prototype.WEIGHT_SATURATION=3,r.prototype.WEIGHT_LUMA=6,r.prototype.WEIGHT_POPULATION=1,r.prototype.VibrantSwatch=void 0,r.prototype.MutedSwatch=void 0,r.prototype.DarkVibrantSwatch=void 0,r.prototype.DarkMutedSwatch=void 0,r.prototype.LightVibrantSwatch=void 0,r.prototype.LightMutedSwatch=void 0,r.prototype.HighestPopulation=0,r.prototype.generateVarationColors=function(){return this.VibrantSwatch=this.findColorVariation(this.TARGET_NORMAL_LUMA,this.MIN_NORMAL_LUMA,this.MAX_NORMAL_LUMA,this.TARGET_VIBRANT_SATURATION,this.MIN_VIBRANT_SATURATION,1),this.LightVibrantSwatch=this.findColorVariation(this.TARGET_LIGHT_LUMA,this.MIN_LIGHT_LUMA,1,this.TARGET_VIBRANT_SATURATION,this.MIN_VIBRANT_SATURATION,1),this.DarkVibrantSwatch=this.findColorVariation(this.TARGET_DARK_LUMA,0,this.MAX_DARK_LUMA,this.TARGET_VIBRANT_SATURATION,this.MIN_VIBRANT_SATURATION,1),this.MutedSwatch=this.findColorVariation(this.TARGET_NORMAL_LUMA,this.MIN_NORMAL_LUMA,this.MAX_NORMAL_LUMA,this.TARGET_MUTED_SATURATION,0,this.MAX_MUTED_SATURATION),this.LightMutedSwatch=this.findColorVariation(this.TARGET_LIGHT_LUMA,this.MIN_LIGHT_LUMA,1,this.TARGET_MUTED_SATURATION,0,this.MAX_MUTED_SATURATION),this.DarkMutedSwatch=this.findColorVariation(this.TARGET_DARK_LUMA,0,this.MAX_DARK_LUMA,this.TARGET_MUTED_SATURATION,0,this.MAX_MUTED_SATURATION)},r.prototype.generateEmptySwatches=function(){var o;if(void 0===this.VibrantSwatch&&void 0!==this.DarkVibrantSwatch&&((o=this.DarkVibrantSwatch.getHsl())[2]=this.TARGET_NORMAL_LUMA,this.VibrantSwatch=new t(r.hslToRgb(o[0],o[1],o[2]),0)),void 0===this.DarkVibrantSwatch&&void 0!==this.VibrantSwatch)return(o=this.VibrantSwatch.getHsl())[2]=this.TARGET_DARK_LUMA,this.DarkVibrantSwatch=new t(r.hslToRgb(o[0],o[1],o[2]),0)},r.prototype.findMaxPopulation=function(){var t,r,o,i,n;for(t=0,o=0,r=(i=this._swatches).length;t<r;t++)o=Math.max(o,(n=i[t]).getPopulation());return o},r.prototype.findColorVariation=function(t,r,o,i,n,e){var s,h,a,u,c,p,$,f,A;for(s=0,u=void 0,c=0,h=(p=this._swatches).length;s<h;s++)$=(f=p[s]).getHsl()[1],a=f.getHsl()[2],$>=n&&$<=e&&a>=r&&a<=o&&!this.isAlreadySelected(f)&&(A=this.createComparisonValue($,i,a,t,f.getPopulation(),this.HighestPopulation),(void 0===u||A>c)&&(u=f,c=A));return u},r.prototype.createComparisonValue=function(t,r,o,i,n,e){return this.weightedMean(this.invertDiff(t,r),this.WEIGHT_SATURATION,this.invertDiff(o,i),this.WEIGHT_LUMA,n/e,this.WEIGHT_POPULATION)},r.prototype.invertDiff=function(t,r){return 1-Math.abs(t-r)},r.prototype.weightedMean=function(){var t,r,i,n,e,s;for(e=1<=arguments.length?o.call(arguments,0):[],r=0,i=0,t=0;t<e.length;)r+=(n=e[t])*(s=e[t+1]),i+=s,t+=2;return r/i},r.prototype.swatches=function(){return{Vibrant:this.VibrantSwatch,Muted:this.MutedSwatch,DarkVibrant:this.DarkVibrantSwatch,DarkMuted:this.DarkMutedSwatch,LightVibrant:this.LightVibrantSwatch,LightMuted:this.LightMuted}},r.prototype.isAlreadySelected=function(t){return this.VibrantSwatch===t||this.DarkVibrantSwatch===t||this.LightVibrantSwatch===t||this.MutedSwatch===t||this.DarkMutedSwatch===t||this.LightMutedSwatch===t},r.rgbToHsl=function(t,r,o){var i,n,e,s,h,a;if(t/=255,r/=255,o/=255,n=void 0,a=void 0,e=((s=Math.max(t,r,o))+(h=Math.min(t,r,o)))/2,s===h)n=a=0;else{switch(i=s-h,a=e>.5?i/(2-s-h):i/(s+h),s){case t:n=(r-o)/i+(r<o?6:0);break;case r:n=(o-t)/i+2;break;case o:n=(t-r)/i+4}n/=6}return[n,a,e]},r.hslToRgb=function(t,r,o){var i,n,e,s,h,a;return a=void 0,n=void 0,i=void 0,e=function(t,r,o){return(o<0&&(o+=1),o>1&&(o-=1),o<1/6)?t+(r-t)*6*o:o<.5?r:o<2/3?t+(r-t)*(2/3-o)*6:t},0===r?a=n=i=o:(h=o<.5?o*(1+r):o+r-o*r,a=e(s=2*o-h,h,t+1/3),n=e(s,h,t),i=e(s,h,t-1/3)),[255*a,255*n,255*i]},r}()}).call(this);