This library can be used to convert RGB values to HSL and vice-versa. These are useful in color cross-fades, color reading and manipulation.
Why HSL is more advantageous than RGB
While RGB (Red, Green, Blue) is the standard color model for display hardware and LEDs because it directly maps to physical light channels, it is not always intuitive or efficient for software logic. HSL (Hue, Saturation, Lightness) reshapes this 3D color cube into a cylinder, offering major practical advantages.
Intuitive Color Manipulation
If you want to make an RGB color "brighter" or "dimmer," you must scale all three values (R, G, and B) proportionally. Changing just one channel inadvertently shifts the color tint. Converting the color to HSL is more advantageous. Lightness (L) is completely decoupled from the color itself. To dim a color, you simply decrease L. To make it more vivid, you increase S.
Color Transitions & Gradients
HSL Advantage: by holding Saturation (S) and Lightness (L) constant and simply sweeping the Hue (H) angle from 0 to 360, you can create smooth, vibrant rainbow sweeps and color cross-fades.
Light-Agnostic Color Sensing
In computer vision or ambient light sensing, a blue object under bright light might report RGB(50, 50, 255), but under a shadow, it reports RGB(10, 10, 50). Writing simple threshold logic for this is difficult.
In HSL, both readings will yield approximately the same Hue (H ~240 degrees), making object tracking and color detection resilient to changes in ambient brightness or shadows.
API
Converting HSL to RGB
void HSLtoRGB(uint16_t h, uint8_t s, uint8_t l, uint8_t rgb[])
Converts HSL (Hue, Saturation, Lightness) color values to RGB. Used primarily for output operations, such as driving RGB LEDs or display pixels with intuitive color controls (e.g., rainbow fades).
hue:
Hue angle in degrees [0 to 360].
saturation:
Saturation percentage [0 to 100] (0 = grayscale, 100 = full saturation).
lightness:
Lightness percentage [0 to 100] (0 = complete black, 50 = pure color, 100 = pure white).
rgb:
Pointer to an array of 3 uint8_t elements where the result is stored:
- rgb[0]: Red channel [0 to 255]
- rgb[1]: Green channel [0
to 255]
- rgb[2]: Blue channel [0 to 255]
Converting RGB to HSL
void RGBtoHSL(const uint8_t *rgb, uint16_t *hue, uint8_t *saturation, uint8_t *lightness)
Converts RGB (Red, Green, Blue) color values to HSL. Used primarily for input operations, such as processing raw color sensor data (e.g., TCS34725) to identify hues independently of ambient light brightness.
rgb:
Pointer to an array containing 3 uint8_t RGB values:
- rgb[0]: Red channel [0 to 255]
- rgb[1]: Green channel [0 to 255]
- rgb[2]: Blue channel [0 to 255]
hue:
Pointer to store the calculated Hue [0 to 360 degrees].
saturation:
Pointer to store the calculated Saturation [0 to 100 percent].
lightness:
Pointer to store the calculated Lightness [0 to 100 percent].
Code example
In this example, I have used the BCM library to control the LEDs
connected to Port C on pins 1 (red), 2 (green), and 3 (blue). Pins are
defined in the bcm.h header file.
#include <stdio.h> #include <stdlib.h> #include <util/delay.h> #include "bcm.h" #include "RGBvsHSL.h" int main(void) { // 100% Saturation, 50% Lightness gives vibrant, pure colors uint8_t saturation = 100; uint8_t lightness = 50; uint16_t hue = 0; uint8_t rgb[3] = {0}; BCM_init(); while (1) { if (BCM_CYCLE_END) { BCM_CYCLE_END = false; // Calculate current RGB for active hue step HSLtoRGB(hue, saturation, lightness, rgb); // Send formatted values to driver BCM_encode(rgb, 'C'); // Increment Hue angle (0° to 360°) hue++; if (hue > 360) { hue = 0; } // Effect speed control _delay_ms(40); } } return 0; }
Links
| v1.1 | |
| RGBvsHSL.h | |
| RGBvsHSL.c | |
| v1.0 | |
| RGBvsHSL.h | |
| Other links | |
| RGB driver | |
| Binary Code Modulation (BCM) library | |
| Math behind colorspace conversions | |
| Changelog | |
| v1.1 |
10-08-2026: - Code formatting improvements. |

No comments:
Post a Comment