Recently my teacher Almir Olivette Artero, gave us a work to convert RGB to HSL or viceversa to start to learn a little about CG.
Then, I decided to share here how I did the calculation and implementation.
Notice that it is possible to visualize with the result the preview of the color.
Lastly, I will post in another post, how to manipulate images showing all code used.
HSL to RGB
Second Wikipedia, HSL are the two most common cylindricalcoordinate representations of points in an RGB color model. The representation rearrange the geometry of RGB in an attempt to be more intuitive and perceptually relevant than the cartesian (cube) representation. Developed in the 1970s for computer graphics applications, HSL are used today in color pickers, in image editing software, and less commonly in image analysis and computer vision.
HSL stands hue, saturation and lightness, where hue is defined by angular value between 0° and 360°. Starting by red at 0°, passing through the green primary at 120° and the blue primary at 240°, and then going back to red at 360°. Another two properties saturation and lightness is defined between 0% and 100%, where lightness from black to white and saturation is about gray colors scales to the definitive color.
To calculate, we used the formula where hue is defined 0 ≤ H < 360, saturation 0 ≤ S ≤ 1 and lightness 0 ≤ L ≤ 1 values.
Then we defined the following variables:
C = (1  2L  1) × S
X = C × (1  (H / 60°) mod 2  1)
m = L  C/2
Finally, with this variables, we can do the following calculation to get RGB through R’G’B’ values:
(R,G,B) = ((R’+m)×255, (G’+m)×255,(B’+m)×255)
Using this formula, we can test with the form below implemented with Javascript:
Algorithm Implemented in Javascript
Obs: the code already verify input values :)


RGB to HSL
Continuing with RGB to HSL, second Wikipédia, The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green and blue. The main purpose of the RGB color model is for the sensing, representation and display of images in electronic systems, such as televisions and computers, though it has also been used in conventional photography. Before the electronic age, the RGB color model already had a solid theory behind it, based in human perception of colors.
The intensity each color is defined with values between 0 and 255. For quick example, if we want the pure red, we can define in RGB: (255, 0, 0).
So, let’s start with the math formula implemented in JavaScript:
Variables:
R’ = R/255
G’ = G/255
B’ = B/255
Cmax = max(R’, G’, B’)
Cmin = min(R’, G’, B’)
Δ = Cmax  Cmin
With these variables, we used the following formula to get Hue, Saturation and Lightness respectly:
Hue
Saturation
Lightness
L = (Cmax + Cmin) / 2
And now, we can test this formula:
Algorithm Implemented in Javascript
Obs: the code already verify input values :)


Conclusion
The two color systems have come to revolutionize color representation in the electronic age. It’s pretty excity to learn about how the colors in the human eye works and how we can represent images in pixels, by mixing colors represented by bits. I really hope to post more things about CG in this blog. If you liked this post, found some error or something I can do better, please comment ! :)