2 * Farbtastic Color Picker 1.2
3 * © 2008 Steven Wittens
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 jQuery.fn.farbtastic = function (callback) {
21 $.farbtastic(this, callback);
25 jQuery.farbtastic = function (container, callback) {
26 var container = $(container).get(0);
27 return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback));
30 jQuery._farbtastic = function (container, callback) {
31 // Store farbtastic object
35 $(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>');
36 var e = $('.farbtastic', container);
37 fb.wheel = $('.wheel', container).get(0);
43 // Fix background PNGs in IE6
44 if (navigator.appVersion.match(/MSIE [0-6]\./)) {
45 $('*', e).each(function () {
46 if (this.currentStyle.backgroundImage != 'none') {
47 var image = this.currentStyle.backgroundImage;
48 image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
50 'backgroundImage': 'none',
51 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
58 * Link to the given element(s) or callback.
60 fb.linkTo = function (callback) {
61 // Unbind previous nodes
62 if (typeof fb.callback == 'object') {
63 $(fb.callback).unbind('keyup', fb.updateValue);
69 // Bind callback or elements
70 if (typeof callback == 'function') {
71 fb.callback = callback;
73 else if (typeof callback == 'object' || typeof callback == 'string') {
74 fb.callback = $(callback);
75 fb.callback.bind('keyup', fb.updateValue);
76 if (fb.callback.get(0).value) {
77 fb.setColor(fb.callback.get(0).value);
82 fb.updateValue = function (event) {
83 if (this.value && this.value != fb.color) {
84 fb.setColor(this.value);
89 * Change color with HTML syntax #123456
91 fb.setColor = function (color) {
92 var unpack = fb.unpack(color);
93 if (fb.color != color && unpack) {
96 fb.hsl = fb.RGBToHSL(fb.rgb);
103 * Change color with HSL triplet [0..1, 0..1, 0..1]
105 fb.setHSL = function (hsl) {
107 fb.rgb = fb.HSLToRGB(hsl);
108 fb.color = fb.pack(fb.rgb);
113 /////////////////////////////////////////////////////
116 * Retrieve the coordinates of the given event relative to the center
119 fb.widgetCoords = function (event) {
121 var el = event.target || event.srcElement;
122 var reference = fb.wheel;
124 if (typeof event.offsetX != 'undefined') {
125 // Use offset coordinates and find common offsetParent
126 var pos = { x: event.offsetX, y: event.offsetY };
128 // Send the coordinates upwards through the offsetParent chain.
133 pos.x += e.offsetLeft;
134 pos.y += e.offsetTop;
138 // Look for the coordinates starting from the wheel widget.
140 var offset = { x: 0, y: 0 }
142 if (typeof e.mouseX != 'undefined') {
143 x = e.mouseX - offset.x;
144 y = e.mouseY - offset.y;
147 offset.x += e.offsetLeft;
148 offset.y += e.offsetTop;
152 // Reset stored coordinates
155 e.mouseX = undefined;
156 e.mouseY = undefined;
161 // Use absolute coordinates
162 var pos = fb.absolutePosition(reference);
163 x = (event.pageX || 0*(event.clientX + $('html').get(0).scrollLeft)) - pos.x;
164 y = (event.pageY || 0*(event.clientY + $('html').get(0).scrollTop)) - pos.y;
166 // Subtract distance to middle
167 return { x: x - fb.width / 2, y: y - fb.width / 2 };
173 fb.mousedown = function (event) {
175 if (!document.dragging) {
176 $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
177 document.dragging = true;
180 // Check which area is being dragged
181 var pos = fb.widgetCoords(event);
182 fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
192 fb.mousemove = function (event) {
193 // Get coordinates relative to color picker center
194 var pos = fb.widgetCoords(event);
196 // Set new HSL parameters
198 var hue = Math.atan2(pos.x, -pos.y) / 6.28;
199 if (hue < 0) hue += 1;
200 fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
203 var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
204 var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
205 fb.setHSL([fb.hsl[0], sat, lum]);
213 fb.mouseup = function () {
215 $(document).unbind('mousemove', fb.mousemove);
216 $(document).unbind('mouseup', fb.mouseup);
217 document.dragging = false;
221 * Update the markers and styles
223 fb.updateDisplay = function () {
225 var angle = fb.hsl[0] * 6.28;
226 $('.h-marker', e).css({
227 left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
228 top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
231 $('.sl-marker', e).css({
232 left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
233 top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
236 // Saturation/Luminance gradient
237 $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
239 // Linked elements or callback
240 if (typeof fb.callback == 'object') {
241 // Set background/foreground color
243 backgroundColor: fb.color,
244 color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
247 // Change linked value
248 $(fb.callback).each(function() {
249 if (this.value && this.value != fb.color) {
250 this.value = fb.color;
254 else if (typeof fb.callback == 'function') {
255 fb.callback.call(fb, fb.color);
260 * Get absolute position of element
262 fb.absolutePosition = function (el) {
263 var r = { x: el.offsetLeft, y: el.offsetTop };
264 // Resolve relative to offsetParent
265 if (el.offsetParent) {
266 var tmp = fb.absolutePosition(el.offsetParent);
273 /* Various color utility functions */
274 fb.pack = function (rgb) {
275 var r = Math.round(rgb[0] * 255);
276 var g = Math.round(rgb[1] * 255);
277 var b = Math.round(rgb[2] * 255);
278 return '#' + (r < 16 ? '0' : '') + r.toString(16) +
279 (g < 16 ? '0' : '') + g.toString(16) +
280 (b < 16 ? '0' : '') + b.toString(16);
283 fb.unpack = function (color) {
284 if (color.length == 7) {
285 return [parseInt('0x' + color.substring(1, 3)) / 255,
286 parseInt('0x' + color.substring(3, 5)) / 255,
287 parseInt('0x' + color.substring(5, 7)) / 255];
289 else if (color.length == 4) {
290 return [parseInt('0x' + color.substring(1, 2)) / 15,
291 parseInt('0x' + color.substring(2, 3)) / 15,
292 parseInt('0x' + color.substring(3, 4)) / 15];
296 fb.HSLToRGB = function (hsl) {
298 var h = hsl[0], s = hsl[1], l = hsl[2];
299 m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
301 return [this.hueToRGB(m1, m2, h+0.33333),
302 this.hueToRGB(m1, m2, h),
303 this.hueToRGB(m1, m2, h-0.33333)];
306 fb.hueToRGB = function (m1, m2, h) {
307 h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
308 if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
309 if (h * 2 < 1) return m2;
310 if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
314 fb.RGBToHSL = function (rgb) {
315 var min, max, delta, h, s, l;
316 var r = rgb[0], g = rgb[1], b = rgb[2];
317 min = Math.min(r, Math.min(g, b));
318 max = Math.max(r, Math.max(g, b));
322 if (l > 0 && l < 1) {
323 s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
327 if (max == r && max != g) h += (g - b) / delta;
328 if (max == g && max != b) h += (2 + (b - r) / delta);
329 if (max == b && max != r) h += (4 + (r - g) / delta);
335 // Install mousedown handler (the others are set on the document on-demand)
336 $('*', e).mousedown(fb.mousedown);
339 fb.setColor('#000000');
341 // Set linked elements/callback