PageRenderTime 81ms CodeModel.GetById 14ms app.highlight 61ms RepoModel.GetById 2ms app.codeStats 0ms

/jquery.colorInput-min.js

http://jquery-colorinput.googlecode.com/
JavaScript | 1 lines | 1 code | 0 blank | 0 comment | 0 complexity | d1ebccda1a24aedfa1296d67da98a6d3 MD5 | raw file
1(function(A){A.fn.colorInput=function(B){B=jQuery.extend(A.colorInput.defaults,B);this.each(function(){A(this).data("colorControl",new A.colorInput(this,B))});return this};A.fn.colorSelected=function(B){if(B){this.bind("colorSelected",B);return this}else{this.trigger("colorSelected")}};A.fn.colorAccept=function(B){if(B){this.bind("colorAccept",B);return this}else{this.trigger("colorAccept")}};A.fn.colorCancel=function(B){if(B){this.bind("colorCancel",B);return this}else{this.trigger("colorCancel")}};A.colorInput=function(E,Z){var g=A(E).addClass(Z.cssClass);if(g.data("colorControl")){return this}var a=this,f=Z.cells*Z.cellSize,N=Z.cells*Z.cellSize+Z.hueWidth;var J="<a href='#' style='".concat(Z.cssButton,"'></a>");var Y=A(J).html(Z.textButton).addClass("ui-colourInput-button ui-state-default ui-corner-all").hover(function(){A(this).addClass("ui-state-hover")},function(){A(this).removeClass("ui-state-hover")}),D,P,Q;g.after(Y);Y.click(function(b){if(A.colorInput.current==a){L()}else{if(A.colorInput.current){A.colorInput.current.cancel()}else{j()}}b.stopPropagation();return false});var R=A("#".concat(Z.dropdownId)),U=A("#".concat(Z.dropdownId,"Shadow")),G=A("#".concat(Z.dropdownId,"HueMap")),K=A("#".concat(Z.dropdownId,"HueMapShadow"));if(R.length==1){if(Z.acceptCancelButtons){D=R.find(".ui-colourInput-tools");P=D.find(":eq(0)");Q=D.find(":eq(1)")}}else{var B=["<div class='saturationBrightnessMap' style='position:relative;display:inline-block;overflow:hidden;width:",Z.cells*Z.cellSize,"px;height:",Z.cells*Z.cellSize,"px;cursor:pointer'>"];for(var t=0;t<Z.cells;t++){for(var m=0;m<Z.cells;m++){B.push("<div ".concat("style='float:left;overflow:hidden;width:",Z.cellSize,"px;height:",Z.cellSize,"px;border:none 0'></div>"))}}B.push("</div>");var F=A("<div />").css({position:"relative",width:f,height:f+(Z.acceptCancelButtons?Z.hueWidth:0),zIndex:1,borderRight:"none"}).append(B.join(""));var q=["<div id='",Z.dropdownId,"HueMap' style='position:absolute;width:",Z.hueWidth,"px;height:",Z.cells*Z.cellSize,"px;cursor:pointer'>"];for(var o=0;o<Z.cells*Z.cellSize;o++){q.push("<div style='float:left;position:relative;overflow:hidden;width:",Z.hueWidth,"px;height:1px;border:none 0'>&nbsp;</div>")}q.push("</div>");G=A(q.join("")).css({marginTop:Z.acceptCancelButtons?Z.hueWidth:0,marginLeft:f-Z.hueWidth});K=A("<div id='".concat(Z.dropdownId,"HueMapShadow', class='ui-widget-shadow' />")).css({display:"none",position:"absolute",left:f,top:Z.acceptCancelButtons?Z.hueWidth:0,width:0,height:f,paddingLeft:0,paddingRight:0,"-moz-border-radius-topleft":0,"-moz-border-radius-bottomleft":0,"-webkit-border-radius-topleft":0,"-webkit-border-radius-bottomleft":0,borderTopLeftRadius:0,borderBottomLeftRadius:0});R=A("<div ".concat("id='",Z.dropdownId,"' class='ui-colourInput-dropdown ui-widget' />")).css({display:"none",position:"absolute",width:0,height:0}).append(K).append(G).append(F);U=A("<div ".concat("id='",Z.dropdownId,"Shadow' class='ui-widget-shadow' />")).css({display:"none",position:"absolute",width:f,height:f+(Z.acceptCancelButtons?Z.hueWidth:0),"-moz-border-radius-bottomright":0,"-webkit-border-radius-bottomright":0,borderBottomRightRadius:0});if(Z.acceptCancelButtons){var C=Math.round(f/2);P=A(J).html(Z.textAccept).css({width:C,height:Z.hueWidth,margin:0});Q=A(J).html(Z.textCancel).css({width:f-C,height:Z.hueWidth,margin:0});D=A("<div class='ui-colourInput-tools'></div>").css({position:"relative",height:Z.hueWidth}).append(P).append(Q);F.prepend(D)}else{U.css({"-moz-border-radius-topright":0,"-webkit-border-radius-topright":0,borderTopRightRadius:0})}A(document.body).append(U).append(R);setTimeout(function(){K.css("marginLeft",U.css("paddingLeft"))},1000)}var e=R.find(".saturationBrightnessMap"),l=new A.colorInput.color(),I,c,V=false;if(Z.hideInput){g.hide()}var T;var r=function(w){if(T){clearTimeout(T);T=null}if(w){T=setTimeout(r,w);return }var y=l.hue*f-Math.round(l.hue*f);var v=new A.colorInput.color(0,1,1);p(null);G.find("div").each(function(){v.hue=y/f;this.hue=v.hue;this.color=v.toHex();this.style.backgroundColor=this.color;if(this.hue==l.hue){p(this)}y++});var x=Z.cells-1,u=Z.cells-1;v=new A.colorInput.color(l.hue,0,0);H(null);e.find("div").each(function(){v.saturation=(x/(Z.cells-1)*(Z.maxSaturation-Z.minSaturation))+Z.minSaturation;v.brightness=(u/(Z.cells-1)*(Z.maxBrightness-Z.minBrightness))+Z.minBrightness;this.saturation=v.saturation;this.brightness=v.brightness;this.color=v.toHex();this.style.backgroundColor=this.color;if(this.color==I){H(this)}x--;if(x<0){x=Z.cells-1;u--}})};var d=function(){var b=g.val();b=b.length>0&&b.charAt(0)!="#"?"#".concat(b):b;if(b!=I){l.fromHex(b);O(b,V)}};var O=function(h,b){I=h;g.val(h[0]=="#"&&Z.noHash?h.slice(1):h);a.isChanged=c&&I!=c;Y.css({backgroundColor:h,color:l.brightnessContrast().toHex()});if(Z.acceptCancelButtons){P.css({backgroundColor:h,color:l.brightnessContrast().toHex()})}if(b){r(Z.hoverSelect?50:0)}};var L=function(){if(c!=I){g.change()}c=null;n()};var M=function(){if(a.isChanged){g.val(c);d();g.colorSelected()}c=null;n()};var n=function(){A.each(Z.cancelOnClick,function(){A(this).unbind("click.color")});G.find("div").unbind("mousedown").unbind("mousemove").unbind("mouseup").unbind("click");e.find("div").unbind("mousedown").unbind("mousemove").unbind("mouseup").unbind("click");e.unbind("dblclick");R.unbind("click");if(Z.acceptCancelButtons){P.unbind("click");Q.unbind("click")}G.animate({marginLeft:f-Z.hueWidth},250);K.animate({width:0},250);U.fadeOut(250,function(){R.animate({height:0,width:0},{duration:250,complete:function(){R.css({display:"none"})}})});A.colorInput.current=null;a.isChanged=false;V=false};var j=function(){c=I;if(Z.acceptCancelButtons){P.add(Q).css({backgroundColor:I,color:l.brightnessContrast().toHex()})}r();A.each(Z.cancelOnClick,function(){A(this).bind("click.color",M)});if(Z.hoverSelect){G.find("div").mousemove(function(h){k(this);g.colorSelected()});e.find("div").mousemove(function(h){W(this);g.colorSelected()});e.dblclick(function(h){L();h.stopPropagation();return false});e.click(function(h){L();h.stopPropagation();return false})}else{G.find("div").mousedown(function(h){return S(h,G)}).mousemove(function(h){if(X(h,G)){k(this);g.colorSelected()}}).mouseup(function(h){i(h,G)}).click(function(h){k(this);g.colorSelected()});e.find("div").mousedown(function(h){return S(h,e)}).mousemove(function(h){if(X(h,e)){W(this);g.colorSelected()}}).mouseup(function(h){i(h,e)}).click(function(h){W(this);g.colorSelected()});e.dblclick(function(h){L();h.stopPropagation();return false})}R.click(function(h){h.stopPropagation();return false});var b=Y.offset();R.add(U).css({top:b.top-1+"px",left:b.left-1+(Z.showLeft?-f:0)+"px"}).end().css({display:""}).animate({height:f+(Z.acceptCancelButtons?Z.hueWidth:0),width:f},{duration:500,complete:function(){G.animate({marginLeft:f},250);K.animate({width:Z.hueWidth},250);if(A.browser.msie){U.css("filter",U.css("filter").toLowerCase())}U.fadeIn(250);if(Z.acceptCancelButtons){P.click(function(h){L();h.stopPropagation();return false});Q.click(function(h){M();h.stopPropagation();return false})}}});A.colorInput.current=a;V=true};if(A.browser.msie){R.attr("unselectable","on");R.find("div").attr("unselectable","on")}var S=function(h,b){if(b.data("drag")){i(h,b)}b.data("drag",true);return false};var i=function(h,b){b.data("drag",false)};var X=function(h,b){if(!b.data("drag")){return false}if(A.browser.msie&&!h.button){i(h,b);return false}return true};var p=function(b){var h=G.data("selected");if(h){h.style.zIndex=0;h.style.width=Z.hueWidth+"px";h.style.margin="0";h.style.border="none 0"}G.data("selected",b);if(b){b.style.zIndex=1;b.style.width=(Z.hueWidth-2)+"px";b.style.margin="-1px 0";b.style.border="solid 1px #000"}};var k=function(b){l.hue=b.hue;O(l.toHex(),V)};var H=function(b){var h=e.data("selected");if(h){h.style.width=Z.cellSize+"px";h.style.height=Z.cellSize+"px";h.style.border="none 0"}e.data("selected",b);if(b){b.style.width=(Z.cellSize-2)+"px";b.style.height=(Z.cellSize-2)+"px";b.style.border="solid 1px ".concat(l.brightnessContrast().toHex())}};var W=function(b){l.saturation=b.saturation;l.brightness=b.brightness;O(l.toHex(),false);H(b)};g.bind("change",d);g.bind("colorAccept",L);g.bind("colorCancel",M);if(Z.change){g.change(Z.change)}if(Z.colorSelected){g.colorSelected(Z.colorSelected)}this.accept=L;this.cancel=M;this.isChanged=false;d();return this};A.colorInput.defaults={acceptCancelButtons:true,cancelOnClick:[document],cells:15,cellSize:10,change:null,colorSelected:null,cssClass:"ui-colorInput",cssButton:"display:inline-block;height:1em;width:1em;margin-bottom:-.25em;background-image:none",dropdownId:"ColorDropdown",hideInput:true,hoverSelect:false,hueWidth:20,minBrightness:0,minSaturation:0,maxBrightness:1,maxSaturation:1,noHash:false,showLeft:false,textButton:"",textAccept:"",textCancel:""};A.colorInput.current=null;A.colorInput.color=function(B,C,D){if(arguments.length==1&&B.constructor==String){this.fromHex(B)}else{this.hue=parseFloat(B)||0;this.saturation=parseFloat(C)||0;this.brightness=parseFloat(D)||0}this.isValid=function(){if(isNaN(this.hue)){return false}while(this.hue>1){this.hue-=1}while(this.hue<0){this.hue+=1}if(isNaN(this.saturation)){return false}if(this.saturation>1){this.saturation=1}else{if(this.saturation<0){this.saturation=0}}if(isNaN(this.brightness)){return false}if(this.brightness>1){this.brightness=1}else{if(this.brightness<0){this.brightness=0}}return true};this.isValid();this.brightnessContrast=function(){return new A.colorInput.color(0,0,this.brightness>=0.75?0:1)};this.valueToHex=function(F){var E=Math.round(F*255).toString(16);return E.length==1?"0"+E:E};this.hexToValue=function(E){return parseInt(E,16)/255};this.fromHex=function(G){if(G.slice(0,1)=="#"){G=G.slice(1)}if(G.length==3){G=G.split("");G=G[0]+G[0]+G[1]+G[1]+G[2]+G[2]}if(G.length!=6){this.brightness=NaN;return }var I=this.hexToValue(G.substr(0,2));var H=this.hexToValue(G.substr(2,2));var E=this.hexToValue(G.substr(4,2));this.brightness=Math.max(Math.max(I,H),E);var F=Math.min(Math.min(I,H),E);if(F==this.brightness){this.hue=0;this.saturation=0}else{var J=this.brightness-F;this.saturation=J/this.brightness;if(I==this.brightness){this.hue=(H-E)/J}else{if(H==this.brightness){this.hue=2+((E-I)/J)}else{this.hue=4+((I-H)/J)}}this.hue/=6;if(this.hue<0){this.hue+=1}if(this.hue>1){this.hue-=1}}return this};this.toHex=function(L){if(!this.isValid()){return"Transparent"}var G,H,K;var I=Math.floor(this.hue*6);var J=(this.hue*6)-I;var F=this.brightness*(1-this.saturation);var E=this.brightness*(1-(this.saturation*J));var M=this.brightness*(1-(this.saturation*(1-J)));switch(I){case 1:G=E;H=this.brightness;K=F;break;case 2:G=F;H=this.brightness;K=M;break;case 3:G=F;H=E;K=this.brightness;break;case 4:G=M;H=F;K=this.brightness;break;case 5:G=this.brightness;H=F;K=E;break;default:G=this.brightness;H=M;K=F;break}return L?"":"#"+this.valueToHex(G)+this.valueToHex(H)+this.valueToHex(K)}}})(jQuery);