-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap2colour.cpp
247 lines (204 loc) · 5.3 KB
/
map2colour.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
//
// FILE: map2colour.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// PURPOSE: Arduino library for mapping a float to colour spectrum
// URL: https://github.com/RobTillaart/map2colour
#include "map2colour.h"
#define M2C_MIN_SIZE 7
map2colour::map2colour(uint8_t size)
{
_size = size;
if (_size < M2C_MIN_SIZE) _size = M2C_MIN_SIZE;
_Red = (uint8_t *)malloc(_size);
_Green = (uint8_t *)malloc(_size);
_Blue = (uint8_t *)malloc(_size);
// for backwards compatibility
uint8_t r[7] = { 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF };
uint8_t g[7] = { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF };
uint8_t b[7] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
memcpy(_Red, r, 7);
memcpy(_Green, g, 7);
memcpy(_Blue, b, 7);
}
map2colour::~map2colour()
{
if (_Red) free(_Red);
if (_Green) free(_Green);
if (_Blue) free(_Blue);
}
bool map2colour::begin(float * values, uint32_t * colourMap)
{
if ((_Red == NULL) || (_Green == NULL) || (_Blue == NULL))
{
// need error codes?
return false;
}
// split colour map in channels to allow interpolation per channel
if (colourMap != NULL)
{
for (int i = 0; i < _size; i++)
{
uint32_t val = colourMap[i];
_Blue[i] = val & 0xFF;
val >>= 8;
_Green[i] = val & 0xFF;
val >>= 8;
_Red[i] = val & 0xFF;
}
}
_values = values;
for (int index = 1; index < _size; index++)
{
float den = _values[index] - _values[index - 1];
if (den < 0.0) return false;
}
return true;
}
uint8_t map2colour::size()
{
return _size;
}
uint32_t map2colour::map2RGB(float value)
{
uint8_t index = 1;
// default values + out of lower range
uint8_t R = _Red[0];
uint8_t G = _Green[0];
uint8_t B = _Blue[0];
if (_values[0] < value)
{
if (value < _values[_size-1] )
{
// search the interval
while (_values[index] < value) index++;
// base value
R = _Red[index];
G = _Green[index];
B = _Blue[index];
// calculate the interpolation factor
// map2colourFast uses pre calculated dividers (costs 24 bytes extra RAM).
float factor = (_values[index] - value) / (_values[index] - _values[index - 1]);
// interpolate only if delta != 0
int delta = _Red[index] - _Red[index - 1];
if (delta != 0 ) R -= factor * delta;
delta = _Green[index] - _Green[index - 1];
if (delta != 0 ) G -= factor * delta;
delta = _Blue[index] - _Blue[index - 1];
if (delta != 0 ) B -= factor * delta;
}
else
{
// out of upper range
R = _Red[_size-1];
G = _Green[_size-1];
B = _Blue[_size-1];
}
}
uint32_t colour = R;
colour <<= 8;
colour |= G;
colour <<= 8;
colour |= B;
return colour;
}
uint16_t map2colour::map2_565(float value)
{
uint32_t RGB = map2RGB(value);
uint16_t colour = 0x0000;
RGB >>= 3;
colour |= (RGB & 0x001F); // B channel 5 bits
RGB >>= 2;
colour |= (RGB & 0x07E0); // G channel 6 bits
RGB >>= 3;
colour |= (RGB & 0xF800); // R channel 5 bits
return colour;
}
/////////////////////////////////////////////////////////////////////////
//
// DERIVED CLASS
//
map2colourFast::map2colourFast(uint8_t size) : map2colour(size)
{
divFactor = (float *) malloc(size * sizeof(float));
}
map2colourFast::~map2colourFast()
{
if (divFactor) free(divFactor);
}
bool map2colourFast::begin(float * values, uint32_t * colourMap)
{
// load the colour-map and check non-decreasing order.
bool OK = map2colour::begin(values, colourMap);
// pre-calculate dividers
for (int index = 1; index < _size; index++)
{
float divider = _values[index] - _values[index - 1];
if (divider > 0)
{
divFactor[index - 1] = 1.0 / divider;
}
else
{
divFactor[index - 1] = 0;
}
}
return OK;
}
uint32_t map2colourFast::map2RGB(float value)
{
int index = 1;
// default values + out of lower range
uint8_t R = _Red[0];
uint8_t G = _Green[0];
uint8_t B = _Blue[0];
if (_values[0] < value)
{
if (value < _values[_size-1] )
{
// search the interval
while (_values[index] < value) index++;
// base value
R = _Red[index];
G = _Green[index];
B = _Blue[index];
// calculate the interpolation factor
float factor = (_values[index] - value) * divFactor[index - 1];
// interpolate if delta <> 0
int delta = _Red[index] - _Red[index - 1];
if (delta != 0 ) R -= factor * delta;
delta = _Green[index] - _Green[index - 1];
if (delta != 0 ) G -= factor * delta;
delta = _Blue[index] - _Blue[index - 1];
if (delta != 0 ) B -= factor * delta;
}
else
{
// out of upper range
R = _Red[_size-1];
G = _Green[_size-1];
B = _Blue[_size-1];
}
}
uint32_t colour = R;
colour <<= 8;
colour |= G;
colour <<= 8;
colour |= B;
return colour;
}
// could be slightly faster (~0.4 us) for AVR by
// - split of R = RGB >> 16 (one third could be 16 bit math.
uint16_t map2colourFast::map2_565(float value)
{
uint32_t RGB = map2RGB(value);
uint16_t colour = 0x0000;
RGB >>= 3;
colour |= (RGB & 0x001F); // B channel 5 bits
RGB >>= 2;
colour |= (RGB & 0x07E0); // G channel 6 bits
RGB >>= 3;
colour |= (RGB & 0xF800); // R channel 5 bits
return colour;
}
// -- END OF FILE --