Skip to content

Instantly share code, notes, and snippets.

@pentiumao
Created June 3, 2014 11:10
Show Gist options
  • Save pentiumao/bca410767547741839d2 to your computer and use it in GitHub Desktop.
Save pentiumao/bca410767547741839d2 to your computer and use it in GitHub Desktop.
HSV to RGB conversion function
/* HSV to RGB conversion function with only integer
* math */
void
hsvtorgb(unsigned char *r, unsigned char *g, unsigned char *b, unsigned char h, unsigned char s, unsigned char v)
{
unsigned char region, fpart, p, q, t;
if(s == 0) {
/* color is grayscale */
*r = *g = *b = v;
return;
}
/* make hue 0-5 */
region = h / 43;
/* find remainder part, make it from 0-255 */
fpart = (h - (region * 43)) * 6;
/* calculate temp vars, doing integer multiplication */
p = (v * (255 - s)) >> 8;
q = (v * (255 - ((s * fpart) >> 8))) >> 8;
t = (v * (255 - ((s * (255 - fpart)) >> 8))) >> 8;
/* assign temp vars based on color cone region */
switch(region) {
case 0:
*r = v; *g = t; *b = p; break;
case 1:
*r = q; *g = v; *b = p; break;
case 2:
*r = p; *g = v; *b = t; break;
case 3:
*r = p; *g = q; *b = v; break;
case 4:
*r = t; *g = p; *b = v; break;
default:
*r = v; *g = p; *b = q; break;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment