@@ -3,29 +3,67 @@ const getColorSchemeBtn = document.getElementById("get-color-scheme");
33const mainEl = document . getElementById ( "container" ) ;
44
55
6+ const color0El = document . getElementById ( "color-0" ) ;
7+ const color1El = document . getElementById ( "color-1" ) ;
8+ const color2El = document . getElementById ( "color-2" ) ;
9+ const color3El = document . getElementById ( "color-3" ) ;
10+ const color4El = document . getElementById ( "color-4" ) ;
11+
12+ const color0NameEl = document . getElementById ( "color-name-0" ) ;
13+ const color1NameEl = document . getElementById ( "color-name-1" ) ;
14+ const color2NameEl = document . getElementById ( "color-name-2" ) ;
15+ const color3NameEl = document . getElementById ( "color-name-3" ) ;
16+ const color4NameEl = document . getElementById ( "color-name-4" ) ;
17+
618function fetchColors ( ) {
719
8- let mainHTML = '' ;
920 const mode = document . getElementById ( "mode" ) . value ;
1021 const cleanHex = colorPickerEl . value . split ( '#' ) [ 1 ] ;
1122 const url = `https://www.thecolorapi.com/scheme?hex=${ cleanHex } &mode=${ mode . toLowerCase ( ) } ` ;
1223
1324 fetch ( url , { method : "GET" } )
1425 . then ( res => res . json ( ) )
1526 . then ( data => {
16- data . colors . forEach ( element => {
17- const color = element . hex . value ;
18- mainHTML += `
19- <div style="background-color: ${ color } ;">
20- <p> ${ color } </p>
21- </div>
22- ` ;
23- } ) ;
24- mainEl . innerHTML = mainHTML ;
27+ for ( let i = 0 ; i < data . colors . length ; i ++ ) {
28+ const color = data . colors [ i ] . hex . value ;
29+
30+ const colorEl = document . getElementById ( `color- ${ i } ` ) ;
31+ colorEl . style . backgroundColor = color ;
32+
33+ const colorNameEl = document . getElementById ( `color-name- ${ i } ` ) ;
34+ colorNameEl . textContent = color ;
35+ }
2536 } ) ;
2637}
2738
39+ function copy ( color ) {
40+ navigator . clipboard . writeText ( color ) ;
41+ alert ( `Copied ${ color } to clipboard` ) ;
42+ }
2843
29- getColorSchemeBtn . addEventListener ( "click" , fetchColors ) ;
44+ function copyColorfromContainer ( event ) {
45+ const color = event . target . style . backgroundColor ;
46+ console . log ( color ) ;
47+ copy ( color ) ;
48+ }
3049
50+ function copyColorfromName ( event ) {
51+ const color = event . target . textContent ;
52+ copy ( color ) ;
53+ }
54+
55+
56+ color0El . addEventListener ( "click" , ( e ) => { copyColorfromContainer ( e ) ; } ) ;
57+ color1El . addEventListener ( "click" , ( e ) => { copyColorfromContainer ( e ) ; } ) ;
58+ color2El . addEventListener ( "click" , ( e ) => { copyColorfromContainer ( e ) ; } ) ;
59+ color3El . addEventListener ( "click" , ( e ) => { copyColorfromContainer ( e ) ; } ) ;
60+ color4El . addEventListener ( "click" , ( e ) => { copyColorfromContainer ( e ) ; } ) ;
61+
62+ color0NameEl . addEventListener ( "click" , ( e ) => { copyColorfromName ( e ) ; } ) ;
63+ color1NameEl . addEventListener ( "click" , ( e ) => { copyColorfromName ( e ) ; } ) ;
64+ color2NameEl . addEventListener ( "click" , ( e ) => { copyColorfromName ( e ) ; } ) ;
65+ color3NameEl . addEventListener ( "click" , ( e ) => { copyColorfromName ( e ) ; } ) ;
66+ color4NameEl . addEventListener ( "click" , ( e ) => { copyColorfromName ( e ) ; } ) ;
67+
68+ getColorSchemeBtn . addEventListener ( "click" , fetchColors ) ;
3169fetchColors ( ) ;
0 commit comments