Created
October 31, 2010 19:03
-
-
Save hdznrrd/656996 to your computer and use it in GitHub Desktop.
HSV to RGB (Arduino)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
As far as I can tell, there is a bug in this: f,p,q and t should all be floats instead of ints.
Checked and confirmed - f,p,q and t should all be floats. Only i should remain integer.
Thanks a lot for sharing :)
not working, change them to floats
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!