// https://www.reddit.com/r/FastLED/comments/14u7t7i/led_trasition_gets_skipped_overflow/

#include <FastLED.h>

#define LED_PIN 2
#define NUM_LEDS 64

CRGB leds[NUM_LEDS];

//array
uint8_t x[NUM_LEDS];
uint8_t y[NUM_LEDS];

//palett
DEFINE_GRADIENT_PALETTE(indigoGP) {
  0, 0, 0, 0,
  100, 0, 0, 5,
  255, 255, 255, 247
};

CRGBPalette16 indigoPalett = indigoGP;
// CRGBPalette16 indigoPalett = RainbowColors_p;


void setup() {
  Serial.begin(2000000);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  for (unsigned short i = 0; i < NUM_LEDS; i++) {
    unsigned char angle = (i * 256) / NUM_LEDS;
    x[i] = cos8(angle);
    y[i] = sin8(angle);
  }
}

void loop() {
  effect();
  FastLED.show();
}


//effect function (gets called in loop)
void effect() {
  const uint16_t scale = 150;
  
  // previously: `x = millis()/40`, then `x *= 150` separately.
  // That's roughly equivalent to `x = millis() * 3.75`
  // Multiply by 4 and substract 1/16th of the result to get 3.75x
  uint32_t shift = millis() * 4;
  shift -= shift >> 4;

  for (uint16_t i = 0; i < NUM_LEDS; i++) {

    uint32_t real_x = shift + x[i] * scale;
    uint32_t real_y = shift + y[i] * scale;

    uint16_t noise = inoise16(real_x, real_y, 4223);
    uint16_t index = noise * 3;

    // leds[i] = ColorFromPalette(indigoPalett, index >> 8);
    leds[i] = ColorFromPaletteExtended(indigoPalett, index, 255, LINEARBLEND);
  }
}


// FastLED uses 4-bit interpolation.  8-bit looks far less janky.
// https://github.com/FastLED/FastLED/pull/202
CRGB ColorFromPaletteExtended(const CRGBPalette16& pal, uint16_t index, uint8_t brightness, TBlendType blendType) {
  // Extract the four most significant bits of the index as a palette index.
  uint8_t index_4bit = (index >> 12);
  // Calculate the 8-bit offset from the palette index.
  uint8_t offset = (uint8_t)(index >> 4);
  // Get the palette entry from the 4-bit index
  const CRGB* entry = &(pal[0]) + index_4bit;
  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_4bit == 15) {
      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);
    nscale8x3(red1, green1, blue1, brightness);
  }
  return CRGB(red1, green1, blue1);
}