-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.38 KB
/
index.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
//
function get(id) {
return document.getElementById(id);
}
const root = get('clk1');
const info = get('info');
let currColor = 'red';
function getRand(max) {
return Math.round(Math.random() * max);
}
function getColor() {
return `rgb(${getRand(255)},${getRand(255)},${getRand(255)})`;
}
function opacities(color, opacity) {
const parts = color.split(/[()]/);
return `rgba(${parts[1]},${opacity})`;
}
root.onclick = () => {
const newColor = currColor = getColor();
root.style['background-color'] = newColor;
info.innerHTML = newColor;
};
// color saving
const save = get('save');
const list = get('list');
save.onclick = () => {
const node = document.createElement('div');
node.className = 'saved-color';
node.style['background-color'] = currColor;
node.innerHTML = currColor;
list.appendChild(node);
}
// gradient making
function setSide(isRight) {
const midPart = '90deg,'
const oldGrad = list.style['background'].split(midPart);
const colors = oldGrad[1].split(/[,)]/);
list.style['backgound'] = oldGrad[0] +
midPart +
(isRight ?
colors[0] + ',' + currColor :
currColor + ',' + colors[1]) +
')';
}
const setLeft = get('setLeft');
const setRight = get('setRight');
[setLeft, setRight].forEach((elem, isRight) => {
elem.onclick = () => {
setSide(!!isRight);
};
});