-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathPatternRainbowSmoke.h
More file actions
360 lines (291 loc) · 9.83 KB
/
PatternRainbowSmoke.h
File metadata and controls
360 lines (291 loc) · 9.83 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Aurora: https://github.com/pixelmatix/aurora
* Copyright (c) 2014 Jason Coon
*
* Portions of this code are adapted from "Rainbow Smoke" by Jozsef Fejes: http://codegolf.stackexchange.com/a/22326
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PatternRainbowSmoke_H
#define PatternRainbowSmoke_H
class PatternRainbowSmoke : public Drawable {
private:
struct Point {
uint8_t x = 0;
uint8_t y = 0;
};
static const uint8_t NUMCOLORS = 11;
static const uint16_t COLOR_COUNT = 1024;
uint8_t startx = 15;
uint8_t starty = 15;
rgb24 colors[COLOR_COUNT];
bool hasColor[MATRIX_WIDTH][MATRIX_HEIGHT];
bool isAvailable[MATRIX_WIDTH][MATRIX_HEIGHT];
uint16_t currentColorIndex = 0;
uint8_t algorithm;
int colorDifference(rgb24 c1, rgb24 c2) {
int r = c1.red - c2.red;
int g = c1.green - c2.green;
int b = c1.blue - c2.blue;
return r * r + g * g + b * b;
}
void markAvailableNeighbors(Point point) {
for (int dy = -1; dy <= 1; dy++) {
int ny = point.y + dy;
if (ny == -1 || ny == MATRIX_HEIGHT)
continue;
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0)
continue;
int nx = point.x + dx;
if (nx == -1 || nx == MATRIX_WIDTH)
continue;
if (!hasColor[nx][ny]) {
isAvailable[nx][ny] = true;
}
}
}
}
Point getAvailablePoint(int algorithm, rgb24 color) {
switch (algorithm) {
case 0:
return getAvailablePointWithClosestNeighborColor(color);
case 1:
default:
return getAvailablePointWithClosestAverageNeighborColor(color);
}
}
Point getAvailablePointWithClosestNeighborColor(rgb24 color) {
Point best;
// find the pixel with the smallest difference between the current color and all of it's neighbors' colors
int smallestDifference = 999999;
for (int y = 0; y < MATRIX_HEIGHT; y++) {
for (int x = 0; x < MATRIX_WIDTH; x++) {
// skip any that arent' available
if (!isAvailable[x][y])
continue;
// loop through its neighbors
int smallestDifferenceAmongNeighbors = 999999;
for (int dy = -1; dy <= 1; dy++) {
if (y + dy == -1 || y + dy == MATRIX_HEIGHT)
continue;
for (int dx = -1; dx <= 1; dx++) {
if (x == 0 && y == 0)
continue;
if (x + dx == -1 || x + dx == MATRIX_WIDTH)
continue;
int nx = x + dx;
int ny = y + dy;
// skip any neighbors that don't already have a color
if (!hasColor[nx][ny])
continue;
rgb24 neighborColor = backgroundLayer.readPixel(nx, ny);
int difference = colorDifference(neighborColor, color);
if (difference < smallestDifferenceAmongNeighbors || (difference == smallestDifferenceAmongNeighbors && random(2) == 1)) {
smallestDifferenceAmongNeighbors = difference;
}
}
}
if (smallestDifferenceAmongNeighbors < smallestDifference || (smallestDifferenceAmongNeighbors == smallestDifference && random(2) == 1)) {
smallestDifference = smallestDifferenceAmongNeighbors;
best.x = x;
best.y = y;
}
}
}
return best;
}
Point getAvailablePointWithClosestAverageNeighborColor(rgb24 color) {
Point best;
int smallestAverageDifference = 999999;
for (int y = 0; y < MATRIX_HEIGHT; y++) {
for (int x = 0; x < MATRIX_WIDTH; x++) {
// skip any that arent' available
if (!isAvailable[x][y])
continue;
int neighborCount = 0;
int neighborColorDifferenceTotal = 0;
// loop through its neighbors
for (int dy = -1; dy <= 1; dy++) {
if (y + dy == -1 || y + dy == MATRIX_HEIGHT)
continue;
for (int dx = -1; dx <= 1; dx++) {
if (x + dx == -1 || x + dx == MATRIX_WIDTH)
continue;
int nx = x + dx;
int ny = y + dy;
// skip any neighbors that don't already have a color
if (!hasColor[nx][ny])
continue;
neighborCount++;
rgb24 neighborColor = backgroundLayer.readPixel(nx, ny);
int difference = colorDifference(neighborColor, color);
neighborColorDifferenceTotal += difference;
}
}
int averageDifferenceAmongNeighbors = neighborColorDifferenceTotal / neighborCount;
if (averageDifferenceAmongNeighbors < smallestAverageDifference || (averageDifferenceAmongNeighbors == smallestAverageDifference && random(2) == 1)) {
smallestAverageDifference = averageDifferenceAmongNeighbors;
best.x = x;
best.y = y;
}
}
}
return best;
}
void createPalette() {
int colorSort = random(4);
switch (colorSort) {
case 0:
createPaletteRGB();
shuffleColors();
break;
case 1:
createPaletteGBR();
shuffleColors();
break;
case 2:
createPaletteBRG();
shuffleColors();
break;
case 3:
createPaletteHSV();
break;
}
}
void createPaletteRGB() {
int i = 0;
for (int b = 0; b < NUMCOLORS; b++) {
for (int g = 0; g < NUMCOLORS; g++) {
for (int r = 0; r < NUMCOLORS; r++) {
rgb24 color;
color.red = r * 255 / (NUMCOLORS - 1);
color.green = g * 255 / (NUMCOLORS - 1);
color.blue = b * 255 / (NUMCOLORS - 1);
colors[i] = color;
i++;
if (i == COLOR_COUNT)
return;
}
}
}
}
void createPaletteGBR() {
int i = 0;
for (int r = 0; r < NUMCOLORS; r++) {
for (int b = 0; b < NUMCOLORS; b++) {
for (int g = 0; g < NUMCOLORS; g++) {
rgb24 color;
color.red = r * 255 / (NUMCOLORS - 1);
color.green = g * 255 / (NUMCOLORS - 1);
color.blue = b * 255 / (NUMCOLORS - 1);
colors[i] = color;
i++;
if (i == COLOR_COUNT)
return;
}
}
}
}
void createPaletteBRG() {
int i = 0;
for (int r = 0; r < NUMCOLORS; r++) {
for (int g = 0; g < NUMCOLORS; g++) {
for (int b = 0; b < NUMCOLORS; b++) {
rgb24 color;
color.red = r * 255 / (NUMCOLORS - 1);
color.green = g * 255 / (NUMCOLORS - 1);
color.blue = b * 255 / (NUMCOLORS - 1);
colors[i] = color;
i++;
if (i == COLOR_COUNT)
return;
}
}
}
}
void shuffleColors() {
for (int a = 0; a < COLOR_COUNT; a++)
{
int r = random(a, COLOR_COUNT);
rgb24 temp = colors[a];
colors[a] = colors[r];
colors[r] = temp;
}
}
void createPaletteHSV() {
int i = 0;
uint8_t startHue = random(0, 255);
for (uint8_t h = startHue; i < 1024; h += 8) {
for (uint16_t s = 0; s < 256; s += 16) {
if (i < COLOR_COUNT)
colors[i] = effects.HsvToRgb(h, s, 255);
i++;
}
for (uint16_t v = 256; v > 0; v -= 16) {
if (i < COLOR_COUNT)
colors[i] = effects.HsvToRgb(h, 255, v);
i++;
}
}
}
public:
PatternRainbowSmoke() {
name = (char *)"RainbowSmoke";
}
unsigned int drawFrame() {
if (currentColorIndex == 0) {
//randomSeed(analogRead(5));
backgroundLayer.fillScreen({ 0, 0, 0 });
createPalette();
algorithm = random(2);
// clear all flags
for (int y = 0; y < MATRIX_HEIGHT; y++) {
for (int x = 0; x < MATRIX_WIDTH; x++) {
hasColor[x][y] = false;
isAvailable[x][y] = false;
}
}
}
rgb24 color = colors[currentColorIndex];
Point point;
if (currentColorIndex == 0) {
// use a random starting point
point.x = random(32);
point.y = random(32);
}
else {
point = getAvailablePoint(algorithm, color);
}
isAvailable[point.x][point.y] = false;
hasColor[point.x][point.y] = true;
backgroundLayer.drawPixel(point.x, point.y, color);
markAvailableNeighbors(point);
currentColorIndex++;
if (currentColorIndex >= COLOR_COUNT) {
currentColorIndex = 0;
return 3000;
}
return 0;
}
void start() {
backgroundLayer.fillScreen({ 0, 0, 0 });
currentColorIndex = 0;
}
};
#endif