Skip to content

Instantly share code, notes, and snippets.

@hdznrrd
Created October 31, 2010 19:03
Show Gist options
  • Save hdznrrd/656996 to your computer and use it in GitHub Desktop.
Save hdznrrd/656996 to your computer and use it in GitHub Desktop.
HSV to RGB (Arduino)
void HSV_to_RGB(float h, float s, float v, byte *r, byte *g, byte *b)
{
int i,f,p,q,t;
h = max(0.0, min(360.0, h));
s = max(0.0, min(100.0, s));
v = max(0.0, min(100.0, v));
s /= 100;
v /= 100;
if(s == 0) {
// Achromatic (grey)
*r = *g = *b = round(v*255);
return;
}
h /= 60; // sector 0 to 5
i = floor(h);
f = h - i; // factorial part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch(i) {
case 0:
*r = round(255*v);
*g = round(255*t);
*b = round(255*p);
break;
case 1:
*r = round(255*q);
*g = round(255*v);
*b = round(255*p);
break;
case 2:
*r = round(255*p);
*g = round(255*v);
*b = round(255*t);
break;
case 3:
*r = round(255*p);
*g = round(255*q);
*b = round(255*v);
break;
case 4:
*r = round(255*t);
*g = round(255*p);
*b = round(255*v);
break;
default: // case 5:
*r = round(255*v);
*g = round(255*p);
*b = round(255*q);
}
}
@ilyakh
Copy link

ilyakh commented Mar 17, 2013

Thanks!

@noouch
Copy link

noouch commented Aug 12, 2013

As far as I can tell, there is a bug in this: f,p,q and t should all be floats instead of ints.

@oberling
Copy link

Checked and confirmed - f,p,q and t should all be floats. Only i should remain integer.
Thanks a lot for sharing :)

@phil123456
Copy link

not working, change them to floats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment