Posts Tagged ‘color’

HSB to RGB [arduino library]

Sunday, August 28th, 2011

The first version of the firmware that I created for Bright Words only supported changing the color of the lights by controlling the RGB values – red, green, and blue. From a coding perspective this is an easy approach since it is consistent with the physical set-up of the device (the red, green and blue lights are each controlled individually).

Unfortunately, this is not a very intuitive way to cycle through colors. The HSB color model (hue, saturation, and brightness) provides a much more natural approach.

Intuitive Control
By supporting direct control of the hue, users are able to cycle through all colors that are of the same saturation and brightness. Users can use the saturation setting to select the appropriate tint of a color. Tints are essentially mixtures of a color with white. The brightness setting enables users to select the appropriate shade of a color. Shades are mixtures of a color with black.

Higher saturation and brightness levels resolve to stronger colors. A color with no saturation will be achromatic, or in other words, it will have no color. Such a color will range between white, grey and black. A color with no brightness will always resolve to black.

The Code
After deciding to take this route I searched online to find existing code samples that I could use to guide my own work. I found a few examples that were written for the Arduino, though in the end I decided to create my own code from scratch so that it would be structured in my own way. I’ve formatted my code into a simple library that you can download from the github repository.

Here is a brief overview of how to use this simple library. It features two methods that do the exact same thing, they just take different input parameters. Both of them accept 4 different parameters: hue, saturation, and brightness values, and a pointer to an integer array that can hold at least 3 elements (the r, g, and b values).

The difference is that method HSBtoRGB accepts hue integer values ranging from 0 to 360, and saturation and brightness integer values ranging from 0 to 100. The method HSBtoRGBfloat accepts hue, saturation and brightness float values ranging from 0 to 1.

Resource Links