UNPKG

5.58 kBJavaScriptView Raw
1/*! Fast Average Color | © 2019 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 n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function a(t,e,r){for(var n={},o=0;o<e;o+=r){var i=t[o],a=t[o+1],s=t[o+2],u=t[o+3],c=Math.round(i/24)+","+Math.round(a/24)+","+Math.round(s/24);n[c]?n[c]=[n[c][0]+i*u,n[c][1]+a*u,n[c][2]+s*u,n[c][3]+u,n[c][4]+1]:n[c]=[i*u,a*u,s*u,u,1]}var h=Object.keys(n).map(function(t){return n[t]}).sort(function(t,e){var r=t[4],n=e[4];return n<r?-1:r===n?0:1})[0],d=h[0],l=h[1],f=h[2],g=h[3],v=h[4];return g?[Math.round(d/g),Math.round(l/g),Math.round(f/g),Math.round(g/v)]:[0,0,0,0]}function s(t,e,r){for(var n=0,o=0,i=0,a=0,s=0,u=0;u<e;u+=r){var c=t[u+3];n+=t[u]*c,o+=t[u+1]*c,i+=t[u+2]*c,a+=c,s++}return a?[Math.round(n/a),Math.round(o/a),Math.round(i/a),Math.round(a/s)]:[0,0,0,0]}function u(t,e,r){for(var n=0,o=0,i=0,a=0,s=0,u=0;u<e;u+=r){var c=t[u],h=t[u+1],d=t[u+2],l=t[u+3];n+=c*c*l,o+=h*h*l,i+=d*d*l,a+=l,s++}return a?[Math.round(Math.sqrt(n/a)),Math.round(Math.sqrt(o/a)),Math.round(Math.sqrt(i/a)),Math.round(a/s)]:[0,0,0,0]}var c="FastAverageColor: ";return function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t)}return function(t,e,r){e&&n(t.prototype,e),r&&n(t,r)}(t,[{key:"getColorAsync",value:function(t,e){if(t){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("Call .getColorAsync(null) without resource."))}},{key:"getColor",value:function(e,r){r=r||{};var t=this._getDefaultColor(r),n=t;if(!e)return this._outputError(r,"call .getColor(null) without resource."),this._prepareResult(t);var o=this._getOriginalSize(e),i=this._prepareSizeAndPosition(o,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;n=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(n)}},{key:"getColorFromArray4",value:function(t,e){e=e||{};var r=t.length;if(r<4)return this._getDefaultColor(e);var n,o=r-r%4,i=4*(e.step||1);switch(e.algorithm||"sqrt"){case"simple":n=s;break;case"sqrt":n=u;break;case"dominant":n=a;break;default:throw new Error("".concat(c).concat(e.algorithm," is unknown algorithm."))}return n(t,o,i)}},{key:"destroy",value:function(){delete this._canvas,delete this._ctx}},{key:"_getDefaultColor",value:function(t){return this._getOption(t,"defaultColor",[255,255,255,255])}},{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),n=this._getOption(e,"top",0),o=this._getOption(e,"width",t.width),i=this._getOption(e,"height",t.height),a=o,s=i;if("precision"===e.mode)return{srcLeft:r,srcTop:n,srcWidth:o,srcHeight:i,destWidth:a,destHeight:s};var u;return i<o?(u=o/i,a=100,s=Math.round(a/u)):(u=i/o,s=100,a=Math.round(s/u)),(o<a||i<s||a<10||s<10)&&(a=o,s=i),{srcLeft:r,srcTop:n,srcWidth:o,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 n(){i(),r(new Error("".concat(c,"Error loading image ").concat(a.src,".")))}function o(){i(),r(new Error("".concat(c,'Image "').concat(a.src,'" loading aborted.')))}var i=function(){a.removeEventListener("load",t),a.removeEventListener("error",n),a.removeEventListener("abort",o)};a.addEventListener("load",t),a.addEventListener("error",n),a.addEventListener("abort",o)})}},{key:"_prepareResult",value:function(t){var e=t.slice(0,3),r=[].concat(e,t[3]/255),n=this._isDark(t);return{value:t,rgb:"rgb("+e.join(",")+")",rgba:"rgba("+r.join(",")+")",hex:this._arrayToHex(e),hexa:this._arrayToHex(t),isDark:n,isLight:!n}}},{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(c).concat(e)),r&&console.error(r))}}]),t}()});
\No newline at end of file