Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't convert from HSL to RGB
    text
    copied!<p>I have a problem with converting a color from HSL to RGB. I've written the following function:</p> <pre><code>struct RGB { float r, g, b; }; RGB hslToRgb( float hue, float saturation, float lightness ) { RGB rgb1, rgbResult; float chroma = ( 1.0 - (float) abs( 2.0 * lightness - 1.0 ) ) * saturation; float h1 = hue / 60.0; float x = chroma * ( 1.0 - (float) abs( (float) ( (int) h1 % 2 ) - 1.0 ) ); if ( ( 0 &lt;= h1 ) &amp;&amp; ( h1 &lt; 1 ) ) { rgb1.r = chroma; rgb1.g = x; rgb1.b = 0.0; } else if ( ( 1 &lt;= h1 ) &amp;&amp; ( h1 &lt; 2 ) ) { rgb1.r = x; rgb1.g = chroma; rgb1.b = 0.0; } else if ( ( 2 &lt;= h1 ) &amp;&amp; ( h1 &lt; 3 ) ) { rgb1.r = 0.0; rgb1.g = chroma; rgb1.b = x; } else if ( ( 3 &lt;= h1 ) &amp;&amp; ( h1 &lt; 4 ) ) { rgb1.r = 0.0; rgb1.g = x; rgb1.b = chroma; } else if ( ( 4 &lt;= h1 ) &amp;&amp; ( h1 &lt; 5 ) ) { rgb1.r = x; rgb1.g = 0.0; rgb1.b = chroma; } else if ( ( 5 &lt;= h1 ) &amp;&amp; ( h1 &lt; 6 ) ) { rgb1.r = chroma; rgb1.g = 0; rgb1.b = x; } else { rgb1.r = 0.0; rgb1.g = 0.0; rgb1.b = 0.0; } float m = lightness - 0.5 * chroma; rgbResult.r = rgb1.r + m; rgbResult.g = rgb1.g + m; rgbResult.b = rgb1.b + m; return rgbResult; } </code></pre> <p>here it is its test:</p> <pre><code>float cHue = 0.0; while ( cHue &lt; 360 ) { RGB rgb1 = hslToRgb( (int) cHue, 1.0, 0.5 ); // max on saturation and a middle value for lightness printf( "r = %f, g = %f, b = %f\n", rgb1.r, rgb1.g, rgb1.b ); cHue += 1.0; } </code></pre> <p>but I get only 1.0 and 0.0 when I need to get all range between this "integers".</p> <pre><code>r = 1.000000, g = 0.000000, b = 1.000000 r = 1.000000, g = 0.000000, b = 1.000000 r = 1.000000, g = 0.000000, b = 1.000000 r = 1.000000, g = 0.000000, b = 0.000000 r = 1.000000, g = 0.000000, b = 0.000000 r = 1.000000, g = 0.000000, b = 0.000000 </code></pre> <p>Can anyone help me with this code? Formulas from: <a href="http://en.wikipedia.org/wiki/HSL_and_HSV" rel="nofollow">http://en.wikipedia.org/wiki/HSL_and_HSV</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload