Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

ilyakh commented Mar 17, 2013

Thanks!

@noouch

This comment has been minimized.

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

This comment has been minimized.

Copy link

oberling commented Feb 18, 2015

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

@phil123456

This comment has been minimized.

Copy link

phil123456 commented Sep 22, 2018

not working, change them to floats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.