From 12127ccdacb46bf30167785070732c347694fb8a Mon Sep 17 00:00:00 2001 From: David Madison Date: Mon, 9 Aug 2021 16:26:24 -0400 Subject: Add fill_rainbow_endless util functions For filling a rainbow pattern that's continuous in hue between the end of the strip and the beginning. Useful for out-of-the-box rainbow animations on looped strips and LED rings. --- keywords.txt | 1 + src/colorutils.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/colorutils.h | 15 +++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/keywords.txt b/keywords.txt index 75df500c..91e7b3b1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -131,6 +131,7 @@ fill_gradient KEYWORD2 fill_gradient_RGB KEYWORD2 fill_palette KEYWORD2 fill_rainbow KEYWORD2 +fill_rainbow_endless KEYWORD2 fill_solid KEYWORD2 map_data_into_colors_through_palette KEYWORD2 nblend KEYWORD2 diff --git a/src/colorutils.cpp b/src/colorutils.cpp index a1acaa5a..d1f45bd9 100644 --- a/src/colorutils.cpp +++ b/src/colorutils.cpp @@ -62,6 +62,45 @@ void fill_rainbow( struct CHSV * targetArray, int numToFill, } +void fill_rainbow_endless(struct CRGB* targetArray, int numToFill, uint8_t initialhue) +{ + if (numToFill == 0) return; // avoiding div/0 + + CHSV hsv; + hsv.hue = initialhue; + hsv.val = 255; + hsv.sat = 240; + + const uint16_t hueChange = 65536 / (uint16_t)numToFill; // hue change for each LED, * 256 for precision + uint16_t hueOffset = 0; // offset for hue value, with precision (*256) + + for (int i = 0; i < numToFill; ++i) { + targetArray[i] = hsv; + hueOffset += hueChange; // increase precise offset + hsv.hue = initialhue + (uint8_t)(hueOffset >> 8); // assign new hue with precise offset (as 8-bit) + } +} + +void fill_rainbow_endless(struct CHSV* targetArray, int numToFill, uint8_t initialhue) +{ + if (numToFill == 0) return; // avoiding div/0 + + CHSV hsv; + hsv.hue = initialhue; + hsv.val = 255; + hsv.sat = 240; + + const uint16_t hueChange = 65536 / (uint16_t)numToFill; // hue change for each LED, * 256 for precision + uint16_t hueOffset = 0; // offset for hue value, with precision (*256) + + for (int i = 0; i < numToFill; ++i) { + targetArray[i] = hsv; + hueOffset += hueChange; // increase precise offset + hsv.hue = initialhue + (uint8_t)(hueOffset >> 8); // assign new hue with precise offset (as 8-bit) + } +} + + void fill_gradient_RGB( CRGB* leds, uint16_t startpos, CRGB startcolor, uint16_t endpos, CRGB endcolor ) diff --git a/src/colorutils.h b/src/colorutils.h index f09d525f..237e9400 100644 --- a/src/colorutils.h +++ b/src/colorutils.h @@ -37,6 +37,21 @@ void fill_rainbow( struct CHSV * targetArray, int numToFill, uint8_t deltahue = 5); +/// fill_rainbow_endless - fill a range of LEDs with a rainbow of colors, at +/// full saturation and full value (brightness), +/// so that the hues are continuous between the end +/// of the strip and the beginning +void fill_rainbow_endless(struct CRGB* targetArray, int numToFill, + uint8_t initialhue); + +/// fill_rainbow_endless - fill a range of LEDs with a rainbow of colors, at +/// full saturation and full value (brightness), +/// so that the hues are continuous between the end +/// of the strip and the beginning +void fill_rainbow_endless(struct CHSV* targetArray, int numToFill, + uint8_t initialhue); + + // fill_gradient - fill an array of colors with a smooth HSV gradient // between two specified HSV colors. // Since 'hue' is a value around a color wheel, -- cgit v1.2.3