forked from xpresas/bte-Makeblock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorpicker.js
More file actions
51 lines (43 loc) · 1.28 KB
/
colorpicker.js
File metadata and controls
51 lines (43 loc) · 1.28 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
var body = document.body,
r = document.querySelector('#r'),
g = document.querySelector('#g'),
b = document.querySelector('#b'),
r_out = document.querySelector('#r_out'),
g_out = document.querySelector('#g_out'),
b_out = document.querySelector('#b_out'),
hex_out = document.querySelector('#hex');
function setColor() {
var r_hex = parseInt(r.value, 10).toString(16),
g_hex = parseInt(g.value, 10).toString(16),
b_hex = parseInt(b.value, 10).toString(16),
hex = "#" + pad(r_hex) + pad(g_hex) + pad(b_hex);
document.getElementById('rgbContainer').style.backgroundColor = hex;
hex_out.value = hex;
}
function pad(n) {
return (n.length < 2) ? "0" + n : n;
}
r.addEventListener('change', function () {
setColor();
r_out.value = r.value;
}, false);
r.addEventListener('input', function () {
setColor();
r_out.value = r.value;
}, false);
g.addEventListener('change', function () {
setColor();
g_out.value = g.value;
}, false);
g.addEventListener('input', function () {
setColor();
g_out.value = g.value;
}, false);
b.addEventListener('change', function () {
setColor();
b_out.value = b.value;
}, false);
b.addEventListener('input', function () {
setColor();
b_out.value = b.value;
}, false);