Converting a color from the cylindrical HSL color space into the cubic RGB color space requires a multi-step mathematical transformation. The process maps an angular Hue coordinate (H∈[0,360∘]) along with percentage values for Saturation (S∈[0,1]) and Lightness (L∈[0,1]) into absolute red, green, and blue intensities (R,G,B∈[0,255]).
1. The Core Color Space Math
First, we find the overall colorfulness factor, known as Chroma (C):
C=(1−∣2L−1∣)×S
Next, we identify the position of the intermediate variable (X) along the secondary components of the color cylinder:
X=C×(1−∣(H/60∘)mod2−1∣)
We calculate the lightness alignment offset factor (m) to anchor the output values within true color space limits:
m=L−2C
Based on the sector position of the Hue angle (H), intermediate variables (R′,G′,B′) are assigned:
- 0∘≤H<60∘: (R′,G′,B′)=(C,X,0)
- 60∘≤H<120∘: (R′,G′,B′)=(X,C,0)
- 120∘≤H<180∘: (R′,G′,B′)=(0,C,X)
- 180∘≤H<240∘: (R′,G′,B′)=(0,X,C)
- 240∘≤H<300∘: (R′,G′,B′)=(X,0,C)
- 300∘≤H<360∘: (R′,G′,B′)=(C,0,X)
Finally, the values are scaled to an 8-bit channel width ([0,255]):
R=round((R′+m)×255)
G=round((G′+m)×255)
B=round((B′+m)×255)
2. Hexadecimal String Encoding
To create a standard string like #1473E6, each decimal channel value (R,G,B) is converted into its base-16 equivalent. Each value is padded with a leading zero if necessary to guarantee a two-character width, then concatenated into a standard web code. When an alpha opacity component is present, it is mapped from an interval of [0.0,1.0] to [0,255], converted to hexadecimal, and appended as an 8-character string (#RRGGBBAA).
3. Calculating WCAG Contrast Ratings
Per standard web accessibility guidelines, the program checks compliance ratios using Relative Luminance (Y):
Y=0.2126×Rlinear+0.7152×Glinear+0.0722×Blinear
Where individual channel transformations remove gamma correction:
If V≤0.03928,Vlinear=12.92VelseVlinear=(1.055V+0.055)2.4
The contrast ratio is calculated by comparing the relative luminance values of the foreground (L1) and background (L2):
Contrast Ratio=L2+0.05L1+0.05
This ratio is evaluated against WCAG targets to determine passing or failing marks for both regular text and display headers.