Skip to content

Instantly share code, notes, and snippets.

Created November 29, 2017 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/31fea9afca2de93f4cd0bf539c108aa4 to your computer and use it in GitHub Desktop.
Save anonymous/31fea9afca2de93f4cd0bf539c108aa4 to your computer and use it in GitHub Desktop.
Changes and questions commented in CAPS
// Convert HSL (Hue, Saturation, Lightness) to RGB (Red, Green, Blue)
//
// hue: 0 to 359 - position on the color wheel, 0=red, 60=orange,
// 120=yellow, 180=green, 240=blue, 300=violet
//
// saturation: 0 to 100 - how bright or dull the color, 100=full, 0=gray
//
// lightness: 0 to 100 - how light the color is, 100=white, 50=color, 0=black
//
int makeColor(unsigned int hue, unsigned int saturation, unsigned int lightness)
{
unsigned int red, green, blue;
unsigned int var1, var2;
if (hue > 359) hue = hue % 360;
if (saturation > 100) saturation = 100;
if (lightness > 100) lightness = 100;
// algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
if (saturation == 0) {
red = green = blue = lightness * 255 / 100;
} else {
if (lightness < 50) {
var2 = lightness * (100 + saturation);
} else {
var2 = ((lightness + saturation) * 100) - (saturation * lightness);
}
var1 = lightness * 200 - var2;
red = h2rgb(100, 0, 0); // LIGHTNESS VALUE SET TO 100 AND SATURATION TO 0
green = h2rgb(100, 0, 0); // TO SET EVERYTHING BUT BLUE COLORS TO BE WHITE
blue = h2rgb(var1, var2, (hue >= 240) ? hue - 240 : hue + 120) * 255 / 600000;
// INSTEAD OF DISPLAYING BLUE, LEDS ARE DISPLAYING YELLOW. WHAT CHANGES ARE NEEDED HERE TO DISPLAY BLUE INSTEAD?
// ALSO, DO I HAVE THOSE h2rgb VALUES RIGHT? (LIGHTNESS, SATURATION, HUE)
}
return (red << 16) | (green << 8) | blue;
}
unsigned int h2rgb(unsigned int v1, unsigned int v2, unsigned int hue)
{
if (hue < 60) return v1 * 60 + (v2 - v1) * hue;
if (hue < 180) return v2 * 60;
if (hue < 240) return v1 * 60 + (v2 - v1) * (240 - hue);
return v1 * 60;
}
// alternate code:
// http://forum.pjrc.com/threads/16469-looking-for-ideas-on-generating-RGB-colors-from-accelerometer-gyroscope?p=37170&viewfull=1#post37170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment