@@ -3,55 +3,60 @@ const h = document.querySelector('h1');
3
3
const gradcolor = document . querySelector ( '#gradc' ) ;
4
4
const buttoncolor = document . querySelector ( '.brndm' )
5
5
6
+ // to generate random color
6
7
const rndmcolor = ( ) => {
7
8
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 ) ;
12
13
13
- return rgb ;
14
+ return rgb ;
14
15
}
16
+ // To Check contrast of the text
15
17
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 ;
19
21
}
20
22
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" ;
29
32
30
33
} ) ;
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 } ` ;
47
60
} ) ;
48
61
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