color.h
Go to the documentation of this file.
1 #ifndef COLOR_H
2 #define COLOR_H
3 
4 #include <cmath>
5 #include <cstdlib>
6 
7 /// RGBA color.
8 typedef uint32_t Color;
9 
10 namespace ColorMap
11 {
12 
13  /// Color maps that associate a color to every float from [0..1].
14  /// This mimics some of the standard MATLABĀ® color maps, see
15  /// http://www.mathworks.de/de/help/matlab/ref/colormap.html.
16  enum Value
17  {
18  /// Ranges from blue to red, and passes through the colors cyan, yellow,
19  /// and orange
20  JET,
21  /// Consists of colors that are shades of green and yellow
23  /// Varies smoothly from red, through orange, to yellow
25  /// Consists of colors that are shades of cyan and magenta
27  };
28 
29 }
30 
31 template <typename T> const T&
32 clamp(const T& value, const T& low, const T& high)
33 {
34  return (value < low ? low : (value > high ? high : value));
35 }
36 
37 /** Get a color from R, G, and B values (chars). */
38 Color
39 getColorFromRGB(uint8_t r, uint8_t g, uint8_t b)
40 {
41  return static_cast<uint32_t>(r) << 16 |
42  static_cast<uint32_t>(g) << 8 |
43  static_cast<uint32_t>(b);
44 }
45 
46 /** Get a color from R, G, and B values (floats).
47  *
48  * The input values are mapped from [0..1] to [0..255]. Input value that are
49  * outside of the [0..1] region are clamped automatically. */
50 Color
51 getColorFromRGB(float r, float g, float b)
52 {
53  return getColorFromRGB(static_cast<uint8_t>(round(clamp(r, 0.0f, 1.0f) * 255)),
54  static_cast<uint8_t>(round(clamp(g, 0.0f, 1.0f) * 255)),
55  static_cast<uint8_t>(round(clamp(b, 0.0f, 1.0f) * 255)));
56 }
57 
58 /** Get R, G, and B values (chars) as array from a color. */
59 void
60 getRGBFromColor(Color color, uint8_t* rgb)
61 {
62  rgb[0] = (color >> 16) & 0xFF;
63  rgb[1] = (color >> 8) & 0xFF;
64  rgb[2] = (color >> 0) & 0xFF;
65 }
66 
67 /** Generate a random color. */
68 Color
70 {
71  uint8_t r = static_cast<uint8_t>((rand() % 256));
72  uint8_t g = static_cast<uint8_t>((rand() % 256));
73  uint8_t b = static_cast<uint8_t>((rand() % 256));
74  return getColorFromRGB(r, g, b);
75 }
76 
77 /** Get the color for a given number in [0..1] using a given colormap.
78  *
79  * Input values that are outside of the [0..1] region are clamped
80  * automatically.
81  *
82  * \see ColorMap */
83 Color
85 {
86  float v = clamp(value, 0.0f, 1.0f);
87  switch (colormap)
88  {
89  case ColorMap::JET:
90  {
91  float four_value = v * 4;
92  float r = std::min(four_value - 1.5, -four_value + 4.5);
93  float g = std::min(four_value - 0.5, -four_value + 3.5);
94  float b = std::min(four_value + 0.5, -four_value + 2.5);
95  return getColorFromRGB(r, g, b);
96  }
97  case ColorMap::SUMMER:
98  {
99  return getColorFromRGB(v, (1.0f + v) * 0.5f, 0.4f);
100  }
101  case ColorMap::AUTUMN:
102  {
103  return getColorFromRGB(1.0f, v, 0.0f);
104  }
105  case ColorMap::COOL:
106  {
107  return getColorFromRGB(v, 1.0f - v, 1.0f);
108  }
109  }
110  return generateRandomColor();
111 }
112 
113 #endif /* COLOR_H */
114 
ColorMap::AUTUMN
@ AUTUMN
Varies smoothly from red, through orange, to yellow.
Definition: color.h:24
ColorMap::COOL
@ COOL
Consists of colors that are shades of cyan and magenta.
Definition: color.h:26
ColorMap::Value
Value
Color maps that associate a color to every float from [0..1].
Definition: color.h:16
ColorMap::SUMMER
@ SUMMER
Consists of colors that are shades of green and yellow.
Definition: color.h:22
clamp
const T & clamp(const T &value, const T &low, const T &high)
Definition: color.h:32
Color
uint32_t Color
RGBA color.
Definition: color.h:8
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
generateRandomColor
Color generateRandomColor()
Generate a random color.
Definition: color.h:69
getColorFromRGB
Color getColorFromRGB(uint8_t r, uint8_t g, uint8_t b)
Get a color from R, G, and B values (chars).
Definition: color.h:39
getRGBFromColor
void getRGBFromColor(Color color, uint8_t *rgb)
Get R, G, and B values (chars) as array from a color.
Definition: color.h:60
ColorMap::JET
@ JET
Ranges from blue to red, and passes through the colors cyan, yellow, and orange.
Definition: color.h:20
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
colormap
This file was automatically created with "create_c++_header.sh".
Definition: colormap.h:9
ColorMap
Definition: color.h:10
getColor
Color getColor(float value, ColorMap::Value colormap=ColorMap::JET)
Get the color for a given number in [0..1] using a given colormap.
Definition: color.h:84
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
min
T min(T t1, T t2)
Definition: gdiam.h:42