Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's my version on how to do that (in C, sorry, but shouldn't be hard to convert, just replace the <code>int *</code>'s and <code>double *</code>'s with <code>out</code> or <code>ref</code> ints, and don't use pointer syntax)</p> <pre><code>void colorlib_hsbtorgb(double hue, double saturation, double brightness, int *red, int *green, int *blue) { if (saturation == 0) { *red = *green = *blue = brightness; } else { // the color wheel consists of 6 sectors. Figure out which sector you're in. double sectorPos = hue / 60.0; int sectorNumber = (int)(floor(sectorPos)); // get the fractional part of the sector double fractionalSector = sectorPos - sectorNumber; // calculate values for the three axes of the color. double p = brightness * (1.0 - saturation); double q = brightness * (1.0 - (saturation * fractionalSector)); double t = brightness * (1.0 - (saturation * (1 - fractionalSector))); // assign the fractional colors to r, g, and b based on the sector the angle is in. switch (sectorNumber) { case 0: *red = brightness; *green = t; *blue = p; break; case 1: *red = q; *green = brightness; *blue = p; break; case 2: *red = p; *green = brightness; *blue = t; break; case 3: *red = p; *green = q; *blue = brightness; break; case 4: *red = t; *green = p; *blue = brightness; break; case 5: *red = brightness; *green = p; *blue = q; break; } } } </code></pre> <p>RGB to hsb:</p> <pre><code>void colorlib_rgbtohsb(int red, int green, int blue, double *hue, double *saturation, double *brightness) { double dRed = red / 255; double dGreen = green / 255; double dBlue = blue / 255; double max = fmax(dRed, fmax(dGreen, dBlue)); double min = fmin(dRed, fmin(dGreen, dBlue)); double h = 0; if (max == dRed &amp;&amp; dGreen &gt;= dBlue) { h = 60 * (dGreen - dBlue) / (max - min); } else if (max == dRed &amp;&amp; dGreen &lt; dBlue) { h = 60 * (dGreen - dBlue) / (max - min) + 360; } else if (max == dGreen) { h = 60 * (dBlue - dRed) / (max - min) + 120; } else if (max == dBlue) { h = 60 * (dRed - dGreen) / (max - min) + 240; } double s = (max == 0) ? 0.0 : (1.0 - (min / max)); *hue = h; *saturation = s; *brightness = max; } </code></pre> <p>If I find my code in C#, I will edit this answer....</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