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/stdint.h"
12#include "fl/int.h"
13#include "fastled_progmem.h"
14
15namespace fl {
16
17// Gamma 2.8 lookup table for 8-bit to 16-bit gamma correction
18// Used for converting linear 8-bit values to gamma-corrected 16-bit values
19extern const u16 gamma_2_8[256] FL_PROGMEM;
20
33
34// 8-bit easing functions
39
44
49
54
59
64
69
74
79
80
81// 16-bit easing functions
84u16 easeInQuad16(u16 i);
85
88u16 easeOutQuad16(u16 i);
89
92u16 easeInOutQuad16(u16 i);
93
96u16 easeInCubic16(u16 i);
97
100u16 easeOutCubic16(u16 i);
101
104u16 easeInOutCubic16(u16 i);
105
108u16 easeInSine16(u16 i);
109
112u16 easeOutSine16(u16 i);
113
116u16 easeInOutSine16(u16 i);
117
118u16 ease16(EaseType type, u16 i);
119void ease16(EaseType type, u16* src, u16* dst, u16 count);
120u8 ease8(EaseType type, u8 i);
121void ease8(EaseType type, u8* src, u8* dst, u8 count);
122
123
125
126inline u16 ease16(EaseType type, u16 i) {
127 switch (type) {
128 case EASE_NONE: return i;
129 case EASE_IN_QUAD: return easeInQuad16(i);
130 case EASE_OUT_QUAD: return easeOutQuad16(i);
131 case EASE_IN_OUT_QUAD: return easeInOutQuad16(i);
132 case EASE_IN_CUBIC: return easeInCubic16(i);
133 case EASE_OUT_CUBIC: return easeOutCubic16(i);
134 case EASE_IN_OUT_CUBIC: return easeInOutCubic16(i);
135 case EASE_IN_SINE: return easeInSine16(i);
136 case EASE_OUT_SINE: return easeOutSine16(i);
137 case EASE_IN_OUT_SINE: return easeInOutSine16(i);
138 default: return i;
139 }
140}
141
142inline void ease16(EaseType type, u16* src, u16* dst, u16 count) {
143 switch (type) {
144 case EASE_NONE: return;
145 case EASE_IN_QUAD: {
146 for (u16 i = 0; i < count; i++) {
147 dst[i] = easeInQuad16(src[i]);
148 }
149 break;
150 }
151 case EASE_OUT_QUAD: {
152 for (u16 i = 0; i < count; i++) {
153 dst[i] = easeOutQuad16(src[i]);
154 }
155 break;
156 }
157 case EASE_IN_OUT_QUAD: {
158 for (u16 i = 0; i < count; i++) {
159 dst[i] = easeInOutQuad16(src[i]);
160 }
161 break;
162 }
163 case EASE_IN_CUBIC: {
164 for (u16 i = 0; i < count; i++) {
165 dst[i] = easeInCubic16(src[i]);
166 }
167 break;
168 }
169 case EASE_OUT_CUBIC: {
170 for (u16 i = 0; i < count; i++) {
171 dst[i] = easeOutCubic16(src[i]);
172 }
173 break;
174 }
175 case EASE_IN_OUT_CUBIC: {
176 for (u16 i = 0; i < count; i++) {
177 dst[i] = easeInOutCubic16(src[i]);
178 }
179 break;
180 }
181 case EASE_IN_SINE: {
182 for (u16 i = 0; i < count; i++) {
183 dst[i] = easeInSine16(src[i]);
184 }
185 break;
186 }
187 case EASE_OUT_SINE: {
188 for (u16 i = 0; i < count; i++) {
189 dst[i] = easeOutSine16(src[i]);
190 }
191 break;
192 }
193 case EASE_IN_OUT_SINE: {
194 for (u16 i = 0; i < count; i++) {
195 dst[i] = easeInOutSine16(src[i]);
196 }
197 break;
198 }
199 }
200}
201
202inline u8 ease8(EaseType type, u8 i) {
203 switch (type) {
204 case EASE_NONE: return i;
205 case EASE_IN_QUAD: return easeInQuad8(i);
206 case EASE_OUT_QUAD: return easeOutQuad8(i);
207 case EASE_IN_OUT_QUAD: return easeInOutQuad8(i);
208 case EASE_IN_CUBIC: return easeInCubic8(i);
209 case EASE_OUT_CUBIC: return easeOutCubic8(i);
210 case EASE_IN_OUT_CUBIC: return easeInOutCubic8(i);
211 case EASE_IN_SINE: return easeInSine8(i);
212 case EASE_OUT_SINE: return easeOutSine8(i);
213 case EASE_IN_OUT_SINE: return easeInOutSine8(i);
214 default: return i;
215 }
216}
217
218inline void ease8(EaseType type, u8* src, u8* dst, u8 count) {
219 switch (type) {
220 case EASE_NONE: return;
221 case EASE_IN_QUAD: {
222 for (u8 i = 0; i < count; i++) {
223 dst[i] = easeInQuad8(src[i]);
224 }
225 break;
226 }
227 case EASE_OUT_QUAD: {
228 for (u8 i = 0; i < count; i++) {
229 dst[i] = easeOutQuad8(src[i]);
230 }
231 break;
232 }
233 case EASE_IN_OUT_QUAD: {
234 for (u8 i = 0; i < count; i++) {
235 dst[i] = easeInOutQuad8(src[i]);
236 }
237 break;
238 }
239 case EASE_IN_CUBIC: {
240 for (u8 i = 0; i < count; i++) {
241 dst[i] = easeInCubic8(src[i]);
242 }
243 break;
244 }
245 case EASE_OUT_CUBIC: {
246 for (u8 i = 0; i < count; i++) {
247 dst[i] = easeOutCubic8(src[i]);
248 }
249 break;
250 }
251 case EASE_IN_OUT_CUBIC: {
252 for (u8 i = 0; i < count; i++) {
253 dst[i] = easeInOutCubic8(src[i]);
254 }
255 break;
256 }
257 case EASE_IN_SINE: {
258 for (u8 i = 0; i < count; i++) {
259 dst[i] = easeInSine8(src[i]);
260 }
261 break;
262 }
263 case EASE_OUT_SINE: {
264 for (u8 i = 0; i < count; i++) {
265 dst[i] = easeOutSine8(src[i]);
266 }
267 break;
268 }
269 case EASE_IN_OUT_SINE: {
270 for (u8 i = 0; i < count; i++) {
271 dst[i] = easeInOutSine8(src[i]);
272 }
273 break;
274 }
275 }
276}
277
278} // namespace fl
#define FL_PROGMEM
PROGMEM keyword for storage.
Wrapper definitions to allow seamless use of PROGMEM in environments that have it.
unsigned char u8
Definition int.h:17
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:317
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:107
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:170
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:161
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:120
u16 ease16(EaseType type, u16 i)
Definition ease.h:126
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:228
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:133
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:265
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:98
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:49
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:296
const u16 gamma_2_8[256]
Definition ease.cpp:20
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:206
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:252
u8 ease8(EaseType type, u8 i)
Definition ease.h:202
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:75
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:239
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:186
EaseType
Definition ease.h:21
@ EASE_OUT_CUBIC
Definition ease.h:27
@ EASE_NONE
Definition ease.h:22
@ EASE_IN_QUAD
Definition ease.h:23
@ EASE_IN_SINE
Definition ease.h:29
@ EASE_OUT_SINE
Definition ease.h:30
@ EASE_IN_CUBIC
Definition ease.h:26
@ EASE_OUT_QUAD
Definition ease.h:24
@ EASE_IN_OUT_CUBIC
Definition ease.h:28
@ EASE_IN_OUT_SINE
Definition ease.h:31
@ EASE_IN_OUT_QUAD
Definition ease.h:25
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:180
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:55
IMPORTANT!
Definition crgb.h:20