FastLED 3.9.15
Loading...
Searching...
No Matches
fill.h
Go to the documentation of this file.
1#pragma once
2
3#include "crgb.h"
5#include "fl/int.h"
6#include "fl/stdint.h"
7
9
16
19#define saccum87 i16
20
21namespace fl {
22
26
30
35void fill_solid(struct CRGB *targetArray, int numToFill,
36 const struct CRGB &color);
37
39void fill_solid(struct CHSV *targetArray, int numToFill,
40 const struct CHSV &color);
41
49void fill_rainbow(struct CRGB *targetArray, int numToFill, fl::u8 initialhue,
50 fl::u8 deltahue = 5);
51
53void fill_rainbow(struct CHSV *targetArray, int numToFill, fl::u8 initialhue,
54 fl::u8 deltahue = 5);
55
64void fill_rainbow_circular(struct CRGB *targetArray, int numToFill,
65 fl::u8 initialhue, bool reversed = false);
66
68void fill_rainbow_circular(struct CHSV *targetArray, int numToFill,
69 fl::u8 initialhue, bool reversed = false);
70
86template <typename T>
87void fill_gradient(T *targetArray, u16 startpos, CHSV startcolor,
88 u16 endpos, CHSV endcolor,
89 TGradientDirectionCode directionCode = SHORTEST_HUES) {
90 // if the points are in the wrong order, straighten them
91 if (endpos < startpos) {
92 u16 t = endpos;
93 CHSV tc = endcolor;
94 endcolor = startcolor;
95 endpos = startpos;
96 startpos = t;
97 startcolor = tc;
98 }
99
100 // If we're fading toward black (val=0) or white (sat=0),
101 // then set the endhue to the starthue.
102 // This lets us ramp smoothly to black or white, regardless
103 // of what 'hue' was set in the endcolor (since it doesn't matter)
104 if (endcolor.value == 0 || endcolor.saturation == 0) {
105 endcolor.hue = startcolor.hue;
106 }
107
108 // Similarly, if we're fading in from black (val=0) or white (sat=0)
109 // then set the starthue to the endhue.
110 // This lets us ramp smoothly up from black or white, regardless
111 // of what 'hue' was set in the startcolor (since it doesn't matter)
112 if (startcolor.value == 0 || startcolor.saturation == 0) {
113 startcolor.hue = endcolor.hue;
114 }
115
116 saccum87 huedistance87;
117 saccum87 satdistance87;
118 saccum87 valdistance87;
119
120 satdistance87 = (endcolor.sat - startcolor.sat) << 7;
121 valdistance87 = (endcolor.val - startcolor.val) << 7;
122
123 fl::u8 huedelta8 = endcolor.hue - startcolor.hue;
124
125 if (directionCode == SHORTEST_HUES) {
126 directionCode = FORWARD_HUES;
127 if (huedelta8 > 127) {
128 directionCode = BACKWARD_HUES;
129 }
130 }
131
132 if (directionCode == LONGEST_HUES) {
133 directionCode = FORWARD_HUES;
134 if (huedelta8 < 128) {
135 directionCode = BACKWARD_HUES;
136 }
137 }
138
139 if (directionCode == FORWARD_HUES) {
140 huedistance87 = huedelta8 << 7;
141 } else /* directionCode == BACKWARD_HUES */
142 {
143 huedistance87 = (fl::u8)(256 - huedelta8) << 7;
144 huedistance87 = -huedistance87;
145 }
146
147 u16 pixeldistance = endpos - startpos;
148 i16 divisor = pixeldistance ? pixeldistance : 1;
149
150#if FASTLED_USE_32_BIT_GRADIENT_FILL
151 // Use higher precision 32 bit math for new micros.
152 i32 huedelta823 = (huedistance87 * 65536) / divisor;
153 i32 satdelta823 = (satdistance87 * 65536) / divisor;
154 i32 valdelta823 = (valdistance87 * 65536) / divisor;
155
156 huedelta823 *= 2;
157 satdelta823 *= 2;
158 valdelta823 *= 2;
159 u32 hue824 = static_cast<u32>(startcolor.hue) << 24;
160 u32 sat824 = static_cast<u32>(startcolor.sat) << 24;
161 u32 val824 = static_cast<u32>(startcolor.val) << 24;
162 for (u16 i = startpos; i <= endpos; ++i) {
163 targetArray[i] = CHSV(hue824 >> 24, sat824 >> 24, val824 >> 24);
164 hue824 += huedelta823;
165 sat824 += satdelta823;
166 val824 += valdelta823;
167 }
168#else
169 // Use 8-bit math for older micros.
170 saccum87 huedelta87 = huedistance87 / divisor;
171 saccum87 satdelta87 = satdistance87 / divisor;
172 saccum87 valdelta87 = valdistance87 / divisor;
173
174 huedelta87 *= 2;
175 satdelta87 *= 2;
176 valdelta87 *= 2;
177
178 accum88 hue88 = startcolor.hue << 8;
179 accum88 sat88 = startcolor.sat << 8;
180 accum88 val88 = startcolor.val << 8;
181 for (u16 i = startpos; i <= endpos; ++i) {
182 targetArray[i] = CHSV(hue88 >> 8, sat88 >> 8, val88 >> 8);
183 hue88 += huedelta87;
184 sat88 += satdelta87;
185 val88 += valdelta87;
186 }
187#endif // defined(__AVR__)
188}
189
197template <typename T>
198void fill_gradient(T *targetArray, u16 numLeds, const CHSV &c1,
199 const CHSV &c2,
200 TGradientDirectionCode directionCode = SHORTEST_HUES) {
201 u16 last = numLeds - 1;
202 fill_gradient(targetArray, 0, c1, last, c2, directionCode);
203}
204
213template <typename T>
214void fill_gradient(T *targetArray, u16 numLeds, const CHSV &c1,
215 const CHSV &c2, const CHSV &c3,
216 TGradientDirectionCode directionCode = SHORTEST_HUES) {
217 u16 half = (numLeds / 2);
218 u16 last = numLeds - 1;
219 fill_gradient(targetArray, 0, c1, half, c2, directionCode);
220 fill_gradient(targetArray, half, c2, last, c3, directionCode);
221}
222
232template <typename T>
233void fill_gradient(T *targetArray, u16 numLeds, const CHSV &c1,
234 const CHSV &c2, const CHSV &c3, const CHSV &c4,
235 TGradientDirectionCode directionCode = SHORTEST_HUES) {
236 u16 onethird = (numLeds / 3);
237 u16 twothirds = ((numLeds * 2) / 3);
238 u16 last = numLeds - 1;
239 fill_gradient(targetArray, 0, c1, onethird, c2, directionCode);
240 fill_gradient(targetArray, onethird, c2, twothirds, c3, directionCode);
241 fill_gradient(targetArray, twothirds, c3, last, c4, directionCode);
242}
243
245#define fill_gradient_HSV fill_gradient
246
256void fill_gradient_RGB(CRGB *leds, u16 startpos, CRGB startcolor,
257 u16 endpos, CRGB endcolor);
258
265void fill_gradient_RGB(CRGB *leds, u16 numLeds, const CRGB &c1,
266 const CRGB &c2);
267
275void fill_gradient_RGB(CRGB *leds, u16 numLeds, const CRGB &c1,
276 const CRGB &c2, const CRGB &c3);
277
286void fill_gradient_RGB(CRGB *leds, u16 numLeds, const CRGB &c1,
287 const CRGB &c2, const CRGB &c3, const CRGB &c4);
288
289} // namespace fl
290
CRGB leds[NUM_LEDS]
#define FL_DISABLE_WARNING_RETURN_TYPE
#define FL_DISABLE_WARNING_IMPLICIT_INT_CONVERSION
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_SIGN_CONVERSION
#define FL_DISABLE_WARNING_POP
#define FL_DISABLE_WARNING_UNUSED_PARAMETER
#define FL_DISABLE_WARNING_FLOAT_CONVERSION
Defines the red, green, and blue (RGB) pixel struct.
static uint32_t t
Definition Luminova.h:54
#define saccum87
ANSI: signed short _Accum.
Definition fill.h:19
void fill_gradient_RGB(CRGB *leds, u16 startpos, CRGB startcolor, u16 endpos, CRGB endcolor)
Fill a range of LEDs with a smooth RGB gradient between two RGB colors.
Definition fill.cpp:107
void fill_gradient(T *targetArray, u16 startpos, CHSV startcolor, u16 endpos, CHSV endcolor, TGradientDirectionCode directionCode=SHORTEST_HUES)
Fill a range of LEDs with a smooth HSV gradient between two HSV colors.
Definition fill.h:87
void fill_rainbow_circular(struct CRGB *targetArray, int numToFill, u8 initialhue, bool reversed)
Fill a range of LEDs with a rainbow of colors, so that the hues are continuous between the end of the...
Definition fill.cpp:53
void fill_rainbow(struct CRGB *targetArray, int numToFill, u8 initialhue, u8 deltahue)
Fill a range of LEDs with a rainbow of colors.
Definition fill.cpp:29
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
Definition fill.cpp:9
unsigned char u8
Definition int.h:17
TGradientDirectionCode
Hue direction for calculating fill gradients.
@ SHORTEST_HUES
Hue goes whichever way is shortest.
@ LONGEST_HUES
Hue goes whichever way is longest.
@ FORWARD_HUES
Hue always goes clockwise around the color wheel.
@ BACKWARD_HUES
Hue always goes counter-clockwise around the color wheel.
u16 accum88
ANSI: unsigned short _Accum. 8 bits int, 8 bits fraction.
Definition int.h:70
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition hsv.h:15