FastLED 3.9.15
Loading...
Searching...
No Matches
gradient.cpp.hpp
Go to the documentation of this file.
1
2#include "fl/gfx/gradient.h"
3#include "fl/stl/assert.h"
4#include "fl/gfx/colorutils.h"
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8
9namespace {
10struct Visitor {
12 void accept(const CRGBPalette16 *palette) {
14 return_val = c;
15 }
16
17 void accept(const CRGBPalette32 *palette) {
19 return_val = c;
20 }
21
22 void accept(const CRGBPalette256 *palette) {
24 return_val = c;
25 }
26
28 CRGB c = func(index);
29 return_val = c;
30 }
31
32 template <typename T> void accept(const T &obj) {
33 // This should never be called, but we need to provide a default
34 // implementation to avoid compilation errors.
35 accept(&obj);
36 }
37
40};
41
45 // This assert was triggering on the corkscrew example. Not sure why
46 // but the corrective action of taking the min was corrective action.
47 // FASTLED_ASSERT(
48 // indices.size() == output.size(),
49 // "Gradient::fill: indices and output must be the same size"
50 // "\nSize was" << indices.size() << " and " << output.size());
51 n = fl::min(indices.size(), output.size());
52 }
53 void accept(const CRGBPalette16 *palette) {
54 for (fl::size i = 0; i < n; ++i) {
56 }
57 }
58
59 void accept(const CRGBPalette32 *palette) {
60 for (fl::size i = 0; i < n; ++i) {
62 }
63 }
64
65 void accept(const CRGBPalette256 *palette) {
66 for (fl::size i = 0; i < n; ++i) {
68 }
69 }
70
72 for (fl::size i = 0; i < n; ++i) {
73 output[i] = func(indices[i]);
74 }
75 }
76
77 template <typename T> void accept(const T &obj) {
78 // This should never be called, but we need to provide a default
79 // implementation to avoid compilation errors.
80 accept(&obj);
81 }
82
85 u8 n = 0;
86};
87
88} // namespace
89
91 Visitor visitor(index);
92 mVariant.visit(visitor);
93 return visitor.return_val;
94}
95
96template <typename T> Gradient::Gradient(T *palette) { set(palette); }
97
99
101 : mVariant(move(other.mVariant)) {}
102
103void Gradient::set(const CRGBPalette32 *palette) { mVariant = palette; }
104
105void Gradient::set(const CRGBPalette256 *palette) { mVariant = palette; }
106
107void Gradient::set(const CRGBPalette16 *palette) { mVariant = palette; }
108
109void Gradient::set(const GradientFunction &func) { mVariant = func; }
110
112 if (this != &other) {
113 mVariant = other.mVariant;
114 }
115 return *this;
116}
117
118void Gradient::fill(span<const u8> input, span<CRGB> output) const {
119 VisitorFill visitor(input, output);
120 mVariant.visit(visitor);
121}
122
124 Visitor visitor(index);
125 mVariant.visit(visitor);
126 return visitor.return_val;
127}
129 span<CRGB> output) const {
130 VisitorFill visitor(input, output);
131 mVariant.visit(visitor);
132}
133
135 // Visitor is cumbersome but guarantees all paths are handled.
136 struct Copy {
137 Copy(Gradient &owner) : mOwner(owner) {}
138 void accept(const CRGBPalette16 &palette) { mOwner.set(&palette); }
139 void accept(const CRGBPalette32 &palette) { mOwner.set(&palette); }
140 void accept(const CRGBPalette256 &palette) { mOwner.set(&palette); }
141 void accept(const GradientFunction &func) { mOwner.set(func); }
142 Gradient &mOwner;
143 };
144 Copy copy_to_self(*this);
145 other.getVariant().visit(copy_to_self);
146}
147
148} // namespace fl
UINumberField palette("Palette", 0, 0, 2)
void set(const CRGBPalette16 *palette)
GradientVariant mVariant
Definition gradient.h:41
fl::function< CRGB(u8 index)> GradientFunction
Definition gradient.h:18
void fill(span< const u8 > input, span< CRGB > output) const
CRGB colorAt(u8 index) const
Gradient & operator=(const Gradient &other)
Gradient() FL_NOEXCEPT=default
void fill(span< const u8 > input, span< CRGB > output) const
CRGB colorAt(u8 index) const
GradientVariant & getVariant()
Definition gradient.h:64
GradientVariant mVariant
Definition gradient.h:68
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType)
CRGB ColorFromPaletteExtended(const CRGBPalette32 &pal, fl::u16 index, fl::u8 brightness, TBlendType blendType)
Utility functions for color fill, palettes, blending, and more.
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition move.h:28
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
void accept(const CRGBPalette32 *palette)
void accept(const Gradient::GradientFunction &func)
void accept(const CRGBPalette16 *palette)
void accept(const CRGBPalette256 *palette)
void accept(const Gradient::GradientFunction &func)
VisitorFill(span< const u8 > indices, span< CRGB > output)