FastLED 3.9.15
Loading...
Searching...
No Matches
gradient.cpp
Go to the documentation of this file.
1
2#include "fl/gradient.h"
3#include "fl/assert.h"
4#include "fl/colorutils.h"
5
6namespace fl {
7
8namespace {
9struct Visitor {
11 void accept(const CRGBPalette16 *palette) {
13 return_val = c;
14 }
15
16 void accept(const CRGBPalette32 *palette) {
18 return_val = c;
19 }
20
21 void accept(const CRGBPalette256 *palette) {
23 return_val = c;
24 }
25
27 CRGB c = func(index);
28 return_val = c;
29 }
30
31 template <typename T> void accept(const T &obj) {
32 // This should never be called, but we need to provide a default
33 // implementation to avoid compilation errors.
34 accept(&obj);
35 }
36
39};
40
44 // This assert was triggering on the corkscrew example. Not sure why
45 // but the corrective action of taking the min was corrective action.
46 // FASTLED_ASSERT(
47 // indices.size() == output.size(),
48 // "Gradient::fill: indices and output must be the same size"
49 // "\nSize was" << indices.size() << " and " << output.size());
50 n = MIN(indices.size(), output.size());
51 }
52 void accept(const CRGBPalette16 *palette) {
53 for (fl::size i = 0; i < n; ++i) {
55 }
56 }
57
58 void accept(const CRGBPalette32 *palette) {
59 for (fl::size i = 0; i < n; ++i) {
61 }
62 }
63
64 void accept(const CRGBPalette256 *palette) {
65 for (fl::size i = 0; i < n; ++i) {
67 }
68 }
69
71 for (fl::size i = 0; i < n; ++i) {
72 output[i] = func(indices[i]);
73 }
74 }
75
76 template <typename T> void accept(const T &obj) {
77 // This should never be called, but we need to provide a default
78 // implementation to avoid compilation errors.
79 accept(&obj);
80 }
81
84 u8 n = 0;
85};
86
87} // namespace
88
90 Visitor visitor(index);
91 mVariant.visit(visitor);
92 return visitor.return_val;
93}
94
95template <typename T> Gradient::Gradient(T *palette) { set(palette); }
96
98
100 : mVariant(move(other.mVariant)) {}
101
102void Gradient::set(const CRGBPalette32 *palette) { mVariant = palette; }
103
104void Gradient::set(const CRGBPalette256 *palette) { mVariant = palette; }
105
106void Gradient::set(const CRGBPalette16 *palette) { mVariant = palette; }
107
108void Gradient::set(const GradientFunction &func) { mVariant = func; }
109
111 if (this != &other) {
112 mVariant = other.mVariant;
113 }
114 return *this;
115}
116
117void Gradient::fill(span<const u8> input, span<CRGB> output) const {
118 VisitorFill visitor(input, output);
119 mVariant.visit(visitor);
120}
121
123 Visitor visitor(index);
124 mVariant.visit(visitor);
125 return visitor.return_val;
126}
128 span<CRGB> output) const {
129 VisitorFill visitor(input, output);
130 mVariant.visit(visitor);
131}
132
134 // Visitor is cumbersome but guarantees all paths are handled.
135 struct Copy {
136 Copy(Gradient &owner) : mOwner(owner) {}
137 void accept(const CRGBPalette16 &palette) { mOwner.set(&palette); }
138 void accept(const CRGBPalette32 &palette) { mOwner.set(&palette); }
139 void accept(const CRGBPalette256 &palette) { mOwner.set(&palette); }
140 void accept(const GradientFunction &func) { mOwner.set(func); }
141 Gradient &mOwner;
142 };
143 Copy copy_to_self(*this);
144 other.variant().visit(copy_to_self);
145}
146
147} // namespace fl
UINumberField palette("Palette", 0, 0, 2)
void set(const CRGBPalette16 *palette)
Definition gradient.cpp:106
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
Definition gradient.cpp:117
CRGB colorAt(u8 index) const
Definition gradient.cpp:89
Gradient()=default
Gradient & operator=(const Gradient &other)
Definition gradient.cpp:110
void fill(span< const u8 > input, span< CRGB > output) const
Definition gradient.cpp:127
CRGB colorAt(u8 index) const
Definition gradient.cpp:122
GradientVariant & variant()
Definition gradient.h:64
GradientVariant mVariant
Definition gradient.h:68
void visit(Visitor &visitor)
Definition variant.h:154
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.
#define MIN(a, b)
Definition math_macros.h:41
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
unsigned char u8
Definition int.h:17
Slice< T > span
Definition span.h:8
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
void accept(const CRGBPalette16 *palette)
Definition gradient.cpp:11
void accept(const Gradient::GradientFunction &func)
Definition gradient.cpp:26
void accept(const CRGBPalette256 *palette)
Definition gradient.cpp:21
void accept(const CRGBPalette32 *palette)
Definition gradient.cpp:16
void accept(const CRGBPalette256 *palette)
Definition gradient.cpp:64
void accept(const Gradient::GradientFunction &func)
Definition gradient.cpp:70
void accept(const CRGBPalette16 *palette)
Definition gradient.cpp:52
void accept(const CRGBPalette32 *palette)
Definition gradient.cpp:58
VisitorFill(span< const u8 > indices, span< CRGB > output)
Definition gradient.cpp:42