-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (96 loc) · 2.93 KB
/
script.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
105
106
107
108
109
110
111
112
113
114
115
116
var css = document.querySelector(".gradientText");
var color1 = document.querySelector(".color1");
var color2 = document.querySelector(".color2");
var body = document.getElementById("gradient");
var random = document.querySelector("button");
var output = document.querySelector(".sliderValue");
var slider = document.querySelector(".slider");
var list = document.querySelector("ul");
var showHistory = document.querySelector("h4");
var historyCount = 0;
var show = false;
output.textContent = "Deg: " + slider.value
setGradient()
function setBackground(color1, color2) {
body.style.background =
"linear-gradient(" + slider.value + "deg, "
+ color1
+ ", "
+ color2
+ ")";
}
function setGradient() {
setBackground(color1.value, color2.value)
css.textContent = body.style.background + ";";
}
color1.addEventListener("input", setGradient);
color2.addEventListener("input", setGradient);
function generateNumber() {
return Math.floor(Math.random() * 256);
}
function generateColor() {
var red = generateNumber();
var green = generateNumber();
var blue = generateNumber();
return rgbToHex(red, green, blue);
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function restoreState (degrees, firstColor, secondColor, background) {
slider.value = degrees;
color1.value = firstColor;
color2.value = secondColor;
body.style.background = background;
output.textContent = "Deg: " + degrees;
css.textContent = background + ";";
}
function addToHistory() {
var li = document.createElement("li");
li.style.background = body.style.background;
var degrees = slider.value;
var firstColor = color1.value;
var secondColor = color2.value;
var background = body.style.background;
li.addEventListener("click", function() {
restoreState(degrees, firstColor, secondColor, background);
})
list.insertBefore(li, list.firstChild);
}
function setRandomColors() {
var firstColor = generateColor();
var secondColor = generateColor();
var degrees = Math.floor(Math.random()*360)
slider.value = degrees;
output.textContent = "Deg: " + slider.value;
setBackground(firstColor, secondColor);
color1.value = firstColor;
color2.value = secondColor;
css.textContent = body.style.background + ";";
if (historyCount < 10) {
historyCount++;
addToHistory();
} else {
list.removeChild(list.childNodes[list.childNodes.length-1])
addToHistory();
}
}
function setSlider() {
output.textContent = "Deg: " + slider.value;
setBackground(color1.value, color2.value);
css.textContent = body.style.background + ";";
}
random.addEventListener("click", setRandomColors)
slider.addEventListener("input", setSlider)
showHistory.addEventListener("click", function() {
if (show) {
this.textContent = "Show Random Color History";
} else {
this.textContent = "Hide Random Color History";
}
show = !show;
})