-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.cpp
242 lines (205 loc) · 7.38 KB
/
palette.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
#include "palette.h"
#include "debug.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
// palette definitions are taken from
// https://github.com/kbinani/colormap-shaders
float Palette::clamp(const float _v, const float _min, const float _max) {
if (_v < _min) {
return _min;
} else if (_max < _v) {
return _max;
} else {
return _v;
}
}
void BlackWhite::setColor(const float _val, const float _scale, unsigned char *_map) {
float v = clamp(_val, 0.0, 1.0);
*(_map + 0)= (unsigned char)(v * _scale);
*(_map + 1)= (unsigned char)(v * _scale);
*(_map + 2)= (unsigned char)(v * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Cool::setColor(const float _val, const float _scale, unsigned char *_map) {
float r = (1.0 + 1.0 / 63.0) * _val - 1.0 / 63.0;
float g = -(1.0 + 1.0 / 63.0) * _val + (1.0 + 1.0 / 63.0);
*(_map + 0)= (unsigned char)(clamp(r, 0.0, 1.0) * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(1.0 * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Hot::setColor(const float _val, const float _scale, unsigned char *_map) {
float r = (8.0 / 3.0 * _val);
float g = (8.0 / 3.0 * _val - 1.0);
float b = (4.0 * _val - 3.0);
*(_map + 0)= (unsigned char)(clamp(r, 0.0, 1.0) * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(clamp(b, 0.0, 1.0) * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Jet::setColor(const float _val, const float _scale, unsigned char *_map) {
float r;
if (_val < 0.7) {
r = 4.0 * _val - 1.5;
} else {
r = -4.0 * _val + 4.5;
}
float g;
if (_val < 0.5) {
g = 4.0 * _val - 0.5;
} else {
g = -4.0 * _val + 3.5;
}
float b;
if (_val < 0.3) {
b = 4.0 * _val + 0.5;
} else {
b = -4.0 * _val + 2.5;
}
*(_map + 0)= (unsigned char)(clamp(r, 0.0, 1.0) * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(clamp(b, 0.0, 1.0) * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void PurpleBlue::setColor(const float _val, const float _scale, unsigned char *_map) {
float r;
if (_val < 0.7520372909206926) {
r = (((9.68615208861418E+02 * _val - 1.16097242960380E+03) * _val + 1.06173672031378E+02) * _val - 1.68616613530379E+02) * _val + 2.56073136099945E+02;
} else {
r = -1.20830453148990E+01 * _val + 1.44337397593436E+01;
}
float g;
if (_val < 0.7485333535031721) {
g = (((-4.58537247030064E+02 * _val + 5.67323181593790E+02) * _val - 2.56714665792882E+02) * _val - 1.14205365680507E+02) * _val + 2.47073841488433E+02;
} else {
g = ((-2.99774273328017E+02 * _val + 4.12147041403012E+02) * _val - 2.49880079288168E+02) * _val + 1.93578601034431E+02;
}
float b;
if (_val < 0.7628468501376879) {
b = ((-5.44257972228224E+01 * _val + 2.70890554876532E+01) * _val - 9.12766750739247E+01) * _val + 2.52166182860177E+02;
} else {
b = (((4.55621137729287E+04 * _val - 1.59960900638524E+05) * _val + 2.09530452721547E+05) * _val - 1.21704642900945E+05) * _val + 2.66644674068694E+04;
}
*(_map + 0)= (unsigned char)(clamp(r / 255.0, 0.0, 1.0) * _scale);
*(_map + 1)= (unsigned char)(clamp(g / 255.0, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(clamp(b / 255.0, 0.0, 1.0) * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void HotMetal::setColor(const float _val, const float _scale, unsigned char *_map) {
float r;
if (_val < 0.0) {
r = 0.0;
} else if (_val <= 0.57147) {
r = 446.22 * _val / 255.0;
} else {
r = 1.0;
}
float g;
if (_val < 0.6) {
g = 0.0;
} else if (_val <= 0.95) {
g = ((_val - 0.6) * 728.57) / 255.0;
} else {
g = 1.0;
}
float b = 0.0;
*(_map + 0)= (unsigned char)(r * _scale);
*(_map + 1)= (unsigned char)(g * _scale);
*(_map + 2)= (unsigned char)(b * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Autumn::setColor(const float _val, const float _scale, unsigned char *_map) {
float r = 1.0;
float g = _val;
float b = 0.0;
*(_map + 0)= (unsigned char)(r * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(b * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Spring::setColor(const float _val, const float _scale, unsigned char *_map) {
float r = 1.0;
float g = _val;
float b = 1.0 - _val;
*(_map + 0)= (unsigned char)(r * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(clamp(b, 0.0, 1.0) * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Summer::setColor(const float _val, const float _scale, unsigned char *_map) {
float r = _val;
float g = 0.5 * _val + 0.5;
float b = 0.4;
*(_map + 0)= (unsigned char)(clamp(r, 0.0, 1.0) * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(b * _scale);
*(_map + 3)= (unsigned char)_scale;
}
void Winter::setColor(const float _val, const float _scale, unsigned char *_map) {
float r = 0.0;
float g = _val;
float b = -0.5 * _val + 1.0;
*(_map + 0)= (unsigned char)(r * _scale);
*(_map + 1)= (unsigned char)(clamp(g, 0.0, 1.0) * _scale);
*(_map + 2)= (unsigned char)(clamp(b, 0.0, 1.0) * _scale);
*(_map + 3)= (unsigned char)_scale;
}
PaletteList::PaletteList() {
items.clear();
map = NULL;
mapSize = 0;
items.push_back(new BlackWhite());
items.push_back(new Cool());
items.push_back(new Hot());
items.push_back(new Jet());
items.push_back(new PurpleBlue());
items.push_back(new HotMetal());
items.push_back(new Autumn());
items.push_back(new Spring());
items.push_back(new Summer());
items.push_back(new Winter());
}
PaletteList::~PaletteList() {
if (map) {
free(map);
}
mapSize = 0;
for (size_t i = 0; i < items.size(); i++) {
delete items[i];
}
}
const char * PaletteList::getName(const unsigned int _index) {
assert(_index < items.size());
return items[_index]->name;
}
unsigned int PaletteList::getCount(void) {
return (unsigned int)items.size();
}
unsigned char * PaletteList::generate(const char *_name, const unsigned int _depth) {
const unsigned int size = 1 << _depth;
for (size_t i = 0; i < items.size(); i++) {
Palette *pal = items[i];
if (strncmp(_name, pal->name, strlen(_name)) == 0) {
// allocate/reallocate palette for RGBA pixel format
if (mapSize == 0) {
assert(map == NULL);
map = (unsigned char *)malloc(size * 4);
} else if (mapSize < size) {
map = (unsigned char *)realloc(map, size * 4);
}
mapSize = size;
float scale = (float)mapSize - 1.0;
for (unsigned int i = 0; i < mapSize; i++) {
float val = (float)i / scale;
pal->setColor(val, scale, &map[i * 4]);
// D("%s RGBA %3d %3d %3d %3d\n", pal->name, map[i * 4 + 0], map[i * 4 + 1], map[i * 4 + 2], map[i * 4 + 3]);
}
D("selected palette %s, size %d\n", pal->name, mapSize);
return map;
}
}
assert(1 != 1);
// palette not found
return NULL;
}