angular-color-interpolator
Version:
Angular provider for color interpolation.
8 lines • 2.03 kB
JavaScript
/*
* angular-color-interpolator
* https://github.com/dnroot/angular-color-interpolator
* @version 1.0.0 <2016-01-27>
* @author Dan Root
* @license MIT
*/
(function(){!function(a,b){var c,d;return c=/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/,d=/^rgb\((?:\s*(\d{1,3})\s*,?){3}\)$/,b.module("ngColorInterpolator",[]).provider("$colorInterpolator",function(){var a,b;return b=function(a){return a.match(d)&&4===a.length?"#"+a[1]+a[1]+a[2]+a[2]+a[3]+a[3]:a},a=function(a){var e,f;return a.match(d)?(f=a.split(","),{r:parseInt(f[0].slice(4)),g:parseInt(f[1]),b:parseInt(f[2])}):a.match(c)?(a=b(a),e=parseInt(a.slice(1),16),{r:e>>16,g:e>>8&255,b:255&e}):void 0},this.blend=function(b,e,f){var g,h,i,j,k;if(null==f&&(f=.5),isNaN(parseFloat(f)))throw new Error("Invalid factor "+f+" - must be a numeric value");if(!b.match(d)&&!b.match(c))throw new Error("Invalid color: "+b+" - must be hex or rgb format");if(!e.match(d)&&!e.match(c))throw new Error("Invalid color: "+e+" - must be hex or rgb format");if(0>f&&(f=-f),f>1&&(f=1),j=a(b),k=a(e),j&&k){if(b.match(d)&&e.match(d))return i=Math.round((k.r-j.r)*f)+j.r,h=Math.round((k.g-j.g)*f)+j.g,g=Math.round((k.b-j.b)*f)+j.b,"rgb("+i+", "+h+", "+g+")";if(b.match(c)&&e.match(c))return i=16777216+65536*(Math.round((k.r-j.r)*f)+j.r),h=256*(Math.round((k.g-j.g)*f)+j.g),g=Math.round((k.b-j.b)*f)+j.b,"#"+(i+h+g).toString(16).slice(1);throw new Error("Color format mismatch - "+b+" and "+e+" are different formats")}},this.lighten=function(a,b){var e;if(isNaN(parseFloat(b)))throw new Error("Invalid factor "+b+" - must be a numeric value");if(b=parseFloat(b),a.match(d))e=0>b?"rgb(0,0,0)":"rgb(255,255,255)";else{if(!a.match(c))throw new Error("Invalid color: "+a+" - must be hex or rgb format");e=0>b?"#000000":"#ffffff"}return e?this.blend(a,e,b):void 0},this.darken=function(a,b){if(isNaN(parseFloat(b)))throw new Error("Invalid factor "+b+" - must be a numeric value");return this.lighten(a,-parseFloat(b))},this.$get=function(){return this},this})}(window,window.angular)}).call(this);