Skip to content

Commit 917fc05

Browse files
authored
Update randomcolor.js
1 parent 0b788d5 commit 917fc05

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

Random Color Generator/randomcolor.js

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,60 @@ const h = document.querySelector('h1');
33
const gradcolor = document.querySelector('#gradc');
44
const buttoncolor = document.querySelector('.brndm')
55

6+
// to generate random color
67
const rndmcolor = () => {
78

8-
let rgb = [1, 1, 1];
9-
rgb[0] = Math.floor(Math.random() * 255);
10-
rgb[1] = Math.floor(Math.random() * 255);
11-
rgb[2] = Math.floor(Math.random() * 255);
9+
let rgb = [1, 1, 1];
10+
rgb[0] = Math.floor(Math.random() * 255);
11+
rgb[1] = Math.floor(Math.random() * 255);
12+
rgb[2] = Math.floor(Math.random() * 255);
1213

13-
return rgb;
14+
return rgb;
1415
}
16+
// To Check contrast of the text
1517
const checkContrast = (tcolor) => {
16-
const textc = Math.floor(((parseInt(tcolor[0]) * 255) + (parseInt(tcolor[1]) * 255) + (parseInt(tcolor[2]) * 255)) / 765);
17-
const textColour = (textc > 96) ? 'black' : 'white';
18-
return textColour;
18+
const textc = Math.floor(((parseInt(tcolor[0]) * 255) + (parseInt(tcolor[1]) * 255) + (parseInt(tcolor[2]) * 255)) / 765);
19+
const textColour = (textc > 96) ? 'black' : 'white';
20+
return textColour;
1921
}
2022

21-
colour.addEventListener('click', function () {
22-
const tempcolor = rndmcolor();
23-
const newcolor = `rgb(${tempcolor[0]},${tempcolor[1]},${tempcolor[2]})`;
24-
const textcolor = checkContrast(tempcolor);
25-
document.body.style.background = newcolor;
26-
h.style.color = textcolor;
27-
h.innerText = `THE BACKGROUND COLOR IS: ${newcolor}`;
28-
buttoncolor.style.background = "white";
23+
//For Random Background color
24+
colour.addEventListener('click', function() {
25+
const tempcolor = rndmcolor();
26+
const newcolor = `rgb(${tempcolor[0]},${tempcolor[1]},${tempcolor[2]})`;
27+
const textcolor = checkContrast(tempcolor);
28+
document.body.style.background = newcolor;
29+
h.style.color = textcolor;
30+
h.innerText = `THE BACKGROUND COLOR IS: ${newcolor}`;
31+
buttoncolor.style.background = "white";
2932

3033
});
31-
32-
gradcolor.addEventListener('click', function () {
33-
const tempcolor = rndmcolor();
34-
const tempcolor2 = rndmcolor();
35-
const newcolor = `rgb(${tempcolor[0]},${tempcolor[1]},${tempcolor[2]})`;
36-
const newcolorr2 = `rgb(${tempcolor2[0]},${tempcolor2[1]},${tempcolor2[2]})`;
37-
38-
document.body.style.background = "linear-gradient(to right, " +
39-
newcolor +
40-
", " +
41-
newcolorr2 +
42-
")";
43-
44-
h.innerText = `THE GRADIENT COLOR IS: ${newcolor},${newcolorr2}`;
45-
h.style.color = "black";
46-
buttoncolor.style.background = "white";
34+
//For Random gradient background
35+
gradcolor.addEventListener('click', function() {
36+
const tempcolor = rndmcolor();
37+
const tempcolor2 = rndmcolor();
38+
const newcolor = `rgb(${tempcolor[0]},${tempcolor[1]},${tempcolor[2]})`;
39+
const newcolorr2 = `rgb(${tempcolor2[0]},${tempcolor2[1]},${tempcolor2[2]})`;
40+
41+
document.body.style.background = "linear-gradient(to right, " +
42+
newcolor +
43+
", " +
44+
newcolorr2 +
45+
")";
46+
47+
h.innerText = `THE GRADIENT COLOR IS: ${newcolor},${newcolorr2}`;
48+
h.style.color = "black";
49+
buttoncolor.style.background = "white";
50+
});
51+
// For random button background
52+
buttoncolor.addEventListener('click', function() {
53+
const tempcolor = rndmcolor();
54+
const newcolor = `rgb(${tempcolor[0]},${tempcolor[1]},${tempcolor[2]})`;
55+
const contrast = checkContrast(tempcolor);
56+
document.body.style.background = contrast;
57+
buttoncolor.style.background = newcolor;
58+
h.style.color = newcolor;
59+
h.innerText = `THE BUTTON BACKGROUND COLOR IS: ${newcolor}`;
4760
});
4861

49-
buttoncolor.addEventListener('click', function () {
50-
const tempcolor = rndmcolor();
51-
const newcolor = `rgb(${tempcolor[0]},${tempcolor[1]},${tempcolor[2]})`;
52-
const contrast = checkContrast(tempcolor);
53-
document.body.style.background = contrast;
54-
buttoncolor.style.background = newcolor;
55-
h.style.color = newcolor;
56-
h.innerText = `THE BUTTON BACKGROUND COLOR IS: ${newcolor}`;
57-
});
62+

0 commit comments

Comments
 (0)