ColorUtils.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
27 
28 #include "MathUtils.h"
29 
31 {
32 
33  DrawColor24Bit
34  HsvToRgb(const HsvColor& in)
35  {
36  double hh, p, q, t, ff;
37  long i;
38  double r, g, b;
39  if (in.s <= 0.0) // < is bogus, just shuts up warnings
40  {
41  r = in.v;
42  g = in.v;
43  b = in.v;
44  return DrawColor24Bit{(Ice::Byte)(r * 255), (Ice::Byte)(g * 255), (Ice::Byte)(b * 255)};
45  }
46  hh = in.h;
47  if (hh >= 360.0)
48  {
49  hh = 0.0;
50  }
51  hh /= 60.0;
52  i = (long)hh;
53  ff = hh - i;
54  p = in.v * (1.0 - in.s);
55  q = in.v * (1.0 - (in.s * ff));
56  t = in.v * (1.0 - (in.s * (1.0 - ff)));
57 
58  switch (i)
59  {
60  case 0:
61  r = in.v;
62  g = t;
63  b = p;
64  break;
65  case 1:
66  r = q;
67  g = in.v;
68  b = p;
69  break;
70  case 2:
71  r = p;
72  g = in.v;
73  b = t;
74  break;
75 
76  case 3:
77  r = p;
78  g = q;
79  b = in.v;
80  break;
81  case 4:
82  r = t;
83  g = p;
84  b = in.v;
85  break;
86  case 5:
87  default:
88  r = in.v;
89  g = p;
90  b = q;
91  break;
92  }
93 
94  return DrawColor24Bit{(Ice::Byte)(r * 255), (Ice::Byte)(g * 255), (Ice::Byte)(b * 255)};
95  }
96 
97  HsvColor
98  RgbToHsv(const DrawColor24Bit& in)
99  {
100  double r = 0.00392156862 * in.r;
101  double g = 0.00392156862 * in.g;
102  double b = 0.00392156862 * in.b;
103  HsvColor out;
104  double min, max, delta;
105 
106  min = r < g ? r : g;
107  min = min < b ? min : b;
108 
109  max = r > g ? r : g;
110  max = max > b ? max : b;
111 
112  out.v = max; // v
113  delta = max - min;
114  if (delta < 0.00001)
115  {
116  out.s = 0;
117  out.h = 0; // undefined, maybe nan?
118  return out;
119  }
120  if (max > 0.0) // NOTE: if Max is == 0, this divide would cause a crash
121  {
122  out.s = (delta / max); // s
123  }
124  else
125  {
126  // if max is 0, then r = g = b = 0
127  // s = 0, h is undefined
128  out.s = 0.0;
129  out.h = 0; // its now undefined
130  return out;
131  }
132  if (r >= max) // > is bogus, just keeps compilor happy
133  {
134  out.h = (g - b) / delta; // between yellow & magenta
135  }
136  else if (g >= max)
137  {
138  out.h = 2.0 + (b - r) / delta; // between cyan & yellow
139  }
140  else
141  {
142  out.h = 4.0 + (r - g) / delta; // between magenta & cyan
143  }
144 
145  out.h *= 60.0; // degrees
146 
147  if (out.h < 0.0)
148  {
149  out.h += 360.0;
150  }
151 
152  return out;
153  }
154 
155  /**
156  * @brief HeatMapColor calculates the color of a value between 0 and 1 on a heat map.
157  * @param percentage value between 0..1
158  * @return color on a heatmap corresponding to parameter. 0 -> blue, 1 -> red. Color has full (255) saturation and value.
159  */
160  HsvColor
161  HeatMapColor(float percentage)
162  {
163  percentage = math::MathUtils::LimitMinMax(0.0f, 1.0f, percentage);
164 
165  return HsvColor{((1.0f - percentage) * 240.f), 1.0f, 1.0f};
166  }
167 
168  DrawColor24Bit
169  HeatMapRGBColor(float percentage)
170  {
171  return HsvToRgb(HeatMapColor(percentage));
172  }
173 
174  DrawColor
175  HeatMapRGBAColor(float percentage)
176  {
177  auto color = HsvToRgb(HeatMapColor(percentage));
178  return DrawColor{0.0039215686f * color.r, //divide by 255
179  0.0039215686f * color.g,
180  0.0039215686f * color.b,
181  1.0};
182  }
183 
184 } // namespace armarx::colorutils
MathUtils.h
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:297
armarx::colorutils::HeatMapRGBColor
DrawColor24Bit HeatMapRGBColor(float percentage)
Definition: ColorUtils.h:169
q
#define q
armarx::math::MathUtils::LimitMinMax
static int LimitMinMax(int min, int max, int value)
Definition: MathUtils.h:43
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:327
armarx::colorutils
Definition: ColorUtils.h:30
armarx::colorutils::HeatMapRGBAColor
DrawColor HeatMapRGBAColor(float percentage)
Definition: ColorUtils.h:175
armarx::colorutils::RgbToHsv
HsvColor RgbToHsv(const DrawColor24Bit &in)
Definition: ColorUtils.h:98
armarx::colorutils::HeatMapColor
HsvColor HeatMapColor(float percentage)
HeatMapColor calculates the color of a value between 0 and 1 on a heat map.
Definition: ColorUtils.h:161
armarx::colorutils::HsvToRgb
DrawColor24Bit HsvToRgb(const HsvColor &in)
Definition: ColorUtils.h:34