-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColorPicker.js
104 lines (92 loc) · 3.69 KB
/
ColorPicker.js
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
103
104
function ColorPicker(parent, btnObj) {
this.parent = parent;
this.button = btnObj;
this.rgb2hex = function (rgb) {
if (rgb.indexOf("rgb") > -1) { //IE9,FF,chrome等浏览器的背景颜色值是rgb格式
var rgbArray = rgb.replace("rgb(", "").replace(")", "").split(",");
var hexValue = "#";
for (var i = 0; i < rgbArray.length; i++) {
hexValue += Math.floor(rgbArray[i] / 16).toString(16) + Math.floor(rgbArray[i] / 16).toString(16);
}
return hexValue.toUpperCase();
} else { //IE6,7,8中的背景颜色值是16进制值,不需要转换
return rgb;
}
}
this.color2int = function (rgb) {
if (rgb.indexOf("rgba") > -1) {
var rgbArray = rgb.replace("rgba(", "").replace(")", "").split(",");
var hexValue = "";
for (var i = 0; i < 3; i++) {
hexValue += Math.floor(rgbArray[i] / 16).toString(16) + Math.floor(rgbArray[i] / 16).toString(16);
}
return parseInt(hexValue.toUpperCase(), 16);
} else if (rgb.indexOf("rgb") > -1) {
var rgbArray = rgb.replace("rgb(", "").replace(")", "").split(",");
var hexValue = "";
for (var i = 0; i < rgbArray.length; i++) {
hexValue += Math.floor(rgbArray[i] / 16).toString(16) + Math.floor(rgbArray[i] / 16).toString(16);
}
return parseInt(hexValue.toUpperCase(), 16);
} else {
return parseInt(rgb, 16);
}
}
}
ColorPicker.prototype.init = function () {
this.parent.toolbar.hover(function () { }, function () {
$(this).find('.color_select').html('');
});
this.button.click(function () {
danmaku.colorpicker.show();
});
};
ColorPicker.prototype.setCurrentColor = function (color) {
var hexcolor = this.rgb2hex(color);
$("#colorValue").val(hexcolor);
$("#current_color").css("background-color", hexcolor);
};
ColorPicker.prototype.setColor = function (color) {
var hexcolor = this.rgb2hex(color);
this.button.css("background-color", hexcolor);
};
ColorPicker.prototype.hide = function () {
this.button.html('');
};
ColorPicker.prototype.show = function () {
var cStart = 0;
var shtml = "";
shtml += '<div id="color_container"><input type="text" value="" id="current_color" /><input type="text" value="" id="colorValue" /><br/>';
for (var r = cStart; r < 16; r += 3) {
for (var g = cStart; g < 16; g += 3) {
for (var n = cStart; n < 16; n += 3) {
shtml += "<span class='color_picker' style='background-color:#" + r.toString(16) + r.toString(16) + g.toString(16)
+ g.toString(16) + n.toString(16) + n.toString(16) + "'></span>";
}
}
shtml += "<br />"
}
shtml += "</div>";
//color_select dlg
$.dialog({
id: 'Color',
//fixed: true,
//lock: true,
follow: this.button.get(0), //document.getElementById('danmaku-fontcolor'),
content: '<div class="tips">请选择一种颜色</div>' + shtml,
title: '请选择一种颜色',
initialize: function () {
var objcontainer = $('#color_container');
var objspan = objcontainer.find('span');
var dlg = this;
objspan.hover(function () {
danmaku.colorpicker.setCurrentColor(this.style.backgroundColor);
}, function () { });
objspan.click(function () {
danmaku.colorpicker.setColor(this.style.backgroundColor);
dlg.close();
});
objcontainer.find('#colorValue').select().focus();
}
});
};