-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGradient.cpp
141 lines (125 loc) · 2.84 KB
/
Gradient.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
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
#include "Gradient.h"
Gradient::Gradient()
: m_stepCount( 0 )
{
}
void Gradient::clearSteps( )
{
m_stepCount = 0;
}
void Gradient::addStep( uint8_t pos, uint32_t color )
{
if ( m_stepCount < 10 )
{
m_steps[ m_stepCount ].pos = pos;
m_steps[ m_stepCount ].color = color;
m_stepCount++;
}
}
void Gradient::setSteps( Step *steps, uint8_t stepCount )
{
m_stepCount = min( stepCount, 10 );
for ( int i = 0; i < m_stepCount; ++i )
{
m_steps[ i ] = steps[ i ];
}
}
uint32_t Gradient::getColor( uint8_t pos )
{
if ( pos <= m_steps[ 0 ].pos )
{
return m_steps[ 0 ].color;
}
else if ( pos >= m_steps[ m_stepCount - 1 ].pos )
{
return m_steps[ m_stepCount - 1 ].color;
}
else
{
int i = 0;
while ( i < ( m_stepCount - 1 ) &&
!( pos >= m_steps[ i ].pos && pos < m_steps[ i + 1 ].pos ) )
{
i++;
}
if ( i >= ( m_stepCount - 1 ) )
{
return 0;
}
if ( m_steps[ i + 1 ].pos == m_steps[ i ].pos )
{
return m_steps[ i ].color;
}
uint8_t f = ( pos - m_steps[ i ].pos ) * 255 /
( m_steps[ i + 1 ].pos - m_steps[ i ].pos );
return Stripper::ColorBlend( m_steps[ i ].color, m_steps[ i + 1 ].color, f );
}
}
/*
void Gradient::smear()
{
uint8_t prevVal, savePixel;
uint16_t _numPixels = strip->numPixels();
if (!strip || !strip->numPixels( ) || !_pixels)
{
return;
}
_pixels[0] = (uint8_t)((int)(_pixels[0] + _pixels[1])/3);
prevVal = _pixels[0];
for (int i=1; i < _numPixels - 1; i++)
{
savePixel = _pixels[i];
_pixels[i] = (uint8_t)((int)(_pixels[i] + _pixels[i+1] + prevVal)/3);
prevVal = savePixel;
}
_pixels[_numPixels-1] = (uint8_t)((int)(_pixels[_numPixels] + _pixels[prevVal])/3);
updateStrip();
}
void Gradient::randomize()
{
for (int i=0; i < strip->numPixels( ); i++)
{
_pixels[i] = random(255);
}
updateStrip();
}
void Gradient::randomize(int low, int high)
{
for (int i=0; i < strip->numPixels( ); i++)
{
_pixels[i] = random(low, high);
}
updateStrip();
}
void Gradient::peturb(int low, int high)
{
int temp;
for (int i=0; i < strip->numPixels( ); i++)
{
temp = _pixels[i] + (random(low, high));
if (temp < 0) temp = 0;
if (temp > 255) temp = 255;
_pixels[i] = temp;
}
updateStrip();
}
void Gradient::fade()
{
for (int i=0; i < strip->numPixels( ); i++)
{
if (_pixels[i] < 4)
_pixels[i] = 0;
else
_pixels[i] -= 4;
}
updateStrip();
}
void Gradient::wipe(byte level)
{
for (int i=0; i < strip->numPixels( ); i++)
{
_pixels[i] = level;
}
updateStrip();
}
*/