colorUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef COLOR_UTILS_H
19 #define COLOR_UTILS_H
20 
21 #include <algorithm>
22 #include <math.h>
23 
24 namespace color
25 {
26 struct rgba
27 {
28  rgba(): r(0.0), g(0.0), b(0.0), a(0.0) {}
29  float r;
30  float g;
31  float b;
32  float a;
33 };
34 
35 struct rgb
36 {
37  rgb(): r(0.0), g(0.0), b(0.0){}
38  float r;
39  float g;
40  float b;
41 };
42 
43 struct hsv
44 {
45  hsv(): h(0.0), s(0.0), v(0.0) {}
46  float h;
47  float s;
48  float v;
49 };
50 
51 class Color
52 {
53 public:
54  static void rgb2hsv (float r, float g, float b, float &h, float &s, float &v)
55  {
56  double var_R = r;
57  double var_G = g;
58  double var_B = b;
59 
60  double var_Min = std::min(std::min(var_R,var_G),var_B);
61  double var_Max = std::max(std::max(var_R,var_G),var_B);
62  double del_Max = var_Max - var_Min;
63  v = var_Max;
64  if (fabs(del_Max)<0.00001) {
65  h = 0;
66  s = 0;
67  }
68  else {
69  s = del_Max/var_Max;
70 
71  if ( var_R == var_Max ) h = (var_G - var_B)/del_Max;
72  else if ( var_G == var_Max ) h = 2.0 + (var_B - var_R)/del_Max;
73  else if ( var_B == var_Max ) h = 4.0 + (var_R - var_G)/del_Max;
74  h /= 6.0;
75 
76  if ( h < 0 ) h += 1;
77  if ( h > 1 ) h -= 1;
78  }
79  }
80 
81  static void hsv2rgb (float h, float s, float v, float &r, float &g, float &b)
82  {
83  float h1 = h*6; // sector 0 to 5
84  int i = floor( h1 );
85  float f = h1 - i; // fractional part of h
86 
87  float p = v * ( 1 - s );
88  float q = v * ( 1 - s * f );
89  float t = v * ( 1 - s * ( 1 - f ) );
90 
91  if (i==0) {r = v; g = t; b = p;}
92  else if (i==1) {r = q; g = v; b = p;}
93  else if (i==2) {r = p; g = v; b = t;}
94  else if (i==3) {r = p; g = q; b = v;}
95  else if (i==4) {r = t; g = p; b = v;}
96  else if (i==5) {r = v; g = p; b = q;}
97  }
98 
99  static float linearInterpolate(float a, float b, float t)
100  {
101  return a * (1 - t) + b * t;
102  }
103 
105  {
106  color::hsv ca;
107  color::hsv cb;
108  color::hsv cr;
109  color::rgba a, b;
110  a = start;
111  b = goal;
112 
113  a.r *= a.a;
114  a.g *= a.a;
115  a.b *= a.a;
116  b.r *= b.a;
117  b.g *= b.a;
118  b.b *= b.a;
119  color::Color::rgb2hsv(a.r, a.g, a.b, ca.h, ca.s, ca.v);
120  color::Color::rgb2hsv(b.r, b.g, b.b, cb.h, cb.s, cb.v);
121 
122  cr.h = linearInterpolate(ca.h, cb.h, t);
123  cr.s = linearInterpolate(ca.s, cb.s, t);
124  cr.v = linearInterpolate(ca.v, cb.v, t);
125 
126  color::rgba result;
127  color::Color::hsv2rgb(cr.h, cr.s, cr.v, result.r, result.g, result.b);
128  result.a = 1.0;
129 
130  return result;
131  }
132 };
133 }
134 #endif
static color::rgba interpolateColor(color::rgba start, color::rgba goal, float t)
Definition: colorUtils.h:104
f
static void rgb2hsv(float r, float g, float b, float &h, float &s, float &v)
Definition: colorUtils.h:54
static float linearInterpolate(float a, float b, float t)
Definition: colorUtils.h:99
XmlRpcServer s
static void hsv2rgb(float h, float s, float v, float &r, float &g, float &b)
Definition: colorUtils.h:81
float g
Definition: colorUtils.h:39
float r
Definition: colorUtils.h:38
float v
Definition: colorUtils.h:48
float s
Definition: colorUtils.h:47
float b
Definition: colorUtils.h:40
float h
Definition: colorUtils.h:46


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39