1- const colorBtn = document . getElementById ( "colorBtn" ) ;
1+ const generateBtn = document . getElementById ( "generateBtn" ) ;
2+ const copyHexBtn = document . getElementById ( "copyHexBtn" ) ;
3+ const copyRgbBtn = document . getElementById ( "copyRgbBtn" ) ;
4+ const darkModeBtn = document . getElementById ( "darkModeBtn" ) ;
5+
6+ const colorPreview = document . getElementById ( "colorPreview" ) ;
7+ const hexValue = document . getElementById ( "hexValue" ) ;
8+ const rgbValue = document . getElementById ( "rgbValue" ) ;
9+ const copyMsg = document . getElementById ( "copyMsg" ) ;
210
311function getRandomColor ( ) {
4- const random = "#" + Math . floor ( Math . random ( ) * 16777215 ) . toString ( 16 ) ;
5- return random ;
12+ const r = Math . floor ( Math . random ( ) * 256 ) ;
13+ const g = Math . floor ( Math . random ( ) * 256 ) ;
14+ const b = Math . floor ( Math . random ( ) * 256 ) ;
15+
16+ const hex = `#${ r . toString ( 16 ) . padStart ( 2 , "0" ) } ${ g
17+ . toString ( 16 )
18+ . padStart ( 2 , "0" ) } ${ b . toString ( 16 ) . padStart ( 2 , "0" ) } `;
19+
20+ return { hex, rgb : `rgb(${ r } , ${ g } , ${ b } )` } ;
21+ }
22+
23+ function generateColor ( ) {
24+ const { hex, rgb } = getRandomColor ( ) ;
25+ colorPreview . style . background = hex ;
26+ hexValue . textContent = hex ;
27+ rgbValue . textContent = rgb ;
628}
729
8- colorBtn . addEventListener ( "click" , ( ) => {
9- const color = getRandomColor ( ) ;
10- document . body . style . backgroundColor = color ;
11- document . getElementById ( "colorCode" ) . textContent = `Current Color: ${ color } ` ;
12- } ) ;
30+ function copyToClipboard ( text ) {
31+ navigator . clipboard . writeText ( text ) ;
32+ copyMsg . textContent = "Copied!" ;
33+ setTimeout ( ( ) => ( copyMsg . textContent = "" ) , 1500 ) ;
34+ }
35+
36+ generateBtn . addEventListener ( "click" , generateColor ) ;
37+ copyHexBtn . addEventListener ( "click" , ( ) => copyToClipboard ( hexValue . textContent ) ) ;
38+ copyRgbBtn . addEventListener ( "click" , ( ) => copyToClipboard ( rgbValue . textContent ) ) ;
39+
40+ darkModeBtn . addEventListener ( "click" , ( ) => {
41+ document . body . classList . toggle ( "dark-mode" ) ;
42+ } ) ;
43+
44+ // Generate a random color on page load
45+ generateColor ( ) ;
0 commit comments