webmaster@7
|
1 // $Id: farbtastic.js,v 1.4.2.1 2008/06/25 09:34:17 goba Exp $ |
webmaster@1
|
2 // Farbtastic 1.2 |
webmaster@1
|
3 |
webmaster@1
|
4 jQuery.fn.farbtastic = function (callback) { |
webmaster@1
|
5 $.farbtastic(this, callback); |
webmaster@1
|
6 return this; |
webmaster@1
|
7 }; |
webmaster@1
|
8 |
webmaster@1
|
9 jQuery.farbtastic = function (container, callback) { |
webmaster@1
|
10 var container = $(container).get(0); |
webmaster@1
|
11 return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback)); |
webmaster@1
|
12 }; |
webmaster@1
|
13 |
webmaster@1
|
14 jQuery._farbtastic = function (container, callback) { |
webmaster@1
|
15 // Store farbtastic object |
webmaster@1
|
16 var fb = this; |
webmaster@1
|
17 |
webmaster@1
|
18 // Insert markup |
webmaster@1
|
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>'); |
webmaster@1
|
20 var e = $('.farbtastic', container); |
webmaster@1
|
21 fb.wheel = $('.wheel', container).get(0); |
webmaster@1
|
22 // Dimensions |
webmaster@1
|
23 fb.radius = 84; |
webmaster@1
|
24 fb.square = 100; |
webmaster@1
|
25 fb.width = 194; |
webmaster@1
|
26 |
webmaster@1
|
27 // Fix background PNGs in IE6 |
webmaster@1
|
28 if (navigator.appVersion.match(/MSIE [0-6]\./)) { |
webmaster@1
|
29 $('*', e).each(function () { |
webmaster@1
|
30 if (this.currentStyle.backgroundImage != 'none') { |
webmaster@1
|
31 var image = this.currentStyle.backgroundImage; |
webmaster@1
|
32 image = this.currentStyle.backgroundImage.substring(5, image.length - 2); |
webmaster@1
|
33 $(this).css({ |
webmaster@1
|
34 'backgroundImage': 'none', |
webmaster@1
|
35 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')" |
webmaster@1
|
36 }); |
webmaster@1
|
37 } |
webmaster@1
|
38 }); |
webmaster@1
|
39 } |
webmaster@1
|
40 |
webmaster@1
|
41 /** |
webmaster@1
|
42 * Link to the given element(s) or callback. |
webmaster@1
|
43 */ |
webmaster@1
|
44 fb.linkTo = function (callback) { |
webmaster@1
|
45 // Unbind previous nodes |
webmaster@1
|
46 if (typeof fb.callback == 'object') { |
webmaster@1
|
47 $(fb.callback).unbind('keyup', fb.updateValue); |
webmaster@1
|
48 } |
webmaster@1
|
49 |
webmaster@1
|
50 // Reset color |
webmaster@1
|
51 fb.color = null; |
webmaster@1
|
52 |
webmaster@1
|
53 // Bind callback or elements |
webmaster@1
|
54 if (typeof callback == 'function') { |
webmaster@1
|
55 fb.callback = callback; |
webmaster@1
|
56 } |
webmaster@1
|
57 else if (typeof callback == 'object' || typeof callback == 'string') { |
webmaster@1
|
58 fb.callback = $(callback); |
webmaster@1
|
59 fb.callback.bind('keyup', fb.updateValue); |
webmaster@1
|
60 if (fb.callback.get(0).value) { |
webmaster@1
|
61 fb.setColor(fb.callback.get(0).value); |
webmaster@1
|
62 } |
webmaster@1
|
63 } |
webmaster@1
|
64 return this; |
webmaster@1
|
65 }; |
webmaster@1
|
66 fb.updateValue = function (event) { |
webmaster@1
|
67 if (this.value && this.value != fb.color) { |
webmaster@1
|
68 fb.setColor(this.value); |
webmaster@1
|
69 } |
webmaster@1
|
70 }; |
webmaster@1
|
71 |
webmaster@1
|
72 /** |
webmaster@1
|
73 * Change color with HTML syntax #123456 |
webmaster@1
|
74 */ |
webmaster@1
|
75 fb.setColor = function (color) { |
webmaster@1
|
76 var unpack = fb.unpack(color); |
webmaster@1
|
77 if (fb.color != color && unpack) { |
webmaster@1
|
78 fb.color = color; |
webmaster@1
|
79 fb.rgb = unpack; |
webmaster@1
|
80 fb.hsl = fb.RGBToHSL(fb.rgb); |
webmaster@1
|
81 fb.updateDisplay(); |
webmaster@1
|
82 } |
webmaster@1
|
83 return this; |
webmaster@1
|
84 }; |
webmaster@1
|
85 |
webmaster@1
|
86 /** |
webmaster@1
|
87 * Change color with HSL triplet [0..1, 0..1, 0..1] |
webmaster@1
|
88 */ |
webmaster@1
|
89 fb.setHSL = function (hsl) { |
webmaster@1
|
90 fb.hsl = hsl; |
webmaster@1
|
91 fb.rgb = fb.HSLToRGB(hsl); |
webmaster@1
|
92 fb.color = fb.pack(fb.rgb); |
webmaster@1
|
93 fb.updateDisplay(); |
webmaster@1
|
94 return this; |
webmaster@1
|
95 }; |
webmaster@1
|
96 |
webmaster@1
|
97 ///////////////////////////////////////////////////// |
webmaster@1
|
98 |
webmaster@1
|
99 /** |
webmaster@1
|
100 * Retrieve the coordinates of the given event relative to the center |
webmaster@1
|
101 * of the widget. |
webmaster@1
|
102 */ |
webmaster@1
|
103 fb.widgetCoords = function (event) { |
webmaster@1
|
104 var x, y; |
webmaster@1
|
105 var el = event.target || event.srcElement; |
webmaster@1
|
106 var reference = fb.wheel; |
webmaster@7
|
107 |
webmaster@7
|
108 // If the offset from the relative element is undefined calculate it. |
webmaster@7
|
109 if ( typeof event.offsetX == 'undefined' && typeof event.offsetY == 'undefined' ) { |
webmaster@7
|
110 var offset = $(event.target).offset(false); |
webmaster@7
|
111 event.offsetX = event.pageX - offset.left; |
webmaster@7
|
112 event.offsetY = event.pageY - offset.top; |
webmaster@7
|
113 } |
webmaster@7
|
114 |
webmaster@7
|
115 // Use offset coordinates and find common offsetParent |
webmaster@7
|
116 var pos = { x: event.offsetX, y: event.offsetY }; |
webmaster@1
|
117 |
webmaster@7
|
118 // Send the coordinates upwards through the offsetParent chain. |
webmaster@7
|
119 var e = el; |
webmaster@7
|
120 while (e) { |
webmaster@7
|
121 e.mouseX = pos.x; |
webmaster@7
|
122 e.mouseY = pos.y; |
webmaster@7
|
123 pos.x += e.offsetLeft; |
webmaster@7
|
124 pos.y += e.offsetTop; |
webmaster@7
|
125 e = e.offsetParent; |
webmaster@7
|
126 } |
webmaster@1
|
127 |
webmaster@7
|
128 // Look for the coordinates starting from the wheel widget. |
webmaster@7
|
129 var e = reference; |
webmaster@7
|
130 var offset = { x: 0, y: 0 }; |
webmaster@7
|
131 while (e) { |
webmaster@7
|
132 if (typeof e.mouseX != 'undefined') { |
webmaster@7
|
133 x = e.mouseX - offset.x; |
webmaster@7
|
134 y = e.mouseY - offset.y; |
webmaster@7
|
135 break; |
webmaster@1
|
136 } |
webmaster@7
|
137 offset.x += e.offsetLeft; |
webmaster@7
|
138 offset.y += e.offsetTop; |
webmaster@7
|
139 e = e.offsetParent; |
webmaster@7
|
140 } |
webmaster@1
|
141 |
webmaster@7
|
142 // Reset stored coordinates |
webmaster@7
|
143 e = el; |
webmaster@7
|
144 while (e) { |
webmaster@7
|
145 e.mouseX = undefined; |
webmaster@7
|
146 e.mouseY = undefined; |
webmaster@7
|
147 e = e.offsetParent; |
webmaster@7
|
148 } |
webmaster@1
|
149 |
webmaster@1
|
150 // Subtract distance to middle |
webmaster@1
|
151 return { x: x - fb.width / 2, y: y - fb.width / 2 }; |
webmaster@1
|
152 }; |
webmaster@1
|
153 |
webmaster@1
|
154 /** |
webmaster@1
|
155 * Mousedown handler |
webmaster@1
|
156 */ |
webmaster@1
|
157 fb.mousedown = function (event) { |
webmaster@1
|
158 // Capture mouse |
webmaster@1
|
159 if (!document.dragging) { |
webmaster@1
|
160 $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup); |
webmaster@1
|
161 document.dragging = true; |
webmaster@1
|
162 } |
webmaster@1
|
163 |
webmaster@1
|
164 // Check which area is being dragged |
webmaster@1
|
165 var pos = fb.widgetCoords(event); |
webmaster@1
|
166 fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square; |
webmaster@1
|
167 |
webmaster@1
|
168 // Process |
webmaster@1
|
169 fb.mousemove(event); |
webmaster@1
|
170 return false; |
webmaster@1
|
171 }; |
webmaster@1
|
172 |
webmaster@1
|
173 /** |
webmaster@1
|
174 * Mousemove handler |
webmaster@1
|
175 */ |
webmaster@1
|
176 fb.mousemove = function (event) { |
webmaster@1
|
177 // Get coordinates relative to color picker center |
webmaster@1
|
178 var pos = fb.widgetCoords(event); |
webmaster@1
|
179 |
webmaster@1
|
180 // Set new HSL parameters |
webmaster@1
|
181 if (fb.circleDrag) { |
webmaster@1
|
182 var hue = Math.atan2(pos.x, -pos.y) / 6.28; |
webmaster@1
|
183 if (hue < 0) hue += 1; |
webmaster@1
|
184 fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]); |
webmaster@1
|
185 } |
webmaster@1
|
186 else { |
webmaster@1
|
187 var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5)); |
webmaster@1
|
188 var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5)); |
webmaster@1
|
189 fb.setHSL([fb.hsl[0], sat, lum]); |
webmaster@1
|
190 } |
webmaster@1
|
191 return false; |
webmaster@1
|
192 }; |
webmaster@1
|
193 |
webmaster@1
|
194 /** |
webmaster@1
|
195 * Mouseup handler |
webmaster@1
|
196 */ |
webmaster@1
|
197 fb.mouseup = function () { |
webmaster@1
|
198 // Uncapture mouse |
webmaster@1
|
199 $(document).unbind('mousemove', fb.mousemove); |
webmaster@1
|
200 $(document).unbind('mouseup', fb.mouseup); |
webmaster@1
|
201 document.dragging = false; |
webmaster@1
|
202 }; |
webmaster@1
|
203 |
webmaster@1
|
204 /** |
webmaster@1
|
205 * Update the markers and styles |
webmaster@1
|
206 */ |
webmaster@1
|
207 fb.updateDisplay = function () { |
webmaster@1
|
208 // Markers |
webmaster@1
|
209 var angle = fb.hsl[0] * 6.28; |
webmaster@1
|
210 $('.h-marker', e).css({ |
webmaster@1
|
211 left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px', |
webmaster@1
|
212 top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px' |
webmaster@1
|
213 }); |
webmaster@1
|
214 |
webmaster@1
|
215 $('.sl-marker', e).css({ |
webmaster@1
|
216 left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px', |
webmaster@1
|
217 top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px' |
webmaster@1
|
218 }); |
webmaster@1
|
219 |
webmaster@1
|
220 // Saturation/Luminance gradient |
webmaster@1
|
221 $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5]))); |
webmaster@1
|
222 |
webmaster@1
|
223 // Linked elements or callback |
webmaster@1
|
224 if (typeof fb.callback == 'object') { |
webmaster@1
|
225 // Set background/foreground color |
webmaster@1
|
226 $(fb.callback).css({ |
webmaster@1
|
227 backgroundColor: fb.color, |
webmaster@1
|
228 color: fb.hsl[2] > 0.5 ? '#000' : '#fff' |
webmaster@1
|
229 }); |
webmaster@1
|
230 |
webmaster@1
|
231 // Change linked value |
webmaster@1
|
232 $(fb.callback).each(function() { |
webmaster@1
|
233 if (this.value && this.value != fb.color) { |
webmaster@1
|
234 this.value = fb.color; |
webmaster@1
|
235 } |
webmaster@1
|
236 }); |
webmaster@1
|
237 } |
webmaster@1
|
238 else if (typeof fb.callback == 'function') { |
webmaster@1
|
239 fb.callback.call(fb, fb.color); |
webmaster@1
|
240 } |
webmaster@1
|
241 }; |
webmaster@1
|
242 |
webmaster@1
|
243 /* Various color utility functions */ |
webmaster@1
|
244 fb.pack = function (rgb) { |
webmaster@1
|
245 var r = Math.round(rgb[0] * 255); |
webmaster@1
|
246 var g = Math.round(rgb[1] * 255); |
webmaster@1
|
247 var b = Math.round(rgb[2] * 255); |
webmaster@1
|
248 return '#' + (r < 16 ? '0' : '') + r.toString(16) + |
webmaster@1
|
249 (g < 16 ? '0' : '') + g.toString(16) + |
webmaster@1
|
250 (b < 16 ? '0' : '') + b.toString(16); |
webmaster@1
|
251 }; |
webmaster@1
|
252 |
webmaster@1
|
253 fb.unpack = function (color) { |
webmaster@1
|
254 if (color.length == 7) { |
webmaster@1
|
255 return [parseInt('0x' + color.substring(1, 3)) / 255, |
webmaster@1
|
256 parseInt('0x' + color.substring(3, 5)) / 255, |
webmaster@1
|
257 parseInt('0x' + color.substring(5, 7)) / 255]; |
webmaster@1
|
258 } |
webmaster@1
|
259 else if (color.length == 4) { |
webmaster@1
|
260 return [parseInt('0x' + color.substring(1, 2)) / 15, |
webmaster@1
|
261 parseInt('0x' + color.substring(2, 3)) / 15, |
webmaster@1
|
262 parseInt('0x' + color.substring(3, 4)) / 15]; |
webmaster@1
|
263 } |
webmaster@1
|
264 }; |
webmaster@1
|
265 |
webmaster@1
|
266 fb.HSLToRGB = function (hsl) { |
webmaster@1
|
267 var m1, m2, r, g, b; |
webmaster@1
|
268 var h = hsl[0], s = hsl[1], l = hsl[2]; |
webmaster@1
|
269 m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s; |
webmaster@1
|
270 m1 = l * 2 - m2; |
webmaster@1
|
271 return [this.hueToRGB(m1, m2, h+0.33333), |
webmaster@1
|
272 this.hueToRGB(m1, m2, h), |
webmaster@1
|
273 this.hueToRGB(m1, m2, h-0.33333)]; |
webmaster@1
|
274 }; |
webmaster@1
|
275 |
webmaster@1
|
276 fb.hueToRGB = function (m1, m2, h) { |
webmaster@1
|
277 h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h); |
webmaster@1
|
278 if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; |
webmaster@1
|
279 if (h * 2 < 1) return m2; |
webmaster@1
|
280 if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6; |
webmaster@1
|
281 return m1; |
webmaster@1
|
282 }; |
webmaster@1
|
283 |
webmaster@1
|
284 fb.RGBToHSL = function (rgb) { |
webmaster@1
|
285 var min, max, delta, h, s, l; |
webmaster@1
|
286 var r = rgb[0], g = rgb[1], b = rgb[2]; |
webmaster@1
|
287 min = Math.min(r, Math.min(g, b)); |
webmaster@1
|
288 max = Math.max(r, Math.max(g, b)); |
webmaster@1
|
289 delta = max - min; |
webmaster@1
|
290 l = (min + max) / 2; |
webmaster@1
|
291 s = 0; |
webmaster@1
|
292 if (l > 0 && l < 1) { |
webmaster@1
|
293 s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l)); |
webmaster@1
|
294 } |
webmaster@1
|
295 h = 0; |
webmaster@1
|
296 if (delta > 0) { |
webmaster@1
|
297 if (max == r && max != g) h += (g - b) / delta; |
webmaster@1
|
298 if (max == g && max != b) h += (2 + (b - r) / delta); |
webmaster@1
|
299 if (max == b && max != r) h += (4 + (r - g) / delta); |
webmaster@1
|
300 h /= 6; |
webmaster@1
|
301 } |
webmaster@1
|
302 return [h, s, l]; |
webmaster@1
|
303 }; |
webmaster@1
|
304 |
webmaster@1
|
305 // Install mousedown handler (the others are set on the document on-demand) |
webmaster@1
|
306 $('*', e).mousedown(fb.mousedown); |
webmaster@1
|
307 |
webmaster@1
|
308 // Init color |
webmaster@1
|
309 fb.setColor('#000000'); |
webmaster@1
|
310 |
webmaster@1
|
311 // Set linked elements/callback |
webmaster@1
|
312 if (callback) { |
webmaster@1
|
313 fb.linkTo(callback); |
webmaster@1
|
314 } |
webmaster@1
|
315 }; |