jquery.colorpicker.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * jQuery插件:颜色拾取器
  3. *
  4. * @author Karson
  5. * @url http://blog.iplaybus.com
  6. * @name jquery.colorpicker.js
  7. * @since 2012-6-4 15:58:41
  8. */
  9. (function($) {
  10. var ColorHex=new Array('00','33','66','99','CC','FF');
  11. var SpColorHex=new Array('FF0000','00FF00','0000FF','FFFF00','00FFFF','FF00FF');
  12. $.fn.colorpicker = function(options) {
  13. var opts = jQuery.extend({}, jQuery.fn.colorpicker.defaults, options);
  14. initColor();
  15. return this.each(function(){
  16. var obj = $(this);
  17. obj.bind(opts.event,function(){
  18. //定位
  19. var ttop = $(this).offset().top; //控件的定位点高
  20. var thei = $(this).height(); //控件本身的高
  21. var tleft = $(this).offset().left; //控件的定位点宽
  22. $("#colorpanel").css({
  23. top:ttop+thei+5,
  24. left:tleft
  25. }).show();
  26. var target = opts.target ? $(opts.target) : obj;
  27. if(target.data("color") == null){
  28. target.data("color",target.css("color"));
  29. }
  30. if(target.data("value") == null){
  31. target.data("value",target.val());
  32. }
  33. $("#_creset").bind("click",function(){
  34. target.css("color", target.data("color")).val(target.data("value"));
  35. $("#colorpanel").hide();
  36. opts.reset(obj);
  37. });
  38. $("#CT tr td").unbind("click").mouseover(function(){
  39. var color=$(this).css("background-color");
  40. $("#DisColor").css("background",color);
  41. $("#HexColor").val($(this).attr("rel"));
  42. }).click(function(){
  43. var color=$(this).attr("rel");
  44. color = opts.ishex ? color : getRGBColor(color);
  45. if(opts.fillcolor) target.val(color);
  46. target.css("color",color);
  47. $("#colorpanel").hide();
  48. $("#_creset").unbind("click");
  49. opts.success(obj,color);
  50. });
  51. });
  52. });
  53. function initColor(){
  54. $("body").append('<div id="colorpanel" style="position: absolute; display: none;"></div>');
  55. var colorTable = '';
  56. var colorValue = '';
  57. for(i=0;i<2;i++){
  58. for(j=0;j<6;j++){
  59. colorTable=colorTable+'<tr height=12>'
  60. colorTable=colorTable+'<td width=11 rel="#000000" style="background-color:#000000">'
  61. colorValue = i==0 ? ColorHex[j]+ColorHex[j]+ColorHex[j] : SpColorHex[j];
  62. colorTable=colorTable+'<td width=11 rel="#'+colorValue+'" style="background-color:#'+colorValue+'">'
  63. colorTable=colorTable+'<td width=11 rel="#000000" style="background-color:#000000">'
  64. for (k=0;k<3;k++){
  65. for (l=0;l<6;l++){
  66. colorValue = ColorHex[k+i*3]+ColorHex[l]+ColorHex[j];
  67. colorTable=colorTable+'<td width=11 rel="#'+colorValue+'" style="background-color:#'+colorValue+'">'
  68. }
  69. }
  70. }
  71. }
  72. colorTable='<table width=253 border="0" cellspacing="0" cellpadding="0" style="border:1px solid #000;">'
  73. +'<tr height=30><td colspan=21 bgcolor=#cccccc>'
  74. +'<table cellpadding="0" cellspacing="1" border="0" style="border-collapse: collapse">'
  75. +'<tr><td width="3"><td><input type="text" id="DisColor" size="6" disabled style="border:solid 1px #000000;background-color:#ffff00"></td>'
  76. +'<td width="3"><td><input type="text" id="HexColor" size="7" style="border:inset 1px;font-family:Arial;" value="#000000"><a href="javascript:void(0);" id="_cclose">关闭</a> | <a href="javascript:void(0);" id="_creset">清除</a></td></tr></table></td></table>'
  77. +'<table id="CT" border="1" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="000000" style="cursor:pointer;">'
  78. +colorTable+'</table>';
  79. $("#colorpanel").html(colorTable);
  80. $("#_cclose").live('click',function(){
  81. $("#colorpanel").hide();
  82. return false;
  83. }).css({
  84. "font-size":"12px",
  85. "padding-left":"20px"
  86. });
  87. }
  88. function getRGBColor(color) {
  89. var result;
  90. if ( color && color.constructor == Array && color.length == 3 )
  91. color = color;
  92. if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
  93. color = [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
  94. if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
  95. color =[parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];
  96. if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
  97. color =[parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];
  98. if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
  99. color =[parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];
  100. return "rgb("+color[0]+","+color[1]+","+color[2]+")";
  101. }
  102. };
  103. jQuery.fn.colorpicker.defaults = {
  104. ishex : true, //是否使用16进制颜色值
  105. fillcolor:false, //是否将颜色值填充至对象的val中
  106. target: null, //目标对象
  107. event: 'click', //颜色框显示的事件
  108. success:function(){}, //回调函数
  109. reset:function(){}
  110. };
  111. })(jQuery);