@@ -25,6 +25,7 @@ description: "This tool creates harmonious palettes for website designs and fron
2525 <input type =" number " id =" hue " value =" 0 " min =" 0 " max =" 360 " style =" font-size :1.2em " >
2626 <button onclick =" increment (' hue' )" style =" font-size :1.2em " >▲</button >
2727 <button onclick =" decrement (' hue' )" style =" font-size :1.2em " >▼</button >
28+ <input type =" color " value =" #ff0000 " id =" rawColorInput " />
2829 <br >
2930
3031 <div id =" colorSquare " class =" colorSquare " ></div >
@@ -45,6 +46,8 @@ description: "This tool creates harmonious palettes for website designs and fron
4546 const accentColorSquare = document .getElementById (' accentColorSquare' );
4647 const darkColorSquare = document .getElementById (' darkColorSquare' );
4748 const hueInput = document .getElementById (' hue' );
49+ const colorPicker = document .getElementById (' rawColorInput' );
50+
4851
4952 function updateColor () {
5053 const hue = hueInput .value ;
@@ -85,6 +88,70 @@ description: "This tool creates harmonious palettes for website designs and fron
8588 }
8689 }
8790
91+ function hexToHSL (hex ) {
92+ // Remove # if present
93+ hex = hex .replace (/ ^ #/ , ' ' );
94+
95+ // Parse the hex values
96+ let r, g, b;
97+ if (hex .length === 3 ) {
98+ r = parseInt (hex[0 ] + hex[0 ], 16 ) / 255 ;
99+ g = parseInt (hex[1 ] + hex[1 ], 16 ) / 255 ;
100+ b = parseInt (hex[2 ] + hex[2 ], 16 ) / 255 ;
101+ } else {
102+ r = parseInt (hex .substr (0 , 2 ), 16 ) / 255 ;
103+ g = parseInt (hex .substr (2 , 2 ), 16 ) / 255 ;
104+ b = parseInt (hex .substr (4 , 2 ), 16 ) / 255 ;
105+ }
106+
107+ // Find min and max
108+ const max = Math .max (r, g, b);
109+ const min = Math .min (r, g, b);
110+ let h, s, l = (max + min) / 2 ;
111+
112+ if (max === min) {
113+ // Achromatic
114+ h = s = 0 ;
115+ } else {
116+ const d = max - min;
117+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
118+
119+ switch (max) {
120+ case r: h = (g - b) / d + (g < b ? 6 : 0 ); break ;
121+ case g: h = (b - r) / d + 2 ; break ;
122+ case b: h = (r - g) / d + 4 ; break ;
123+ }
124+ h /= 6 ;
125+ }
126+
127+ // Convert to degrees and percentages
128+ return {
129+ h: Math .round (h * 360 ),
130+ s: Math .round (s * 100 ),
131+ l: Math .round (l * 100 )
132+ };
133+ }
134+
135+ function colorPickerUpdate () {
136+ const baseColor = colorPicker .value ;
137+ const {h , s , l } = hexToHSL (colorPicker .value );
138+
139+ const darkColor = ` hsl(${ h} , 80%, ${ l < 50 ? 10 : 90 } %)` ;
140+ const lightColor = ` hsl(${ h} , 70%, ${ l < 50 ? 90 : 20 } %)` ;
141+
142+ colorSquare .style .backgroundColor = baseColor;
143+ darkColorSquare .style .backgroundColor = darkColor;
144+ accentColorSquare .style .backgroundColor = lightColor;
145+
146+ const theDiv = document .getElementById (' theDiv' );
147+ const theH2 = document .getElementById (' theH2' );
148+ const theP = document .getElementById (' theP' );
149+ theDiv .style .backgroundColor = lightColor;
150+ theH2 .style .color = baseColor;
151+ theP .style .color = darkColor;
152+ }
153+
88154 hueInput .addEventListener (' change' , updateColor);
155+ colorPicker .addEventListener (' change' , colorPickerUpdate, false );
89156 updateColor ();
90157 </script >
0 commit comments