Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / vendor / jquery.farbtastic.js @ e15a992e

History | View | Annotate | Download (7.7 kB)

1

    
2
/*!
3
 * Farbtastic: jQuery color picker plug-in v1.3u
4
 *
5
 * Licensed under the GPL license:
6
 *   http://www.gnu.org/licenses/gpl.html
7
 */
8
(function($) {
9

    
10
$.fn.farbtastic = function (options) {
11
  $.farbtastic(this, options);
12
  return this;
13
};
14

    
15
$.farbtastic = function (container, callback) {
16
  console.log('fb: container->', container);
17
  var container = $(container).get(0);
18
  
19
  return container.farbtastic || (container.farbtastic = new $._farbtastic(container, callback));
20
};
21

    
22
$._farbtastic = function (container, callback) {
23
  // Store farbtastic object
24
  var fb = this;
25

    
26
  // Insert markup
27
  $(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>');
28
  var e = $('.farbtastic', container);
29
  fb.wheel = $('.wheel', container).get(0);
30
  // Dimensions
31
  fb.radius = 84;
32
  fb.square = 100;
33
  fb.width = 194;
34

    
35
  // Fix background PNGs in IE6
36
  if (navigator.appVersion.match(/MSIE [0-6]\./)) {
37
    $('*', e).each(function () {
38
      if (this.currentStyle.backgroundImage != 'none') {
39
        var image = this.currentStyle.backgroundImage;
40
        image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
41
        $(this).css({
42
          'backgroundImage': 'none',
43
          'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
44
        });
45
      }
46
    });
47
  }
48

    
49
  /**
50
   * Link to the given element(s) or callback.
51
   */
52
  fb.linkTo = function (callback) {
53
    // Unbind previous nodes
54
    if (typeof fb.callback == 'object') {
55
      $(fb.callback).unbind('keyup', fb.updateValue);
56
    }
57

    
58
    // Reset color
59
    fb.color = null;
60

    
61
    // Bind callback or elements
62
    if (typeof callback == 'function') {
63
      fb.callback = callback;
64
    }
65
    else if (typeof callback == 'object' || typeof callback == 'string') {
66
      fb.callback = $(callback);
67
      fb.callback.bind('keyup', fb.updateValue);
68
      if (fb.callback.get(0).value) {
69
        fb.setColor(fb.callback.get(0).value);
70
      }
71
    }
72
    return this;
73
  };
74
  fb.updateValue = function (event) {
75
    if (this.value && this.value != fb.color) {
76
      console.log('updateValue');
77
      console.log(fb);
78
      fb.setColor(this.value);
79
    }
80
  };
81

    
82
  /**
83
   * Change color with HTML syntax #123456
84
   */
85
  fb.setColor = function (color) {
86
    var unpack = fb.unpack(color);
87
    if (fb.color != color && unpack) {
88
      fb.color = color;
89
      fb.rgb = unpack;
90
      fb.hsl = fb.RGBToHSL(fb.rgb);
91
      fb.updateDisplay();
92
    }
93
    return this;
94
  };
95

    
96
  /**
97
   * Change color with HSL triplet [0..1, 0..1, 0..1]
98
   */
99
  fb.setHSL = function (hsl) {
100
    fb.hsl = hsl;
101
    fb.rgb = fb.HSLToRGB(hsl);
102
    fb.color = fb.pack(fb.rgb);
103
    fb.updateDisplay();
104
    return this;
105
  };
106

    
107
  /////////////////////////////////////////////////////
108

    
109
  /**
110
   * Retrieve the coordinates of the given event relative to the center
111
   * of the widget.
112
   */
113
  fb.widgetCoords = function (event) {
114
    var offset = $(fb.wheel).offset();
115
    return { x: (event.pageX - offset.left) - fb.width / 2, y: (event.pageY - offset.top) - fb.width / 2 };
116
  };
117

    
118
  /**
119
   * Mousedown handler
120
   */
121
  fb.mousedown = function (event) {
122
    // Capture mouse
123
    if (!document.dragging) {
124
      $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
125
      document.dragging = true;
126
    }
127

    
128
    // Check which area is being dragged
129
    var pos = fb.widgetCoords(event);
130
    fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
131

    
132
    // Process
133
    fb.mousemove(event);
134
    return false;
135
  };
136

    
137
  /**
138
   * Mousemove handler
139
   */
140
  fb.mousemove = function (event) {
141
    // Get coordinates relative to color picker center
142
    var pos = fb.widgetCoords(event);
143

    
144
    // Set new HSL parameters
145
    if (fb.circleDrag) {
146
      var hue = Math.atan2(pos.x, -pos.y) / 6.28;
147
      if (hue < 0) hue += 1;
148
      fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
149
    }
150
    else {
151
      var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
152
      var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
153
      fb.setHSL([fb.hsl[0], sat, lum]);
154
    }
155
    return false;
156
  };
157

    
158
  /**
159
   * Mouseup handler
160
   */
161
  fb.mouseup = function () {
162
    // Uncapture mouse
163
    $(document).unbind('mousemove', fb.mousemove);
164
    $(document).unbind('mouseup', fb.mouseup);
165
    document.dragging = false;
166
  };
167

    
168
  /**
169
   * Update the markers and styles
170
   */
171
  fb.updateDisplay = function () {
172
    // Markers
173
    var angle = fb.hsl[0] * 6.28;
174
    $('.h-marker', e).css({
175
      left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
176
      top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
177
    });
178

    
179
    $('.sl-marker', e).css({
180
      left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
181
      top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
182
    });
183

    
184
    // Saturation/Luminance gradient
185
    $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
186

    
187
    // Linked elements or callback
188
    if (typeof fb.callback == 'object') {
189
      // Set background/foreground color
190
      // doesn't change the font color in the input field (snf)
191
      $(fb.callback).css({
192
        backgroundColor: fb.color/*,
193
        color: fb.hsl[2] > 0.5 ? '#000' : '#fff'*/
194
      });
195

    
196
      // Change linked value
197
      $(fb.callback).each(function() {
198
        if (this.value && this.value != fb.color) {
199
          this.value = fb.color;
200
        }
201
      });
202
    }
203
    else if (typeof fb.callback == 'function') {
204
      fb.callback.call(fb, fb.color);
205
    }
206
  };
207

    
208
  /* Various color utility functions */
209
  fb.pack = function (rgb) {
210
    var r = Math.round(rgb[0] * 255);
211
    var g = Math.round(rgb[1] * 255);
212
    var b = Math.round(rgb[2] * 255);
213
    return '#' + (r < 16 ? '0' : '') + r.toString(16) +
214
           (g < 16 ? '0' : '') + g.toString(16) +
215
           (b < 16 ? '0' : '') + b.toString(16);
216
  };
217

    
218
  fb.unpack = function (color) {
219
    if (color.length == 7) {
220
      return [parseInt('0x' + color.substring(1, 3)) / 255,
221
        parseInt('0x' + color.substring(3, 5)) / 255,
222
        parseInt('0x' + color.substring(5, 7)) / 255];
223
    }
224
    else if (color.length == 4) {
225
      return [parseInt('0x' + color.substring(1, 2)) / 15,
226
        parseInt('0x' + color.substring(2, 3)) / 15,
227
        parseInt('0x' + color.substring(3, 4)) / 15];
228
    }
229
  };
230

    
231
  fb.HSLToRGB = function (hsl) {
232
    var m1, m2, r, g, b;
233
    var h = hsl[0], s = hsl[1], l = hsl[2];
234
    m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
235
    m1 = l * 2 - m2;
236
    return [this.hueToRGB(m1, m2, h+0.33333),
237
        this.hueToRGB(m1, m2, h),
238
        this.hueToRGB(m1, m2, h-0.33333)];
239
  };
240

    
241
  fb.hueToRGB = function (m1, m2, h) {
242
    h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
243
    if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
244
    if (h * 2 < 1) return m2;
245
    if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
246
    return m1;
247
  };
248

    
249
  fb.RGBToHSL = function (rgb) {
250
    var min, max, delta, h, s, l;
251
    var r = rgb[0], g = rgb[1], b = rgb[2];
252
    min = Math.min(r, Math.min(g, b));
253
    max = Math.max(r, Math.max(g, b));
254
    delta = max - min;
255
    l = (min + max) / 2;
256
    s = 0;
257
    if (l > 0 && l < 1) {
258
      s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
259
    }
260
    h = 0;
261
    if (delta > 0) {
262
      if (max == r && max != g) h += (g - b) / delta;
263
      if (max == g && max != b) h += (2 + (b - r) / delta);
264
      if (max == b && max != r) h += (4 + (r - g) / delta);
265
      h /= 6;
266
    }
267
    return [h, s, l];
268
  };
269

    
270
  // Install mousedown handler (the others are set on the document on-demand)
271
  $('*', e).mousedown(fb.mousedown);
272

    
273
    // Init color
274
  fb.setColor('#000000');
275

    
276
  // Set linked elements/callback
277
  if (callback) {
278
    fb.linkTo(callback);
279
  }
280
};
281

    
282
})(jQuery);