-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (57 loc) · 1.86 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
const red = document.querySelector("#red");
const green = document.querySelector("#green");
const blue = document.querySelector("#blue");
const body = document.querySelector("body");
const rgb = document.querySelector("#rgb-code"); //change with .innerText
const btnRandomColor = document.getElementById("random-color-btn");
//
red.addEventListener("input", changeBackground);
green.addEventListener("input", changeBackground);
blue.addEventListener("input", changeBackground);
red.addEventListener("input", changeCode);
green.addEventListener("input", changeCode);
blue.addEventListener("input", changeCode);
changeCode();
changeBackground();
// changing the innerHTML in rgb-code
function changeCode() {
const redRange = red.value;
const greenRange = green.value;
const blueRange = blue.value;
rgb.textContent =
"rgb(" + redRange + ", " + greenRange + ", " + blueRange + ")";
}
// changing the background
function changeBackground() {
const redRange = red.value;
const greenRange = green.value;
const blueRange = blue.value;
document.body.style.setProperty(
"background-color",
"rgb(" + redRange + ", " + greenRange + ", " + blueRange + ")"
);
}
// random color function
function randomColor() {
const colorData = fetch("https://dummy-apis.netlify.app/api/color");
colorData
.then((response) => {
console.log(colorData);
return response.json();
})
.then((data) => {
// changeBackground();
console.log(data);
document.body.style.setProperty(
"background-color",
"rgb(" + data.rgb.r + ", " + data.rgb.g + ", " + data.rgb.b + ")"
);
rgb.innerHTML =
"rgb(" + data.rgb.r + ", " + data.rgb.g + ", " + data.rgb.b + ")";
red.value = data.rgb.r;
green.value = data.rgb.g;
blue.value = data.rgb.b;
});
}
// event listener
btnRandomColor.addEventListener("click", randomColor);