The Adafruit NeoPixel library does exactly this: packs 3 bytes into a long. And has a static method to do that. Several methods that need a RGB take either r,g,b, or one of those packed-longs. One function only takes a packed-long (fill()
).
A color value can be stored as three bytes:
Also, the NeoPixel library deals in RGBW, where there is a specific value for White (which is a distinct led in some NeoPixels). That can also be packed into the uint32.
Use decimal (0d to 255d) or hexadecimal (00h to FFh) byte literals.
HTML/CSS colors are well known, so the 2 character hex seems like a useful thing, as well as the html style “#A0274C” style. Are there very many 2byte-per-color devices?
Each component should be in the range [0, 1].
This echos the analog-read, and pwm out. Which is nice. And, covers the 1-byte-per-pixel vs. 2-byte-per-pixel possibilities. But, is a pain if you want to work in 0-255 ranges.
color-hsl … hsv etc
Is it common enough to want it? Wait till we need it? A note in the “color library” to request it? Don’t forget CMYK, lest ink/paint/print people get annoyed! Or NTSC & PAL.
I work with an artist that uses LEDs extensively to achieve some interesting dynamic color effects. He originally thought in RGB, but has more recently been working in HSV (to achieve constant value). It seems that different places have different ideas of what HSV means. Apparently Excel has a … different idea (I think it’s actually HSL?). Then, of course, there’s all the human-perceptual-system-corrected-color-spaces, e.g. CIE color-spaces. Which he’d really rather use, because that’s actually what he wants (but lots of math, so he did his own crude approximation). Wikipedia has an article about that.
I also work with an art-college, and the students like things like “warm” and “cool” and “white-point”.