1 // $Id: farbtastic.js,v 1.2 2007/01/08 22:53:01 unconed Exp $
4 jQuery.fn.farbtastic = function (callback) {
5 $.farbtastic(this, callback);
9 jQuery.farbtastic = function (container, callback) {
10 var container = $(container).get(0);
11 return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback));
14 jQuery._farbtastic = function (container, callback) {
15 // Store farbtastic object
19 $(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>');
20 var e = $('.farbtastic', container);
21 fb.wheel = $('.wheel', container).get(0);
27 // Fix background PNGs in IE6
28 if (navigator.appVersion.match(/MSIE [0-6]\./)) {
29 $('*', e).each(function () {
30 if (this.currentStyle.backgroundImage != 'none') {
31 var image = this.currentStyle.backgroundImage;
32 image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
34 'backgroundImage': 'none',
35 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
42 * Link to the given element(s) or callback.
44 fb.linkTo = function (callback) {
45 // Unbind previous nodes
46 if (typeof fb.callback == 'object') {
47 $(fb.callback).unbind('keyup', fb.updateValue);
53 // Bind callback or elements
54 if (typeof callback == 'function') {
55 fb.callback = callback;
57 else if (typeof callback == 'object' || typeof callback == 'string') {
58 fb.callback = $(callback);
59 fb.callback.bind('keyup', fb.updateValue);
60 if (fb.callback.get(0).value) {
61 fb.setColor(fb.callback.get(0).value);
66 fb.updateValue = function (event) {
67 if (this.value && this.value != fb.color) {
68 fb.setColor(this.value);
73 * Change color with HTML syntax #123456
75 fb.setColor = function (color) {
76 var unpack = fb.unpack(color);
77 if (fb.color != color && unpack) {
80 fb.hsl = fb.RGBToHSL(fb.rgb);
87 * Change color with HSL triplet [0..1, 0..1, 0..1]
89 fb.setHSL = function (hsl) {
91 fb.rgb = fb.HSLToRGB(hsl);
92 fb.color = fb.pack(fb.rgb);
97 /////////////////////////////////////////////////////
100 * Retrieve the coordinates of the given event relative to the center
103 fb.widgetCoords = function (event) {
105 var el = event.target || event.srcElement;
106 var reference = fb.wheel;
108 if (typeof event.offsetX != 'undefined') {
109 // Use offset coordinates and find common offsetParent
110 var pos = { x: event.offsetX, y: event.offsetY };
112 // Send the coordinates upwards through the offsetParent chain.
117 pos.x += e.offsetLeft;
118 pos.y += e.offsetTop;
122 // Look for the coordinates starting from the wheel widget.
124 var offset = { x: 0, y: 0 }
126 if (typeof e.mouseX != 'undefined') {
127 x = e.mouseX - offset.x;
128 y = e.mouseY - offset.y;
131 offset.x += e.offsetLeft;
132 offset.y += e.offsetTop;
136 // Reset stored coordinates
139 e.mouseX = undefined;
140 e.mouseY = undefined;
145 // Use absolute coordinates
146 var pos = fb.absolutePosition(reference);
147 x = (event.pageX || 0*(event.clientX + $('html').get(0).scrollLeft)) - pos.x;
148 y = (event.pageY || 0*(event.clientY + $('html').get(0).scrollTop)) - pos.y;
150 // Subtract distance to middle
151 return { x: x - fb.width / 2, y: y - fb.width / 2 };
157 fb.mousedown = function (event) {
159 if (!document.dragging) {
160 $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
161 document.dragging = true;
164 // Check which area is being dragged
165 var pos = fb.widgetCoords(event);
166 fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
176 fb.mousemove = function (event) {
177 // Get coordinates relative to color picker center
178 var pos = fb.widgetCoords(event);
180 // Set new HSL parameters
182 var hue = Math.atan2(pos.x, -pos.y) / 6.28;
183 if (hue < 0) hue += 1;
184 fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
187 var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
188 var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
189 fb.setHSL([fb.hsl[0], sat, lum]);
197 fb.mouseup = function () {
199 $(document).unbind('mousemove', fb.mousemove);
200 $(document).unbind('mouseup', fb.mouseup);
201 document.dragging = false;
205 * Update the markers and styles
207 fb.updateDisplay = function () {
209 var angle = fb.hsl[0] * 6.28;
210 $('.h-marker', e).css({
211 left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
212 top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
215 $('.sl-marker', e).css({
216 left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
217 top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
220 // Saturation/Luminance gradient
221 $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
223 // Linked elements or callback
224 if (typeof fb.callback == 'object') {
225 // Set background/foreground color
227 backgroundColor: fb.color,
228 color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
231 // Change linked value
232 $(fb.callback).each(function() {
233 if (this.value && this.value != fb.color) {
234 this.value = fb.color;
238 else if (typeof fb.callback == 'function') {
239 fb.callback.call(fb, fb.color);
244 * Get absolute position of element
246 fb.absolutePosition = function (el) {
247 var r = { x: el.offsetLeft, y: el.offsetTop };
248 // Resolve relative to offsetParent
249 if (el.offsetParent) {
250 var tmp = fb.absolutePosition(el.offsetParent);
257 /* Various color utility functions */
258 fb.pack = function (rgb) {
259 var r = Math.round(rgb[0] * 255);
260 var g = Math.round(rgb[1] * 255);
261 var b = Math.round(rgb[2] * 255);
262 return '#' + (r < 16 ? '0' : '') + r.toString(16) +
263 (g < 16 ? '0' : '') + g.toString(16) +
264 (b < 16 ? '0' : '') + b.toString(16);
267 fb.unpack = function (color) {
268 if (color.length == 7) {
269 return [parseInt('0x' + color.substring(1, 3)) / 255,
270 parseInt('0x' + color.substring(3, 5)) / 255,
271 parseInt('0x' + color.substring(5, 7)) / 255];
273 else if (color.length == 4) {
274 return [parseInt('0x' + color.substring(1, 2)) / 15,
275 parseInt('0x' + color.substring(2, 3)) / 15,
276 parseInt('0x' + color.substring(3, 4)) / 15];
280 fb.HSLToRGB = function (hsl) {
282 var h = hsl[0], s = hsl[1], l = hsl[2];
283 m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
285 return [this.hueToRGB(m1, m2, h+0.33333),
286 this.hueToRGB(m1, m2, h),
287 this.hueToRGB(m1, m2, h-0.33333)];
290 fb.hueToRGB = function (m1, m2, h) {
291 h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
292 if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
293 if (h * 2 < 1) return m2;
294 if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
298 fb.RGBToHSL = function (rgb) {
299 var min, max, delta, h, s, l;
300 var r = rgb[0], g = rgb[1], b = rgb[2];
301 min = Math.min(r, Math.min(g, b));
302 max = Math.max(r, Math.max(g, b));
306 if (l > 0 && l < 1) {
307 s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
311 if (max == r && max != g) h += (g - b) / delta;
312 if (max == g && max != b) h += (2 + (b - r) / delta);
313 if (max == b && max != r) h += (4 + (r - g) / delta);
319 // Install mousedown handler (the others are set on the document on-demand)
320 $('*', e).mousedown(fb.mousedown);
323 fb.setColor('#000000');
325 // Set linked elements/callback