-
Notifications
You must be signed in to change notification settings - Fork 10
/
colorObj.cpp
397 lines (294 loc) · 8.04 KB
/
colorObj.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <colorObj.h>
// Having the basics handy is very handy!
colorObj red(LC_RED);
colorObj blue(LC_BLUE);
colorObj white(LC_WHITE);
colorObj black(LC_BLACK);
colorObj green(LC_GREEN);
colorObj cyan(LC_CYAN);
colorObj magenta(LC_MAGENTA);
colorObj yellow(LC_YELLOW);
// colormapper extremes..
#define START_COLOR 0
#define END_COLOR 100
colorMapper mixMapper;
// ****** colorObj ******
colorObj::colorObj(RGBpack* buff) {
red = buff->r;
green = buff->g;
blue = buff->b;
}
colorObj::colorObj(byte inRed, byte inGreen, byte inBlue) { setColor(inRed,inGreen,inBlue); }
//colorObj::colorObj(colorObj* inColor) { setColor(inColor); }
colorObj::colorObj(word inColor16) { setColor(inColor16); }
colorObj::colorObj(void) { setColor(0,0,0); }
colorObj::~colorObj(void) { }
void colorObj::setColor(byte inRed, byte inGreen, byte inBlue) {
red = inRed;
green = inGreen;
blue = inBlue;
}
// Handed a packed 16 bit color we want to create one of our 24 bit colors.
// If it falls on one of our known colors, possibly it started out as one of ours?
// The switch statement will quickly bring it back to our original.
// Otherwise, we do the best we can with whatever bits we have to play with.
void colorObj::setColor(word color16) {
switch(color16) {
/*****************************************************************************************/
/************** This case block is auto generated by colorListGenerator.ino **************/
/*****************************************************************************************/
case 0x0 : // BLACK
red = 0;
green = 0;
blue = 0;
break;
case 0x3186 : // LC_CHARCOAL
red = 50;
green = 50;
blue = 50;
break;
case 0x8C71 : // DARK_GREY
red = 140;
green= 140;
blue = 140;
break;
case 0xBDD7 : // LC_GREY
red = 185;
green= 185;
blue = 185;
break;
case 0xFFDF : // LIGHT_GREY
red = 250;
green= 250;
blue = 250;
break;
case 0xFFFF : // WHITE
red = 255;
green= 255;
blue = 255;
break;
case 0xF800 : // RED
red = 255;
green = 0;
blue = 0;
break;
case 0xFC1A : // PINK
red = 255;
green= 130;
blue = 208;
break;
case 0x7E0 : // GREEN
red = 0;
green= 255;
blue = 0;
break;
case 0xE0 : // DARK_GREEN
red = 0;
green = 30;
blue = 0;
break;
case 0x18E0 : // OLIVE
red = 30;
green = 30;
blue = 1;
break;
case 0x1F : // BLUE
red = 0;
green = 0;
blue = 255;
break;
case 0xA67F : // LIGHT_BLUE
red = 164;
green = 205;
blue = 255;
break;
case 0x3 : // NAVY
red = 0;
green = 0;
blue = 30;
break;
case 0x881F : // PURPLE
red = 140;
green = 0;
blue = 255;
break;
case 0xDCBF : // LAVENDER
red = 218;
green = 151;
blue = 255;
break;
case 0xFC00 : // ORANGE
red = 255;
green = 128;
blue = 0;
break;
case 0x7FF : // CYAN
red = 0;
green = 255;
blue = 255;
break;
case 0xF81F : // MAGENTA
red = 255;
green = 0;
blue = 255;
break;
case 0xFFE0 : // YELLOW
red = 255;
green = 255;
blue = 0;
break;
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
default :
red = highByte(color16);
red = red & 0b11111000;
green = lowByte(color16>>5);
green = green << 2;
blue = lowByte(color16);
blue = blue << 3;
}
}
void colorObj::setColor(RGBpack* buff) {
red = buff->r;
green = buff->g;
blue = buff->b;
}
void colorObj::setColor(colorObj* inColor) {
red =inColor->getRed();
green =inColor->getGreen();
blue = inColor->getBlue();
}
// Copied from Adafruit'
word colorObj::getColor16(void) {
return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
}
// Average out the colors to a one byte greyscale value.
byte colorObj::getGreyscale(void) {
int sum;
float ave;
sum = red + green + blue;
ave = sum/3.0;
return round(ave);
}
byte colorObj::getRed(void) { return red; }
byte colorObj::getGreen(void) { return green; }
byte colorObj::getBlue(void) { return blue; }
// Pack up our color info.
RGBpack colorObj::packColor(void) {
RGBpack temp;
temp.r = red;
temp.g = green;
temp.b = blue;
return temp;
}
//Creates a new color by mixing yourself with some new color.
colorObj colorObj::mixColors(colorObj* mixinColor,byte mixPercent) {
if (mixPercent>=100) return *mixinColor;
else if (mixPercent<=0) return *this;
else {
mixMapper.setColors(this,mixinColor);
return mixMapper.map(mixPercent);
}
}
//Changes yourself by blending in some new color.
void colorObj::blend(colorObj* mixinColor,byte mixPercent) {
if (mixPercent>=100) { // If >= 100 means totally mixin color.
setColor(mixinColor);
} else if (mixPercent>0) { // So its NOT >= 100 but it is > 0.. Blend it.
mixMapper.setColors(this,mixinColor);
colorObj temp = mixMapper.map(mixPercent);
setColor(&temp);
} // Otherwise, there is no change.
}
#ifdef PRINT_COLOR
void colorObj::printRGB(void) {
Serial.print( "ColorObj RGB : ");
Serial.print(red);
Serial.print(", ");
Serial.print(green);
Serial.print(", ");
Serial.println(blue);
Serial.flush();
}
#endif
// ****** colorMapper ******
colorMapper::colorMapper(void) {
redMapper = new mapper(START_COLOR,END_COLOR,0,0);
greenMapper = new mapper(START_COLOR,END_COLOR,0,0);
blueMapper = new mapper(START_COLOR,END_COLOR,0,0);
}
colorMapper::colorMapper(colorObj* inStart, colorObj* inEnd) {
redMapper = new mapper(START_COLOR,END_COLOR,inStart->getRed(),inEnd->getRed());
greenMapper = new mapper(START_COLOR,END_COLOR,inStart->getGreen(),inEnd->getGreen());
blueMapper = new mapper(START_COLOR,END_COLOR,inStart->getBlue(),inEnd->getBlue());
}
colorMapper::colorMapper(word startC16,word endC16) {
colorObj startColor(startC16);
colorObj endColor(endC16);
redMapper = new mapper(START_COLOR,END_COLOR,startColor.getRed(),endColor.getRed());
greenMapper = new mapper(START_COLOR,END_COLOR,startColor.getGreen(),endColor.getGreen());
blueMapper = new mapper(START_COLOR,END_COLOR,startColor.getBlue(),endColor.getBlue());
}
colorMapper::~colorMapper(void) {
delete(redMapper);
delete(greenMapper);
delete(blueMapper);
}
void colorMapper::setColors(colorObj* inStart, colorObj* inEnd) {
redMapper->setValues(START_COLOR,END_COLOR,inStart->getRed(),inEnd->getRed());
greenMapper->setValues(START_COLOR,END_COLOR,inStart->getGreen(),inEnd->getGreen());
blueMapper->setValues(START_COLOR,END_COLOR,inStart->getBlue(),inEnd->getBlue());
}
colorObj colorMapper::map(float percent) {
colorObj theColor(
(byte)round(redMapper->map(percent)),
(byte)round(greenMapper->map(percent)),
(byte)round(blueMapper->map(percent))
);
return theColor;
}
colorObj colorMapper::Map(float percent) { return map(percent); }
#ifdef PRINT_COLOR
void colorMapper::printColors(void) {
colorObj startColor(
(byte)round(redMapper->map(START_COLOR)),
(byte)round(greenMapper->map(START_COLOR)),
(byte)round(blueMapper->map(START_COLOR))
);
colorObj endColor(
(byte)round(redMapper->map(END_COLOR)),
(byte)round(greenMapper->map(END_COLOR)),
(byte)round(blueMapper->map(END_COLOR))
);
Serial.println("Mapper start / end");
startColor.printRGB();
endColor.printRGB();
}
#endif
// ****** colorMultiMap ******
colorMultiMap::colorMultiMap(void) { }
colorMultiMap::~colorMultiMap(void) { }
void colorMultiMap::addColor(double inX, colorObj* color) {
if (color) {
redMap.addPoint(inX,color->getRed());
greenMap.addPoint(inX,color->getGreen());
blueMap.addPoint(inX,color->getBlue());
}
}
void colorMultiMap::clearMap(void) {
redMap.clearMap();
greenMap.clearMap();
blueMap.clearMap();
}
colorObj colorMultiMap::map(double inVal) {
colorObj result;
byte red;
byte green;
byte blue;
red = round(redMap.map(inVal));
green = round(greenMap.map(inVal));
blue = round(blueMap.map(inVal));
result.setColor(red,green,blue);
return result;
}