ColorPalette code & example
The FastLED v2.1 branch on GitHub now contains support for “color palettes”, as well as an example showing a few ways they can be used. Here’s a video of the example code running; the entire code for this example is about 150 lines, and takes up just 6.5Kb.
There are dozens of ways that color palettes can be used, but basically they can make it very easy to colorize and re-colorize your animations as you wish.
I recommend that you just download the example and run it, and while it’s going, read over the source code, tinker, and play!
The code for this example is here
you guys never cease to amaze me with the work you put into this.
this will be going into my project this weekend.
Q. What’s the speed performance of color palettes?
A. With blending enabled and variable pixel brightness, “ColorFromPalette” is just about as fast as HSV-to-RGB translation, i.e., more than 100,000 conversions per second on an standard Arduino Uno. With blending disabled, or when using the large (768-byte) palettes, performance is even faster. Basically, using a color palette is about the same speed as using HSV colors; there’s no speed penalty.
Q. Can I hand-code my own color palettes?
A. Yes! Check out the ColorPalette example; it’s pretty easy to hand-code a color palette.
Q. Can I generate a color palette programatically?
A. Yes! Check out the ColorPalette example code to see some ways to do this.
Q. Can I alter a palette on the fly?
A. Yes, but it won’t automatically re-color your LEDs. You have to use ColorFromPalette to re-color your LEDs. (Dan and I have a crazy idea that someday this might not be necessary, but that’s another project for another day.)
Q. How much storage do palettes take up?
A. Only one palette is needed in RAM, and the compact palettes (CRGBPalette16, as shown in the example above) takes up just 48 bytes of RAM. Static color palettes can be stored in PROGMEM, where they take up 64 bytes of flash each. They’re copied into RAM as needed, and as mentioned above, only one need be in RAM at a time.
Q. I’m confused. How many entries are there in a palette? 16? Or 256?
A. Well first of all there are two kinds of palettes: BIG 256-entry palettes (CRGBPalette256) that take up 768 bytes, and “compact” palettes (CRGBPalette16) that take up just 48 bytes. Both ‘act like’ they have 256 colors in them, but in the case of the compact ones, there are really only 16 distinct entries and all of the ‘in between’ entires are blended (RGB interpolated) on the fly. This lets you program as if you had 256 palette colors without incurring the storage burden of 256 CRGB values. You can also turn the inter-entry blending on and off as you wish. It’s easier to use than to explain; read the code and play around.
Just as a heads-up, my support bandwidth on this is a little constrained for the next few days, so I mostly encourage everyone to just dig in and play and see what works and what doesn’t, and discuss it here in the group. I’ll pop in to offer guidance and support as I’m able, but I’d recommend just experimentation for a while, given how new this is.
Also, still to come: perlin/simplex noise plus color palettes…
On 8x100 grid… looks amazing!
Wow… The blending creates a strong antialiased effect that really smoothes it out there. Compare the white stripes unblended (around 0:18) and blended (around 0:22). Thanks for sharing the video.
Have you looked at the code at all yet, or tried to make your own palette? The green and purple stripes code is probably the easiest to read and tinker.
i will be this weekend. i was just eager to plug it in and check it out. so much potential!
the code for this function is really simple and straight forward too for my intermediate coding skills. So mega thanks for that
Great, and glad it looks interesting. Sort of can’t wait to see what people do with it!
noob; so please have patience!
Saved FastLed v2.1 into folder under \ardunio. Attempt to complie example sketch ColorPallete using Ardunio IDE 1.0.5 and get several errors: “does not name a type”, “not declared in this scope”.
I would have thought they would have been picked up from other .h files that FastLED is linkled to. Names like “CRGBColorPallete16”, “TBlendType”, “TProgmemPalette16” are named in the error messages as well as several others.
Am I as linked as I think I am, have files in the wrong folders, need to use a better complier or need to further define things like ColorPallet16 in the colorutils.h file?
Thanks for the trouble report, Bob.
Do you know if the FastLED v2.1 folder got put under “Arduino” or “Arduino/Libraries”? (It needs to go in “Arduino/Libraries”.)
Also: do you know if there is still a copy of an older version of the FastLED library in the Arduino/Libraries directory?
And finally, if you try one of the other simpler FastLED examples from the examples menu, do they also generate errors? (E.g. “Cylon”?)
Was excited to try on my 2801 leds. Though you would like to see this error.
Received an error - ‘CRGB’ does not name a type - CRGB leds[NUM_LEDS]; I am confident that the FastLED v2.1 is in the libraries folder and I do not have any other versions of FastLED
Thanks, Budd. Do any of the simple examples, like Cylon, work?
Also, is this under Arduino 1.0.5, or a different version? And what Board type do you have selected under the Tools>Board menu? Thanks again for the help in figuring out what’s up; it’s clearly working ‘automagically’ in some environments, and not at all in others.
@Budd_Termin : did you change the FastLED.addLeds<…> line in the example code to include pin definitions for not only the Data pin number, but also the Clock pin number, which is needed for WS2801s? The example is set up for single-pin LED strips like WS2811, but it should work OK if you change that one line. I’m not sure that’d be causing the particular compile errors you’re seeing, but it certainly won’t be helping…
Thanks for the quick response Mark. Appreciate the help.
FastLED v2.1 was put under Arduino. Moved it to Ardunio/Libraries. Had originally named the folder of the older version of FastLED to FastLEDOld. I came back and removed it completely.
Yes; simpler sketches in the FASTLED v2.1 Examples folder will compile error-free in the Arduino IDE v1.0.5: AnalogOutput, Blink, Cylon, RGBCalibrate. However, they ran okay before I moved v2.1 to /Libraries and removed the old version. Other sketches will not; ColorPallete, ColorTemperature, Fire2012, FirstLight, Noise, NoisePlayground, PinText, XYMatrix. Did not try to compile the sketches in Multiple.
For what it’s worth, I’m running an Arduino Uno.
Hrm. Mysterious. Using Arduino 1.0.5, right? (On what OS?)
Would you please, if you can, paste the compiler output and error massages from FirstLight in their entirety someplace shareable? Thanks!
Yes, Arduino 1.0.5. Loaded it only a few weeks ago. Running Windows 8.1.
Here are the error messages from FirstLight sketch complie. I did not change anything in the sketch before compiling::
FirstLight.ino: In function ‘void setup()’:
FirstLight:43: error: ‘UCS1903B’ was not declared in this scope
FirstLight:43: error: no matching function for call to ‘CFastLED::addLeds(CRGB , int)’
Ah hah! On line 18 of FirstLight, change DATA_PIN from 23 to just plain 3, and see if that makes any difference. (It did for me- now it compiles fine.)
Now on the other hand, that doesn’t explain the problem with the other sketches. For example, XYMatrix seems to compile fine for me on Arduino 1.0.5 (board= Arduino Uno) using FastLED v2.1. Still digging.