FastLED 3.9.15
Loading...
Searching...
No Matches
fill.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/int.h"
5#include "fl/stl/span.h"
6
8#include "fl/stl/noexcept.h"
9
16
19#define saccum87 i16
20
21namespace fl {
22
26
30
35void fill_solid(CRGB *targetArray, int numToFill,
36 const CRGB &color) FL_NOEXCEPT;
37
39inline void fill_solid(fl::span<CRGB> leds, const CRGB &color) FL_NOEXCEPT {
40 fill_solid(leds.data(), static_cast<int>(leds.size()), color);
41}
42
44void fill_solid(CHSV *targetArray, int numToFill,
45 const CHSV &color) FL_NOEXCEPT;
46
54void fill_rainbow(CRGB *targetArray, int numToFill, fl::u8 initialhue,
55 fl::u8 deltahue = 5) FL_NOEXCEPT;
56
58inline void fill_rainbow(fl::span<CRGB> leds, fl::u8 initialhue,
59 fl::u8 deltahue = 5) FL_NOEXCEPT {
60 fill_rainbow(leds.data(), static_cast<int>(leds.size()), initialhue, deltahue);
61}
62
64void fill_rainbow(CHSV *targetArray, int numToFill, fl::u8 initialhue,
65 fl::u8 deltahue = 5) FL_NOEXCEPT;
66
75void fill_rainbow_circular(CRGB *targetArray, int numToFill,
76 fl::u8 initialhue, bool reversed = false) FL_NOEXCEPT;
77
79inline void fill_rainbow_circular(fl::span<CRGB> leds, fl::u8 initialhue,
80 bool reversed = false) FL_NOEXCEPT {
81 fill_rainbow_circular(leds.data(), static_cast<int>(leds.size()),
82 initialhue, reversed);
83}
84
86void fill_rainbow_circular(CHSV *targetArray, int numToFill,
87 fl::u8 initialhue, bool reversed = false) FL_NOEXCEPT;
88
104template <typename T>
105void fill_gradient(T *targetArray, u16 startpos, CHSV startcolor,
106 u16 endpos, CHSV endcolor,
108 // if the points are in the wrong order, straighten them
109 if (endpos < startpos) {
110 u16 t = endpos;
111 CHSV tc = endcolor;
112 endcolor = startcolor;
113 endpos = startpos;
114 startpos = t;
115 startcolor = tc;
116 }
117
118 // If we're fading toward black (val=0) or white (sat=0),
119 // then set the endhue to the starthue.
120 // This lets us ramp smoothly to black or white, regardless
121 // of what 'hue' was set in the endcolor (since it doesn't matter)
122 if (endcolor.value == 0 || endcolor.saturation == 0) {
123 endcolor.hue = startcolor.hue;
124 }
125
126 // Similarly, if we're fading in from black (val=0) or white (sat=0)
127 // then set the starthue to the endhue.
128 // This lets us ramp smoothly up from black or white, regardless
129 // of what 'hue' was set in the startcolor (since it doesn't matter)
130 if (startcolor.value == 0 || startcolor.saturation == 0) {
131 startcolor.hue = endcolor.hue;
132 }
133
134 saccum87 huedistance87;
135 saccum87 satdistance87;
136 saccum87 valdistance87;
137
138 satdistance87 = (endcolor.sat - startcolor.sat) << 7;
139 valdistance87 = (endcolor.val - startcolor.val) << 7;
140
141 fl::u8 huedelta8 = endcolor.hue - startcolor.hue;
142
143 if (directionCode == SHORTEST_HUES) {
144 directionCode = FORWARD_HUES;
145 if (huedelta8 > 127) {
146 directionCode = BACKWARD_HUES;
147 }
148 }
149
150 if (directionCode == LONGEST_HUES) {
151 directionCode = FORWARD_HUES;
152 if (huedelta8 < 128) {
153 directionCode = BACKWARD_HUES;
154 }
155 }
156
157 if (directionCode == FORWARD_HUES) {
158 huedistance87 = huedelta8 << 7;
159 } else /* directionCode == BACKWARD_HUES */
160 {
161 huedistance87 = (fl::u8)(256 - huedelta8) << 7;
162 huedistance87 = -huedistance87;
163 }
164
165 u16 pixeldistance = endpos - startpos;
166 i16 divisor = pixeldistance ? pixeldistance : 1;
167
168#if FASTLED_USE_32_BIT_GRADIENT_FILL
169 // Use higher precision 32 bit math for new micros.
170 i32 huedelta823 = (huedistance87 * 65536) / divisor;
171 i32 satdelta823 = (satdistance87 * 65536) / divisor;
172 i32 valdelta823 = (valdistance87 * 65536) / divisor;
173
174 huedelta823 *= 2;
175 satdelta823 *= 2;
176 valdelta823 *= 2;
177 u32 hue824 = static_cast<u32>(startcolor.hue) << 24;
178 u32 sat824 = static_cast<u32>(startcolor.sat) << 24;
179 u32 val824 = static_cast<u32>(startcolor.val) << 24;
180 for (u16 i = startpos; i <= endpos; ++i) {
181 targetArray[i] = CHSV(hue824 >> 24, sat824 >> 24, val824 >> 24);
182 hue824 += huedelta823;
183 sat824 += satdelta823;
184 val824 += valdelta823;
185 }
186#else
187 // Use 8-bit math for older micros.
188 saccum87 huedelta87 = huedistance87 / divisor;
189 saccum87 satdelta87 = satdistance87 / divisor;
190 saccum87 valdelta87 = valdistance87 / divisor;
191
192 huedelta87 *= 2;
193 satdelta87 *= 2;
194 valdelta87 *= 2;
195
196 accum88 hue88 = startcolor.hue << 8;
197 accum88 sat88 = startcolor.sat << 8;
198 accum88 val88 = startcolor.val << 8;
199 for (u16 i = startpos; i <= endpos; ++i) {
200 targetArray[i] = CHSV(hue88 >> 8, sat88 >> 8, val88 >> 8);
201 hue88 += huedelta87;
202 sat88 += satdelta87;
203 val88 += valdelta87;
204 }
205#endif // defined(FL_IS_AVR)
206}
207
215template <typename T>
216void fill_gradient(T *targetArray, u16 numLeds, const CHSV &c1,
217 const CHSV &c2,
219 u16 last = numLeds - 1;
220 fill_gradient(targetArray, 0, c1, last, c2, directionCode);
221}
222
231template <typename T>
232void fill_gradient(T *targetArray, u16 numLeds, const CHSV &c1,
233 const CHSV &c2, const CHSV &c3,
235 u16 half = (numLeds / 2);
236 u16 last = numLeds - 1;
237 fill_gradient(targetArray, 0, c1, half, c2, directionCode);
238 fill_gradient(targetArray, half, c2, last, c3, directionCode);
239}
240
250template <typename T>
251void fill_gradient(T *targetArray, u16 numLeds, const CHSV &c1,
252 const CHSV &c2, const CHSV &c3, const CHSV &c4,
254 u16 onethird = (numLeds / 3);
255 u16 twothirds = ((numLeds * 2) / 3);
256 u16 last = numLeds - 1;
257 fill_gradient(targetArray, 0, c1, onethird, c2, directionCode);
258 fill_gradient(targetArray, onethird, c2, twothirds, c3, directionCode);
259 fill_gradient(targetArray, twothirds, c3, last, c4, directionCode);
260}
261
263#define fill_gradient_HSV fill_gradient
264
274void fill_gradient_RGB(CRGB *leds, u16 startpos, CRGB startcolor,
275 u16 endpos, CRGB endcolor) FL_NOEXCEPT;
276
283void fill_gradient_RGB(CRGB *leds, u16 numLeds, const CRGB &c1,
284 const CRGB &c2) FL_NOEXCEPT;
285
293void fill_gradient_RGB(CRGB *leds, u16 numLeds, const CRGB &c1,
294 const CRGB &c2, const CRGB &c3) FL_NOEXCEPT;
295
304void fill_gradient_RGB(CRGB *leds, u16 numLeds, const CRGB &c1,
305 const CRGB &c2, const CRGB &c3, const CRGB &c4) FL_NOEXCEPT;
306
309 const CRGB &c2) FL_NOEXCEPT {
310 fill_gradient_RGB(leds.data(), static_cast<u16>(leds.size()), c1, c2);
311}
312
315 const CRGB &c2, const CRGB &c3) FL_NOEXCEPT {
316 fill_gradient_RGB(leds.data(), static_cast<u16>(leds.size()), c1, c2, c3);
317}
318
321 const CRGB &c2, const CRGB &c3,
322 const CRGB &c4) FL_NOEXCEPT {
323 fill_gradient_RGB(leds.data(), static_cast<u16>(leds.size()), c1, c2, c3,
324 c4);
325}
326
327} // namespace fl
328
fl::CRGB leds[NUM_LEDS]
void fill_rainbow_circular(CRGB *targetArray, int numToFill, fl::u8 initialhue, bool reversed=false) FL_NOEXCEPT
Fill a range of LEDs with a rainbow of colors, so that the hues are continuous between the end of the...
Definition fill.cpp.hpp:53
#define saccum87
ANSI: signed short _Accum.
Definition fill.h:19
void fill_rainbow_circular(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.hpp:53
void fill_rainbow(CRGB *targetArray, int numToFill, u8 initialhue, u8 deltahue)
Fill a range of LEDs with a rainbow of colors.
Definition fill.cpp.hpp:29
void fill_gradient(T *targetArray, u16 startpos, CHSV startcolor, u16 endpos, CHSV endcolor, TGradientDirectionCode directionCode=SHORTEST_HUES) FL_NOEXCEPT
Fill a range of LEDs with a smooth HSV gradient between two HSV colors.
Definition fill.h:105
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.hpp:107
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color)
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:9
fl::hsv8 CHSV
Definition chsv.h:11
fl::CRGB CRGB
Definition crgb.h:25
unsigned char u8
Definition stdint.h:131
unsigned char u8
Definition s16x16x4.h:132
u16 accum88
ANSI: unsigned short _Accum. 8 bits int, 8 bits fraction.
Definition s16x16x4.h:182
unsigned char u8
Definition stdint.h:131
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.
Base definition for an LED controller.
Definition crgb.hpp:179
#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
#define FL_NOEXCEPT
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38