-
Notifications
You must be signed in to change notification settings - Fork 2
/
colormap.h
243 lines (231 loc) · 6.84 KB
/
colormap.h
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
/*! \file
* \author Christian Knapp and Matthias Wiesenberger; colormap designed by Josef Peer
* \date 14.03.2012
*
* Originally written for convection code and adapted for C++.
*/
#ifndef _DG_TEXTURE_
#define _DG_TEXTURE_
#include <cmath>
namespace draw{
/*! @brief POD that contains RGB values for 256 colors*/
struct Color
{
float r; //!< R contains red value for color
float g; //!< G contains green value for color
float b; //!< B contains blue value for color
};
/**
* @brief A Colormap from black - blue over white to red and gold
*/
struct ColorMapRedBlueExt
{
/*! @brief Create an extended redblue colormap
the extra colors are black beyond the blue and gold in the infrared
@param scale The scale specifies which value corresponds to red
(-scale corresponds to blue, 0 to white)
*/
ColorMapRedBlueExt( float scale = 1.);
//maps [-scale, scale] to a color
/**
* @brief map a value to a color
*
* @param x value
*
* @return corresponding color according to the colormap
*/
Color operator()( float x);
/**
* @brief Set the scale
*
* @return reference to the scale value
*/
float& scale( ) { return scale_;}
/**
* @brief The scale currently in use
*
* @return The scale currently in use
*/
float scale() const { return scale_;}
private:
void initcolorarray();
float scale_;
Color M[384];
};
ColorMapRedBlueExt::ColorMapRedBlueExt( float scale): scale_(scale)
{
initcolorarray();
}
// Maps scale to Red and -scale to Blue, > scale to gold and < -scale to black
//on device direct evaluation is faster (probably map remains on host??)
/*
Color ColorMapRedBlueExt::operator()( float x)
{
float scalefact = 127./scale_;
x = scalefact*x + 192; // +192 instead of +128 due to extended colormap
x = x<0 ? 0 : ( x>383 ? 383 : x ); //clip values
Color c;
x/= 64.;
if( x < 1.) { c.r = 0.; c.g = 0.; c.b = 0.5*x;}
else if( x < 2. ) { x-= 1.; c.r = 0.; c.g = 0.25*x; c.b = 0.5 + (0.5*x);}
else if( x < 3. ) { x-= 2.; c.r = x; c.g = 0.25 + 0.75*x; c.b = 1.;}
else if( x < 4. ) { x-= 3.; c.r = 1.0; c.g = 1.0 - x; c.b = 1. - x;}
else if( x < 5. ) { x-= 4.; c.r = 1.0 - 0.5*x; c.g = 0.; c.b = 0.;}
else if( x < 6. ) { x-= 5.; c.r = 0.5 + 0.5*x; c.g = x; c.b = 0.;}
return c;
}
*/
Color ColorMapRedBlueExt::operator()( float x)
{
Color c;
float scalefact = 127./scale_;
int k;
k = (int)floor(scalefact*x) + 192; // +192 instead of +128 due to extended colormap
k = k<0 ? 0 : ( k>383 ? 383 : k ); //clip values
c.r = M[k].r;
c.g = M[k].g;
c.b = M[k].b;
return c;
}
void ColorMapRedBlueExt::initcolorarray() {
float scal = 1.0/64.0;
for ( int i=0; i < 64; i++) {
M[i].r = 0.0;
M[i].g = 0.0;
M[i].b = 0.5*scal*(float)i;
}
for ( int i=0; i < 64; i++) {
M[i+64].r = 0.0;
M[i+64].g = 0.25*scal*(float)i;
M[i+64].b = 0.5 + (0.5*scal*(float)i);
}
for ( int i=0; i < 64; i++) {
M[i+128].r = scal*(float)i;
M[i+128].g = 0.25 + (0.75*scal*(float)i);
M[i+128].b = 1.0;
}
for ( int i=0; i < 64; i++) {
M[i+192].r = 1.0;
M[i+192].g = 1.0 - (scal*(float)i);
M[i+192].b = 1.0 - (scal*(float)i);
}
for ( int i=0; i < 64; i++) {
M[i+256].r = 1.0 - (0.5*scal*(float)i);
M[i+256].g = 0.0;
M[i+256].b = 0.0;
}
for ( int i=0; i < 64; i++) {
M[i+320].r = 0.5 + (0.5*scal*(float)i);
M[i+320].g = scal*(float)i;
M[i+320].b = 0.0;
}
M[383].b = 5.0;
}
/*! @addtogroup Utility
* @{
*/
/**
* @brief A Colormap from black - blue over white to red and gold with
* arbitrary minumum and maximum value
*/
struct ColorMapRedBlueExtMinMax
{
/*! @brief Create an extended redblue colormap
the extra colors are black beyond the blue and gold in the infrared
@param scalemin The scale specifies which value corresponds to blue
@param scalemax The scale specifies which value corresponds to red
*/
ColorMapRedBlueExtMinMax(float scalemin=-1.,float scalemax=1.);
//maps [-scale, scale] to a color
/**
* @brief map a value to a color
*
* @param x value
*
* @return corresponding color according to the colormap
*/
Color operator()( float x);
/**
* @brief Set the minimum scale
*
* @return reference to the minimum scale value
*/
float& scalemin( ) { return scalemin_;}
/**
* @brief Set the maximum scale
*
* @return reference to the maximum scale value
*/
float& scalemax( ) { return scalemax_;}
/**
* @brief The minimum scale currently in use
*
* @return The minimum scale currently in use
*/
float scalemin() const { return scalemin_;}
/**
* @brief The maximum scale currently in use
*
* @return The maximum scale currently in use
*/
float scalemax() const { return scalemax_;}
private:
void initcolorarray();
float scalemin_;
float scalemax_;
Color M[384];
};
ColorMapRedBlueExtMinMax::ColorMapRedBlueExtMinMax(float scalemin,float scalemax): scalemin_(scalemin),scalemax_(scalemax)
{
initcolorarray();
}
Color ColorMapRedBlueExtMinMax::operator()( float x)
{
Color c;
float scalewidth = (scalemax_-scalemin_)*0.5;
float scalefact = 127./scalewidth;
int k;
k = (int)floor((x -scalemin_-scalewidth)*scalefact) + 192; // +192 instead of +128 due to extended colormap
k = k<0 ? 0 : ( k>383 ? 383 : k ); //clip values
c.r = M[k].r;
c.g = M[k].g;
c.b = M[k].b;
return c;
}
void ColorMapRedBlueExtMinMax::initcolorarray() {
float scal = 1.0/64.0;
for ( int i=0; i < 64; i++) {
M[i].r = 0.0;
M[i].g = 0.0;
M[i].b = 0.5*scal*(float)i;
}
for ( int i=0; i < 64; i++) {
M[i+64].r = 0.0;
M[i+64].g = 0.25*scal*(float)i;
M[i+64].b = 0.5 + (0.5*scal*(float)i);
}
for ( int i=0; i < 64; i++) {
M[i+128].r = scal*(float)i;
M[i+128].g = 0.25 + (0.75*scal*(float)i);
M[i+128].b = 1.0;
}
for ( int i=0; i < 64; i++) {
M[i+192].r = 1.0;
M[i+192].g = 1.0 - (scal*(float)i);
M[i+192].b = 1.0 - (scal*(float)i);
}
for ( int i=0; i < 64; i++) {
M[i+256].r = 1.0 - (0.5*scal*(float)i);
M[i+256].g = 0.0;
M[i+256].b = 0.0;
}
for ( int i=0; i < 64; i++) {
M[i+320].r = 0.5 + (0.5*scal*(float)i);
M[i+320].g = scal*(float)i;
M[i+320].b = 0.0;
}
M[383].b = 5.0;
}
///@}
} //namespace draw
#endif // _DG_TEXTURE_