Need help understanding the purpose of startIndex and colorIndex in the FastLED example code

Need help understanding the purpose of startIndex and colorIndex in the FastLED example code “CollorPalette”

void loop()
{
ChangePalettePeriodically();

static uint8_t startIndex = 0;
startIndex = startIndex + 1;            /* motion speed */

FillLEDsFromPaletteColors( startIndex);

FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);

}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;

for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness,, currentBlending);
    colorIndex += 3;
}

}

I’ve just learned about Function Parameters and arguments. To me it looks like we are assigning a 0 argument to the FillLEDsFromPaletteColors function. That argument is then going to set colorIndex =0, then it gets fed through the for loop and we finally add three to our color index. I’m i on the right path here?

Side questions:
how does startIndex = startIndex +1 control the motion speed?
Why do we add 3 to the colorIndex?
what is the purpose of the colorIndex when using ColorFromPalette?

When using ColorFromPalette, the current colorIndex (range 0-255) specifies what color to pull from the palette. If the palette went from red, to green in the middle, and then back to red at the end of the palette, then using colorIndex 0 would return red, 128 would return green, and something in between like colorIndex 64 would return a orange-yellow color (as it transitioned from red to green).

In the FastLED “CollorPalette” example, the colorIndex +=3 makes each consecutive LED use a color a bit further along in the palette. If you comment out that line (line 67) then you will see all pixels will be the same color. If you change it to a smaller number the transition through the palette will be slower down the strip. If you make it higher number you will progress through the palette much faster. If you change line 67 to:
colorIndex = colorIndex + (255/NUM_LEDS);
it will transition through the entire palette over the length of your strip.

The variable startIndex that is passed to the function FillLEDsFromPaletteColors determines the color of the first pixel each time through the loop. Instead of + 1, if you add a larger number the palette will “move” down the strip much faster.

Play with both and see how it changes the look!

Also, have a look at this page. In the example here heatindex is similar to colorIndex in that it determines where in the palette (from 0-255) the color is picked.

@marmil Thank you so much. Your explanation has helped me out immensely. I do have one addition question. Further down that same code the author defines a Function called SetupBlackAndWhiteStripedPalette.

void SetupBlackAndWhiteStripedPalette()
{
// ‘black out’ all 16 palette entries…
fill_solid( currentPalette, 16, CRGB::Black);
}

When i run this line of code the matrix remains completely black, as expected. When i change the middle value to anything less then 16 my matrix will begin to display colors. Why is this?

These palettes need exactly 16 entries. If you change that to something less then 16 then I’m guessing it’s using values left over from another palette. If you want to share your modified code please put your code on http://gist.github.com and share the link here.