[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7