Main MRPT website > C++ reference for MRPT 1.9.9
color_maps.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/color_maps.h>
13 #include <mrpt/math/interp_fit.hpp>
14 
15 using namespace mrpt;
16 using namespace mrpt::utils;
17 using namespace std;
18 
19 /*-------------------------------------------------------------
20  hsv2rgb
21 -------------------------------------------------------------*/
23  float h, float s, float v, float& r, float& g, float& b)
24 {
25  // See: http://en.wikipedia.org/wiki/HSV_color_space
26  h = max(0.0f, min(1.0f, h));
27  s = max(0.0f, min(1.0f, s));
28  v = max(0.0f, min(1.0f, v));
29 
30  int Hi = ((int)floor(h * 6)) % 6;
31  float f = (h * 6) - Hi;
32  float p = v * (1 - s);
33  float q = v * (1 - f * s);
34  float t = v * (1 - (1 - f) * s);
35 
36  switch (Hi)
37  {
38  case 0:
39  r = v;
40  g = t;
41  b = p;
42  break;
43  case 1:
44  r = q;
45  g = v;
46  b = p;
47  break;
48  case 2:
49  r = p;
50  g = v;
51  b = t;
52  break;
53  case 3:
54  r = p;
55  g = q;
56  b = v;
57  break;
58  case 4:
59  r = t;
60  g = p;
61  b = v;
62  break;
63  case 5:
64  r = v;
65  g = p;
66  b = q;
67  break;
68  }
69 }
70 
71 /*-------------------------------------------------------------
72  rgb2hsv
73 -------------------------------------------------------------*/
75  float r, float g, float b, float& h, float& s, float& v)
76 {
77  // See: http://en.wikipedia.org/wiki/HSV_color_space
78  r = max(0.0f, min(1.0f, r));
79  g = max(0.0f, min(1.0f, g));
80  b = max(0.0f, min(1.0f, b));
81 
82  float Max = max3(r, g, b);
83  float Min = min3(r, g, b);
84 
85  if (Max == Min)
86  {
87  h = 0;
88  }
89  else
90  {
91  if (Max == r)
92  {
93  if (g >= b)
94  h = (g - b) / (6 * (Max - Min));
95  else
96  h = 1 - (g - b) / (6 * (Max - Min));
97  }
98  else if (Max == g)
99  h = 1 / 3.0f + (b - r) / (6 * (Max - Min));
100  else
101  h = 2 / 3.0f + (r - g) / (6 * (Max - Min));
102  }
103 
104  if (Max == 0)
105  s = 0;
106  else
107  s = 1 - Min / Max;
108 
109  v = Max;
110 }
111 
112 /*-------------------------------------------------------------
113  colormap
114 -------------------------------------------------------------*/
116  const TColormap& color_map, const float color_index, float& r, float& g,
117  float& b)
118 {
119  MRPT_START
120  switch (color_map)
121  {
122  case cmJET:
123  jet2rgb(color_index, r, g, b);
124  break;
125  case cmGRAYSCALE:
126  r = g = b = color_index;
127  break;
128  case cmHOT:
129  hot2rgb(color_index, r, g, b);
130  break;
131  default:
132  THROW_EXCEPTION("Invalid color_map");
133  };
134  MRPT_END
135 }
136 
137 /*-------------------------------------------------------------
138  jet2rgb
139 -------------------------------------------------------------*/
140 void mrpt::utils::jet2rgb(const float color_index, float& r, float& g, float& b)
141 {
142  static bool jet_table_done = false;
143  static Eigen::VectorXf jet_r, jet_g, jet_b;
144 
145  // Initialize tables
146  if (!jet_table_done)
147  {
148  jet_table_done = true;
149 
150  // Refer to source code of "jet" in MATLAB:
151  double JET_R[] = {
152  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
153  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
154  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
155  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
156  0.062500, 0.125000, 0.187500, 0.250000, 0.312500, 0.375000,
157  0.437500, 0.500000, 0.562500, 0.625000, 0.687500, 0.750000,
158  0.812500, 0.875000, 0.937500, 1.000000, 1.000000, 1.000000,
159  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
160  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
161  1.000000, 1.000000, 0.937500, 0.875000, 0.812500, 0.750000,
162  0.687500, 0.625000, 0.562500, 0.500000};
163  double JET_G[] = {
164  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
165  0.000000, 0.000000, 0.062500, 0.125000, 0.187500, 0.250000,
166  0.312500, 0.375000, 0.437500, 0.500000, 0.562500, 0.625000,
167  0.687500, 0.750000, 0.812500, 0.875000, 0.937500, 1.000000,
168  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
169  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
170  1.000000, 1.000000, 1.000000, 1.000000, 0.937500, 0.875000,
171  0.812500, 0.750000, 0.687500, 0.625000, 0.562500, 0.500000,
172  0.437500, 0.375000, 0.312500, 0.250000, 0.187500, 0.125000,
173  0.062500, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
174  0.000000, 0.000000, 0.000000, 0.000000};
175  double JET_B[] = {
176  0.562500, 0.625000, 0.687500, 0.750000, 0.812500, 0.875000,
177  0.937500, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
178  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
179  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
180  0.937500, 0.875000, 0.812500, 0.750000, 0.687500, 0.625000,
181  0.562500, 0.500000, 0.437500, 0.375000, 0.312500, 0.250000,
182  0.187500, 0.125000, 0.062500, 0.000000, 0.000000, 0.000000,
183  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
184  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
185  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
186  0.000000, 0.000000, 0.000000, 0.000000};
187  const size_t N = sizeof(JET_B) / sizeof(JET_B[0]);
188 
189  jet_r.resize(N);
190  jet_g.resize(N);
191  jet_b.resize(N);
192  for (size_t i = 0; i < N; i++)
193  {
194  jet_r[i] = JET_R[i];
195  jet_g[i] = JET_G[i];
196  jet_b[i] = JET_B[i];
197  }
198  }
199 
200  // Return interpolate value:
201  r = math::interpolate(color_index, jet_r, 0.0f, 1.0f);
202  g = math::interpolate(color_index, jet_g, 0.0f, 1.0f);
203  b = math::interpolate(color_index, jet_b, 0.0f, 1.0f);
204 }
205 
206 void mrpt::utils::hot2rgb(const float color_index, float& r, float& g, float& b)
207 {
208  static bool table_done = false;
209  static Eigen::VectorXf hot_r, hot_g, hot_b;
210 
211  // Initialize tables
212  if (!table_done)
213  {
214  table_done = true;
215 
216  // Refer to source code of "hot" in MATLAB:
217  double HOT_R[] = {
218  0.041667, 0.083333, 0.125000, 0.166667, 0.208333, 0.250000,
219  0.291667, 0.333333, 0.375000, 0.416667, 0.458333, 0.500000,
220  0.541667, 0.583333, 0.625000, 0.666667, 0.708333, 0.750000,
221  0.791667, 0.833333, 0.875000, 0.916667, 0.958333, 1.000000,
222  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
223  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
224  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
225  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
226  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
227  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
228  1.000000, 1.000000, 1.000000, 1.000000};
229  double HOT_G[] = {
230  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
231  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
232  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
233  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
234  0.041667, 0.083333, 0.125000, 0.166667, 0.208333, 0.250000,
235  0.291667, 0.333333, 0.375000, 0.416667, 0.458333, 0.500000,
236  0.541667, 0.583333, 0.625000, 0.666667, 0.708333, 0.750000,
237  0.791667, 0.833333, 0.875000, 0.916667, 0.958333, 1.000000,
238  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
239  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
240  1.000000, 1.000000, 1.000000, 1.000000};
241  double HOT_B[] = {
242  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
243  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
244  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
245  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
246  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
247  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
248  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
249  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
250  0.062500, 0.125000, 0.187500, 0.250000, 0.312500, 0.375000,
251  0.437500, 0.500000, 0.562500, 0.625000, 0.687500, 0.750000,
252  0.812500, 0.875000, 0.937500, 1.000000};
253  const size_t N = sizeof(HOT_B) / sizeof(HOT_B[0]);
254 
255  hot_r.resize(N);
256  hot_g.resize(N);
257  hot_b.resize(N);
258  for (size_t i = 0; i < N; i++)
259  {
260  hot_r[i] = HOT_R[i];
261  hot_g[i] = HOT_G[i];
262  hot_b[i] = HOT_B[i];
263  }
264  }
265 
266  // Return interpolate value:
267  r = math::interpolate(color_index, hot_r, 0.0f, 1.0f);
268  g = math::interpolate(color_index, hot_g, 0.0f, 1.0f);
269  b = math::interpolate(color_index, hot_b, 0.0f, 1.0f);
270 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble t
Definition: glext.h:3689
#define min(a, b)
const T min3(const T &A, const T &B, const T &C)
Definition: bits.h:156
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:31
[New in MRPT 1.5.0]
Definition: color_maps.h:36
#define THROW_EXCEPTION(msg)
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
T interpolate(const T &x, const VECTOR &ys, const T &x0, const T &x1)
Interpolate a data sequence "ys" ranging from "x0" to "x1" (equally spaced), to obtain the approximat...
Definition: interp_fit.hpp:17
STL namespace.
void colormap(const TColormap &color_map, const float color_index, float &r, float &g, float &b)
Transform a float number in the range [0,1] into RGB components.
Definition: color_maps.cpp:115
GLdouble s
Definition: glext.h:3676
#define MRPT_END
GLubyte g
Definition: glext.h:6279
GLubyte GLubyte b
Definition: glext.h:6279
#define MRPT_START
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
void hot2rgb(const float color_index, float &r, float &g, float &b)
Computes the RGB color components (range [0,1]) for the corresponding color index in the range [0...
Definition: color_maps.cpp:206
void jet2rgb(const float color_index, float &r, float &g, float &b)
Computes the RGB color components (range [0,1]) for the corresponding color index in the range [0...
Definition: color_maps.cpp:140
const T max3(const T &A, const T &B, const T &C)
Definition: bits.h:161
void hsv2rgb(float h, float s, float v, float &r, float &g, float &b)
Transform HSV color components to RGB, all of them in the range [0,1].
Definition: color_maps.cpp:22
void rgb2hsv(float r, float g, float b, float &h, float &s, float &v)
Transform RGB color components to HSV, all of them in the range [0,1].
Definition: color_maps.cpp:74
GLfloat GLfloat p
Definition: glext.h:6305



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019