FastLED 3.9.15
Loading...
Searching...
No Matches
ease.h
Go to the documentation of this file.
1#pragma once
2
3/*
4This are accurate and tested easing functions.
5
6Note that the easing functions in lib8tion.h are tuned are implemented wrong, such as easeInOutCubic8 and easeInOutCubic16.
7Modern platforms are so fast that the extra performance is not needed, but accuracy is important.
8
9*/
10
11#include "fl/stl/int.h"
12#include "fl/stl/span.h"
13#include "fl/stl/shared_ptr.h" // IWYU pragma: keep
14#include "fl/math/fixed_point.h"
15#include "fl/stl/noexcept.h"
16
17namespace fl {
18
19// LUT for 8-bit to 16-bit gamma correction at pow(x, 2.8)
21
22// Direct access to the 64-byte aligned gamma 2.8 LUT (256 entries, u16).
23// Use with FL_PGM_READ_WORD_ALIGNED for fast reads in hot loops.
24extern const u16 GAMMA_2_8_LUT[256];
25
26
39
40// 8-bit easing functions
45
50
55
60
65
70
75
80
85
86
87// 16-bit easing functions
90u16 easeInQuad16(u16 i) FL_NOEXCEPT;
91
94u16 easeOutQuad16(u16 i) FL_NOEXCEPT;
95
99
102u16 easeInCubic16(u16 i) FL_NOEXCEPT;
103
106u16 easeOutCubic16(u16 i) FL_NOEXCEPT;
107
111
114u16 easeInSine16(u16 i) FL_NOEXCEPT;
115
118u16 easeOutSine16(u16 i) FL_NOEXCEPT;
119
123
124u16 ease16(EaseType type, u16 i) FL_NOEXCEPT;
125void ease16(EaseType type, u16* src, u16* dst, u16 count) FL_NOEXCEPT;
127void ease8(EaseType type, u8* src, u8* dst, u8 count) FL_NOEXCEPT;
128
129
131
132inline u16 ease16(EaseType type, u16 i) FL_NOEXCEPT {
133 switch (type) {
134 case EaseType::EASE_NONE: return i;
135 case EaseType::EASE_IN_QUAD: return easeInQuad16(i);
141 case EaseType::EASE_IN_SINE: return easeInSine16(i);
144 default: return i;
145 }
146}
147
148inline void ease16(EaseType type, u16* src, u16* dst, u16 count) FL_NOEXCEPT {
149 switch (type) {
150 case EaseType::EASE_NONE: return;
152 for (u16 i = 0; i < count; i++) {
153 dst[i] = easeInQuad16(src[i]);
154 }
155 break;
156 }
158 for (u16 i = 0; i < count; i++) {
159 dst[i] = easeOutQuad16(src[i]);
160 }
161 break;
162 }
164 for (u16 i = 0; i < count; i++) {
165 dst[i] = easeInOutQuad16(src[i]);
166 }
167 break;
168 }
170 for (u16 i = 0; i < count; i++) {
171 dst[i] = easeInCubic16(src[i]);
172 }
173 break;
174 }
176 for (u16 i = 0; i < count; i++) {
177 dst[i] = easeOutCubic16(src[i]);
178 }
179 break;
180 }
182 for (u16 i = 0; i < count; i++) {
183 dst[i] = easeInOutCubic16(src[i]);
184 }
185 break;
186 }
188 for (u16 i = 0; i < count; i++) {
189 dst[i] = easeInSine16(src[i]);
190 }
191 break;
192 }
194 for (u16 i = 0; i < count; i++) {
195 dst[i] = easeOutSine16(src[i]);
196 }
197 break;
198 }
200 for (u16 i = 0; i < count; i++) {
201 dst[i] = easeInOutSine16(src[i]);
202 }
203 break;
204 }
205 }
206}
207
209 switch (type) {
210 case EaseType::EASE_NONE: return i;
211 case EaseType::EASE_IN_QUAD: return easeInQuad8(i);
217 case EaseType::EASE_IN_SINE: return easeInSine8(i);
220 default: return i;
221 }
222}
223
224inline void ease8(EaseType type, u8* src, u8* dst, u8 count) FL_NOEXCEPT {
225 switch (type) {
226 case EaseType::EASE_NONE: return;
228 for (u8 i = 0; i < count; i++) {
229 dst[i] = easeInQuad8(src[i]);
230 }
231 break;
232 }
234 for (u8 i = 0; i < count; i++) {
235 dst[i] = easeOutQuad8(src[i]);
236 }
237 break;
238 }
240 for (u8 i = 0; i < count; i++) {
241 dst[i] = easeInOutQuad8(src[i]);
242 }
243 break;
244 }
246 for (u8 i = 0; i < count; i++) {
247 dst[i] = easeInCubic8(src[i]);
248 }
249 break;
250 }
252 for (u8 i = 0; i < count; i++) {
253 dst[i] = easeOutCubic8(src[i]);
254 }
255 break;
256 }
258 for (u8 i = 0; i < count; i++) {
259 dst[i] = easeInOutCubic8(src[i]);
260 }
261 break;
262 }
264 for (u8 i = 0; i < count; i++) {
265 dst[i] = easeInSine8(src[i]);
266 }
267 break;
268 }
270 for (u8 i = 0; i < count; i++) {
271 dst[i] = easeOutSine8(src[i]);
272 }
273 break;
274 }
276 for (u8 i = 0; i < count; i++) {
277 dst[i] = easeInOutSine8(src[i]);
278 }
279 break;
280 }
281 }
282}
283
284// Gamma8: cached 8-bit to 16-bit gamma correction with 64-byte aligned LUT.
285// Use getOrCreate(gamma) to obtain a shared instance. Instances with the
286// same gamma value are deduplicated via an internal weak_ptr cache that
287// automatically expires when all callers release their shared_ptr.
288class Gamma8 {
289public:
291 virtual ~Gamma8() FL_NOEXCEPT = default;
292
293 virtual void convert(fl::span<const u8> input, fl::span<u16> output) const FL_NOEXCEPT = 0;
294 virtual void convert(fl::span<const fl::ufixed_point<8, 8>> input, fl::span<u16> output) const FL_NOEXCEPT = 0;
295 virtual void convert(fl::span<const fl::ufixed_point<8, 8>> input, fl::span<fl::ufixed_point<8, 8>> output) const FL_NOEXCEPT = 0;
296};
297
298} // namespace fl
static fl::shared_ptr< const Gamma8 > getOrCreate(float gamma) FL_NOEXCEPT
Definition ease.cpp.hpp:459
virtual void convert(fl::span< const u8 > input, fl::span< u16 > output) const FL_NOEXCEPT=0
virtual ~Gamma8() FL_NOEXCEPT=default
unsigned char u8
Definition stdint.h:131
unsigned char u8
Definition stdint.h:131
u8 easeOutSine8(u8 i)
8-bit sine ease-out function Takes an input value 0-255 and returns an eased value 0-255 Smooth sinus...
Definition ease.cpp.hpp:170
u8 ease8(EaseType type, u8 i) FL_NOEXCEPT
Definition ease.h:208
constexpr int type_rank< T >::value
u8 easeInQuad8(u8 i)
8-bit quadratic ease-in function Takes an input value 0-255 and returns an eased value 0-255 The curv...
Definition ease.cpp.hpp:58
u8 easeInOutSine8(u8 i)
8-bit sine ease-in/ease-out function Takes an input value 0-255 and returns an eased value 0-255 Smoo...
Definition ease.cpp.hpp:179
u16 easeInQuad16(u16 i)
16-bit quadratic ease-in function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:189
u8 easeInCubic8(u8 i)
8-bit cubic ease-in function Takes an input value 0-255 and returns an eased value 0-255 More pronoun...
Definition ease.cpp.hpp:116
const u16 GAMMA_2_8_LUT[256]
u8 easeInSine8(u8 i)
8-bit sine ease-in function Takes an input value 0-255 and returns an eased value 0-255 Smooth sinuso...
Definition ease.cpp.hpp:142
u8 easeOutCubic8(u8 i)
8-bit cubic ease-out function Takes an input value 0-255 and returns an eased value 0-255 More pronou...
Definition ease.cpp.hpp:129
u16 easeOutQuad16(u16 i)
16-bit quadratic ease-out function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:237
u16 easeInCubic16(u16 i)
16-bit cubic ease-in function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:248
u16 easeInSine16(u16 i)
16-bit sine ease-in function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:274
u16 gamma_2_8(u8 value)
Definition ease.cpp.hpp:53
u8 easeInOutQuad8(u8 i)
8-bit quadratic ease-in/ease-out function Takes an input value 0-255 and returns an eased value 0-255...
Definition ease.cpp.hpp:64
u16 easeInOutSine16(u16 i)
16-bit sine ease-in/ease-out function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:326
constexpr u32 gamma(float g) FL_NOEXCEPT
Definition gamma_lut.h:36
u16 easeOutCubic16(u16 i)
16-bit cubic ease-out function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:261
u16 easeOutSine16(u16 i)
16-bit sine ease-out function Takes an input value 0-65535 and returns an eased value 0-65535
Definition ease.cpp.hpp:305
u16 easeInOutCubic16(u16 x)
16-bit cubic ease-in/ease-out function Takes an input value 0-65535 and returns an eased value 0-6553...
Definition ease.cpp.hpp:215
u16 ease16(EaseType type, u16 i) FL_NOEXCEPT
Definition ease.h:132
u8 easeOutQuad8(u8 i)
8-bit quadratic ease-out function Takes an input value 0-255 and returns an eased value 0-255 The cur...
Definition ease.cpp.hpp:107
EaseType
Definition ease.h:27
@ EASE_IN_OUT_CUBIC
Definition ease.h:34
@ EASE_OUT_QUAD
Definition ease.h:30
@ EASE_IN_QUAD
Definition ease.h:29
@ EASE_NONE
Definition ease.h:28
@ EASE_IN_OUT_QUAD
Definition ease.h:31
@ EASE_IN_CUBIC
Definition ease.h:32
@ EASE_IN_SINE
Definition ease.h:35
@ EASE_OUT_CUBIC
Definition ease.h:33
@ EASE_IN_OUT_SINE
Definition ease.h:37
@ EASE_OUT_SINE
Definition ease.h:36
ufixed_integer< IntBits, FracBits > ufixed_point
u8 easeInOutCubic8(u8 i)
8-bit cubic ease-in/ease-out function Takes an input value 0-255 and returns an eased value 0-255 Mor...
Definition ease.cpp.hpp:84
u16 easeInOutQuad16(u16 x)
16-bit quadratic ease-in/ease-out function Takes an input value 0-65535 and returns an eased value 0-...
Definition ease.cpp.hpp:195
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT