/**
* Farbtastic Color Picker 1.2
* © 2008 Steven Wittens
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
jQuery.fn.farbtastic = function(a) { $.farbtastic(this, a); return this }; jQuery.farbtastic = function(a, b) { var a = $(a).get(0); return a.farbtastic || (a.farbtastic = new jQuery._farbtastic(a, b)) }; jQuery._farbtastic = function(a, d) { var b = this; $(a).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>'); var c = $(".farbtastic", a); b.wheel = $(".wheel", a).get(0); b.radius = 84; b.square = 100; b.width = 194; if (navigator.appVersion.match(/MSIE [0-6]\./)) { $("*", c).each(function() { if (this.currentStyle.backgroundImage != "none") { var e = this.currentStyle.backgroundImage; e = this.currentStyle.backgroundImage.substring(5, e.length - 2); $(this).css({ backgroundImage: "none", filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + e + "')" }) } }) } b.linkTo = function(e) { if (typeof b.callback == "object") { $(b.callback).unbind("keyup", b.updateValue) } b.color = null; if (typeof e == "function") { b.callback = e } else { if (typeof e == "object" || typeof e == "string") { b.callback = $(e); b.callback.bind("keyup", b.updateValue); if (b.callback.get(0).value) { b.setColor(b.callback.get(0).value) } } } return this }; b.updateValue = function(e) { if (this.value && this.value != b.color) { b.setColor(this.value) } }; b.setColor = function(e) { var f = b.unpack(e); if (b.color != e && f) { b.color = e; b.rgb = f; b.hsl = b.RGBToHSL(b.rgb); b.updateDisplay() } return this }; b.setHSL = function(e) { b.hsl = e; b.rgb = b.HSLToRGB(e); b.color = b.pack(b.rgb); b.updateDisplay(); return this }; b.widgetCoords = function(i) { var g, m; var h = i.target || i.srcElement; var f = b.wheel; if (typeof i.offsetX != "undefined") { var l = { x: i.offsetX, y: i.offsetY }; var j = h; while (j) { j.mouseX = l.x; j.mouseY = l.y; l.x += j.offsetLeft; l.y += j.offsetTop; j = j.offsetParent } var j = f; var k = { x: 0, y: 0 }; while (j) { if (typeof j.mouseX != "undefined") { g = j.mouseX - k.x; m = j.mouseY - k.y; break } k.x += j.offsetLeft; k.y += j.offsetTop; j = j.offsetParent } j = h; while (j) { j.mouseX = undefined; j.mouseY = undefined; j = j.offsetParent } } else { var l = b.absolutePosition(f); g = (i.pageX || 0 * (i.clientX + $("html").get(0).scrollLeft)) - l.x; m = (i.pageY || 0 * (i.clientY + $("html").get(0).scrollTop)) - l.y } return { x: g - b.width / 2, y: m - b.width / 2} }; b.mousedown = function(e) { if (!document.dragging) { $(document).bind("mousemove", b.mousemove).bind("mouseup", b.mouseup); document.dragging = true } var f = b.widgetCoords(e); b.circleDrag = Math.max(Math.abs(f.x), Math.abs(f.y)) * 2 > b.square; b.mousemove(e); return false }; b.mousemove = function(h) { var i = b.widgetCoords(h); if (b.circleDrag) { var g = Math.atan2(i.x, -i.y) / 6.28; if (g < 0) { g += 1 } b.setHSL([g, b.hsl[1], b.hsl[2]]) } else { var f = Math.max(0, Math.min(1, -(i.x / b.square) + 0.5)); var e = Math.max(0, Math.min(1, -(i.y / b.square) + 0.5)); b.setHSL([b.hsl[0], f, e]) } return false }; b.mouseup = function() { $(document).unbind("mousemove", b.mousemove); $(document).unbind("mouseup", b.mouseup); document.dragging = false }; b.updateDisplay = function() { var e = b.hsl[0] * 6.28; $(".h-marker", c).css({ left: Math.round(Math.sin(e) * b.radius + b.width / 2) + "px", top: Math.round(-Math.cos(e) * b.radius + b.width / 2) + "px" }); $(".sl-marker", c).css({ left: Math.round(b.square * (0.5 - b.hsl[1]) + b.width / 2) + "px", top: Math.round(b.square * (0.5 - b.hsl[2]) + b.width / 2) + "px" }); $(".color", c).css("backgroundColor", b.pack(b.HSLToRGB([b.hsl[0], 1, 0.5]))); if (typeof b.callback == "object") { $(b.callback).css({ backgroundColor: b.color, color: b.hsl[2] > 0.5 ? "#000" : "#fff" }); $(b.callback).each(function() { if (this.value && this.value != b.color) { this.value = b.color } }) } else { if (typeof b.callback == "function") { b.callback.call(b, b.color) } } }; b.absolutePosition = function(f) { var g = { x: f.offsetLeft, y: f.offsetTop }; if (f.offsetParent) { var e = b.absolutePosition(f.offsetParent); g.x += e.x; g.y += e.y } return g }; b.pack = function(f) { var i = Math.round(f[0] * 255); var h = Math.round(f[1] * 255); var e = Math.round(f[2] * 255); return "#" + (i < 16 ? "0" : "") + i.toString(16) + (h < 16 ? "0" : "") + h.toString(16) + (e < 16 ? "0" : "") + e.toString(16) }; b.unpack = function(e) { if (e.length == 7) { return [parseInt("0x" + e.substring(1, 3)) / 255, parseInt("0x" + e.substring(3, 5)) / 255, parseInt("0x" + e.substring(5, 7)) / 255] } else { if (e.length == 4) { return [parseInt("0x" + e.substring(1, 2)) / 15, parseInt("0x" + e.substring(2, 3)) / 15, parseInt("0x" + e.substring(3, 4)) / 15] } } }; b.HSLToRGB = function(m) { var o, n, e, j, k; var i = m[0], p = m[1], f = m[2]; n = (f <= 0.5) ? f * (p + 1) : f + p - f * p; o = f * 2 - n; return [this.hueToRGB(o, n, i + 0.33333), this.hueToRGB(o, n, i), this.hueToRGB(o, n, i - 0.33333)] }; b.hueToRGB = function(f, e, g) { g = (g < 0) ? g + 1 : ((g > 1) ? g - 1 : g); if (g * 6 < 1) { return f + (e - f) * g * 6 } if (g * 2 < 1) { return e } if (g * 3 < 2) { return f + (e - f) * (0.66666 - g) * 6 } return f }; b.RGBToHSL = function(m) { var i, o, p, j, q, f; var e = m[0], k = m[1], n = m[2]; i = Math.min(e, Math.min(k, n)); o = Math.max(e, Math.max(k, n)); p = o - i; f = (i + o) / 2; q = 0; if (f > 0 && f < 1) { q = p / (f < 0.5 ? (2 * f) : (2 - 2 * f)) } j = 0; if (p > 0) { if (o == e && o != k) { j += (k - n) / p } if (o == k && o != n) { j += (2 + (n - e) / p) } if (o == n && o != e) { j += (4 + (e - k) / p) } j /= 6 } return [j, q, f] }; $("*", c).mousedown(b.mousedown); b.setColor("#000000"); if (d) { b.linkTo(d) } };