00001 /**************************************************************** 00002 * 00003 * Copyright (c) 2010 00004 * 00005 * Fraunhofer Institute for Manufacturing Engineering 00006 * and Automation (IPA) 00007 * 00008 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00009 * 00010 * Project name: care-o-bot 00011 * ROS stack name: cob_driver 00012 * ROS package name: cob_light 00013 * Description: Switch robots led color by sending data to 00014 * the led-µC over serial connection. 00015 * 00016 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00017 * 00018 * Author: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de 00019 * Supervised by: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de 00020 * 00021 * Date of creation: August 2012 00022 * ToDo: 00023 * 00024 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00025 * 00026 * Redistribution and use in source and binary forms, with or without 00027 * modification, are permitted provided that the following conditions are met: 00028 * 00029 * * Redistributions of source code must retain the above copyright 00030 * notice, this list of conditions and the following disclaimer. 00031 * * Redistributions in binary form must reproduce the above copyright 00032 * notice, this list of conditions and the following disclaimer in the 00033 * documentation and/or other materials provided with the distribution. 00034 * * Neither the name of the Fraunhofer Institute for Manufacturing 00035 * Engineering and Automation (IPA) nor the names of its 00036 * contributors may be used to endorse or promote products derived from 00037 * this software without specific prior written permission. 00038 * 00039 * This program is free software: you can redistribute it and/or modify 00040 * it under the terms of the GNU Lesser General Public License LGPL as 00041 * published by the Free Software Foundation, either version 3 of the 00042 * License, or (at your option) any later version. 00043 * 00044 * This program is distributed in the hope that it will be useful, 00045 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00046 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00047 * GNU Lesser General Public License LGPL for more details. 00048 * 00049 * You should have received a copy of the GNU Lesser General Public 00050 * License LGPL along with this program. 00051 * If not, see <http://www.gnu.org/licenses/>. 00052 * 00053 ****************************************************************/ 00054 00055 #ifndef COLOR_UTILS_H 00056 #define COLOR_UTILS_H 00057 00058 #include <algorithm> 00059 #include <math.h> 00060 00061 namespace color 00062 { 00063 struct rgba 00064 { 00065 rgba(): r(0.0), g(0.0), b(0.0), a(0.0) {} 00066 float r; 00067 float g; 00068 float b; 00069 float a; 00070 }; 00071 00072 struct hsv 00073 { 00074 hsv(): h(0.0), s(0.0), v(0.0) {} 00075 float h; 00076 float s; 00077 float v; 00078 }; 00079 00080 class Color 00081 { 00082 public: 00083 static void rgb2hsv (float r, float g, float b, float &h, float &s, float &v) 00084 { 00085 double var_R = r; 00086 double var_G = g; 00087 double var_B = b; 00088 00089 double var_Min = std::min(std::min(var_R,var_G),var_B); 00090 double var_Max = std::max(std::max(var_R,var_G),var_B); 00091 double del_Max = var_Max - var_Min; 00092 v = var_Max; 00093 if (fabs(del_Max)<0.00001) { 00094 h = 0; 00095 s = 0; 00096 } 00097 else { 00098 s = del_Max/var_Max; 00099 00100 if ( var_R == var_Max ) h = (var_G - var_B)/del_Max; 00101 else if ( var_G == var_Max ) h = 2.0 + (var_B - var_R)/del_Max; 00102 else if ( var_B == var_Max ) h = 4.0 + (var_R - var_G)/del_Max; 00103 h /= 6.0; 00104 00105 if ( h < 0 ) h += 1; 00106 if ( h > 1 ) h -= 1; 00107 } 00108 } 00109 00110 static void hsv2rgb (float h, float s, float v, float &r, float &g, float &b) 00111 { 00112 float h1 = h*6; // sector 0 to 5 00113 int i = floor( h1 ); 00114 float f = h1 - i; // fractional part of h 00115 00116 float p = v * ( 1 - s ); 00117 float q = v * ( 1 - s * f ); 00118 float t = v * ( 1 - s * ( 1 - f ) ); 00119 00120 if (i==0) {r = v; g = t; b = p;} 00121 else if (i==1) {r = q; g = v; b = p;} 00122 else if (i==2) {r = p; g = v; b = t;} 00123 else if (i==3) {r = p; g = q; b = v;} 00124 else if (i==4) {r = t; g = p; b = v;} 00125 else if (i==5) {r = v; g = p; b = q;} 00126 } 00127 }; 00128 } 00129 #endif