1 | /** |
---|
2 | * Farbtastic Color Picker 1.2 |
---|
3 | * © 2008 Steven Wittens |
---|
4 | * |
---|
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. |
---|
9 | * |
---|
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. |
---|
14 | * |
---|
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 |
---|
18 | */ |
---|
19 | |
---|
20 | jQuery.fn.farbtastic = function (callback) { |
---|
21 | $.farbtastic(this, callback); |
---|
22 | return this; |
---|
23 | }; |
---|
24 | |
---|
25 | jQuery.farbtastic = function (container, callback) { |
---|
26 | var container = $(container).get(0); |
---|
27 | return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback)); |
---|
28 | } |
---|
29 | |
---|
30 | jQuery._farbtastic = function (container, callback) { |
---|
31 | // Store farbtastic object |
---|
32 | var fb = this; |
---|
33 | |
---|
34 | // Insert markup |
---|
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); |
---|
38 | // Dimensions |
---|
39 | fb.radius = 84; |
---|
40 | fb.square = 100; |
---|
41 | fb.width = 194; |
---|
42 | |
---|
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); |
---|
49 | $(this).css({ |
---|
50 | 'backgroundImage': 'none', |
---|
51 | 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')" |
---|
52 | }); |
---|
53 | } |
---|
54 | }); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Link to the given element(s) or callback. |
---|
59 | */ |
---|
60 | fb.linkTo = function (callback) { |
---|
61 | // Unbind previous nodes |
---|
62 | if (typeof fb.callback == 'object') { |
---|
63 | $(fb.callback).unbind('keyup', fb.updateValue); |
---|
64 | } |
---|
65 | |
---|
66 | // Reset color |
---|
67 | fb.color = null; |
---|
68 | |
---|
69 | // Bind callback or elements |
---|
70 | if (typeof callback == 'function') { |
---|
71 | fb.callback = callback; |
---|
72 | } |
---|
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); |
---|
78 | } |
---|
79 | } |
---|
80 | return this; |
---|
81 | } |
---|
82 | fb.updateValue = function (event) { |
---|
83 | if (this.value && this.value != fb.color) { |
---|
84 | fb.setColor(this.value); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Change color with HTML syntax #123456 |
---|
90 | */ |
---|
91 | fb.setColor = function (color) { |
---|
92 | var unpack = fb.unpack(color); |
---|
93 | if (fb.color != color && unpack) { |
---|
94 | fb.color = color; |
---|
95 | fb.rgb = unpack; |
---|
96 | fb.hsl = fb.RGBToHSL(fb.rgb); |
---|
97 | fb.updateDisplay(); |
---|
98 | } |
---|
99 | return this; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Change color with HSL triplet [0..1, 0..1, 0..1] |
---|
104 | */ |
---|
105 | fb.setHSL = function (hsl) { |
---|
106 | fb.hsl = hsl; |
---|
107 | fb.rgb = fb.HSLToRGB(hsl); |
---|
108 | fb.color = fb.pack(fb.rgb); |
---|
109 | fb.updateDisplay(); |
---|
110 | return this; |
---|
111 | } |
---|
112 | |
---|
113 | ///////////////////////////////////////////////////// |
---|
114 | |
---|
115 | /** |
---|
116 | * Retrieve the coordinates of the given event relative to the center |
---|
117 | * of the widget. |
---|
118 | */ |
---|
119 | fb.widgetCoords = function (event) { |
---|
120 | var x, y; |
---|
121 | var el = event.target || event.srcElement; |
---|
122 | var reference = fb.wheel; |
---|
123 | |
---|
124 | if (typeof event.offsetX != 'undefined') { |
---|
125 | // Use offset coordinates and find common offsetParent |
---|
126 | var pos = { x: event.offsetX, y: event.offsetY }; |
---|
127 | |
---|
128 | // Send the coordinates upwards through the offsetParent chain. |
---|
129 | var e = el; |
---|
130 | while (e) { |
---|
131 | e.mouseX = pos.x; |
---|
132 | e.mouseY = pos.y; |
---|
133 | pos.x += e.offsetLeft; |
---|
134 | pos.y += e.offsetTop; |
---|
135 | e = e.offsetParent; |
---|
136 | } |
---|
137 | |
---|
138 | // Look for the coordinates starting from the wheel widget. |
---|
139 | var e = reference; |
---|
140 | var offset = { x: 0, y: 0 } |
---|
141 | while (e) { |
---|
142 | if (typeof e.mouseX != 'undefined') { |
---|
143 | x = e.mouseX - offset.x; |
---|
144 | y = e.mouseY - offset.y; |
---|
145 | break; |
---|
146 | } |
---|
147 | offset.x += e.offsetLeft; |
---|
148 | offset.y += e.offsetTop; |
---|
149 | e = e.offsetParent; |
---|
150 | } |
---|
151 | |
---|
152 | // Reset stored coordinates |
---|
153 | e = el; |
---|
154 | while (e) { |
---|
155 | e.mouseX = undefined; |
---|
156 | e.mouseY = undefined; |
---|
157 | e = e.offsetParent; |
---|
158 | } |
---|
159 | } |
---|
160 | else { |
---|
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; |
---|
165 | } |
---|
166 | // Subtract distance to middle |
---|
167 | return { x: x - fb.width / 2, y: y - fb.width / 2 }; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Mousedown handler |
---|
172 | */ |
---|
173 | fb.mousedown = function (event) { |
---|
174 | // Capture mouse |
---|
175 | if (!document.dragging) { |
---|
176 | $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup); |
---|
177 | document.dragging = true; |
---|
178 | } |
---|
179 | |
---|
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; |
---|
183 | |
---|
184 | // Process |
---|
185 | fb.mousemove(event); |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Mousemove handler |
---|
191 | */ |
---|
192 | fb.mousemove = function (event) { |
---|
193 | // Get coordinates relative to color picker center |
---|
194 | var pos = fb.widgetCoords(event); |
---|
195 | |
---|
196 | // Set new HSL parameters |
---|
197 | if (fb.circleDrag) { |
---|
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]]); |
---|
201 | } |
---|
202 | else { |
---|
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]); |
---|
206 | } |
---|
207 | return false; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * Mouseup handler |
---|
212 | */ |
---|
213 | fb.mouseup = function () { |
---|
214 | // Uncapture mouse |
---|
215 | $(document).unbind('mousemove', fb.mousemove); |
---|
216 | $(document).unbind('mouseup', fb.mouseup); |
---|
217 | document.dragging = false; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Update the markers and styles |
---|
222 | */ |
---|
223 | fb.updateDisplay = function () { |
---|
224 | // Markers |
---|
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' |
---|
229 | }); |
---|
230 | |
---|
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' |
---|
234 | }); |
---|
235 | |
---|
236 | // Saturation/Luminance gradient |
---|
237 | $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5]))); |
---|
238 | |
---|
239 | // Linked elements or callback |
---|
240 | if (typeof fb.callback == 'object') { |
---|
241 | // Set background/foreground color |
---|
242 | $(fb.callback).css({ |
---|
243 | backgroundColor: fb.color, |
---|
244 | color: fb.hsl[2] > 0.5 ? '#000' : '#fff' |
---|
245 | }); |
---|
246 | |
---|
247 | // Change linked value |
---|
248 | $(fb.callback).each(function() { |
---|
249 | if (this.value && this.value != fb.color) { |
---|
250 | this.value = fb.color; |
---|
251 | } |
---|
252 | }); |
---|
253 | } |
---|
254 | else if (typeof fb.callback == 'function') { |
---|
255 | fb.callback.call(fb, fb.color); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Get absolute position of element |
---|
261 | */ |
---|
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); |
---|
267 | r.x += tmp.x; |
---|
268 | r.y += tmp.y; |
---|
269 | } |
---|
270 | return r; |
---|
271 | }; |
---|
272 | |
---|
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); |
---|
281 | } |
---|
282 | |
---|
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]; |
---|
288 | } |
---|
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]; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | fb.HSLToRGB = function (hsl) { |
---|
297 | var m1, m2, r, g, b; |
---|
298 | var h = hsl[0], s = hsl[1], l = hsl[2]; |
---|
299 | m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s; |
---|
300 | m1 = l * 2 - m2; |
---|
301 | return [this.hueToRGB(m1, m2, h+0.33333), |
---|
302 | this.hueToRGB(m1, m2, h), |
---|
303 | this.hueToRGB(m1, m2, h-0.33333)]; |
---|
304 | } |
---|
305 | |
---|
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; |
---|
311 | return m1; |
---|
312 | } |
---|
313 | |
---|
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)); |
---|
319 | delta = max - min; |
---|
320 | l = (min + max) / 2; |
---|
321 | s = 0; |
---|
322 | if (l > 0 && l < 1) { |
---|
323 | s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l)); |
---|
324 | } |
---|
325 | h = 0; |
---|
326 | if (delta > 0) { |
---|
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); |
---|
330 | h /= 6; |
---|
331 | } |
---|
332 | return [h, s, l]; |
---|
333 | } |
---|
334 | |
---|
335 | // Install mousedown handler (the others are set on the document on-demand) |
---|
336 | $('*', e).mousedown(fb.mousedown); |
---|
337 | |
---|
338 | // Init color |
---|
339 | fb.setColor('#000000'); |
---|
340 | |
---|
341 | // Set linked elements/callback |
---|
342 | if (callback) { |
---|
343 | fb.linkTo(callback); |
---|
344 | } |
---|
345 | } |
---|