-
+ 7635F283443EE28AF79C1528487294C23BC99FCDFFD16D714E8C4175CE924CD6EA13FA5FA74D966393C57571EA9E9ACBA1EB87F27BBC5312E7999EA6C411F9BB
mp-wp/wp-admin/js/farbtastic.js
(0 . 0)(1 . 333)
49473 // $Id: farbtastic.js,v 1.2 2007/01/08 22:53:01 unconed Exp $
49474 // Farbtastic 1.2
49475
49476 var farbtastic_click = false;
49477
49478 jQuery.fn.farbtastic = function (callback) {
49479 jQuery.farbtastic(this, callback);
49480 return this;
49481 };
49482
49483 jQuery.farbtastic = function (container, callback) {
49484 var container = jQuery(container).get(0);
49485 return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback));
49486 }
49487
49488 jQuery._farbtastic = function (container, callback) {
49489 // Store farbtastic object
49490 var fb = this;
49491
49492 // Insert markup
49493 jQuery(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>');
49494 var e = jQuery('.farbtastic', container);
49495 fb.wheel = jQuery('.wheel', container).get(0);
49496 // Dimensions
49497 fb.radius = 84;
49498 fb.square = 100;
49499 fb.width = 194;
49500
49501 // Fix background PNGs in IE6
49502 if (navigator.appVersion.match(/MSIE [0-6]\./)) {
49503 jQuery('*', e).each(function () {
49504 if (this.currentStyle.backgroundImage != 'none') {
49505 var image = this.currentStyle.backgroundImage;
49506 image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
49507 jQuery(this).css({
49508 'backgroundImage': 'none',
49509 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
49510 });
49511 }
49512 });
49513 }
49514
49515 /**
49516 * Link to the given element(s) or callback.
49517 */
49518 fb.linkTo = function (callback) {
49519 // Unbind previous nodes
49520 if (typeof fb.callback == 'object') {
49521 jQuery(fb.callback).unbind('keyup', fb.updateValue);
49522 }
49523
49524 // Reset color
49525 fb.color = null;
49526
49527 // Bind callback or elements
49528 if (typeof callback == 'function') {
49529 fb.callback = callback;
49530 }
49531 else if (typeof callback == 'object' || typeof callback == 'string') {
49532 fb.callback = jQuery(callback);
49533 fb.callback.bind('keyup', fb.updateValue);
49534 if (fb.callback.get(0).value) {
49535 fb.setColor(fb.callback.get(0).value);
49536 }
49537 }
49538 return this;
49539 }
49540 fb.updateValue = function (event) {
49541 if (this.value && this.value != fb.color) {
49542 fb.setColor(this.value);
49543 }
49544 }
49545
49546 /**
49547 * Change color with HTML syntax #123456
49548 */
49549 fb.setColor = function (color) {
49550 var unpack = fb.unpack(color);
49551 if (fb.color != color && unpack) {
49552 fb.color = color;
49553 fb.rgb = unpack;
49554 fb.hsl = fb.RGBToHSL(fb.rgb);
49555 fb.updateDisplay();
49556 }
49557 return this;
49558 }
49559
49560 /**
49561 * Change color with HSL triplet [0..1, 0..1, 0..1]
49562 */
49563 fb.setHSL = function (hsl) {
49564 fb.hsl = hsl;
49565 fb.rgb = fb.HSLToRGB(hsl);
49566 fb.color = fb.pack(fb.rgb);
49567 fb.updateDisplay();
49568 return this;
49569 }
49570
49571 /////////////////////////////////////////////////////
49572
49573 /**
49574 * Retrieve the coordinates of the given event relative to the center
49575 * of the widget.
49576 */
49577 fb.widgetCoords = function (event) {
49578 var x, y;
49579 var el = event.target || event.srcElement;
49580 var reference = fb.wheel;
49581
49582 if (typeof event.offsetX != 'undefined') {
49583 // Use offset coordinates and find common offsetParent
49584 var pos = { x: event.offsetX, y: event.offsetY };
49585
49586 // Send the coordinates upwards through the offsetParent chain.
49587 var e = el;
49588 while (e) {
49589 e.mouseX = pos.x;
49590 e.mouseY = pos.y;
49591 pos.x += e.offsetLeft;
49592 pos.y += e.offsetTop;
49593 e = e.offsetParent;
49594 }
49595
49596 // Look for the coordinates starting from the wheel widget.
49597 var e = reference;
49598 var offset = { x: 0, y: 0 }
49599 while (e) {
49600 if (typeof e.mouseX != 'undefined') {
49601 x = e.mouseX - offset.x;
49602 y = e.mouseY - offset.y;
49603 break;
49604 }
49605 offset.x += e.offsetLeft;
49606 offset.y += e.offsetTop;
49607 e = e.offsetParent;
49608 }
49609
49610 // Reset stored coordinates
49611 e = el;
49612 while (e) {
49613 e.mouseX = undefined;
49614 e.mouseY = undefined;
49615 e = e.offsetParent;
49616 }
49617 }
49618 else {
49619 // Use absolute coordinates
49620 var pos = fb.absolutePosition(reference);
49621 x = (event.pageX || 0*(event.clientX + jQuery('html').get(0).scrollLeft)) - pos.x;
49622 y = (event.pageY || 0*(event.clientY + jQuery('html').get(0).scrollTop)) - pos.y;
49623 }
49624 // Subtract distance to middle
49625 return { x: x - fb.width / 2, y: y - fb.width / 2 };
49626 }
49627
49628 /**
49629 * Mousedown handler
49630 */
49631 fb.mousedown = function (event) {
49632 farbtastic_click = true;
49633 // Capture mouse
49634 if (!document.dragging) {
49635 jQuery(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
49636 document.dragging = true;
49637 }
49638
49639 // Check which area is being dragged
49640 var pos = fb.widgetCoords(event);
49641 fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
49642
49643 // Process
49644 fb.mousemove(event);
49645 return false;
49646 }
49647
49648 /**
49649 * Mousemove handler
49650 */
49651 fb.mousemove = function (event) {
49652 // Get coordinates relative to color picker center
49653 var pos = fb.widgetCoords(event);
49654
49655 // Set new HSL parameters
49656 if (fb.circleDrag) {
49657 var hue = Math.atan2(pos.x, -pos.y) / 6.28;
49658 if (hue < 0) hue += 1;
49659 fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
49660 }
49661 else {
49662 var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
49663 var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
49664 fb.setHSL([fb.hsl[0], sat, lum]);
49665 }
49666 return false;
49667 }
49668
49669 /**
49670 * Mouseup handler
49671 */
49672 fb.mouseup = function () {
49673 // Uncapture mouse
49674 farbtastic_click = false;
49675 jQuery(document).unbind('mousemove', fb.mousemove);
49676 jQuery(document).unbind('mouseup', fb.mouseup);
49677 document.dragging = false;
49678 }
49679
49680 /**
49681 * Update the markers and styles
49682 */
49683 fb.updateDisplay = function () {
49684 // Markers
49685 var angle = fb.hsl[0] * 6.28;
49686 jQuery('.h-marker', e).css({
49687 left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
49688 top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
49689 });
49690
49691 jQuery('.sl-marker', e).css({
49692 left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
49693 top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
49694 });
49695
49696 // Saturation/Luminance gradient
49697 jQuery('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
49698
49699 // Linked elements or callback
49700 if (typeof fb.callback == 'object') {
49701 // Set background/foreground color
49702 jQuery(fb.callback).css({
49703 backgroundColor: fb.color,
49704 color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
49705 });
49706
49707 // Change linked value
49708 jQuery(fb.callback).each(function() {
49709 if (this.value && this.value != fb.color) {
49710 this.value = fb.color;
49711 }
49712 });
49713 }
49714 else if (typeof fb.callback == 'function') {
49715 fb.callback.call(fb, fb.color);
49716 }
49717 }
49718
49719 /**
49720 * Get absolute position of element
49721 */
49722 fb.absolutePosition = function (el) {
49723 var r = { x: el.offsetLeft, y: el.offsetTop };
49724 // Resolve relative to offsetParent
49725 if (el.offsetParent) {
49726 var tmp = fb.absolutePosition(el.offsetParent);
49727 r.x += tmp.x;
49728 r.y += tmp.y;
49729 }
49730 return r;
49731 };
49732
49733 /* Various color utility functions */
49734 fb.pack = function (rgb) {
49735 var r = Math.round(rgb[0] * 255);
49736 var g = Math.round(rgb[1] * 255);
49737 var b = Math.round(rgb[2] * 255);
49738 return '#' + (r < 16 ? '0' : '') + r.toString(16) +
49739 (g < 16 ? '0' : '') + g.toString(16) +
49740 (b < 16 ? '0' : '') + b.toString(16);
49741 }
49742
49743 fb.unpack = function (color) {
49744 if (color.length == 7) {
49745 return [parseInt('0x' + color.substring(1, 3)) / 255,
49746 parseInt('0x' + color.substring(3, 5)) / 255,
49747 parseInt('0x' + color.substring(5, 7)) / 255];
49748 }
49749 else if (color.length == 4) {
49750 return [parseInt('0x' + color.substring(1, 2)) / 15,
49751 parseInt('0x' + color.substring(2, 3)) / 15,
49752 parseInt('0x' + color.substring(3, 4)) / 15];
49753 }
49754 }
49755
49756 fb.HSLToRGB = function (hsl) {
49757 var m1, m2, r, g, b;
49758 var h = hsl[0], s = hsl[1], l = hsl[2];
49759 m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
49760 m1 = l * 2 - m2;
49761 return [this.hueToRGB(m1, m2, h+0.33333),
49762 this.hueToRGB(m1, m2, h),
49763 this.hueToRGB(m1, m2, h-0.33333)];
49764 }
49765
49766 fb.hueToRGB = function (m1, m2, h) {
49767 h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
49768 if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
49769 if (h * 2 < 1) return m2;
49770 if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
49771 return m1;
49772 }
49773
49774 fb.RGBToHSL = function (rgb) {
49775 var min, max, delta, h, s, l;
49776 var r = rgb[0], g = rgb[1], b = rgb[2];
49777 min = Math.min(r, Math.min(g, b));
49778 max = Math.max(r, Math.max(g, b));
49779 delta = max - min;
49780 l = (min + max) / 2;
49781 s = 0;
49782 if (l > 0 && l < 1) {
49783 s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
49784 }
49785 h = 0;
49786 if (delta > 0) {
49787 if (max == r && max != g) h += (g - b) / delta;
49788 if (max == g && max != b) h += (2 + (b - r) / delta);
49789 if (max == b && max != r) h += (4 + (r - g) / delta);
49790 h /= 6;
49791 }
49792 return [h, s, l];
49793 }
49794
49795 // Install mousedown handler (the others are set on the document on-demand)
49796 jQuery('*', e).mousedown(fb.mousedown);
49797
49798 // Init color
49799 fb.setColor('#000000');
49800
49801 // Set linked elements/callback
49802 if (callback) {
49803 fb.linkTo(callback);
49804 }
49805 }