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