Skip to content

Add 8-bit palette interpolation function and comparative example. - #202

Closed
generalelectrix wants to merge 1 commit into
FastLED:masterfrom
generalelectrix:master
Closed

generalelectrix wants to merge 1 commit into
FastLED:masterfrom
generalelectrix:master

Conversation

@generalelectrix

Copy link
Copy Markdown

The FastLED compact palettes are really nice creative tools for quickly creating color textures. However, the effective 4-bit interpolation between palette entries (256-color palette) implies that animating a color fade from one palette entry to another will appear stepped/jagged if the fade time is longer than 16 frames.

I defined an "extended" version of the ColorFromPalette function which uses the 12 most significant bits of a 16-bit index; like the regular 8-bit version, the four most significant bits index the color palette, and the next 8 less significant bits are used as an offset to interpolate between the palette entries (the four LSB are ignored). Essentially no major changes were required in the underlying function as the scale functions already accept 8-bit values. This implies full 8-bit color fades between palette entries, so very slow animations using compact palettes are perfectly smooth at no extra expense (with 4-bit interpolation, they are jagged and aesthetically displeasing except when all color fades are faster than about 500 ms - very fast animation!).

I'm not entirely happy with the "ignore the 4 LSB" approach, but it does keep the interface basically identical to the regular 8-bit palette indexing, allows the use of overflow to reset index counters, etc. Mostly it means that you have to use larger numbers to increment and index into palettes. I'm happy to hear feedback on this design decision.

This function was the difference in getting a project working perfectly and beautifully with very little effort. Load up 13 compact palettes, run the animations nice and slow, change them every minute or two, and boom.

Unfortunately the LED rig I wrote this to use on has left for the playa, so I don't have anything to test the example. In a couple weeks I'll run it on that platform and check to make sure it exhibits the correct behavior.

(btw Hi @focalintent, long time no cross paths. This is a very nice library you folks have put together! -Chris Macklin)

Also slightly enhanced documentation in colorutils.cpp.  The SmoothPalette
example directly compares the low-speed animation quality performance of 4-bit
interpolation vs. 8-bit interpolation.  8-bit interpolation is essentially
exactly as efficient as 4-bit as the scale functions are already 8-bit.  This
enables the use of compact palettes as useful tools in the creation of
animations that are designed to run at very slow rates.
@kriegsman

Copy link
Copy Markdown
Member

Thanks for the idea, suggestion, and code!
I'll take a look and see what and how we want to integrate it, but I like the direction here.

@msurguy

msurguy commented May 17, 2016

Copy link
Copy Markdown

@generalelectrix this is exactly what I need to slowly shift colors in a palette, thanks for this PR!

@ghost

ghost commented May 19, 2016

Copy link
Copy Markdown

nice work.

@devoh747

Copy link
Copy Markdown
Contributor

Is this going to be merged?

@RomRider

RomRider commented May 9, 2020

Copy link
Copy Markdown

Hi,
Anyway to have this merged?
It works well (with a slight modification). Please see here #1014 :)

@5chmidti 5chmidti left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem described in #1014 has to do with this line. All other ColorFromPalette's calc f1 like this:

Comment thread colorutils.cpp

// Calculate the scaling factor and scaled values for the lower palette value.

uint8_t f1 = 256 - offset;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint8_t f1 = 256 - offset;
uint8_t f1 = 255 - offset;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and this works. I didn't spot it 😊

@sutaburosu

sutaburosu commented May 20, 2020 •

Copy link
Copy Markdown
Contributor

This works beautifully. I guess it hasn't been merged because it's incomplete; it doesn't support the 32-entry palette type. That's a simple enough fix:

CRGB ColorFromPaletteExtended(const CRGBPalette32& pal, uint16_t index, uint8_t brightness, TBlendType blendType) {
  // Extract the five most significant bits of the index as a palette index.
  uint8_t index_5bit = (index >> 11);
  // Calculate the 8-bit offset from the palette index.
  uint8_t offset = (uint8_t)(index >> 3);
  // Get the palette entry from the 5-bit index
  const CRGB* entry = &(pal[0]) + index_5bit;
  uint8_t red1   = entry->red;
  uint8_t green1 = entry->green;
  uint8_t blue1  = entry->blue;

  uint8_t blend = offset && (blendType != NOBLEND);
  if (blend) {
    if (index_5bit == 31) {
      entry = &(pal[0]);
    } else {
      entry++;
    }

    // Calculate the scaling factor and scaled values for the lower palette value.
    uint8_t f1 = 255 - offset;
    red1   = scale8_LEAVING_R1_DIRTY(red1,   f1);
    green1 = scale8_LEAVING_R1_DIRTY(green1, f1);
    blue1  = scale8_LEAVING_R1_DIRTY(blue1,  f1);

    // Calculate the scaled values for the neighbouring palette value.
    uint8_t red2   = entry->red;
    uint8_t green2 = entry->green;
    uint8_t blue2  = entry->blue;
    red2   = scale8_LEAVING_R1_DIRTY(red2,   offset);
    green2 = scale8_LEAVING_R1_DIRTY(green2, offset);
    blue2  = scale8_LEAVING_R1_DIRTY(blue2,  offset);
    cleanup_R1();

    // These sums can't overflow, so no qadd8 needed.
    red1   += red2;
    green1 += green2;
    blue1  += blue2;
  }
  if (brightness != 255) {
    nscale8x3_video(red1, green1, blue1, brightness);
  }
  return CRGB(red1, green1, blue1);
}

Here's an example of the difference this function can make.

@netmindz

netmindz commented Feb 2, 2022

Copy link
Copy Markdown

Any updates?

@Flavourdynamics

Copy link
Copy Markdown

Hi there, this is a great feature that I would love to see included into a future release. I think there many applications where this would really add to a project.

@Sarah-C

Sarah-C commented Aug 1, 2022

Copy link
Copy Markdown

Just a reminder for removing janky color fades.... =)

@netmindz

Copy link
Copy Markdown

All I want for Christmas is ... Smooth colour fades 😅

@zackees

zackees commented Aug 31, 2024

Copy link
Copy Markdown
Member

It's been merged. Will go in on Monday for the next release.

@generalelectrix

Copy link
Copy Markdown
Author

I love that a variant of this made it in!

@zackees

zackees commented Sep 15, 2024

Copy link
Copy Markdown
Member

Here's the question: is this fast enough for AVR to be the default?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.