colorUtils.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #ifndef COLOR_UTILS_H
00019 #define COLOR_UTILS_H
00020 
00021 #include <algorithm>
00022 #include <math.h>
00023 
00024 namespace color
00025 {
00026 struct rgba
00027 {
00028         rgba(): r(0.0), g(0.0), b(0.0), a(0.0) {}
00029         float r;
00030         float g;
00031         float b;
00032         float a;
00033 };
00034 
00035 struct rgb
00036 {
00037   rgb(): r(0.0), g(0.0), b(0.0){}
00038   float r;
00039   float g;
00040   float b;
00041 };
00042 
00043 struct hsv
00044 {
00045         hsv(): h(0.0), s(0.0), v(0.0) {}
00046         float h;
00047         float s;
00048         float v;
00049 };
00050 
00051 class Color
00052 {
00053 public:
00054         static void rgb2hsv (float r, float g, float b, float &h, float &s, float &v)
00055         {
00056                 double var_R = r;
00057                 double var_G = g;
00058                 double var_B = b;
00059 
00060                 double var_Min = std::min(std::min(var_R,var_G),var_B);
00061                 double var_Max = std::max(std::max(var_R,var_G),var_B);
00062                 double del_Max = var_Max - var_Min;
00063                 v = var_Max;
00064                 if (fabs(del_Max)<0.00001) {
00065                         h = 0;
00066                         s = 0;
00067                 }
00068                 else {
00069                         s = del_Max/var_Max;
00070 
00071                         if      ( var_R == var_Max ) h = (var_G - var_B)/del_Max;
00072                         else if ( var_G == var_Max ) h = 2.0 + (var_B - var_R)/del_Max;
00073                         else if ( var_B == var_Max ) h = 4.0 + (var_R - var_G)/del_Max;
00074                         h /= 6.0;
00075 
00076                         if ( h < 0 )  h += 1;
00077                         if ( h > 1 )  h -= 1;
00078                 }
00079         }
00080 
00081         static void hsv2rgb (float h, float s, float v, float &r, float &g, float &b)
00082         {
00083                 float h1 = h*6; // sector 0 to 5
00084                 int i = floor( h1 );
00085                 float f = h1 - i; // fractional part of h
00086 
00087                 float p = v * ( 1 - s );
00088                 float q = v * ( 1 - s * f );
00089                 float t = v * ( 1 - s * ( 1 - f ) );
00090 
00091                 if      (i==0) {r = v;  g = t;  b = p;}
00092                 else if (i==1) {r = q;  g = v;  b = p;}
00093                 else if (i==2) {r = p;  g = v;  b = t;}
00094                 else if (i==3) {r = p;  g = q;  b = v;}
00095                 else if (i==4) {r = t;  g = p;  b = v;}
00096                 else if (i==5) {r = v;  g = p;  b = q;}
00097         }
00098 
00099     static float linearInterpolate(float a, float b, float t)
00100     {
00101       return a * (1 - t) + b * t;
00102     }
00103 
00104     static color::rgba interpolateColor(color::rgba start, color::rgba goal, float t)
00105     {
00106       color::hsv ca;
00107       color::hsv cb;
00108       color::hsv cr;
00109       color::rgba a, b;
00110       a = start;
00111       b = goal;
00112 
00113       a.r *= a.a;
00114       a.g *= a.a;
00115       a.b *= a.a;
00116       b.r *= b.a;
00117       b.g *= b.a;
00118       b.b *= b.a;
00119       color::Color::rgb2hsv(a.r, a.g, a.b, ca.h, ca.s, ca.v);
00120       color::Color::rgb2hsv(b.r, b.g, b.b, cb.h, cb.s, cb.v);
00121 
00122       cr.h = linearInterpolate(ca.h, cb.h, t);
00123       cr.s = linearInterpolate(ca.s, cb.s, t);
00124       cr.v = linearInterpolate(ca.v, cb.v, t);
00125 
00126       color::rgba result;
00127       color::Color::hsv2rgb(cr.h, cr.s, cr.v, result.r, result.g, result.b);
00128       result.a = 1.0;
00129 
00130       return result;
00131     }
00132 };
00133 }
00134 #endif


cob_light
Author(s): Benjamin Maidel
autogenerated on Sat Jun 8 2019 21:02:07