How the HEX to HSL conversion works
Read more
The conversion goes through RGB. First the HEX pairs become red, green, and blue channels from 0 to 255, which are scaled down to the 0-1 range. Call the largest channel max and the smallest min. Lightness is their midpoint: (max + min) / 2. Saturation measures how far apart they are, scaled so it stays meaningful at every lightness: (max - min) / (1 - |2L - 1|). Hue depends on which channel is the max; the formula measures how far around the 360-degree wheel the color sits, with red at 0, green at 120, and blue at 240.
Worked example: #34d399 has channels 52, 211, and 153, or 0.204, 0.827, and 0.6 after scaling. Lightness is (0.827 + 0.204) / 2, about 52%. The range is 0.624, giving saturation of about 64%. Green is the dominant channel and the blue side pulls slightly ahead of red, landing the hue at 158 degrees, so the result is hsl(158, 64%, 52%). The reverse conversion rebuilds RGB from the hue angle and then formats the channels back into base-16 pairs.