[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/misc/farbtastic/ -> 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 x, y;
 104      var el = event.target || event.srcElement;
 105      var reference = fb.wheel;
 106      
 107      // If the offset from the relative element is undefined calculate it.
 108      if ( typeof event.offsetX == 'undefined' && typeof event.offsetY == 'undefined' ) {
 109        var offset = $(event.target).offset(false);
 110        event.offsetX = event.pageX - offset.left;
 111        event.offsetY = event.pageY - offset.top;
 112      }
 113      
 114      // Use offset coordinates and find common offsetParent
 115      var pos = { x: event.offsetX, y: event.offsetY };
 116  
 117      // Send the coordinates upwards through the offsetParent chain.
 118      var e = el;
 119      while (e) {
 120        e.mouseX = pos.x;
 121        e.mouseY = pos.y;
 122        pos.x += e.offsetLeft;
 123        pos.y += e.offsetTop;
 124        e = e.offsetParent;
 125      }
 126  
 127      // Look for the coordinates starting from the wheel widget.
 128      var e = reference;
 129      var offset = { x: 0, y: 0 };
 130      while (e) {
 131        if (typeof e.mouseX != 'undefined') {
 132          x = e.mouseX - offset.x;
 133          y = e.mouseY - offset.y;
 134          break;
 135        }
 136        offset.x += e.offsetLeft;
 137        offset.y += e.offsetTop;
 138        e = e.offsetParent;
 139      }
 140  
 141      // Reset stored coordinates
 142      e = el;
 143      while (e) {
 144        e.mouseX = undefined;
 145        e.mouseY = undefined;
 146        e = e.offsetParent;
 147      }
 148  
 149      // Subtract distance to middle
 150      return { x: x - fb.width / 2, y: y - fb.width / 2 };
 151    };
 152  
 153    /**
 154     * Mousedown handler
 155     */
 156    fb.mousedown = function (event) {
 157      // Capture mouse
 158      if (!document.dragging) {
 159        $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
 160        document.dragging = true;
 161      }
 162  
 163      // Check which area is being dragged
 164      var pos = fb.widgetCoords(event);
 165      fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
 166  
 167      // Process
 168      fb.mousemove(event);
 169      return false;
 170    };
 171  
 172    /**
 173     * Mousemove handler
 174     */
 175    fb.mousemove = function (event) {
 176      // Get coordinates relative to color picker center
 177      var pos = fb.widgetCoords(event);
 178  
 179      // Set new HSL parameters
 180      if (fb.circleDrag) {
 181        var hue = Math.atan2(pos.x, -pos.y) / 6.28;
 182        if (hue < 0) hue += 1;
 183        fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
 184      }
 185      else {
 186        var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
 187        var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
 188        fb.setHSL([fb.hsl[0], sat, lum]);
 189      }
 190      return false;
 191    };
 192  
 193    /**
 194     * Mouseup handler
 195     */
 196    fb.mouseup = function () {
 197      // Uncapture mouse
 198      $(document).unbind('mousemove', fb.mousemove);
 199      $(document).unbind('mouseup', fb.mouseup);
 200      document.dragging = false;
 201    };
 202  
 203    /**
 204     * Update the markers and styles
 205     */
 206    fb.updateDisplay = function () {
 207      // Markers
 208      var angle = fb.hsl[0] * 6.28;
 209      $('.h-marker', e).css({
 210        left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
 211        top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
 212      });
 213  
 214      $('.sl-marker', e).css({
 215        left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
 216        top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
 217      });
 218  
 219      // Saturation/Luminance gradient
 220      $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
 221  
 222      // Linked elements or callback
 223      if (typeof fb.callback == 'object') {
 224        // Set background/foreground color
 225        $(fb.callback).css({
 226          backgroundColor: fb.color,
 227          color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
 228        });
 229  
 230        // Change linked value
 231        $(fb.callback).each(function() {
 232          if (this.value && this.value != fb.color) {
 233            this.value = fb.color;
 234          }
 235        });
 236      }
 237      else if (typeof fb.callback == 'function') {
 238        fb.callback.call(fb, fb.color);
 239      }
 240    };
 241  
 242    /* Various color utility functions */
 243    fb.pack = function (rgb) {
 244      var r = Math.round(rgb[0] * 255);
 245      var g = Math.round(rgb[1] * 255);
 246      var b = Math.round(rgb[2] * 255);
 247      return '#' + (r < 16 ? '0' : '') + r.toString(16) +
 248             (g < 16 ? '0' : '') + g.toString(16) +
 249             (b < 16 ? '0' : '') + b.toString(16);
 250    };
 251  
 252    fb.unpack = function (color) {
 253      if (color.length == 7) {
 254        return [parseInt('0x' + color.substring(1, 3)) / 255,
 255          parseInt('0x' + color.substring(3, 5)) / 255,
 256          parseInt('0x' + color.substring(5, 7)) / 255];
 257      }
 258      else if (color.length == 4) {
 259        return [parseInt('0x' + color.substring(1, 2)) / 15,
 260          parseInt('0x' + color.substring(2, 3)) / 15,
 261          parseInt('0x' + color.substring(3, 4)) / 15];
 262      }
 263    };
 264  
 265    fb.HSLToRGB = function (hsl) {
 266      var m1, m2, r, g, b;
 267      var h = hsl[0], s = hsl[1], l = hsl[2];
 268      m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
 269      m1 = l * 2 - m2;
 270      return [this.hueToRGB(m1, m2, h+0.33333),
 271          this.hueToRGB(m1, m2, h),
 272          this.hueToRGB(m1, m2, h-0.33333)];
 273    };
 274  
 275    fb.hueToRGB = function (m1, m2, h) {
 276      h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
 277      if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
 278      if (h * 2 < 1) return m2;
 279      if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
 280      return m1;
 281    };
 282  
 283    fb.RGBToHSL = function (rgb) {
 284      var min, max, delta, h, s, l;
 285      var r = rgb[0], g = rgb[1], b = rgb[2];
 286      min = Math.min(r, Math.min(g, b));
 287      max = Math.max(r, Math.max(g, b));
 288      delta = max - min;
 289      l = (min + max) / 2;
 290      s = 0;
 291      if (l > 0 && l < 1) {
 292        s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
 293      }
 294      h = 0;
 295      if (delta > 0) {
 296        if (max == r && max != g) h += (g - b) / delta;
 297        if (max == g && max != b) h += (2 + (b - r) / delta);
 298        if (max == b && max != r) h += (4 + (r - g) / delta);
 299        h /= 6;
 300      }
 301      return [h, s, l];
 302    };
 303  
 304    // Install mousedown handler (the others are set on the document on-demand)
 305    $('*', e).mousedown(fb.mousedown);
 306  
 307      // Init color
 308    fb.setColor('#000000');
 309  
 310    // Set linked elements/callback
 311    if (callback) {
 312      fb.linkTo(callback);
 313    }
 314  };


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