forked from FastLED/FastLED
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathease.h
More file actions
275 lines (241 loc) · 8.03 KB
/
ease.h
File metadata and controls
275 lines (241 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#pragma once
/*
This are accurate and tested easing functions.
Note that the easing functions in lib8tion.h are tuned are implemented wrong, such as easeInOutCubic8 and easeInOutCubic16.
Modern platforms are so fast that the extra performance is not needed, but accuracy is important.
*/
#include "fl/int.h"
namespace fl {
// LUT for 8-bit to 16-bit gamma correction at pow(x, 2.8)
u16 gamma_2_8(u8 value);
enum EaseType {
EASE_NONE,
EASE_IN_QUAD,
EASE_OUT_QUAD,
EASE_IN_OUT_QUAD,
EASE_IN_CUBIC,
EASE_OUT_CUBIC,
EASE_IN_OUT_CUBIC,
EASE_IN_SINE,
EASE_OUT_SINE,
EASE_IN_OUT_SINE,
};
// 8-bit easing functions
/// 8-bit quadratic ease-in function
/// Takes an input value 0-255 and returns an eased value 0-255
/// The curve starts slow and accelerates (ease-in only)
u8 easeInQuad8(u8 i);
/// 8-bit quadratic ease-out function
/// Takes an input value 0-255 and returns an eased value 0-255
/// The curve starts fast and decelerates (ease-out only)
u8 easeOutQuad8(u8 i);
/// 8-bit quadratic ease-in/ease-out function
/// Takes an input value 0-255 and returns an eased value 0-255
/// The curve starts slow, accelerates in the middle, then slows down again
u8 easeInOutQuad8(u8 i);
/// 8-bit cubic ease-in function
/// Takes an input value 0-255 and returns an eased value 0-255
/// More pronounced acceleration than quadratic
u8 easeInCubic8(u8 i);
/// 8-bit cubic ease-out function
/// Takes an input value 0-255 and returns an eased value 0-255
/// More pronounced deceleration than quadratic
u8 easeOutCubic8(u8 i);
/// 8-bit cubic ease-in/ease-out function
/// Takes an input value 0-255 and returns an eased value 0-255
/// More pronounced easing curve than quadratic
u8 easeInOutCubic8(u8 i);
/// 8-bit sine ease-in function
/// Takes an input value 0-255 and returns an eased value 0-255
/// Smooth sinusoidal acceleration
u8 easeInSine8(u8 i);
/// 8-bit sine ease-out function
/// Takes an input value 0-255 and returns an eased value 0-255
/// Smooth sinusoidal deceleration
u8 easeOutSine8(u8 i);
/// 8-bit sine ease-in/ease-out function
/// Takes an input value 0-255 and returns an eased value 0-255
/// Smooth sinusoidal acceleration and deceleration
u8 easeInOutSine8(u8 i);
// 16-bit easing functions
/// 16-bit quadratic ease-in function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeInQuad16(u16 i);
/// 16-bit quadratic ease-out function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeOutQuad16(u16 i);
/// 16-bit quadratic ease-in/ease-out function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeInOutQuad16(u16 i);
/// 16-bit cubic ease-in function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeInCubic16(u16 i);
/// 16-bit cubic ease-out function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeOutCubic16(u16 i);
/// 16-bit cubic ease-in/ease-out function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeInOutCubic16(u16 i);
/// 16-bit sine ease-in function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeInSine16(u16 i);
/// 16-bit sine ease-out function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeOutSine16(u16 i);
/// 16-bit sine ease-in/ease-out function
/// Takes an input value 0-65535 and returns an eased value 0-65535
u16 easeInOutSine16(u16 i);
u16 ease16(EaseType type, u16 i);
void ease16(EaseType type, u16* src, u16* dst, u16 count);
u8 ease8(EaseType type, u8 i);
void ease8(EaseType type, u8* src, u8* dst, u8 count);
//////// INLINE FUNCTIONS ////////
inline u16 ease16(EaseType type, u16 i) {
switch (type) {
case EASE_NONE: return i;
case EASE_IN_QUAD: return easeInQuad16(i);
case EASE_OUT_QUAD: return easeOutQuad16(i);
case EASE_IN_OUT_QUAD: return easeInOutQuad16(i);
case EASE_IN_CUBIC: return easeInCubic16(i);
case EASE_OUT_CUBIC: return easeOutCubic16(i);
case EASE_IN_OUT_CUBIC: return easeInOutCubic16(i);
case EASE_IN_SINE: return easeInSine16(i);
case EASE_OUT_SINE: return easeOutSine16(i);
case EASE_IN_OUT_SINE: return easeInOutSine16(i);
default: return i;
}
}
inline void ease16(EaseType type, u16* src, u16* dst, u16 count) {
switch (type) {
case EASE_NONE: return;
case EASE_IN_QUAD: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeInQuad16(src[i]);
}
break;
}
case EASE_OUT_QUAD: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeOutQuad16(src[i]);
}
break;
}
case EASE_IN_OUT_QUAD: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeInOutQuad16(src[i]);
}
break;
}
case EASE_IN_CUBIC: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeInCubic16(src[i]);
}
break;
}
case EASE_OUT_CUBIC: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeOutCubic16(src[i]);
}
break;
}
case EASE_IN_OUT_CUBIC: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeInOutCubic16(src[i]);
}
break;
}
case EASE_IN_SINE: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeInSine16(src[i]);
}
break;
}
case EASE_OUT_SINE: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeOutSine16(src[i]);
}
break;
}
case EASE_IN_OUT_SINE: {
for (u16 i = 0; i < count; i++) {
dst[i] = easeInOutSine16(src[i]);
}
break;
}
}
}
inline u8 ease8(EaseType type, u8 i) {
switch (type) {
case EASE_NONE: return i;
case EASE_IN_QUAD: return easeInQuad8(i);
case EASE_OUT_QUAD: return easeOutQuad8(i);
case EASE_IN_OUT_QUAD: return easeInOutQuad8(i);
case EASE_IN_CUBIC: return easeInCubic8(i);
case EASE_OUT_CUBIC: return easeOutCubic8(i);
case EASE_IN_OUT_CUBIC: return easeInOutCubic8(i);
case EASE_IN_SINE: return easeInSine8(i);
case EASE_OUT_SINE: return easeOutSine8(i);
case EASE_IN_OUT_SINE: return easeInOutSine8(i);
default: return i;
}
}
inline void ease8(EaseType type, u8* src, u8* dst, u8 count) {
switch (type) {
case EASE_NONE: return;
case EASE_IN_QUAD: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeInQuad8(src[i]);
}
break;
}
case EASE_OUT_QUAD: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeOutQuad8(src[i]);
}
break;
}
case EASE_IN_OUT_QUAD: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeInOutQuad8(src[i]);
}
break;
}
case EASE_IN_CUBIC: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeInCubic8(src[i]);
}
break;
}
case EASE_OUT_CUBIC: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeOutCubic8(src[i]);
}
break;
}
case EASE_IN_OUT_CUBIC: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeInOutCubic8(src[i]);
}
break;
}
case EASE_IN_SINE: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeInSine8(src[i]);
}
break;
}
case EASE_OUT_SINE: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeOutSine8(src[i]);
}
break;
}
case EASE_IN_OUT_SINE: {
for (u8 i = 0; i < count; i++) {
dst[i] = easeInOutSine8(src[i]);
}
break;
}
}
}
} // namespace fl