-
Notifications
You must be signed in to change notification settings - Fork 3
/
iDropper.min.js
1 lines (1 loc) · 9.19 KB
/
iDropper.min.js
1
(function(z){var i=function(P){return typeof P==="string"&&P.match(/^#?[0-9a-fA-F]{6}$/i);},N=function(Q,T,S){var R,U=Q/60,P=T*(1-Math.abs(U%2-1));if(U<1){R=[T,P,0];}else{if(U<2){R=[P,T,0];}else{if(U<3){R=[0,T,P];}else{if(U<4){R=[0,P,T];}else{if(U<5){R=[P,0,T];}else{if(U<6){R=[T,0,P];}}}}}}return[parseInt(255*(R[0]+S),10),parseInt(255*(R[1]+S),10),parseInt(255*(R[2]+S),10)];},c=function(R){var U=R[0],T=R[1],Q=R[2],V=(1-Math.abs(2*Q-1))*T,P=Q-0.5*V,S=[];return N(U,V,P);},j=function(S){var U=S[0],T=S[1],Q=S[2],V=Q*T,P=Q-V,R=[];return N(U,V,P);},G=function(P){var R=[],S;if(P[3]===0){return"transparent";}for(var Q=0;Q<3;Q++){S=(P[Q]-0).toString(16);R.push(S.length==1?("0"+S):S);}return"#"+R.join("");},k=function(Q){var P;if(typeof Q==="string"){if(i(Q)){return Q;}P=Q.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);if(P){P.shift();Q=P;}}if(typeof Q!=="object"||!(Q instanceof Array)){return null;}else{return G(Q);}},h=function(Q){Q=Q.replace(/#/g,"");if(Q.length!==6){return false;}var P=parseInt(Q.substr(0,2),16);g=parseInt(Q.substr(2,2),16);b=parseInt(Q.substr(4,2),16);return[P,g,b];},L=function(V){var P=V[0]/255,T=V[1]/255,W=V[2]/255,X=Math.max(P,T,W),R=Math.min(P,T,W),U,S,Y,Q=(X+R)/2;if(X===R){S=Y=0;}else{U=X-R;Y=Q>0.5?U/(2-X-R):U/(X+R);switch(X){case P:S=(T-W)/U+(T<W?6:0);break;case T:S=(W-P)/U+2;break;case W:S=(P-T)/U+4;break;}S*=60;}return[S,Y,Q];},F=function(U){var P=U[0]/255,S=U[1]/255,V=U[2]/255,W=Math.max(P,S,V),Q=Math.min(P,S,V),R,Y,X=W,T=W-Q;Y=W===0?0:T/W;if(W===Q){R=0;}else{switch(W){case P:R=(S-V)/T+(S<V?6:0);break;case S:R=(V-P)/T+2;break;case V:R=(P-S)/T+4;break;}R*=60;}return[R,Y,X];},o=function(P){return L(h(P));},C=function(P){return G(c(P));},l=function(P){return F(h(P));},M=function(P){return G(j(P));},p=function(R){var V=R[0],U=R[1],Q=R[2],P=Q-0.5*Q*U,T=Q*U/(1-Math.abs(2*P-1));if(!T){T=0;}return[V,T,P];},A=function(U,P,S,R){if(P>S){var Q=P;P=S;S=Q;}if(R){var T=S-P;U=(U-P)%T;if(U<0){U+=T;}else{if(U>T){U-=T;}}U+=P;}else{if(U<0){U=P;}else{if(U>S){U=S;}}}return U;},u=function(R,Q){if(typeof Q!=="object"){return null;}var P;if(i(R)){P=o(R);}else{if(Object.prototype.toString.call(R)==="[object Array]"&&R.length===3){P=[R[0],R[1],R[2]];}else{return null;}}if(typeof Q.h==="number"){P[0]=A(P[0]+Q.h,0,360,true);}if(typeof Q.s==="number"){P[1]=A(P[1]+Q.s,0,1,Q.wrap||Q.wrapS);}if(typeof Q.l==="number"){P[2]=A(P[2]+Q.l,0,1,Q.wrap||Q.wrapL);}return C(P);},q=function(P,Q){return u(P,{l:Q});},m=function(P,Q){return q(P,-Q);},H=function(P,Q){return u(P,{s:Q});},w=function(P,Q){return H(P,-Q);},a=function(Q,P){return u(Q,{h:P});},D=function(P){return u(P,{h:180});},K=function(P,Q){var S=P+" {\n";for(var R in Q){if(Q.hasOwnProperty(R)){S+="\t"+R+": "+Q[R]+";\n";}}S+="}\n";return S;};z.iDropper={lighten:q,darken:m,saturate:H,desaturate:w,changeHue:a,complement:D,changeColor:u,RgbToHex:k,HslToHex:C,cssStringify:K};var O=z("html,body");var y=z("body");var s=null;var J={preventGhost:function(){return false;},mouseup:function(P){if(s){s.trigger("mouseup",P);}s=null;},mousemove:function(P){if(s){s.trigger("mousedrag",P);}}};y.bind("mousemove.iDfn",J.mousemove);y.bind("mouseup.iDfn",J.mouseup);y.delegate("img.iD-pick","mousedown",J.preventGhost);var B=z("<div/>").appendTo(y),x={SATVAL:B.attr("class","iD-img-sv").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig,""),HUEBAR:B.attr("class","iD-img-huebar").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig,""),HUERING:B.attr("class","iD-img-huering").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig,"")};B.remove();var v=256,f=482,I=50/2,E=(f/2-I)/f,d=360/(2*Math.PI),r=/MSIE (\d+\.\d+);/.test(navigator.userAgent)?parseFloat(RegExp.$1):NaN,t=r===6,n=[8,9,37,38,39,40,48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,86,96,97,98,99,100,101,102,103,104,105];var e=function(ae){this.el=ae.$el;this.hooks={};this.hideHash=ae.hideHash;var ai=this,ah=ae.size||v,P=f*ah/v,ag=P/2,ak=P*E,Q=[0,1,1],ao=ae.layout==="ring"?"ring":"bar",R={type:"",tx:0,ty:0},V=false;var ar=ae.$el,X=z("<div/>").addClass("iD iD-layout-"+ao).appendTo(ar),ap=z("<div/>").addClass("iD-sv-container iD-sv-container-"+ao).appendTo(X),ab=z("<img/>").addClass("iD-img").attr("src",x.SATVAL).appendTo(ap),ac=z("<div/>").addClass("iD-indicator-color").appendTo(ap),Y=z("<div/>").addClass("iD-cover-color iD-pick iD-sv-pick").appendTo(ap),aj=z("<div/>").addClass("iD-hue-container iD-hue-container-"+ao).appendTo(X),aa=z("<img/>").addClass("iD-img").attr("src",(ao==="ring"?x.HUERING:x.HUEBAR)).appendTo(aj),ad=z("<div/>").addClass("iD-indicator-hue").appendTo(aj),W=z("<div/>").addClass("iD-cover-hue iD-pick iD-hue-pick").appendTo(aj),an=z("<div/>").addClass("iD-preview-input").appendTo(X),af=z("<div/>").addClass("iD-preview").appendTo(an),aq=z("<div/>").addClass("iD-input-container").appendTo(an),T=z("<input/>").addClass("iD-input-field").attr("type","text").appendTo(aq);var Z={set:function(at,au){at=Z.setColor(at);if(at){Z.updateInput(at);if(!au){ai.trigger("change",at,ai.hsl);}}return at;},setColor:function(au){au=k(au);if(i(au)){var at=l(au);Q=at;ai.hex=au;ai.hsl=p(at);if(ao==="ring"){Z.huedrag({theta:(270-at[0])/d});}else{Z.huedrag({y:ah-ah*at[0]/360});}Z.svdrag({x:ah*at[1],y:ah*(1-at[2])});au=Z.setPreview(au);return au;}},setFlag:function(aw,at){var au=aw.manual?aw:z(aw.target).offset(),av=O.scrollTop()||y.scrollTop()||0;au.left-=O.scrollLeft();au.top-=av;R={type:at,tx:au.left,ty:au.top};s=ai;},setSVFlag:function(at){Z.setFlag(at,"svdrag");Z.mousedrag(at);},setHueFlag:function(at){Z.setFlag(at,"huedrag");Z.mousedrag(at);},inputKeydown:function(at){return(z.inArray(at.keyCode,n)!==-1);},inputKeyup:function(au){var at=Z.setColor(T.val());if(at){ai.trigger("change",at,ai.hsl);}return false;},mousedown:function(at){V=true;ai.trigger("start",ai.hex,ai.hsl);y.addClass("iD-dragging");},mouseup:function(at){if(V){ai.trigger("end",ai.hex,ai.hsl);ai.trigger("change",ai.hex,ai.hsl);}V=false;y.removeClass("iD-dragging");},mousedrag:function(av){var au,at={x:av.clientX-R.tx,y:av.clientY-R.ty};if(at.x<0){at.x=0;}if(at.y<0){at.y=0;}Z[R.type](at);if(au=Z.setPreview()){ai.hex=au;ai.hsl=p(Q);Z.updateInput(au);ai.trigger("drag",au,ai.hsl);}},huedrag:function(au){var aw;if(ao==="ring"){var at,ay,av,ax;if(au.theta){av=au.theta;}else{if(au.y>P){au.y=P;}at=au.x-ag;ay=au.y-ag;if(at===0){at=1e-8;}if(ay===0){ay=1e-8;}av=Math.atan(ay/at);ax=90-av*d;if((at>0&&ay>0)||(at>0&&ay<0)){ax+=180;}Q[0]=parseInt(ax-1,10);}at=parseInt(ak*Math.cos(av)+ag,10);ay=parseInt(ak*Math.sin(av)+ag,10);if(au.x<ag){at=P-at;ay=P-ay;}ad.css({top:ay,left:at});}else{if(ao==="bar"){if(au.y>ah){au.y=ah-1;}Q[0]=parseInt(360*(1-au.y/ah),10);if(Q[0]>=360){Q[0]=359;}ad.css({top:au.y});}}ap.css("background-color",aw=Z.getHex([Q[0],1,1]));},svdrag:function(at){if(at.x>ah){at.x=ah;}if(at.y>ah){at.y=ah;}ac.css({left:at.x-3,top:at.y-3});Q[1]=at.x/ah;Q[2]=1-at.y/ah;},getHex:function(at){if(!at){at=Q;}return G(j(at));},setPreview:function(at){if(!at){at=Z.getHex();}if(i(at)){if(at.charAt(0)!=="#"){at="#"+at;}af.css("background-color",at);return at;}},updateInput:function(at){if(ai.hideHash){at=at.substr(1);}T.val(at);},colorMath:function(at,au){if(au){ai.set(at);}return at;},darken:function(at,au){return Z.colorMath(m(ai.hsl,at),au);},lighten:function(at,au){return Z.colorMath(q(ai.hsl,at),au);},saturate:function(at,au){return Z.colorMath(H(ai.hsl,at),au);},desaturate:function(at,au){return Z.colorMath(w(ai.hsl,at),au);},changeHue:function(at,au){return Z.colorMath(a(ai.hsl,at),au);},complement:function(at,au){return Z.colorMath(D(ai.hsl,at),au);},changeColor:function(at,au){return Z.colorMath(u(ai.hsl,at),au);}};var S=[[".iD-hue-pick","mousedown","mousedown"],[".iD-hue-pick","mousedown","setHueFlag"],[".iD-sv-pick","mousedown","mousedown"],[".iD-sv-pick","mousedown","setSVFlag"],[".iD-input-field","keyup","inputKeyup"],[".iD-input-field","keydown","inputKeydown"]];for(var al=0,am=S.length;al<am;al++){ar.find(S[al][0]).bind(S[al][1],Z[S[al][2]]);}this.bind("mousedrag",Z.mousedrag);this.bind("mouseup",Z.mouseup);if(typeof ae.onChange==="function"){this.bind("change",ae.onChange);}if(typeof ae.onStart==="function"){this.bind("start",ae.onStart);}if(typeof ae.onDrag==="function"){this.bind("drag",ae.onDrag);}if(typeof ae.onEnd==="function"){this.bind("end",ae.onEnd);}this.set=Z.set;this.darken=Z.darken;this.lighten=Z.lighten;this.saturate=Z.saturate;this.desaturate=Z.desaturate;this.changeHue=Z.changeHue;this.complement=Z.complement;this.changeColor=Z.changeColor;this.utils=z.iDropper;var U=parseInt(ah/13,10);if(typeof ae.size==="number"){ap.css({width:ah,height:ah});if(ao==="ring"){X.css({width:P,height:P});aj.css({width:P,height:P});}else{aj.css({width:U,height:ah});}}if(r){y.addClass("ie ie"+r);}if(t){if(ao==="ring"){aa.remove();z("<span/>").addClass("iD-ie6huefix iD-pick").prependTo(aj).height(P);}ab.remove();z("<span/>").addClass("iD-ie6svfix iD-pick").prependTo(ap).height(ah);}ae.color=ae.color||"#ff0000";Z.set(ae.color,true);};e.prototype.bind=function(Q,P){if(typeof P!=="function"){return this;}if(!this.hooks[Q]){this.hooks[Q]=[];}this.hooks[Q].push(P);return this;};e.prototype.trigger=function(S){var R=this.hooks[S],P;if(!R){return false;}P=Array.prototype.slice.call(arguments,1);for(var Q=0;Q<R.length;Q++){R[Q].apply(this,P);}};z.fn.iDropper=function(Q){var P=this;return P.each(function(S){var R=P.eq(S),T={$el:R};z.extend(T,Q);R.data("iDropper",new e(T));});};})(jQuery);