I had a project where I needed to crossfade RGB colors and I thought why not using the HSL color space instead of the RGB color space because with the HSL the code looks neater and the Hue (color), Saturation and Lightness can easily be modified to create all kinds of light effects.
Converting HSL to RGB
HSLtoRGB(hue, saturation, lightness, rgb[])
unsigned int hue - a number between 0 and 360 that represents the color on the 360 degrees HSL color space circle.
unsigned char saturation and lightness - expressed in percentage from 0 to 100.
unsigned char rgb[] - an array where red, green and blue values will be placed after conversion. The values are 8-bit (0 - 255).
Example:
uint8_t rgb[3] = {0}; HSLtoRGB(0, 100, 50, rgb);
Converting HSL to RGB
RGBtoHSL(red, green, blue, hsl[])
unsigned char red, green, blue - RGB balues from 0 to 255.
unsigned int hsl[] - an array where hue, saturation and lightness values will be returned after conversion. Hue: a value between 0 and 360. Saturation and lightness are percentage values between 0 and 100.
Example:
uint16_t hsl[3] = {0}; RGBtoHSL(255, 0, 0, hsl);
I have written the code thanks to this guy's explanation https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
For details on how to crossfade the colors of RGB leds check this link https://www.programming-electronics-diy.xyz/2021/02/how-to-control-rgb-leds-crossfading-rgb.html
Download
v1.0
No comments:
Post a Comment