Skip to content

Instantly share code, notes, and snippets.

@tai2
Created May 5, 2013 07:37
Show Gist options
  • Select an option

  • Save tai2/5520037 to your computer and use it in GitHub Desktop.

Select an option

Save tai2/5520037 to your computer and use it in GitHub Desktop.
HSV to RGV conversion
void
hsv_to_rgb(double h, double s, double v, double *r, double *g, double *b):
if (s * v == 0) {
*r = *g = *b = v;
} else {
hi = ((int)(floor(3 * h / M_PI))) % 6
f = 3 * h / M_PI - hi;
p = v * (1.0 - s)
q = v * (1.0 - f * s)
t = v * (1.0 - (1.0 - f) * s)
if (hi == 0) {
*r = v;
*g = t;
*b = p;
} else if (hi == 1) {
*r = q;
*g = v;
*b = p;
} else if (hi == 2) {
*r = p;
*g = v;
*b = t;
} else if (hi == 3) {
*r = p;
*g = q;
*b = v;
} else if (hi == 4) {
*r = t;
*g = p;
*b = v;
} else {
*r = v;
*g = p;
*b = q;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment