User Tools

This is an old revision of the document!


HSL->RGB

The text below is based on an Excel workbook attached to this page: hsl-rgb.xlsm. That Excel file provides a sort of playground where you can try various input data and see the final results, both “precise” and “optimized”, as well as the discrepancy between the two methods.

Preface

Much is written and is available on the color space conversion from HSL to RGB (for example this Wikipedia article). However not only this is not meant to be used with integer arithmetics but is also very costly due to division operations. And once you realize that a 4K video running at 60FPS needs to do this conversion half a billion times a second(!) you see why this needs to be heavily optimized ;-)

The below article details the way to perform this conversion without the use of division operations with high enough precision as to satisfy the imaging pipeline quality requirements for the SUB2r camera based on Artix-7 100T FPGA.

Setup and constraints

The HSL pixels used in SUB2r's imaging pipeline have the following data width components:

  • \(H\) (hue) is a signed 14-bit field that maps into \([-180°..+180°]\) range
  • \(S\) (saturation) is an 8-bit unsigned value which maps into \([0\%..100\%]\) range
  • \(L\) (luminosity) is a 9-bit unsigned value that maps into \([0\%..100\%]\) range just like the \(saturation\) above

The output of this conversion is an \(RGB\) triplet 24-bits wide with 8 bits unsigned value per color channel in range \([0..255]\)

Division-less division

Of course it is mathematically impossible to divide without performing a division. Yet any division by \(x\) in math is equivalent to a multiplication by an inverse \(^1/_x\) value and provided there exists a trick to “cheaply” calculate the value of \(^1/_x\) with sufficient precision it is possible to optimize divisions for a predefined value of a divisor.

Observing that for any value of \(x\) the following is true: \[\frac{1}{x} = \frac{1*N}{x*N}\] and choosing \(N\) such that \(x*N\) is a whole power of \(2\) we have an optimization where a division is replaced by a pair of a multiplication followed by a (super cheap!) bit-shift operation by \(Z\) bits: \[\frac{C}{x} = C*\frac{1}{x} = C*\frac{1*N}{x*N} = C*\frac{N}{2^Z} = [(C*N)>>Z]\] The value \(Z\) depends on the needed precision and, of course, the higher the \(Z\) the less precision loss there will be in the end.

For example, to divide by \(60\) using a “complementary” precision of \(14\) bit: \[60*N = 2^{14} \implies N = \frac{2^{14}}{60} = 273.06(6) \approx 273\] \[\frac{C}{60} \approx C*\frac{273}{2^{14}}\]

The last part, losing the 14 LSB, doesn't have to be done right away and with the goal of preserving as much precision as possible this operation can be postponed until later time (just need to remember that the “real” value is now augmented by that \(2^{14}\) factor and adjust accordingly).

Step-by-step algorithm

Precise calculations

As a refresher this is how Wikipedia lists the way to convert HSL to RGB:

Given a color with hue \(H ∈ [0°, 360°]\), saturation \(S_{HSL} ∈ [0, 1]\), and lightness \(L ∈ [0, 1]\), we first find chroma: \[ C = (1 - \left\vert 2 L - 1 \right\vert) \times S_{HSL} \]

Then we can find a point \((R_1, G_1, B_1)\) along the bottom three faces of the RGB cube, with the same hue and chroma as our color (using the intermediate value \(X\) for the second largest component of this color): \[ H^\prime = \frac{H}{60^\circ} \] \[ X = C \times (1 - |H^\prime \bmod 2 - 1|) \] \[ (R_1, G_1, B_1) = \begin{cases} (0, 0, 0) &\text{if } H \text{ is undefined} \\ (C, X, 0) &\text{if } 0 \leq H^\prime \leq 1 \\ (X, C, 0) &\text{if } 1 \leq H^\prime \leq 2 \\ (0, C, X) &\text{if } 2 \leq H^\prime \leq 3 \\ (0, X, C) &\text{if } 3 \leq H^\prime \leq 4 \\ (X, 0, C) &\text{if } 4 \leq H^\prime \leq 5 \\ (C, 0, X) &\text{if } 5 \leq H^\prime \leq 6 \end{cases} \]

Overlap (when \(H^\prime\) is an integer) occurs because two ways to calculate the value are equivalent: \(X = 0\) or \(X = C\), as appropriate.

Finally, we can find \(R, G, B\) by adding the same amount to each component, to match lightness:

\[m=L-C/2\] \[(R,G,B)=(R_1+m,G_1+m,B_1+m)\]

Detailed algorithm

Armed with the above information we can now compile the necessary sequence of calculations and format it into an easy-to-use table:

# name math bits range C-like code factor1)
1 \(H\) \[\] 14 signed \(-8192..+8191\) 2)
H = pixel.hue();
\(\frac{180}{2^{13}}\)
2 \(S\) \[\] \(\)

\(\)
3 \(L\) \[\] \(\)

\(\)
4 \(\) \[\] \(\)

\(\)
5 \(\) \[\] \(\)

\(\)
1)
multiply by that much to get the “real” value
2)
mapped into \(-180^\circ..+180^\circ\)

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information