UNPKG

5.91 kBJavaScriptView Raw
1/*! Fast Average Color | © 2020 Denis Seleznev | MIT License | https://github.com/fast-average-color/fast-average-color */
2!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.FastAverageColor=e()}(this,function(){"use strict";function n(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function _(t,e,r){return t[e]===r[0]&&t[e+1]===r[1]&&t[e+2]===r[2]&&t[e+3]===r[3]}function s(t,e,r){for(var o={},n=r.ignoredColor,i=0;i<e;i+=r.step){var a=t[i],s=t[i+1],u=t[i+2],c=t[i+3];if(!n||!_(t,i,n)){var h=Math.round(a/24)+","+Math.round(s/24)+","+Math.round(u/24);o[h]?o[h]=[o[h][0]+a*c,o[h][1]+s*c,o[h][2]+u*c,o[h][3]+c,o[h][4]+1]:o[h]=[a*c,s*c,u*c,c,1]}}var d=Object.keys(o).map(function(t){return o[t]}).sort(function(t,e){var r=t[4],o=e[4];return o<r?-1:r===o?0:1})[0],l=d[0],f=d[1],g=d[2],v=d[3],p=d[4];return v?[Math.round(l/v),Math.round(f/v),Math.round(g/v),Math.round(v/p)]:r.defaultColor}function u(t,e,r){for(var o=0,n=0,i=0,a=0,s=0,u=r.ignoredColor,c=0;c<e;c+=r.step){var h=t[c+3],d=t[c]*h,l=t[c+1]*h,f=t[c+2]*h;u&&_(t,c,u)||(o+=d,n+=l,i+=f,a+=h,s++)}return a?[Math.round(o/a),Math.round(n/a),Math.round(i/a),Math.round(a/s)]:r.defaultColor}function c(t,e,r){for(var o=0,n=0,i=0,a=0,s=0,u=r.ignoredColor,c=0;c<e;c+=r.step){var h=t[c],d=t[c+1],l=t[c+2],f=t[c+3];u&&_(t,c,r)||(o+=h*h*f,n+=d*d*f,i+=l*l*f,a+=f,s++)}return a?[Math.round(Math.sqrt(o/a)),Math.round(Math.sqrt(n/a)),Math.round(Math.sqrt(i/a)),Math.round(a/s)]:r.defaultColor}var h="FastAverageColor: ";return function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t)}var e,r,o;return e=t,(r=[{key:"getColorAsync",value:function(t,e){if(t){if("string"==typeof t)return this._bindImageEvents(new Image(t),e);if(t.complete){var r=this.getColor(t,e);return r.error?Promise.reject(r.error):Promise.resolve(r)}return this._bindImageEvents(t,e)}return Promise.reject(Error("".concat(h,"call .getColorAsync() without resource.")))}},{key:"getColor",value:function(e,r){r=r||{};var t=this._getDefaultColor(r),o=t;if(!e)return this._outputError(r,"call .getColor(null) without resource."),this._prepareResult(t);var n=this._getOriginalSize(e),i=this._prepareSizeAndPosition(n,r);if(!(i.srcWidth&&i.srcHeight&&i.destWidth&&i.destHeight))return this._outputError(r,'incorrect sizes for resource "'.concat(e.src,'".')),this._prepareResult(t);if(!this._ctx&&(this._canvas=this._makeCanvas(),this._ctx=this._canvas.getContext&&this._canvas.getContext("2d"),!this._ctx))return this._outputError(r,"Canvas Context 2D is not supported in this browser."),this._prepareResult(t);this._canvas.width=i.destWidth,this._canvas.height=i.destHeight;try{this._ctx.clearRect(0,0,i.destWidth,i.destHeight),this._ctx.drawImage(e,i.srcLeft,i.srcTop,i.srcWidth,i.srcHeight,0,0,i.destWidth,i.destHeight);var a=this._ctx.getImageData(0,0,i.destWidth,i.destHeight).data;o=this.getColorFromArray4(a,r)}catch(t){this._outputError(r,"security error (CORS) for resource ".concat(e.src,".\nDetails: https://developer.mozilla.org/en/docs/Web/HTML/CORS_enabled_image"),t)}return this._prepareResult(o)}},{key:"getColorFromArray4",value:function(t,e){e=e||{};var r=t.length,o=this._getDefaultColor(e);if(r<4)return o;var n,i=r-r%4,a=4*(e.step||1);switch(e.algorithm||"sqrt"){case"simple":n=u;break;case"sqrt":n=c;break;case"dominant":n=s;break;default:throw Error("".concat(h).concat(e.algorithm," is unknown algorithm."))}return n(t,i,{defaultColor:o,ignoredColor:e.ignoredColor,step:a})}},{key:"destroy",value:function(){delete this._canvas,delete this._ctx}},{key:"_getDefaultColor",value:function(t){return this._getOption(t,"defaultColor",[0,0,0,0])}},{key:"_getOption",value:function(t,e,r){return void 0===t[e]?r:t[e]}},{key:"_prepareSizeAndPosition",value:function(t,e){var r=this._getOption(e,"left",0),o=this._getOption(e,"top",0),n=this._getOption(e,"width",t.width),i=this._getOption(e,"height",t.height),a=n,s=i;if("precision"===e.mode)return{srcLeft:r,srcTop:o,srcWidth:n,srcHeight:i,destWidth:a,destHeight:s};var u;return i<n?(u=n/i,a=100,s=Math.round(a/u)):(u=i/n,s=100,a=Math.round(s/u)),(n<a||i<s||a<10||s<10)&&(a=n,s=i),{srcLeft:r,srcTop:o,srcWidth:n,srcHeight:i,destWidth:a,destHeight:s}}},{key:"_bindImageEvents",value:function(a,s){var u=this;return new Promise(function(e,r){function t(){i();var t=u.getColor(a,s);t.error?r(t.error):e(t)}function o(){i(),r(Error("".concat(h,"Error loading image ").concat(a.src,".")))}function n(){i(),r(Error("".concat(h,'Image "').concat(a.src,'" loading aborted.')))}var i=function(){a.removeEventListener("load",t),a.removeEventListener("error",o),a.removeEventListener("abort",n)};a.addEventListener("load",t),a.addEventListener("error",o),a.addEventListener("abort",n)})}},{key:"_prepareResult",value:function(t){var e=t.slice(0,3),r=[].concat(e,t[3]/255),o=this._isDark(t);return{value:t,rgb:"rgb("+e.join(",")+")",rgba:"rgba("+r.join(",")+")",hex:this._arrayToHex(e),hexa:this._arrayToHex(t),isDark:o,isLight:!o}}},{key:"_getOriginalSize",value:function(t){return t instanceof HTMLImageElement?{width:t.naturalWidth,height:t.naturalHeight}:t instanceof HTMLVideoElement?{width:t.videoWidth,height:t.videoHeight}:{width:t.width,height:t.height}}},{key:"_toHex",value:function(t){var e=t.toString(16);return 1===e.length?"0"+e:e}},{key:"_arrayToHex",value:function(t){return"#"+t.map(this._toHex).join("")}},{key:"_isDark",value:function(t){return(299*t[0]+587*t[1]+114*t[2])/1e3<128}},{key:"_makeCanvas",value:function(){return"undefined"==typeof window?new OffscreenCanvas(1,1):document.createElement("canvas")}},{key:"_outputError",value:function(t,e,r){t.silent||(console.error("".concat(h).concat(e)),r&&console.error(r))}}])&&n(e.prototype,r),o&&n(e,o),t}()});
\No newline at end of file