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