Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
isp:hsl-_rgb [2019/05/31 03:52] – [Division-less division] Igor Yefmov | isp:hsl-_rgb [2022/04/04 23:32] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== HSL->RGB ====== | ====== HSL->RGB ====== | ||
- | The text below is based on an Excel workbook attached to this page: {{ :isp:hsl-rgb.xlsm |}}. That Excel file provides a sort of playground where you can try various input data and see the final results, both " | + | The text below is based on a {{ https://docs.google.com/ |
===== Preface ===== | ===== Preface ===== | ||
Much is written and is available on the color space conversion from HSL to RGB (for example this [[https:// | Much is written and is available on the color space conversion from HSL to RGB (for example this [[https:// | ||
Line 20: | Line 20: | ||
\[\frac{1}{x} = \frac{1*N}{x*N}\] | \[\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: | 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) \gg Z]\] | + | \[\frac{C}{x} = C*\frac{1}{x} = C*\frac{1*N}{x*N}\Bigg|_{\{x*N=2^Z\}} = C*\frac{N}{2^Z} = [(C*N) \gg 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. | 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. | ||
Line 58: | Line 58: | ||
\[(R, | \[(R, | ||
- | ===== Step-by-step algorithm ===== | + | ===== Step-by-step algorithm |
Armed with the above information we can now compile the necessary sequence of calculations and format it into an easy-to-use table: | Armed with the above information we can now compile the necessary sequence of calculations and format it into an easy-to-use table: | ||
Line 83: | Line 83: | ||
| 14 | \[m\] | \[L - C/2\] | '' | | 14 | \[m\] | \[L - C/2\] | '' | ||
| 15 | \[(R,G,B)\] | \[(R_1+m, | | 15 | \[(R,G,B)\] | \[(R_1+m, | ||
+ | |||
+ | |||
+ | ===== Step-by-step algorithm (10x3 bit channel, 30-bit RGB) ===== | ||
+ | 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 excerpt from Wikipedia | ||
+ | | 1 | \[H\] | \[hue(pixel)\] | '' | ||
+ | | 2 | \[S\] | \[sat(pixel)\] | '' | ||
+ | | 3 | \[L\] | \[luma(pixel)\] | '' | ||
+ | | 4 | \[h\] | \[(H+360^\circ) \bmod 360^\circ\] | '' | ||
+ | | 5 | \[L^\prime\] | \[1-|2L-1|\] | '' | ||
+ | | 6 | \[C\] | \[L^\prime \times S\] | '' | ||
+ | | 7 | \[H^\prime\] | \[\frac{H}{60^\circ}\] | '' | ||
+ | | 8 | \[H^\prime_2\] | \[H^\prime \bmod 2\] | '' | ||
+ | | 9 | \[H^\prime_{2-1}\] | \[H^\prime \bmod 2 - 1\] | '' | ||
+ | | 11 | \[H^\prime_{final}\] | \[1-|H^\prime \bmod 2 - 1|\] | '' | ||
+ | | 12 | \[X\] | \[C \times H^\prime_{final}\] | '' | ||
+ | | 13 | \[(R_1, | ||
+ | /*(C,X,0)*/ | ||
+ | }else if(Hp < 745472*2){ | ||
+ | /*(X,C,0)*/ | ||
+ | }// | ||
+ | | 14 | \[m\] | \[L - C/2\] | '' | ||
+ | | 15 | \[(R,G,B)\] | \[(R_1+m, | ||