/** * Really Simple Color Picker in jQuery * * Copyright (c) 2008 Lakshan Perera (www.laktek.com) * Licensed under the MIT (MIT-LICENSE.txt) licenses. * */ (function($){ $.fn.colorPicker = function() { if(!$.fn.colorPicker.selector) $.fn.colorPicker.buildSelector(); this.each(function(i) { if($(this).attr('type') != 'text') return; $(this).bind('change', function() { $(this).css('color', $(this).val()); }).bind('click', function() { $.fn.colorPicker.toggleSelector(this); }); }); return this; } $.fn.colorPicker.toggleSelector = function(o) { if(!o || this.selectorOwner == o) this.hideSelector(); else this.showSelector(o); } $.fn.colorPicker.buildSelector = function() { if(this.selector) return; this.selectorOwner = null; this.ownerValue = ''; this.selector = $("
"); //add color pallete for(var i in this.defaultColors) { $("
 
") .css("background-color", "#" + this.defaultColors[i]) .bind("click", function(e) { $.fn.colorPicker.changeColor($(this).css("background-color")); }) .bind("mouseover", function(e) { $(this).css("border-color", "#598FEF"); $($.fn.colorPicker.selectorOwner) .val($.fn.colorPicker.toHex($(this).css("background-color"))) .css('color', $(this).css("background-color")); }) .bind("mouseout", function(e) { $(this).css("border-color", "#000000"); $($.fn.colorPicker.selectorOwner) .val($.fn.colorPicker.ownerValue) .css('color', $.fn.colorPicker.ownerValue); }) .appendTo(this.selector); } //add HEX value field /* $.fn.colorPicker.selectorOwner = $("") .bind("keydown", function(event) { if(event.keyCode == 13) $.fn.colorPicker.changeColor(); if(event.keyCode == 27) $.fn.colorPicker.toggleSelector(); }); */ $("body").append(this.selector); } $.fn.colorPicker.changeColor = function(value) { if(!this.selectorOwner) return; if(value = this.toHex(value)) { this.ownerValue = value; $(this.selectorOwner).css("color", value).val(value).change(); this.hideSelector(); } } $.fn.colorPicker.hideSelector = function() { $(document).unbind("mousedown", this.checkMouse); this.selector.hide(); if(this.selectorOwner) $(this.selectorOwner).css('color', this.ownerValue).val(this.ownerValue); this.selectorOwner = null; this.ownerValue = ''; } $.fn.colorPicker.showSelector = function(o) { this.selectorOwner = o; this.ownerValue = $(o).val(); this.selector.css({ top: $(o).offset().top + ($(o).outerHeight()), left: $(o).offset().left }).show(); //bind close event handler $(document).bind("mousedown", this.checkMouse); } $.fn.colorPicker.checkMouse = function(event) { //check the click was on selector itself or on selectorOwner if($.fn.colorPicker.selector && event.target != $.fn.colorPicker.selector[0] && event.target != $.fn.colorPicker.selectorOwner && !$(event.target).parents().filter($.fn.colorPicker.selector).length) $.fn.colorPicker.hideSelector(); } // converts RGB string to HEX - inspired by http://code.google.com/p/jquery-color-utils $.fn.colorPicker.toHex = function(color) { //valid HEX code is entered if(color.match(/^#?[0-9a-fA-F]{3}$/) || color.match(/^#?[0-9a-fA-F]{6}$/)) return (color.charAt(0) == "#") ? color : ("#" + color); // rgb color value is entered (by selecting a swatch) else if(!color.match(/^rgb\(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]),[ ]{0,1}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]),[ ]{0,1}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\)$/)) return false; var c = ([parseInt(RegExp.$1),parseInt(RegExp.$2),parseInt(RegExp.$3)]); var pad = function(str) { if(str.length < 2) for(var i = 0,len = 2 - str.length ; i