2
* colourmap.cpp - contiuum colourmap header
3
* Copyright (C) 2010, ViewerGTKQt project
4
* Modifed by D Haley 2013
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "colourmap.h"
26
#include "common/translation.h"
28
void jetColorMap(unsigned char *rgb,float value,float min,float max)
30
float max4=(max-min)/4;
34
rgb[0]=rgb[1]=rgb[2]=255;
38
rgb[0]=rgb[1]=rgb[2]=0;
45
rgb[2]=c1+(unsigned char)((255-c1)*value/max4);
47
else if (value<2*max4)
50
rgb[1]=(unsigned char)(255*(value-max4)/max4);
53
else if (value<3*max4)
55
rgb[0]=(unsigned char)(255*(value-2*max4)/max4);
62
rgb[1]=(unsigned char)(255-255*(value-3*max4)/max4);
71
void hotColorMap(unsigned char *rgb,float value,float min,float max)
73
float max3=(max-min)/3;
76
{rgb[0]=rgb[1]=rgb[2]=255;}
78
{rgb[0]=rgb[1]=rgb[2]=0;}
80
{rgb[0]=(unsigned char)(255*value/max3);rgb[1]=0;rgb[2]=0;}
82
{rgb[0]=255;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=0;}
84
{rgb[0]=255;rgb[1]=255;rgb[2]=(unsigned char)(255*(value-2*max3)/max3);}
85
else {rgb[0]=rgb[1]=rgb[2]=255;}
88
void coldColorMap(unsigned char *rgb,float value,float min,float max)
90
float max3=(max-min)/3;
93
{rgb[0]=rgb[1]=rgb[2]=255;}
95
{rgb[0]=rgb[1]=rgb[2]=0;}
97
{rgb[0]=0;rgb[1]=0;rgb[2]=(unsigned char)(255*value/max3);}
99
{rgb[0]=0;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=255;}
101
{rgb[0]=(unsigned char)(255*(value-2*max3)/max3);rgb[1]=255;rgb[2]=255;}
102
else {rgb[0]=rgb[1]=rgb[2]=255;}
105
void blueColorMap(unsigned char *rgb,float value,float min,float max)
109
{rgb[0]=rgb[1]=rgb[2]=255;}
111
{rgb[0]=rgb[1]=rgb[2]=0;}
113
{rgb[0]=0;rgb[1]=0;rgb[2]=(unsigned char)(255*value/max);}
114
else {rgb[0]=rgb[1]=0;rgb[2]=255;}
117
void positiveColorMap(unsigned char *rgb,float value,float min,float max)
124
rgb[0]=rgb[1]=rgb[2]=0;
128
rgb[0]=rgb[1]=rgb[2]=255;
132
rgb[0]=192;rgb[1]=0;rgb[2]=0;
133
rgb[0]+=(unsigned char)(63*value);
134
rgb[1]+=(unsigned char)(255*value);
136
rgb[2]+=(unsigned char)(255*2*(value-0.5));
139
void negativeColorMap(unsigned char *rgb,float value,float min,float max)
143
rgb[0]=0;rgb[1]=0;rgb[2]=0;
145
if(max>std::numeric_limits<float>::epsilon())
153
rgb[1]+=(unsigned char)(255*value);
155
rgb[2]+=(unsigned char)(255*2*(value-0.5));
159
void colorMap(unsigned char *rgb,float value,float min,float max)
162
positiveColorMap(rgb,value,0,max);
164
negativeColorMap(rgb,value,min,0);
167
void cyclicColorMap(unsigned char *rgb,float value,float min,float max)
169
float max3=(max-min)/3;
170
value-=(max-min)*(float)floor((value-min)/(max-min));
172
{rgb[0]=(unsigned char)(255-255*value/max3);rgb[1]=0;rgb[2]=255-rgb[0];}
173
else if(value<2*max3)
174
{rgb[0]=0;rgb[1]=(unsigned char)(255*(value-max3)/max3);rgb[2]=255-rgb[1];}
176
{rgb[0]=(unsigned char)(255*(value-2*max3)/max3);rgb[1]=255-rgb[0];rgb[2]=0;}
179
void randColorMap(unsigned char *rgb,float value,float min,float max)
181
srand((int)(65000*(value-min)/(max-min)));
182
rgb[0]=(unsigned char)(255*rand());
183
rgb[1]=(unsigned char)(255*rand());
184
rgb[2]=(unsigned char)(255*rand());
187
void grayColorMap(unsigned char *rgb,float value,float min,float max)
191
rgb[0]=rgb[1]=rgb[2]=(unsigned char)(255*value/max);
194
void colourMapWrap(unsigned int mapID,unsigned char *rgb, float v, float min, float max)
196
//Colour functions assume positive value, so remap
200
//Select the desired colour map
204
jetColorMap(rgb, v, min, max);
207
hotColorMap(rgb, v, min, max);
210
coldColorMap(rgb, v, min, max);
213
grayColorMap(rgb, v, min, max);
216
cyclicColorMap(rgb, v, min, max);
219
colorMap(rgb, v, min, max);
222
blueColorMap(rgb, v, min, max);
225
randColorMap(rgb, v, min, max);
231
std::string getColourMapName(unsigned int mapID)
234
const char *mapNames[] = { NTRANS("Jet"),
241
NTRANS("Pseudo-Random")};
243
return TRANS(mapNames[mapID]);