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