1 // $Id: farbtastic.js,v 1.2 2007/01/08 22:53:01 unconed Exp $
4 var farbtastic_click = false;
6 jQuery.fn.farbtastic = function (callback) {
7 jQuery.farbtastic(this, callback);
11 jQuery.farbtastic = function (container, callback) {
12 var container = jQuery(container).get(0);
13 return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback));
16 jQuery._farbtastic = function (container, callback) {
17 // Store farbtastic object
21 jQuery(container).html('<div class="farbtastic"><div class="color"></div><div class="wheel"></div><div class="overlay"></div><div class="h-marker marker"></div><div class="sl-marker marker"></div></div>');
22 var e = jQuery('.farbtastic', container);
23 fb.wheel = jQuery('.wheel', container).get(0);
29 // Fix background PNGs in IE6
30 if (navigator.appVersion.match(/MSIE [0-6]\./)) {
31 jQuery('*', e).each(function () {
32 if (this.currentStyle.backgroundImage != 'none') {
33 var image = this.currentStyle.backgroundImage;
34 image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
36 'backgroundImage': 'none',
37 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
44 * Link to the given element(s) or callback.
46 fb.linkTo = function (callback) {
47 // Unbind previous nodes
48 if (typeof fb.callback == 'object') {
49 jQuery(fb.callback).unbind('keyup', fb.updateValue);
55 // Bind callback or elements
56 if (typeof callback == 'function') {
57 fb.callback = callback;
59 else if (typeof callback == 'object' || typeof callback == 'string') {
60 fb.callback = jQuery(callback);
61 fb.callback.bind('keyup', fb.updateValue);
62 if (fb.callback.get(0).value) {
63 fb.setColor(fb.callback.get(0).value);
68 fb.updateValue = function (event) {
69 if (this.value && this.value != fb.color) {
70 fb.setColor(this.value);
75 * Change color with HTML syntax #123456
77 fb.setColor = function (color) {
78 var unpack = fb.unpack(color);
79 if (fb.color != color && unpack) {
82 fb.hsl = fb.RGBToHSL(fb.rgb);
89 * Change color with HSL triplet [0..1, 0..1, 0..1]
91 fb.setHSL = function (hsl) {
93 fb.rgb = fb.HSLToRGB(hsl);
94 fb.color = fb.pack(fb.rgb);
99 /////////////////////////////////////////////////////
102 * Retrieve the coordinates of the given event relative to the center
105 fb.widgetCoords = function (event) {
107 var el = event.target || event.srcElement;
108 var reference = fb.wheel;
110 if (typeof event.offsetX != 'undefined') {
111 // Use offset coordinates and find common offsetParent
112 var pos = { x: event.offsetX, y: event.offsetY };
114 // Send the coordinates upwards through the offsetParent chain.
119 pos.x += e.offsetLeft;
120 pos.y += e.offsetTop;
124 // Look for the coordinates starting from the wheel widget.
126 var offset = { x: 0, y: 0 }
128 if (typeof e.mouseX != 'undefined') {
129 x = e.mouseX - offset.x;
130 y = e.mouseY - offset.y;
133 offset.x += e.offsetLeft;
134 offset.y += e.offsetTop;
138 // Reset stored coordinates
141 e.mouseX = undefined;
142 e.mouseY = undefined;
147 // Use absolute coordinates
148 var pos = fb.absolutePosition(reference);
149 x = (event.pageX || 0*(event.clientX + jQuery('html').get(0).scrollLeft)) - pos.x;
150 y = (event.pageY || 0*(event.clientY + jQuery('html').get(0).scrollTop)) - pos.y;
152 // Subtract distance to middle
153 return { x: x - fb.width / 2, y: y - fb.width / 2 };
159 fb.mousedown = function (event) {
160 farbtastic_click = true;
162 if (!document.dragging) {
163 jQuery(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
164 document.dragging = true;
167 // Check which area is being dragged
168 var pos = fb.widgetCoords(event);
169 fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
179 fb.mousemove = function (event) {
180 // Get coordinates relative to color picker center
181 var pos = fb.widgetCoords(event);
183 // Set new HSL parameters
185 var hue = Math.atan2(pos.x, -pos.y) / 6.28;
186 if (hue < 0) hue += 1;
187 fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
190 var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
191 var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
192 fb.setHSL([fb.hsl[0], sat, lum]);
200 fb.mouseup = function () {
202 farbtastic_click = false;
203 jQuery(document).unbind('mousemove', fb.mousemove);
204 jQuery(document).unbind('mouseup', fb.mouseup);
205 document.dragging = false;
209 * Update the markers and styles
211 fb.updateDisplay = function () {
213 var angle = fb.hsl[0] * 6.28;
214 jQuery('.h-marker', e).css({
215 left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
216 top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
219 jQuery('.sl-marker', e).css({
220 left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
221 top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
224 // Saturation/Luminance gradient
225 jQuery('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
227 // Linked elements or callback
228 if (typeof fb.callback == 'object') {
229 // Set background/foreground color
230 jQuery(fb.callback).css({
231 backgroundColor: fb.color,
232 color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
235 // Change linked value
236 jQuery(fb.callback).each(function() {
237 if (this.value && this.value != fb.color) {
238 this.value = fb.color;
242 else if (typeof fb.callback == 'function') {
243 fb.callback.call(fb, fb.color);
248 * Get absolute position of element
250 fb.absolutePosition = function (el) {
251 var r = { x: el.offsetLeft, y: el.offsetTop };
252 // Resolve relative to offsetParent
253 if (el.offsetParent) {
254 var tmp = fb.absolutePosition(el.offsetParent);
261 /* Various color utility functions */
262 fb.pack = function (rgb) {
263 var r = Math.round(rgb[0] * 255);
264 var g = Math.round(rgb[1] * 255);
265 var b = Math.round(rgb[2] * 255);
266 return '#' + (r < 16 ? '0' : '') + r.toString(16) +
267 (g < 16 ? '0' : '') + g.toString(16) +
268 (b < 16 ? '0' : '') + b.toString(16);
271 fb.unpack = function (color) {
272 if (color.length == 7) {
273 return [parseInt('0x' + color.substring(1, 3)) / 255,
274 parseInt('0x' + color.substring(3, 5)) / 255,
275 parseInt('0x' + color.substring(5, 7)) / 255];
277 else if (color.length == 4) {
278 return [parseInt('0x' + color.substring(1, 2)) / 15,
279 parseInt('0x' + color.substring(2, 3)) / 15,
280 parseInt('0x' + color.substring(3, 4)) / 15];
284 fb.HSLToRGB = function (hsl) {
286 var h = hsl[0], s = hsl[1], l = hsl[2];
287 m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
289 return [this.hueToRGB(m1, m2, h+0.33333),
290 this.hueToRGB(m1, m2, h),
291 this.hueToRGB(m1, m2, h-0.33333)];
294 fb.hueToRGB = function (m1, m2, h) {
295 h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
296 if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
297 if (h * 2 < 1) return m2;
298 if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
302 fb.RGBToHSL = function (rgb) {
303 var min, max, delta, h, s, l;
304 var r = rgb[0], g = rgb[1], b = rgb[2];
305 min = Math.min(r, Math.min(g, b));
306 max = Math.max(r, Math.max(g, b));
310 if (l > 0 && l < 1) {
311 s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
315 if (max == r && max != g) h += (g - b) / delta;
316 if (max == g && max != b) h += (2 + (b - r) / delta);
317 if (max == b && max != r) h += (4 + (r - g) / delta);
323 // Install mousedown handler (the others are set on the document on-demand)
324 jQuery('*', e).mousedown(fb.mousedown);
327 fb.setColor('#000000');
329 // Set linked elements/callback