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.
8typedef uint32_t Color;
9
10namespace 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
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} // namespace ColorMap
30
31template <typename T>
32const T&
33clamp(const T& value, const T& low, const T& high)
34{
35 return (value < low ? low : (value > high ? high : value));
36}
37
38/** Get a color from R, G, and B values (chars). */
40getColorFromRGB(uint8_t r, uint8_t g, uint8_t b)
41{
42 return static_cast<uint32_t>(r) << 16 | 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. */
51getColorFromRGB(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. */
59void
60getRGBFromColor(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. */
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 */
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 }
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 */
uint32_t Color
RGBA color.
Definition color.h:8
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
Color generateRandomColor()
Generate a random color.
Definition color.h:69
Color getColorFromRGB(uint8_t r, uint8_t g, uint8_t b)
Get a color from R, G, and B values (chars).
Definition color.h:40
const T & clamp(const T &value, const T &low, const T &high)
Definition color.h:33
void getRGBFromColor(Color color, uint8_t *rgb)
Get R, G, and B values (chars) as array from a color.
Definition color.h:60
Value
Color maps that associate a color to every float from [0..1].
Definition color.h:17
@ COOL
Consists of colors that are shades of cyan and magenta.
Definition color.h:26
@ JET
Ranges from blue to red, and passes through the colors cyan, yellow, and orange.
Definition color.h:20
@ AUTUMN
Varies smoothly from red, through orange, to yellow.
Definition color.h:24
@ SUMMER
Consists of colors that are shades of green and yellow.
Definition color.h:22
This file was automatically created with "create_c++_header.sh".
Definition colormap.h:10