-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.html
69 lines (64 loc) · 2.52 KB
/
color.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
R <input name="r1e" value="255" class="rle" maxlength="3" onkeypress="getkey(event,0);">
G <input name="r1e" value="180" class="rle" maxlength="3" onkeypress="getkey(event,0);">
B <input name="r1e" value="0" class="rle" maxlength="3" onkeypress="getkey(event,0);">
<input onclick="showRGB()" type="button" value="转换" name="button">
<div id="a_mo2"> </div>
<div id="color1" style="width: 50px;height: 30px;"></div><br />
<input name="rgb2" value="#CC00FF" size="8" maxlength="20" onkeypress="getkey(event,1);">
<input onclick="showRGB2()" type="button" value="转换" name="button">
<div id="a_mo3"> </div>
<div id="color2" style="width: 50px;height: 30px;"></div>
<script type="text/javascript">
function showRGB() {
hexcode = "#";
for(x = 0; x < 3; x++) {;
var n = document.getElementsByName("r1e")[x].value;
if(n == "") n = "0";
if(parseInt(n) != n) return alert("请输入数字!");
if(n > 255) return alert("数字在0-255之间!");
var c = "0123456789ABCDEF",
b = "",
a = n % 16;
b = c.substr(a, 1);
a = (n - a) / 16;
hexcode += c.substr(a, 1) + b
}
// document.getElementById("color1").bgColor = a_mo2.innerHTML = hexcode
document.getElementById("color1").style.backgroundColor= a_mo2.innerHTML = hexcode;
}
function showRGB2() {;
var a = document.getElementsByName("rgb2")[0].value;
if(a.substr(0, 1) == "#") a = a.substring(1);
if(a.length != 6) return alert("请输入正确的十六进制颜色码!")
// document.getElementById("color2").bgColor = "#" + a;
document.getElementById("color2").style.backgroundColor="#" + a;
a = a.toLowerCase()
b = new Array();
for(x = 0; x < 3; x++) {
b[0] = a.substr(x * 2, 2)
b[3] = "0123456789abcdef";
b[1] = b[0].substr(0, 1)
b[2] = b[0].substr(1, 1)
b[20 + x] = b[3].indexOf(b[1]) * 16 + b[3].indexOf(b[2])
}
a_mo3.innerHTML = b[20] + "," + b[21] + "," + b[22];
}
function getkey(e, n) {
var keynum;
if(window.event) keynum = e.keyCode;
else if(e.which) keynum = e.which;
if(keynum == 13) {
if(n == 0) showRGB();
else showRGB2();
}
}
</script>
</body>
</html>