Created
December 16, 2017 10:24
-
-
Save christophevg/49a2fee16ea23ce091d17923591629e6 to your computer and use it in GitHub Desktop.
Function to convert a color to its Red, Green, and Blue components
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
// Courtesy http://www.instructables.com/id/How-to-Use-an-RGB-LED/?ALLSTEPS | |
// function to convert a color to its Red, Green, and Blue components. | |
void hueToRGB(uint8_t hue, uint8_t brightness, bool invert) { | |
uint16_t scaledHue = (hue * 6); | |
uint8_t segment = scaledHue / 256; // segment 0 to 5 around the | |
// color wheel | |
uint16_t segmentOffset = | |
scaledHue - (segment * 256); // position within the segment | |
uint8_t complement = 0; | |
uint16_t prev = (brightness * ( 255 - segmentOffset)) / 256; | |
uint16_t next = (brightness * segmentOffset) / 256; | |
if(invert) { | |
brightness = 255 - brightness; | |
complement = 255; | |
prev = 255 - prev; | |
next = 255 - next; | |
} | |
switch(segment ) { | |
case 0: // red | |
R = brightness; | |
G = next; | |
B = complement; | |
break; | |
case 1: // yellow | |
R = prev; | |
G = brightness; | |
B = complement; | |
break; | |
case 2: // green | |
R = complement; | |
G = brightness; | |
B = next; | |
break; | |
case 3: // cyan | |
R = complement; | |
G = prev; | |
B = brightness; | |
break; | |
case 4: // blue | |
R = next; | |
G = complement; | |
B = brightness; | |
break; | |
case 5: // magenta | |
default: | |
R = brightness; | |
G = complement; | |
B = prev; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment