-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImgMainColor.min.js
1 lines (1 loc) · 3.56 KB
/
ImgMainColor.min.js
1
!function(t,e){"use strict";var i=function(t,e){this.COLOR_SIZE=40,this.EXCLUDE_COLOR=[],this.LEVEL=32,this.EXCLUDE_COLOR_LEVEL=[],this.RGBA_REG=/^rgba*\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d*\.*\d+\s*)*\)$/,this.options=t||{src:""},this.callback=e||function(){},this.getImgData=function(t){try{var e=document.createElement("canvas");e.width=t.width,e.height=t.height;var i=e.getContext("2d");return i.drawImage(t,0,0),i.getImageData(0,0,t.width,t.height).data}catch(t){return[]}},this.initImg=function(t,e){var i=new Image;i.src=t;var r=this;i.onload=function(){var t=r.getImgData(i);e(t)},i.onerror=function(){e([]),console.log("图片加载失败!")}},this.getAverageColor=function(t){var e=t.length,i=0,r=0,n=0,s=0;return t.map(function(t){i+=t.r,r+=t.g,n+=t.b,s+=t.a}),{r:Math.round(i/e),g:Math.round(r/e),b:Math.round(n/e),a:Math.round(s/e)}},this.getMostColor=function(t){var e=null,i=0;for(var r in t)!this.isKeyExclude(r)&&t[r].length>i&&(e=t[r],i=t[r].length);return e},this.isKeyExclude=function(t){for(var e=0;e<this.EXCLUDE_COLOR_LEVEL.length;e++)if(this.EXCLUDE_COLOR_LEVEL[e]==t)return!0;return!1},this.getColorLevel=function(t){return this.getLevel(t.r)+"_"+this.getLevel(t.g)+"_"+this.getLevel(t.b)+"_"+this.getLevel(t.a)},this.getLevel=function(t){return Math.round(t/this.LEVEL)},this.getBlockColor=function(t,e){var i=[],r=this.COLOR_SIZE,n=4*this.COLOR_SIZE;t.length<=e+n&&(r=Math.floor((t.length-e-1)/4));for(var s=0;s<r;s+=4)i.push({r:t[e+s+0],g:t[e+s+1],b:t[e+s+2],a:t[e+s+3]});return this.getAverageColor(i)},this.Hex2Rgba=function(t){if(4!=t.length&&5!=t.length&&7!=t.length&&9!=t.length)return{r:0,g:0,b:0,a:255};var e=2;(4==t.length||5==t.length)&&(e=1);var i=t.substr(1+0*e,e),r=t.substr(1+1*e,e),n=t.substr(1+2*e,e),s="f";return 5==t.length?s=t.substr(4,1):9==t.length&&(s=t.substr(7,2)),1==e&&(i+=i,r+=r,n+=n,s+=s),{r:parseInt(i,16),g:parseInt(r,16),b:parseInt(n,16),a:parseInt(s,16)}},this.rgbaStr2Rgba=function(t){t=t.replace(/rgba*|\(|\)|\s/gi,"");var e=t.split(","),i=e[3]?255*e[3]:255;return i=i>255||i<0?255:i,{r:1*e[0],g:1*e[1],b:1*e[2],a:i}},this.initExcludeLevel=function(){for(var t=0;t<this.EXCLUDE_COLOR.length;t++){var e=this.EXCLUDE_COLOR[t]+"";if(e=0==e.indexOf("#")?this.Hex2Rgba(e):this.RGBA_REG.test(e)?this.rgbaStr2Rgba(e):null){var i=this.getColorLevel(e);this.EXCLUDE_COLOR_LEVEL.push(i)}}},this.getLevelData=function(t){for(var e=t.length,i={},r=0;r<e;r+=4*this.COLOR_SIZE){var n=this.getBlockColor(t,r),s=this.getColorLevel(n);!i[s]&&(i[s]=[]),i[s].push(n)}return i},this.getResult=function(t){var e="rgba("+t.r+","+t.g+","+t.b+","+(t.a/255).toFixed(4).replace(/\.*0+$/,'')+")",i="rgb("+t.r+","+t.g+","+t.b+")",r="#"+this.Num2Hex(t.r)+this.Num2Hex(t.g)+this.Num2Hex(t.b),n=r+this.Num2Hex(t.a);return{rgba:e,rgb:i,hex:r,hexa:n}},this.Num2Hex=function(t){var e=t.toString(16)+"";return e.length<2?"0"+e:e},this.init=function(){try{var t=this.options.src||"";t||console.log("图片地址不能为空"),"number"==typeof this.options.size&&this.options.size>0&&(this.COLOR_SIZE=this.options.size),"number"==typeof this.options.level&&this.options.level>0&&this.options.level<255&&(this.LEVEL=this.options.level),"[object Array]"===Object.prototype.toString.call(this.options.exclude)&&(this.EXCLUDE_COLOR=this.options.exclude)}catch(t){return console.log("出现了一些问题",t),!1}var e=this;this.initImg(t,function(t){var i={rgb:"",rgba:"",hex:"",hexa:""};if(t.length<4)e.callback(i);else{e.initExcludeLevel();var r=e.getLevelData(t),n=e.getMostColor(r);if(n){var s=e.getAverageColor(n);e.callback(e.getResult(s))}else e.callback(i)}})},this.init()};t.ImgMainColor=t.ImgMainColor||i}(window);