Skip to content

Instantly share code, notes, and snippets.

@kriegsman
Created December 11, 2016 22:44
Show Gist options
  • Save kriegsman/d0a5ed3c8f38c64adcb4837dafb6e690 to your computer and use it in GitHub Desktop.
Save kriegsman/d0a5ed3c8f38c64adcb4837dafb6e690 to your computer and use it in GitHub Desktop.
A function for fading one RGB color toward a target RGB color
#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 50
#define LED_PIN 3
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
void setup() {
delay(3000); // sanity delay
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
}
// 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);
}
}
void loop()
{
CRGB bgColor( 0, 15, 2); // pine green ?
// fade all existing pixels toward bgColor by "5" (out of 255)
fadeTowardColor( leds, NUM_LEDS, bgColor, 5);
// periodically set random pixel to a random color, to show the fading
EVERY_N_MILLISECONDS( 300 ) {
uint16_t pos = random16( NUM_LEDS);
CRGB color = CHSV( random8(), 255, 255);
leds[ pos ] = color;
}
FastLED.show();
FastLED.delay(10);
}
@SteveRMann
Copy link

In your example sketch, fadeTowardColor.ino, You have two functions with the same name:
CRGB fadeTowardColor() that returns a CRGB data type, and
void fadeTowardColor() in the same sketch.

How does this work without error?

@hosentraeger
Copy link

@Steve, because its cplusplus.
The functions have different parameter lists, so they are treated as different functions
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount)

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