jquery.color.js 6.9 KB

12345678
  1. /*!
  2. * jQuery Color Animations v2.0pre
  3. * http://jquery.org/
  4. *
  5. * Copyright 2011 John Resig
  6. * Dual licensed under the MIT or GPL Version 2 licenses.
  7. * http://jquery.org/license
  8. */(function(f,r){function t(a,c,d){var b=w[c.type]||{};if((c.empty||d)&&null==a)return null;if(c.def&&null==a)return c.def;a=b.floor?~~a:parseFloat(a);return null==a||isNaN(a)?c.def:b.mod?(a%=b.mod,0>a?b.mod+a:a):b.min>a?b.min:b.max<a?b.max:a}function x(a){var c=h(),d=c._rgba=[];a=a.toLowerCase();m(A,function(b,g){var e=g.re.exec(a),k=e&&g.parse(e),f=g.space||"rgba",e=l[f].cache;if(k)return k=c[f](k),c[e]=k[e],d=c._rgba=k._rgba,!1});if(0!==d.length)return 0===Math.max.apply(Math,d)&&f.extend(d,u.transparent),c;if(a=u[a])return a}function v(a,c,d){d=(d+1)%1;return 1>6*d?a+6*(c-a)*d:1>2*d?c:2>3*d?a+(c-a)*(2/3-d)*6:a}var B=/^([\-+])=\s*(\d+\.?\d*)/,A=[{re:/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,parse:function(a){return[a[1],a[2],a[3],a[4]]}},{re:/rgba?\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,parse:function(a){return[2.55*a[1],2.55*a[2],2.55*a[3],a[4]]}},{re:/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,parse:function(a){return[parseInt(a[1],16),parseInt(a[2],16),parseInt(a[3],16)]}},{re:/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,parse:function(a){return[parseInt(a[1]+a[1],16),parseInt(a[2]+a[2],16),parseInt(a[3]+a[3],16)]}},{re:/hsla?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,space:"hsla",parse:function(a){return[a[1],a[2]/100,a[3]/100,a[4]]}}],h=f.Color=function(a,c,d,b){return new f.Color.fn.parse(a,c,d,b)},l={rgba:{cache:"_rgba",props:{red:{idx:0,type:"byte",empty:!0},green:{idx:1,type:"byte",empty:!0},blue:{idx:2,type:"byte",empty:!0},alpha:{idx:3,type:"percent",def:1}}},hsla:{cache:"_hsla",props:{hue:{idx:0,type:"degrees",empty:!0},saturation:{idx:1,type:"percent",empty:!0},lightness:{idx:2,type:"percent",empty:!0}}}},w={"byte":{floor:!0,min:0,max:255},percent:{min:0,max:1},degrees:{mod:360,floor:!0}},y=l.rgba.props,z=h.support={},u,m=f.each;l.hsla.props.alpha=y.alpha;h.fn=h.prototype={constructor:h,parse:function(a,c,d,b){if(a===r)return this._rgba=[null,null,null,null],this;if(a instanceof f||a.nodeType)a=a instanceof f?a.css(c):f(a).css(c),c=r;var g=this,e=f.type(a),k=this._rgba=[];c!==r&&(a=[a,c,d,b],e="array");if("string"===e)return this.parse(x(a)||u._default);if("array"===e)return m(y,function(d,b){k[b.idx]=t(a[b.idx],b)}),this;if("object"===e)return a instanceof h?m(l,function(d,b){a[b.cache]&&(g[b.cache]=a[b.cache].slice())}):m(l,function(d,b){m(b.props,function(d,c){var e=b.cache;if(!g[e]&&b.to){if(null==a[d]||"alpha"===d)return;g[e]=b.to(g._rgba)}g[e][c.idx]=t(a[d],c,!0)})}),this},is:function(a){var c=h(a),d=!0,b=this;m(l,function(a,e){var k=c[e.cache],f;k&&(f=b[e.cache]||e.to&&e.to(b._rgba)||[],m(e.props,function(a,b){if(null!=k[b.idx])return d=k[b.idx]===f[b.idx]}));return d});return d},_space:function(){var a=[],c=this;m(l,function(d,b){c[b.cache]&&a.push(d)});return a.pop()},transition:function(a,c){var d=h(a),b=d._space(),g=l[b],e=this[g.cache]||g.to(this._rgba),k=e.slice(),d=d[g.cache];m(g.props,function(a,b){var g=b.idx,f=e[g],h=d[g],q=w[b.type]||{};null!==h&&(null===f?k[g]=h:(q.mod&&(h-f>q.mod/2?f+=q.mod:f-h>q.mod/2&&(f-=q.mod)),k[b.idx]=t((h-f)*c+f,b)))});return this[b](k)},blend:function(a){if(1===this._rgba[3])return this;var c=this._rgba.slice(),d=c.pop(),b=h(a)._rgba;return h(f.map(c,function(a,c){return(1-d)*b[c]+d*a}))},toRgbaString:function(){var a="rgba(",c=f.map(this._rgba,function(a,b){return null==a?2<b?1:0:a});1===c[3]&&(c.pop(),a="rgb(");return a+c.join(",")+")"},toHslaString:function(){var a="hsla(",c=f.map(this.hsla(),function(a,b){null==a&&(a=2<b?1:0);b&&3>b&&(a=Math.round(100*a)+"%");return a});1===c[3]&&(c.pop(),a="hsl(");return a+c.join(",")+")"},toHexString:function(a){var c=this._rgba.slice(),d=c.pop();a&&c.push(~~(255*d));return"#"+f.map(c,function(a,d){a=(a||0).toString(16);return 1===a.length?"0"+a:a}).join("")},toString:function(){return 0===this._rgba[3]?"transparent":this.toRgbaString()}};h.fn.parse.prototype=h.fn;l.hsla.to=function(a){if(null==a[0]||null==a[1]||null==a[2])return[null,null,null,a[3]];var c=a[0]/255,d=a[1]/255,b=a[2]/255;a=a[3];var f=Math.max(c,d,b),e=Math.min(c,d,b),k=f-e,h=f+e,n=.5*h;return[Math.round(e===f?0:c===f?60*(d-b)/k+360:d===f?60*(b-c)/k+120:60*(c-d)/k+240)%360,0===n||1===n?n:.5>=n?k/h:k/(2-h),n,null==a?1:a]};l.hsla.from=function(a){if(null==a[0]||null==a[1]||null==a[2])return[null,null,null,a[3]];var c=a[0]/360,d=a[1],b=a[2];a=a[3];d=.5>=b?b*(1+d):b+d-b*d;b=2*b-d;return[Math.round(255*v(b,d,c+1/3)),Math.round(255*v(b,d,c)),Math.round(255*v(b,d,c-1/3)),a]};m(l,function(a,c){var d=c.props,b=c.cache,g=c.to,e=c.from;h.fn[a]=function(a){g&&!this[b]&&(this[b]=g(this._rgba));if(a===r)return this[b].slice();var c=f.type(a),n="array"===c||"object"===c?a:arguments,p=this[b].slice(),l;m(d,function(a,b){var d=n["object"===c?a:b.idx];null==d&&(d=p[b.idx]);p[b.idx]=t(d,b)});return e?(l=h(e(p)),l[b]=p,l):h(p)};m(d,function(b,d){h.fn[b]||(h.fn[b]=function(c){var e=f.type(c),h="alpha"===b?this._hsla?"hsla":"rgba":a,g=this[h](),l=g[d.idx];if("undefined"===e)return l;"function"===e&&(c=c.call(this,l),e=f.type(c));if(null==c&&d.empty)return this;"string"===e&&(e=B.exec(c))&&(c=l+parseFloat(e[2])*("+"===e[1]?1:-1));g[d.idx]=c;return this[h](g)})})});m("backgroundColor borderBottomColor borderLeftColor borderRightColor borderTopColor color outlineColor".split(" "),function(a,c){f.cssHooks[c]={set:function(a,b){var g,e;if("string"!==f.type(b)||(g=x(b))){b=h(g||b);if(!z.rgba&&1!==b._rgba[3]){e="backgroundColor"===c?a.parentNode:a;do g=f.curCSS(e,"backgroundColor");while((""===g||"transparent"===g)&&(e=e.parentNode)&&e.style);b=b.blend(g&&"transparent"!==g?g:"_default")}b=b.toRgbaString()}a.style[c]=b}};f.fx.step[c]=function(a){a.colorInit||(a.start=h(a.elem,c),a.end=h(a.end),a.colorInit=!0);f.cssHooks[c].set(a.elem,a.start.transition(a.end,a.pos))}});f(function(){var a=document.createElement("div").style;a.cssText="background-color:rgba(1,1,1,.5)";z.rgba=-1<a.backgroundColor.indexOf("rgba")});u=f.Color.names={aqua:"#00ffff",azure:"#f0ffff",beige:"#f5f5dc",black:"#000000",blue:"#0000ff",brown:"#a52a2a",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgrey:"#a9a9a9",darkgreen:"#006400",darkkhaki:"#bdb76b",darkmagenta:"#8b008b",darkolivegreen:"#556b2f",darkorange:"#ff8c00",darkorchid:"#9932cc",darkred:"#8b0000",darksalmon:"#e9967a",darkviolet:"#9400d3",fuchsia:"#ff00ff",gold:"#ffd700",green:"#008000",indigo:"#4b0082",khaki:"#f0e68c",lightblue:"#add8e6",lightcyan:"#e0ffff",lightgreen:"#90ee90",lightgrey:"#d3d3d3",lightpink:"#ffb6c1",lightyellow:"#ffffe0",lime:"#00ff00",magenta:"#ff00ff",maroon:"#800000",navy:"#000080",olive:"#808000",orange:"#ffa500",pink:"#ffc0cb",purple:"#800080",violet:"#800080",red:"#ff0000",silver:"#c0c0c0",white:"#ffffff",yellow:"#ffff00",transparent:[null,null,null,0],_default:"#ffffff"}})(jQuery);