-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGradient.cpp
More file actions
190 lines (145 loc) · 5.37 KB
/
Gradient.cpp
File metadata and controls
190 lines (145 loc) · 5.37 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
/*
Copyright 2014 Mark Briggs
This file is part of ColorRing.
ColorRing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
ColorRing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
There is a copy of the GNU General Public License in the
accompanying COPYING file
*/
#include "Gradient.h"
Gradient::Gradient() {
}
Gradient::Gradient(PixelColor startPixelColor, PixelColor endPixelColor, word numTweens) {
this->startPixelColor = startPixelColor;
this->endPixelColor = endPixelColor;
this->numTweens = numTweens; // Inclusive of first & last
byte tempDiff;
// Red
if (endPixelColor.R - startPixelColor.R >= 0) {
rIncrDir = true; // Byte Increases (gets brighter)
tempDiff = endPixelColor.R - startPixelColor.R;
} else {
rIncrDir = false; // Byte Decreases (gets dimmer)
tempDiff = startPixelColor.R - endPixelColor.R;
}
rIncrAmt1024 = (((unsigned long)tempDiff) * 1024) / (numTweens - 1); // *1024 to keep some accuracy in the math ('cuz 1.5=>1, but 1500=>1500)
// Green
if (endPixelColor.G - startPixelColor.G >= 0) {
gIncrDir = true; // Byte Increases (gets brighter)
tempDiff = endPixelColor.G - startPixelColor.G;
} else {
gIncrDir = false; // Byte Decreases (gets dimmer)
tempDiff = startPixelColor.G - endPixelColor.G;
}
gIncrAmt1024 = (((unsigned long)tempDiff) * 1024) / (numTweens - 1);
// Blue
if (endPixelColor.B - startPixelColor.B >= 0) {
bIncrDir = true; // Byte Increases (gets brighter)
tempDiff = endPixelColor.B - startPixelColor.B;
} else {
bIncrDir = false; // Byte Decreases (gets dimmer)
tempDiff = startPixelColor.B - endPixelColor.B;
}
bIncrAmt1024 = (((unsigned long)tempDiff) * 1024) / (numTweens - 1);
}
PixelColor Gradient::getTweenPixelColor(word tweenNum) {
PixelColor newColor;
if (tweenNum == 0) {
newColor = startPixelColor;
} else if (tweenNum >= numTweens - 1) {
newColor = endPixelColor;
} else {
byte newR, newG, newB;
// Red
if (rIncrDir == true) {
newR = startPixelColor.R + (tweenNum * rIncrAmt1024 / 1024);
} else {
newR = startPixelColor.R - (tweenNum * rIncrAmt1024 / 1024);
}
// Green
if (gIncrDir == true) {
newG = startPixelColor.G + (tweenNum * gIncrAmt1024 / 1024);
} else {
newG = startPixelColor.G - (tweenNum * gIncrAmt1024 / 1024);
}
// Blue
if (bIncrDir == true) {
newB = startPixelColor.B + (tweenNum * bIncrAmt1024 / 1024);
} else {
newB = startPixelColor.B - (tweenNum * bIncrAmt1024 / 1024);
}
newColor = PixelColor(newR, newG, newB);
}
//Serial.print("newColor: "); newColor.println();
return newColor;
}
/********************
MultiGradient
********************/
MultiGradient::MultiGradient() {
isValid = false;
}
MultiGradient::MultiGradient(PixelColor *colorSeriesArr, byte numColorsInSeries, byte numPixelsEachColor, bool gradiateLastPixelFirstColor) {
isValid = true;
if (numColorsInSeries < 2 || numPixelsEachColor < 1) {
isValid = false;
return; // Nothing more to do.
}
numPixels = numColorsInSeries * numPixelsEachColor; // Inclusive
byte numGradients = gradiateLastPixelFirstColor ? numColorsInSeries : numColorsInSeries - 1;
if (numGradients == 255) { numGradients = 0; }
//cout << F("NumGradients: ") << (int)numGradients << endl;
float fNumIncrPerGradientSection;
if (gradiateLastPixelFirstColor) {
fNumIncrPerGradientSection = numPixelsEachColor;
} else {
fNumIncrPerGradientSection = (numPixels-1) * 1.0 / (numColorsInSeries - 1);
}
//cout << F("fNumIncrPerGradientSection: ") << fNumIncrPerGradientSection << endl;
PixelColor startPixelColor, endPixelColor;
word startPixel, endPixel, numTweens;
word ctr = 0;
for (int currGradNum = 0; currGradNum < numGradients; currGradNum++) {
//cout << F("currGradNum: ") << (int)currGradNum << endl;
startPixelColor = colorSeriesArr[currGradNum];
if (gradiateLastPixelFirstColor && (currGradNum == numGradients - 1)) {
endPixelColor = colorSeriesArr[0];
} else {
endPixelColor = colorSeriesArr[currGradNum+1];
}
startPixel = (int)(fNumIncrPerGradientSection * currGradNum);
endPixel = (int)((fNumIncrPerGradientSection * (currGradNum + 1)) - 1);
//cout << F("startPixel: ") << (int)startPixel << F(", endPixel: ") << (int)endPixel << endl;
numTweens = endPixel - startPixel + 1 + 1; // +1 to get to next gradient's START pixel, +1 more to be inclusive
Gradient grad = Gradient(startPixelColor, endPixelColor, numTweens);
for (int currTweenNum = 0; currTweenNum < numTweens-1; currTweenNum++) {
PixelColor pc = grad.getTweenPixelColor(currTweenNum);
//cout << F("ctr: ") << (int)ctr << endl;
//cout << F("pc: "); pc.println();
mgArr.push_back(pc);
ctr++;
}
}
// Last Pixel
mgArr.push_back((gradiateLastPixelFirstColor ? colorSeriesArr[0] : colorSeriesArr[numColorsInSeries-1]));
//cout << F("mgArr size: ") << mgArr.size() << endl;
isValid = true;
}
/*
word MultiGradient::getNumPixels() {
// Inclusive
return numPixels;
}
*/
PixelColor MultiGradient::getTweenPixelColor(word tweenNum) {
return mgArr[tweenNum % numPixels];
}
bool MultiGradient::getIsValid() {
return isValid;
}