-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorgen.cpp
75 lines (67 loc) · 3.63 KB
/
colorgen.cpp
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
#include <arduino.h>
#include <stdint.h>
#include "colorgen.h"
uint8_t rainbow_r[256]={255,252,249,246,243,240,237,234,231,228,225,222,219,216,213,210,207,204,201,198,195,192,189,186,183,180,177,174,171,168,165,162,
159,156,153,150,147,144,141,138,135,132,129,127,124,121,118,115,112,109,106,103,100,97,94,91,88,85,82,79,76,73,70,67,
64,61,58,55,52,49,46,43,40,37,34,31,28,25,22,19,16,13,10,7,4,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,
64,67,70,73,76,79,82,85,88,91,94,97,100,103,106,109,112,115,118,121,124,127,129,132,135,138,141,144,147,150,153,156,
159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252};
uint8_t rainbow_g[256]={0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,
96,99,102,105,108,111,114,117,120,123,126,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,176,179,182,185,188,
191,194,197,200,203,206,209,212,215,218,221,224,227,230,233,236,239,242,245,248,251,254,253,250,247,244,241,238,235,232,229,226,
223,220,217,214,211,208,205,202,199,196,193,190,187,184,181,178,175,172,169,166,163,160,157,154,151,148,145,142,139,136,133,130,
128,125,122,119,116,113,110,107,104,101,98,95,92,89,86,83,80,77,74,71,68,65,62,59,56,53,50,47,44,41,38,35,
32,29,26,23,20,17,14,11,8,5,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
uint8_t rainbow_b[256]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,8,11,14,17,20,23,26,29,
32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83,86,89,92,95,98,101,104,107,110,113,116,119,122,125,
128,130,133,136,139,142,145,148,151,154,157,160,163,166,169,172,175,178,181,184,187,190,193,196,199,202,205,208,211,214,217,220,
223,226,229,232,235,238,241,244,247,250,253,254,251,248,245,242,239,236,233,230,227,224,221,218,215,212,209,206,203,200,197,194,
191,188,185,182,179,176,173,170,167,164,161,158,155,152,149,146,143,140,137,134,131,128,126,123,120,117,114,111,108,105,102,99,
96,93,90,87,84,81,78,75,72,69,66,63,60,57,54,51,48,45,42,39,36,33,30,27,24,21,18,15,12,9,6,3};
colorgen::colorgen(char *name):parametric(name) {
r = g = b = 0;
mincol = newintparam("min",0,255,0);
maxcol = newintparam("max",0,255,255);
newcoltime = newintparam("time",10,1000,100);
startr = startg = startb = 0;
colorstep=0;
newnextcol();
}
void colorgen::newnextcol() {
int dist = maxcol->d.i.val - mincol->d.i.val;
int start = mincol->d.i.val;
if (dist < 0) {
dist = - dist;
start = maxcol->d.i.val;
}
int q = start + rand() % (dist+1);
if (q < 0) q = 0;
if (q > 255) q = 255;
nextr = rainbow_r[q];
nextg = rainbow_g[q];
nextb = rainbow_b[q];
}
uint8_t colorgen::interpolate8(uint8_t start, uint8_t stop, uint16_t step, uint16_t steps) {
return (uint8_t)((int32_t)start + ((int32_t)stop - start) * step / steps);
}
void colorgen::tick() {
if (colorstep >= newcoltime->d.i.val) {
// New target color
startr = r;
startg = g;
startb = b;
colorstep=0;
newnextcol();
}
r = interpolate8(startr, nextr, colorstep, newcoltime->d.i.val);
g = interpolate8(startg, nextg, colorstep, newcoltime->d.i.val);
b = interpolate8(startb, nextb, colorstep, newcoltime->d.i.val);
colorstep++;
}