#include <FastLED.h>

// fadeTowardColor example code.
//
// Sample code that includes a function for fading one RGB color toward a target RGB color
// Also includes a function for fading a whole array of pixels toward a given color
//
// Both of these functions _modify_ the existing color, in place.
//
// All fades are done in RGB color space.
//
// Mark Kriegsman
// December 2016


#define NUM_LEDS 120
#define LED_PIN 2
#define LED_TYPE NEOPIXEL
#define COLOR_ORDER GRB
#define POWER_MA 6000

CHSV colors[] = {
  CHSV(224, 255, 255),
  CHSV(201,255,255),
  CHSV(127, 255, 255),
  CHSV(62,255,255),
  CHSV(190,255,255),
  CHSV(0,255,255),
  CHSV(130,255,255)
};

uint8_t colorSize = (sizeof(colors)/sizeof(colors[0]));

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS);
  // limit my draw to 1A at 5v of power draw
  FastLED.setMaxPowerInVoltsAndMilliamps(5,POWER_MA);
  FastLED.setBrightness(255);
  FastLED.clear();
}

// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
  if( cur == target) return;
  
  if( cur < target ) {
    uint8_t delta = target - cur;
    delta = scale8_video( delta, amount);
    cur += delta;
  } else {
    uint8_t delta = cur - target;
    delta = scale8_video( delta, amount);
    cur -= delta;
  }
}

// Blend one CRGB color toward another CRGB color by a given amount.
// Blending is linear, and done in the RGB color space.
// This function modifies 'cur' in place.
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
  nblendU8TowardU8( cur.red,   target.red,   amount);
  nblendU8TowardU8( cur.green, target.green, amount);
  nblendU8TowardU8( cur.blue,  target.blue,  amount);
  return cur;
}

// Fade an entire array of CRGBs toward a given background color by a given amount
// This function modifies the pixel array in place.
void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount)
{
  for( uint16_t i = 0; i < N; i++) {
    fadeTowardColor( L[i], bgColor, fadeAmount);
  }
}

CHSV toColor = colors[random16(colorSize)];

void loop(){

  EVERY_N_MILLISECONDS(10000){
    // toColor = CRGB(random16(255),random16(255),random16(255));
    // toColor  = CHSV(random8(255),255,150);
    toColor = colors[random16(colorSize)];
  }
  fadeTowardColor( leds, NUM_LEDS, toColor, 20);

  // fill_solid(leds, NUM_LEDS, toColor);


  FastLED.show();
  FastLED.delay(20);
}