The code creates a simple colour converter webpage. The user enters a hexadecimal colour such as #ff0000, and the JavaScript automatically converts it into RGB and HSL values. The page also includes copy buttons so each colour value can be copied to the clipboard.
The HTML provides the structure of the page. The .colorWrapper contains the heading and three text inputs for HEX, RGB, and HSL values. The HEX input starts with #000000, while the RGB and HSL inputs start with their corresponding black-colour values. Each input has a copy button containing a Font Awesome copy icon. The page also imports Google Fonts, Font Awesome, jQuery, and the external styles.css stylesheet.
The CSS controls the appearance and layout. The body uses Flexbox to centre the converter on the page, while .colorWrapper uses CSS Grid to arrange the colour fields and buttons. The inputs are styled with large text, padding, borders, and rounded corners. The heading uses the imported pixel-style font and various visual effects. The wrapper initially has a black background, but the JavaScript later changes this background to whatever colour the user enters.
The main part of the JavaScript listens for changes to the HEX input. When the user enters a colour, the code removes the #, expands three-character HEX codes such as #f00 into six-character codes, and converts each pair of hexadecimal digits into a decimal RGB value. For example, #ff0000 becomes rgb(255,0,0). The resulting RGB value is then displayed in the RGB input.
The rgbToHsl() function converts the RGB values into HSL. It first normalises the RGB values to a range between 0 and 1, determines the maximum and minimum components, and then calculates the hue, saturation, and lightness using the standard RGB-to-HSL conversion formula. The result is placed in the HSL input, for example hsl(0,100%,50%) for red.
After conversion, the script changes the .colorWrapper background using the generated RGB value. This means that entering a different HEX colour should immediately change the converter's background to that colour. There is also code intended to change the heading colour.
The copy-to-clipboard functionality finds all elements with the .trace-btn class and all inputs with the .color-selected class. When a copy button is clicked, its position is used to find the corresponding input. The input's value is then copied using the browser's modern navigator.clipboard.writeText() API, followed by an alert confirming that the value was copied.
To use the converter, save the HTML as something such as index.html, place the CSS in styles.css, and add the JavaScript either inside a <script> element or in a separate .js file. Open the HTML file in a modern web browser, enter a HEX colour such as #3498db into the first field, and the RGB and HSL fields should update automatically. You can then click the copy button beside any value to copy it.