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 #include "MathUtils.h"
29 {
30 
31  DrawColor24Bit HsvToRgb(const HsvColor& in)
32  {
33  double hh, p, q, t, ff;
34  long i;
35  double r, g, b;
36  if (in.s <= 0.0) // < is bogus, just shuts up warnings
37  {
38  r = in.v;
39  g = in.v;
40  b = in.v;
41  return DrawColor24Bit {(Ice::Byte)(r * 255), (Ice::Byte)(g * 255), (Ice::Byte)(b * 255)};
42  }
43  hh = in.h;
44  if (hh >= 360.0)
45  {
46  hh = 0.0;
47  }
48  hh /= 60.0;
49  i = (long)hh;
50  ff = hh - i;
51  p = in.v * (1.0 - in.s);
52  q = in.v * (1.0 - (in.s * ff));
53  t = in.v * (1.0 - (in.s * (1.0 - ff)));
54 
55  switch (i)
56  {
57  case 0:
58  r = in.v;
59  g = t;
60  b = p;
61  break;
62  case 1:
63  r = q;
64  g = in.v;
65  b = p;
66  break;
67  case 2:
68  r = p;
69  g = in.v;
70  b = t;
71  break;
72 
73  case 3:
74  r = p;
75  g = q;
76  b = in.v;
77  break;
78  case 4:
79  r = t;
80  g = p;
81  b = in.v;
82  break;
83  case 5:
84  default:
85  r = in.v;
86  g = p;
87  b = q;
88  break;
89  }
90 
91  return DrawColor24Bit {(Ice::Byte)(r * 255), (Ice::Byte)(g * 255), (Ice::Byte)(b * 255)};
92  }
93 
94  HsvColor RgbToHsv(const DrawColor24Bit& in)
95  {
96  double r = 0.00392156862 * in.r;
97  double g = 0.00392156862 * in.g;
98  double b = 0.00392156862 * in.b;
99  HsvColor out;
100  double min, max, delta;
101 
102  min = r < g ? r : g;
103  min = min < b ? min : b;
104 
105  max = r > g ? r : g;
106  max = max > b ? max : b;
107 
108  out.v = max; // v
109  delta = max - min;
110  if (delta < 0.00001)
111  {
112  out.s = 0;
113  out.h = 0; // undefined, maybe nan?
114  return out;
115  }
116  if (max > 0.0) // NOTE: if Max is == 0, this divide would cause a crash
117  {
118  out.s = (delta / max); // s
119  }
120  else
121  {
122  // if max is 0, then r = g = b = 0
123  // s = 0, h is undefined
124  out.s = 0.0;
125  out.h = 0; // its now undefined
126  return out;
127  }
128  if (r >= max) // > is bogus, just keeps compilor happy
129  {
130  out.h = (g - b) / delta; // between yellow & magenta
131  }
132  else if (g >= max)
133  {
134  out.h = 2.0 + (b - r) / delta; // between cyan & yellow
135  }
136  else
137  {
138  out.h = 4.0 + (r - g) / delta; // between magenta & cyan
139  }
140 
141  out.h *= 60.0; // degrees
142 
143  if (out.h < 0.0)
144  {
145  out.h += 360.0;
146  }
147 
148  return out;
149 
150 
151  }
152 
153  /**
154  * @brief HeatMapColor calculates the color of a value between 0 and 1 on a heat map.
155  * @param percentage value between 0..1
156  * @return color on a heatmap corresponding to parameter. 0 -> blue, 1 -> red. Color has full (255) saturation and value.
157  */
158  HsvColor HeatMapColor(float percentage)
159  {
160  percentage = math::MathUtils::LimitMinMax(0.0f, 1.0f, percentage);
161 
162  return HsvColor {((1.0f - percentage) * 240.f), 1.0f, 1.0f};
163  }
164 
165  DrawColor24Bit HeatMapRGBColor(float percentage)
166  {
167  return HsvToRgb(HeatMapColor(percentage));
168  }
169 
170  DrawColor HeatMapRGBAColor(float percentage)
171  {
172  auto color = HsvToRgb(HeatMapColor(percentage));
173  return DrawColor {0.0039215686f * color.r, //divide by 255
174  0.0039215686f * color.g,
175  0.0039215686f * color.b,
176  1.0
177  };
178  }
179 
180 }
MathUtils.h
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::colorutils::HeatMapRGBColor
DrawColor24Bit HeatMapRGBColor(float percentage)
Definition: ColorUtils.h:165
q
#define q
armarx::math::MathUtils::LimitMinMax
static int LimitMinMax(int min, int max, int value)
Definition: MathUtils.h:39
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::colorutils
Definition: ColorUtils.h:28
armarx::colorutils::HeatMapRGBAColor
DrawColor HeatMapRGBAColor(float percentage)
Definition: ColorUtils.h:170
armarx::colorutils::RgbToHsv
HsvColor RgbToHsv(const DrawColor24Bit &in)
Definition: ColorUtils.h:94
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:158
armarx::colorutils::HsvToRgb
DrawColor24Bit HsvToRgb(const HsvColor &in)
Definition: ColorUtils.h:31