diff --git a/farbtastic.js b/farbtastic.js index a609eec..c11252b 100644 --- a/farbtastic.js +++ b/farbtastic.js @@ -158,6 +158,7 @@ $._farbtastic = function (container, callback) { $(document).unbind('mousemove', fb.mousemove); $(document).unbind('mouseup', fb.mouseup); document.dragging = false; + fb.callback.val(fb.color); }; /** diff --git a/farbtastic.min.js b/farbtastic.min.js index e4c8528..2bcc3c5 100644 --- a/farbtastic.min.js +++ b/farbtastic.min.js @@ -4,11 +4,4 @@ * Licensed under the GPL license: * http://www.gnu.org/licenses/gpl.html */ -(function(e){e.fn.farbtastic=function(f){e.farbtastic(this,f);return this};e.farbtastic=function(f,l){f=e(f).get(0);return f.farbtastic||(f.farbtastic=new e._farbtastic(f,l))};e._farbtastic=function(f,l){var a=this;e(f).html('
');var k=e(".farbtastic",f);a.wheel=e(".wheel",f).get(0);a.radius=84;a.square=100;a.width=194;navigator.appVersion.match(/MSIE [0-6]\./)&& -e("*",k).each(function(){if(this.currentStyle.backgroundImage!="none"){var b=this.currentStyle.backgroundImage;b=this.currentStyle.backgroundImage.substring(5,b.length-2);e(this).css({backgroundImage:"none",filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='"+b+"')"})}});a.linkTo=function(b){typeof a.callback=="object"&&e(a.callback).unbind("keyup",a.updateValue);a.color=null;if(typeof b=="function")a.callback=b;else if(typeof b=="object"||typeof b=="string"){a.callback= -e(b);a.callback.bind("keyup",a.updateValue);a.callback.get(0).value&&a.setColor(a.callback.get(0).value)}return this};a.updateValue=function(){this.value&&this.value!=a.color&&a.setColor(this.value)};a.setColor=function(b){var c=a.unpack(b);if(a.color!=b&&c){a.color=b;a.rgb=c;a.hsl=a.RGBToHSL(a.rgb);a.updateDisplay()}return this};a.setHSL=function(b){a.hsl=b;a.rgb=a.HSLToRGB(b);a.color=a.pack(a.rgb);a.updateDisplay();return this};a.widgetCoords=function(b){var c=e(a.wheel).offset();return{x:b.pageX- -c.left-a.width/2,y:b.pageY-c.top-a.width/2}};a.mousedown=function(b){if(!document.dragging){e(document).bind("mousemove",a.mousemove).bind("mouseup",a.mouseup);document.dragging=true}var c=a.widgetCoords(b);a.circleDrag=Math.max(Math.abs(c.x),Math.abs(c.y))*2>a.square;a.mousemove(b);return false};a.mousemove=function(b){var c=a.widgetCoords(b);if(a.circleDrag){b=Math.atan2(c.x,-c.y)/6.28;if(b<0)b+=1;a.setHSL([b,a.hsl[1],a.hsl[2]])}else{b=Math.max(0,Math.min(1,-(c.x/a.square)+0.5));c=Math.max(0,Math.min(1, --(c.y/a.square)+0.5));a.setHSL([a.hsl[0],b,c])}return false};a.mouseup=function(){e(document).unbind("mousemove",a.mousemove);e(document).unbind("mouseup",a.mouseup);document.dragging=false};a.updateDisplay=function(){var b=a.hsl[0]*6.28;e(".h-marker",k).css({left:Math.round(Math.sin(b)*a.radius+a.width/2)+"px",top:Math.round(-Math.cos(b)*a.radius+a.width/2)+"px"});e(".sl-marker",k).css({left:Math.round(a.square*(0.5-a.hsl[1])+a.width/2)+"px",top:Math.round(a.square*(0.5-a.hsl[2])+a.width/2)+"px"}); -e(".color",k).css("backgroundColor",a.pack(a.HSLToRGB([a.hsl[0],1,0.5])));if(typeof a.callback=="object"){e(a.callback).css({backgroundColor:a.color,color:a.hsl[2]>0.5?"#000":"#fff"});e(a.callback).each(function(){if(this.value&&this.value!=a.color)this.value=a.color})}else typeof a.callback=="function"&&a.callback.call(a,a.color)};a.pack=function(b){var c=Math.round(b[0]*255),d=Math.round(b[1]*255);b=Math.round(b[2]*255);return"#"+(c<16?"0":"")+c.toString(16)+(d<16?"0":"")+d.toString(16)+(b<16?"0": -"")+b.toString(16)};a.unpack=function(b){if(b.length==7)return[parseInt("0x"+b.substring(1,3))/255,parseInt("0x"+b.substring(3,5))/255,parseInt("0x"+b.substring(5,7))/255];else if(b.length==4)return[parseInt("0x"+b.substring(1,2))/15,parseInt("0x"+b.substring(2,3))/15,parseInt("0x"+b.substring(3,4))/15]};a.HSLToRGB=function(b){var c,d=b[0];c=b[1];b=b[2];c=b<=0.5?b*(c+1):b+c-b*c;b=b*2-c;return[this.hueToRGB(b,c,d+0.33333),this.hueToRGB(b,c,d),this.hueToRGB(b,c,d-0.33333)]};a.hueToRGB=function(b,c, -d){d=d<0?d+1:d>1?d-1:d;if(d*6<1)return b+(c-b)*d*6;if(d*2<1)return c;if(d*3<2)return b+(c-b)*(0.66666-d)*6;return b};a.RGBToHSL=function(b){var c,d,m,g,h=b[0],i=b[1],j=b[2];c=Math.min(h,Math.min(i,j));b=Math.max(h,Math.max(i,j));d=b-c;g=(c+b)/2;m=0;if(g>0&&g<1)m=d/(g<0.5?2*g:2-2*g);c=0;if(d>0){if(b==h&&b!=i)c+=(i-j)/d;if(b==i&&b!=j)c+=2+(j-h)/d;if(b==j&&b!=h)c+=4+(h-i)/d;c/=6}return[c,m,g]};e("*",k).mousedown(a.mousedown);a.setColor("#000000");l&&a.linkTo(l)}})(jQuery); +!function(t){t.fn.farbtastic=function(a){return t.farbtastic(this,a),this},t.farbtastic=function(a,r){return(a=t(a).get(0)).farbtastic||(a.farbtastic=new t._farbtastic(a,r))},t._farbtastic=function(a,r){var e=this;t(a).html('
');var o=t(".farbtastic",a);e.wheel=t(".wheel",a).get(0),e.radius=84,e.square=100,e.width=194,navigator.appVersion.match(/MSIE [0-6]\./)&&t("*",o).each(function(){if("none"!=this.currentStyle.backgroundImage){var a=this.currentStyle.backgroundImage;a=this.currentStyle.backgroundImage.substring(5,a.length-2),t(this).css({backgroundImage:"none",filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='"+a+"')"})}}),e.linkTo=function(a){return"object"==typeof e.callback&&t(e.callback).unbind("keyup",e.updateValue),e.color=null,"function"==typeof a?e.callback=a:"object"!=typeof a&&"string"!=typeof a||(e.callback=t(a),e.callback.bind("keyup",e.updateValue),e.callback.get(0).value&&e.setColor(e.callback.get(0).value)),this},e.updateValue=function(t){this.value&&this.value!=e.color&&e.setColor(this.value)},e.setColor=function(t){var a=e.unpack(t);return e.color!=t&&a&&(e.color=t,e.rgb=a,e.hsl=e.RGBToHSL(e.rgb),e.updateDisplay()),this},e.setHSL=function(t){return e.hsl=t,e.rgb=e.HSLToRGB(t),e.color=e.pack(e.rgb),e.updateDisplay(),this},e.widgetCoords=function(a){var r=t(e.wheel).offset();return{x:a.pageX-r.left-e.width/2,y:a.pageY-r.top-e.width/2}},e.mousedown=function(a){document.dragging||(t(document).bind("mousemove",e.mousemove).bind("mouseup",e.mouseup),document.dragging=!0);var r=e.widgetCoords(a);return e.circleDrag=2*Math.max(Math.abs(r.x),Math.abs(r.y))>e.square,e.mousemove(a),!1},e.mousemove=function(t){var a=e.widgetCoords(t);if(e.circleDrag){var r=Math.atan2(a.x,-a.y)/6.28;r<0&&(r+=1),e.setHSL([r,e.hsl[1],e.hsl[2]])}else{var o=Math.max(0,Math.min(1,-a.x/e.square+.5)),n=Math.max(0,Math.min(1,-a.y/e.square+.5));e.setHSL([e.hsl[0],o,n])}return!1},e.mouseup=function(){t(document).unbind("mousemove",e.mousemove),t(document).unbind("mouseup",e.mouseup),document.dragging=!1,e.callback.val(e.color)},e.updateDisplay=function(){var a=6.28*e.hsl[0];t(".h-marker",o).css({left:Math.round(Math.sin(a)*e.radius+e.width/2)+"px",top:Math.round(-Math.cos(a)*e.radius+e.width/2)+"px"}),t(".sl-marker",o).css({left:Math.round(e.square*(.5-e.hsl[1])+e.width/2)+"px",top:Math.round(e.square*(.5-e.hsl[2])+e.width/2)+"px"}),t(".color",o).css("backgroundColor",e.pack(e.HSLToRGB([e.hsl[0],1,.5]))),"object"==typeof e.callback?(t(e.callback).css({backgroundColor:e.color,color:e.hsl[2]>.5?"#000":"#fff"}),t(e.callback).each(function(){this.value&&this.value!=e.color&&(this.value=e.color)})):"function"==typeof e.callback&&e.callback.call(e,e.color)},e.pack=function(t){var a=Math.round(255*t[0]),r=Math.round(255*t[1]),e=Math.round(255*t[2]);return"#"+(a<16?"0":"")+a.toString(16)+(r<16?"0":"")+r.toString(16)+(e<16?"0":"")+e.toString(16)},e.unpack=function(t){return 7==t.length?[parseInt("0x"+t.substring(1,3))/255,parseInt("0x"+t.substring(3,5))/255,parseInt("0x"+t.substring(5,7))/255]:4==t.length?[parseInt("0x"+t.substring(1,2))/15,parseInt("0x"+t.substring(2,3))/15,parseInt("0x"+t.substring(3,4))/15]:void 0},e.HSLToRGB=function(t){var a,r,e=t[0],o=t[1],n=t[2];return a=2*n-(r=n<=.5?n*(o+1):n+o-n*o),[this.hueToRGB(a,r,e+.33333),this.hueToRGB(a,r,e),this.hueToRGB(a,r,e-.33333)]},e.hueToRGB=function(t,a,r){return 6*(r=r<0?r+1:r>1?r-1:r)<1?t+(a-t)*r*6:2*r<1?a:3*r<2?t+(a-t)*(.66666-r)*6:t},e.RGBToHSL=function(t){var a,r,e,o,n,s,u=t[0],i=t[1],c=t[2];return a=Math.min(u,Math.min(i,c)),e=(r=Math.max(u,Math.max(i,c)))-a,n=0,(s=(a+r)/2)>0&&s<1&&(n=e/(s<.5?2*s:2-2*s)),o=0,e>0&&(r==u&&r!=i&&(o+=(i-c)/e),r==i&&r!=c&&(o+=2+(c-u)/e),r==c&&r!=u&&(o+=4+(u-i)/e),o/=6),[o,n,s]},t("*",o).mousedown(e.mousedown),e.setColor("#000000"),r&&e.linkTo(r)}}(jQuery); \ No newline at end of file