-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-average-color.min.js
2 lines (2 loc) · 5.62 KB
/
fast-average-color.min.js
1
2
/*! Fast Average Color | © 2021 Denis Seleznev | MIT License | https://github.com/fast-average-color/fast-average-color */
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t="undefined"!=typeof globalThis?globalThis:t||self).FastAverageColor=r()}(this,(function(){"use strict";function t(t){var r=t.toString(16);return 1===r.length?"0"+r:r}function r(r){return"#"+r.map(t).join("")}function e(t){return t?(r=t,Array.isArray(r[0])?t:[t]):[];var r}function n(t,r,e){for(var n=0;n<e.length;n++)if(o(t,r,e[n]))return!0;return!1}function o(t,r,e){switch(e.length){case 3:if(function(t,r,e){if(255!==t[r+3])return!0;if(t[r]===e[0]&&t[r+1]===e[1]&&t[r+2]===e[2])return!0;return!1}(t,r,e))return!0;break;case 4:if(function(t,r,e){if(t[r+3]&&e[3])return t[r]===e[0]&&t[r+1]===e[1]&&t[r+2]===e[2]&&t[r+3]===e[3];return t[r+3]===e[3]}(t,r,e))return!0;break;case 5:if(function(t,r,e){var n=e[0],o=e[1],a=e[2],s=e[3],u=e[4],h=t[r+3],c=i(h,s,u);if(!s)return c;if(!h&&c)return!0;if(i(t[r],n,u)&&i(t[r+1],o,u)&&i(t[r+2],a,u)&&c)return!0;return!1}(t,r,e))return!0;break;default:return!1}}function i(t,r,e){return t>=r-e&&t<=r+e}function a(t,r,e){for(var o={},i=e.ignoredColor,a=e.step,s=[0,0,0,0,0],u=0;u<r;u+=a){var h=t[u],c=t[u+1],d=t[u+2],f=t[u+3];if(!i||!n(t,u,i)){var l=Math.round(h/24)+","+Math.round(c/24)+","+Math.round(d/24);o[l]?o[l]=[o[l][0]+h*f,o[l][1]+c*f,o[l][2]+d*f,o[l][3]+f,o[l][4]+1]:o[l]=[h*f,c*f,d*f,f,1],s[4]<o[l][4]&&(s=o[l])}}var g=s[0],v=s[1],p=s[2],m=s[3],C=s[4];return m?[Math.round(g/m),Math.round(v/m),Math.round(p/m),Math.round(m/C)]:e.defaultColor}function s(t,r,e){for(var o=0,i=0,a=0,s=0,u=0,h=e.ignoredColor,c=e.step,d=0;d<r;d+=c){var f=t[d+3],l=t[d]*f,g=t[d+1]*f,v=t[d+2]*f;h&&n(t,d,h)||(o+=l,i+=g,a+=v,s+=f,u++)}return s?[Math.round(o/s),Math.round(i/s),Math.round(a/s),Math.round(s/u)]:e.defaultColor}function u(t,r,e){for(var o=0,i=0,a=0,s=0,u=0,h=e.ignoredColor,c=e.step,d=0;d<r;d+=c){var f=t[d],l=t[d+1],g=t[d+2],v=t[d+3];h&&n(t,d,h)||(o+=f*f*v,i+=l*l*v,a+=g*g*v,s+=v,u++)}return s?[Math.round(Math.sqrt(o/s)),Math.round(Math.sqrt(i/s)),Math.round(Math.sqrt(a/s)),Math.round(s/u)]:e.defaultColor}function h(t){return c(t,"defaultColor",[0,0,0,0])}function c(t,r,e){return void 0===t[r]?e:t[r]}function d(t){return t instanceof HTMLCanvasElement?"canvas":t.src}var f="FastAverageColor: ";function l(t,r,e){r||(console.error(f+t),e&&console.error(e))}function g(t){return Error(f+t)}return function(){function t(){this.canvas=null,this.ctx=null}return t.prototype.getColorAsync=function(t,r){if(!t)return Promise.reject(g("call .getColorAsync() without resource."));if("string"==typeof t){var e=new Image;return e.crossOrigin="",e.src=t,this.bindImageEvents(e,r)}if(t instanceof Image&&!t.complete)return this.bindImageEvents(t,r);var n=this.getColor(t,r);return n.error?Promise.reject(n.error):Promise.resolve(n)},t.prototype.getColor=function(t,r){var e=h(r=r||{});if(!t)return l("call .getColor(null) without resource.",r.silent),this.prepareResult(e);var n=function(t){if(t instanceof HTMLImageElement){var r=t.naturalWidth,e=t.naturalHeight;return t.naturalWidth||-1===t.src.search(/\.svg(\?|$)/i)||(r=e=100),{width:r,height:e}}return t instanceof HTMLVideoElement?{width:t.videoWidth,height:t.videoHeight}:{width:t.width,height:t.height}}(t),o=function(t,r){var e,n=c(r,"left",0),o=c(r,"top",0),i=c(r,"width",t.width),a=c(r,"height",t.height),s=i,u=a;return"precision"===r.mode||(i>a?(e=i/a,s=100,u=Math.round(s/e)):(e=a/i,u=100,s=Math.round(u/e)),(s>i||u>a||s<10||u<10)&&(s=i,u=a)),{srcLeft:n,srcTop:o,srcWidth:i,srcHeight:a,destWidth:s,destHeight:u}}(n,r);if(!(o.srcWidth&&o.srcHeight&&o.destWidth&&o.destHeight))return l('incorrect sizes for resource "'+d(t)+'".',r.silent),this.prepareResult(e);if(this.canvas||(this.canvas="undefined"==typeof window?new OffscreenCanvas(1,1):document.createElement("canvas")),!this.ctx&&(this.ctx=this.canvas.getContext&&this.canvas.getContext("2d"),!this.ctx))return l("Canvas Context 2D is not supported in this browser.",r.silent),this.prepareResult(e);this.canvas.width=o.destWidth,this.canvas.height=o.destHeight;var i=e;try{this.ctx.clearRect(0,0,o.destWidth,o.destHeight),this.ctx.drawImage(t,o.srcLeft,o.srcTop,o.srcWidth,o.srcHeight,0,0,o.destWidth,o.destHeight);var a=this.ctx.getImageData(0,0,o.destWidth,o.destHeight).data;i=this.getColorFromArray4(a,r)}catch(e){l("security error (CORS) for resource "+d(t)+".\nDetails: https://developer.mozilla.org/en/docs/Web/HTML/CORS_enabled_image",r.silent,e)}return this.prepareResult(i)},t.prototype.getColorFromArray4=function(t,r){r=r||{};var n=t.length,o=h(r);if(n<4)return o;var i,c=n-n%4,d=4*(r.step||1);switch(r.algorithm||"sqrt"){case"simple":i=s;break;case"sqrt":i=u;break;case"dominant":i=a;break;default:throw g(r.algorithm+" is unknown algorithm.")}return i(t,c,{defaultColor:o,ignoredColor:e(r.ignoredColor),step:d})},t.prototype.prepareResult=function(t){var e,n=t.slice(0,3),o=[t[0],t[1],t[2],t[3]/255],i=(299*(e=t)[0]+587*e[1]+114*e[2])/1e3<128;return{value:[t[0],t[1],t[2],t[3]],rgb:"rgb("+n.join(",")+")",rgba:"rgba("+o.join(",")+")",hex:r(n),hexa:r(t),isDark:i,isLight:!i}},t.prototype.destroy=function(){this.canvas=null,this.ctx=null},t.prototype.bindImageEvents=function(t,r){var e=this;return new Promise((function(n,o){var i=function(){u();var i=e.getColor(t,r);i.error?o(i.error):n(i)},a=function(){u(),o(g('Error loading image "'+t.src+'".'))},s=function(){u(),o(g('Image "'+t.src+'" loading aborted.'))},u=function(){t.removeEventListener("load",i),t.removeEventListener("error",a),t.removeEventListener("abort",s)};t.addEventListener("load",i),t.addEventListener("error",a),t.addEventListener("abort",s)}))},t}()}));