[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/jquery_update/replace/ -> farbtastic.js (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7