Main MRPT website > C++ reference for MRPT 1.5.7
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 /*-------------------------------------------------------------
21  hsv2rgb
22 -------------------------------------------------------------*/
24  float h,
25  float s,
26  float v,
27  float &r,
28  float &g,
29  float &b)
30 {
31  // See: http://en.wikipedia.org/wiki/HSV_color_space
32  h = max(0.0f, min(1.0f,h));
33  s = max(0.0f, min(1.0f,s));
34  v = max(0.0f, min(1.0f,v));
35 
36  int Hi = ((int)floor(h *6)) % 6;
37  float f = (h*6) - Hi;
38  float p = v*(1-s);
39  float q = v*(1-f*s);
40  float t = v*(1-(1-f)*s);
41 
42  switch (Hi)
43  {
44  case 0: r=v; g=t;b=p; break;
45  case 1: r=q; g=v;b=p; break;
46  case 2: r=p; g=v;b=t; break;
47  case 3: r=p; g=q;b=v; break;
48  case 4: r=t; g=p;b=v; break;
49  case 5: r=v; g=p;b=q; break;
50  }
51 }
52 
53 /*-------------------------------------------------------------
54  rgb2hsv
55 -------------------------------------------------------------*/
57  float r,
58  float g,
59  float b,
60  float &h,
61  float &s,
62  float &v )
63 {
64  // See: http://en.wikipedia.org/wiki/HSV_color_space
65  r = max(0.0f, min(1.0f,r));
66  g = max(0.0f, min(1.0f,g));
67  b = max(0.0f, min(1.0f,b));
68 
69  float Max = max3(r,g,b);
70  float Min = min3(r,g,b);
71 
72  if (Max==Min)
73  {
74  h = 0;
75  }
76  else
77  {
78  if (Max==r)
79  {
80  if (g>=b)
81  h = (g-b)/(6*(Max-Min));
82  else h = 1-(g-b)/(6*(Max-Min));
83  }
84  else
85  if (Max==g)
86  h = 1/3.0f + (b-r)/(6*(Max-Min));
87  else h = 2/3.0f + (r-g)/(6*(Max-Min));
88  }
89 
90  if (Max == 0)
91  s = 0;
92  else s = 1 - Min/Max;
93 
94  v = Max;
95 }
96 
97 
98 /*-------------------------------------------------------------
99  colormap
100 -------------------------------------------------------------*/
102  const TColormap &color_map,
103  const float color_index,
104  float &r,
105  float &g,
106  float &b)
107 {
108  MRPT_START
109  switch (color_map)
110  {
111  case cmJET:
112  jet2rgb(color_index,r,g,b);
113  break;
114  case cmGRAYSCALE:
115  r = g = b = color_index;
116  break;
117  case cmHOT:
118  hot2rgb(color_index, r, g, b);
119  break;
120  default:
121  THROW_EXCEPTION("Invalid color_map");
122  };
123  MRPT_END
124 }
125 
126 /*-------------------------------------------------------------
127  jet2rgb
128 -------------------------------------------------------------*/
129 void mrpt::utils::jet2rgb(const float color_index, float &r, float &g, float &b)
130 {
131  static bool jet_table_done = false;
132  static Eigen::VectorXf jet_r,jet_g,jet_b;
133 
134 
135  // Initialize tables
136  if (!jet_table_done)
137  {
138  jet_table_done = true;
139 
140  // Refer to source code of "jet" in MATLAB:
141  double JET_R[] = { 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.062500,0.125000,0.187500,0.250000,0.312500,0.375000,0.437500,0.500000,0.562500,0.625000,0.687500,0.750000,0.812500,0.875000,0.937500,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,0.937500,0.875000,0.812500,0.750000,0.687500,0.625000,0.562500,0.500000 };
142  double JET_G[] = { 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.062500,0.125000,0.187500,0.250000,0.312500,0.375000,0.437500,0.500000,0.562500,0.625000,0.687500,0.750000,0.812500,0.875000,0.937500,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,0.937500,0.875000,0.812500,0.750000,0.687500,0.625000,0.562500,0.500000,0.437500,0.375000,0.312500,0.250000,0.187500,0.125000,0.062500,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 };
143  double JET_B[] = { 0.562500,0.625000,0.687500,0.750000,0.812500,0.875000,0.937500,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,0.937500,0.875000,0.812500,0.750000,0.687500,0.625000,0.562500,0.500000,0.437500,0.375000,0.312500,0.250000,0.187500,0.125000,0.062500,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 };
144  const size_t N = sizeof(JET_B)/sizeof(JET_B[0]);
145 
146  jet_r.resize(N);
147  jet_g.resize(N);
148  jet_b.resize(N);
149  for (size_t i=0;i<N;i++)
150  {
151  jet_r[i] = JET_R[i];
152  jet_g[i] = JET_G[i];
153  jet_b[i] = JET_B[i];
154  }
155  }
156 
157  // Return interpolate value:
158  r = math::interpolate(color_index, jet_r, 0.0f,1.0f);
159  g = math::interpolate(color_index, jet_g, 0.0f,1.0f);
160  b = math::interpolate(color_index, jet_b, 0.0f,1.0f);
161 }
162 
163 void mrpt::utils::hot2rgb(const float color_index, float &r, float &g, float &b)
164 {
165  static bool table_done = false;
166  static Eigen::VectorXf hot_r, hot_g, hot_b;
167 
168  // Initialize tables
169  if (!table_done)
170  {
171  table_done = true;
172 
173  // Refer to source code of "hot" in MATLAB:
174  double HOT_R[] = { 0.041667,0.083333,0.125000,0.166667,0.208333,0.250000,0.291667,0.333333,0.375000,0.416667,0.458333,0.500000,0.541667,0.583333,0.625000,0.666667,0.708333,0.750000,0.791667,0.833333,0.875000,0.916667,0.958333,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000 };
175  double HOT_G[] = { 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.041667,0.083333,0.125000,0.166667,0.208333,0.250000,0.291667,0.333333,0.375000,0.416667,0.458333,0.500000,0.541667,0.583333,0.625000,0.666667,0.708333,0.750000,0.791667,0.833333,0.875000,0.916667,0.958333,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000 };
176  double HOT_B[] = { 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.062500,0.125000,0.187500,0.250000,0.312500,0.375000,0.437500,0.500000,0.562500,0.625000,0.687500,0.750000,0.812500,0.875000,0.937500,1.000000 };
177  const size_t N = sizeof(HOT_B) / sizeof(HOT_B[0]);
178 
179  hot_r.resize(N);
180  hot_g.resize(N);
181  hot_b.resize(N);
182  for (size_t i = 0; i<N; i++)
183  {
184  hot_r[i] = HOT_R[i];
185  hot_g[i] = HOT_G[i];
186  hot_b[i] = HOT_B[i];
187  }
188  }
189 
190  // Return interpolate value:
191  r = math::interpolate(color_index, hot_r, 0.0f, 1.0f);
192  g = math::interpolate(color_index, hot_g, 0.0f, 1.0f);
193  b = math::interpolate(color_index, hot_b, 0.0f, 1.0f);
194 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
GLdouble GLdouble t
Definition: glext.h:3610
#define min(a, b)
const T min3(const T &A, const T &B, const T &C)
Definition: bits.h:128
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:30
[New in MRPT 1.5.0]
Definition: color_maps.h:34
#define THROW_EXCEPTION(msg)
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3626
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 BASE_IMPEXP 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:101
GLdouble s
Definition: glext.h:3602
#define MRPT_END
GLubyte g
Definition: glext.h:5575
GLubyte GLubyte b
Definition: glext.h:5575
#define MRPT_START
const GLdouble * v
Definition: glext.h:3603
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
void BASE_IMPEXP 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:163
void BASE_IMPEXP 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:129
const T max3(const T &A, const T &B, const T &C)
Definition: bits.h:129
void BASE_IMPEXP 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:23
void BASE_IMPEXP 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:56
GLfloat GLfloat p
Definition: glext.h:5587



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019