# Can someone explain this formula for parse RGB to HSL?

Im trying to complain the algorithm to calculate the HSL value form a RGB color. I understand the algorithm for the Hue, but im lost with the Lightness and saturation. Someone can explain why?

There is the algorithm:

$$R'=R/255$$

$$G'=G/255$$

$$B'=B/255$$

$$C_{max}=\max\left(R',G',B'\right)$$

$$C_{min}=\min\left(R',G',B'\right)$$

$$\Delta=C_{max}-C_{min}$$

Hue Calculation:

$$H = \begin{cases} 0^\circ & ,\Delta=0\\ 60^\circ \times \left(\frac{G'-B'}{\Delta}\mod 6\right) & ,C_{max}=R'\\ 60^\circ \times \left(\frac{B'-R'}{\Delta}+2\right) & ,C_{max}=G'\\ 60^\circ \times \left(\frac{R'-G'}{\Delta}+4\right) & ,C_{max}=B' \end{cases}$$

Lightness calculation:

$$L=\frac{C_{max} + C_{min}}{2}$$

Saturation calculation:

$$S=\begin{cases} 0 & ,\Delta=0\\ \frac{\Delta}{1-|2L-1|} & ,\Delta <> 0 \end{cases}$$

The division in the hue calculation generates a value between -1 and 1. If red is the maximum component, then green - blue is either the middle value minus the lower value, or it's the lower value minus the middle value. And since the middle and lower value are never greater than the maximum, dividing by the delta will always give a value between -1 and 1. Once you have a value between -1 and 1, you multiply it by 60 to get the degrees. For the other 2 cases, you add either 2 or 4 before the multiply in order to rotate it 120° or 240° for the green and blue.
Finally, it calculates the saturation. If the delta is 0, then saturation is 0. Otherwise it divides the delta by 1 - abs(2 * L - 1). Essentially, it's saying that saturation is proportional to the distance between the minimum and the maximum values and inversely proportional to the lightness's distance from 50% lightness. In other words, as you move away from the vertical center of the double hex cone, towards the pointy ends, the distance between the min and max components has a bigger effect on saturation. A difference in the delta of, say, .1 at the center will not show much difference in saturation, but when the lightness is very close to either 0 or 1, it will make a large difference in saturation.