SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00010 // A RGB-color definition 00011 /****************************************************************************/ 00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00014 /****************************************************************************/ 00015 // 00016 // This file is part of SUMO. 00017 // SUMO is free software: you can redistribute it and/or modify 00018 // it under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation, either version 3 of the License, or 00020 // (at your option) any later version. 00021 // 00022 /****************************************************************************/ 00023 00024 00025 // =========================================================================== 00026 // included modules 00027 // =========================================================================== 00028 #ifdef _MSC_VER 00029 #include <windows_config.h> 00030 #else 00031 #include <config.h> 00032 #endif 00033 00034 #include <cmath> 00035 #include <string> 00036 #include <sstream> 00037 #include <utils/common/StringTokenizer.h> 00038 #include <utils/common/TplConvert.h> 00039 #include <utils/common/MsgHandler.h> 00040 #include <utils/common/StdDefs.h> 00041 #include "RGBColor.h" 00042 00043 #ifdef CHECK_MEMORY_LEAKS 00044 #include <foreign/nvwa/debug_new.h> 00045 #endif // CHECK_MEMORY_LEAKS 00046 00047 00048 // =========================================================================== 00049 // static member definitions 00050 // =========================================================================== 00051 const std::string RGBColor::DEFAULT_COLOR_STRING = "1,1,0"; 00052 const RGBColor RGBColor::DEFAULT_COLOR = RGBColor::parseColor(RGBColor::DEFAULT_COLOR_STRING); 00053 00054 00055 // =========================================================================== 00056 // method definitions 00057 // =========================================================================== 00058 RGBColor::RGBColor() 00059 : myRed(-1), myGreen(-1), myBlue(-1) {} 00060 00061 00062 RGBColor::RGBColor(SUMOReal red, SUMOReal green, SUMOReal blue) 00063 : myRed(red), myGreen(green), myBlue(blue) {} 00064 00065 00066 RGBColor::RGBColor(const RGBColor& col) 00067 : myRed(col.myRed), myGreen(col.myGreen), myBlue(col.myBlue) {} 00068 00069 00070 RGBColor::~RGBColor() {} 00071 00072 00073 void 00074 RGBColor::set(SUMOReal r, SUMOReal g, SUMOReal b) { 00075 myRed = r; 00076 myGreen = g; 00077 myBlue = b; 00078 } 00079 00080 00081 00082 std::ostream& 00083 operator<<(std::ostream& os, const RGBColor& col) { 00084 os 00085 << col.myRed << "," 00086 << col.myGreen << "," 00087 << col.myBlue; 00088 return os; 00089 } 00090 00091 00092 bool 00093 RGBColor::operator==(const RGBColor& c) const { 00094 return fabs(myRed - c.myRed) < 0.1 && fabs(myGreen - c.myGreen) < 0.1 && fabs(myBlue - c.myBlue) < 0.1; 00095 //return myRed==c.myRed&&myGreen==c.myGreen&&myBlue==c.myBlue; 00096 } 00097 00098 00099 bool 00100 RGBColor::operator!=(const RGBColor& c) const { 00101 return fabs(myRed - c.myRed) > 0.1 || fabs(myGreen - c.myGreen) > 0.1 || fabs(myBlue - c.myBlue) > 0.1; 00102 //return myRed!=c.myRed||myGreen!=c.myGreen||myBlue!=c.myBlue; 00103 } 00104 00105 00106 RGBColor 00107 RGBColor::changedBrightness(SUMOReal change) { 00108 SUMOReal red = MIN2(MAX2(myRed + change, (SUMOReal)0), (SUMOReal)1); 00109 SUMOReal blue = MIN2(MAX2(myBlue + change, (SUMOReal)0), (SUMOReal)1); 00110 SUMOReal green = MIN2(MAX2(myGreen + change, (SUMOReal)0), (SUMOReal)1); 00111 return RGBColor(red, green, blue); 00112 00113 } 00114 00115 RGBColor 00116 RGBColor::parseColor(const std::string& coldef) throw(EmptyData, NumberFormatException) { 00117 StringTokenizer st(coldef, ","); 00118 if (st.size() < 3) { 00119 throw EmptyData(); 00120 } 00121 SUMOReal r = TplConvert<char>::_2SUMOReal(st.next().c_str()); 00122 SUMOReal g = TplConvert<char>::_2SUMOReal(st.next().c_str()); 00123 SUMOReal b = TplConvert<char>::_2SUMOReal(st.next().c_str()); 00124 return RGBColor(r, g, b); 00125 } 00126 00127 00128 RGBColor 00129 RGBColor::parseColorReporting( 00130 const std::string& coldef, const std::string& objecttype, 00131 const char* objectid, bool report, bool& ok) { 00132 UNUSED_PARAMETER(report); 00133 try { 00134 return parseColor(coldef); 00135 } catch (NumberFormatException&) { 00136 } catch (EmptyData&) { 00137 } 00138 ok = false; 00139 std::ostringstream oss; 00140 oss << "Attribute 'color' in definition of "; 00141 if (objectid == 0) { 00142 oss << "a "; 00143 } 00144 oss << objecttype; 00145 if (objectid != 0) { 00146 oss << " '" << objectid << "'"; 00147 } 00148 oss << " is not a valid color."; 00149 WRITE_ERROR(oss.str()); 00150 return RGBColor(); 00151 } 00152 00153 00154 RGBColor 00155 RGBColor::getDefaultColor() { 00156 return parseColor(RGBColor::DEFAULT_COLOR_STRING); 00157 } 00158 00159 00160 RGBColor 00161 RGBColor::interpolate(const RGBColor& minColor, const RGBColor& maxColor, SUMOReal weight) { 00162 if (weight < 0) { 00163 weight = 0; 00164 } 00165 if (weight > 1) { 00166 weight = 1; 00167 } 00168 SUMOReal r = minColor.myRed + (maxColor.myRed - minColor.myRed) * weight; 00169 SUMOReal g = minColor.myGreen + (maxColor.myGreen - minColor.myGreen) * weight; 00170 SUMOReal b = minColor.myBlue + (maxColor.myBlue - minColor.myBlue) * weight; 00171 return RGBColor(r, g, b); 00172 } 00173 00174 00175 RGBColor 00176 RGBColor::fromHSV(SUMOReal h, SUMOReal s, SUMOReal v) { 00177 // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1]. 00178 // RGB are each returned on [0, 1]. 00179 //float h = HSV.H, s = HSV.S, v = HSV.V, 00180 float m, n, f; 00181 h /= 60.; 00182 int i; 00183 //if (h == UNDEFINED) RETURN_RGB(v, v, v); 00184 i = int(floor(h)); 00185 f = float(h - i); 00186 if (!(i & 1)) { 00187 f = 1 - f; // if i is even 00188 } 00189 m = float(v * (1 - s)); 00190 n = float(v * (1 - s * f)); 00191 switch (i) { 00192 case 6: 00193 case 0: 00194 return RGBColor(v, n, m); 00195 case 1: 00196 return RGBColor(n, v, m); 00197 case 2: 00198 return RGBColor(m, v, n); 00199 case 3: 00200 return RGBColor(m, n, v); 00201 case 4: 00202 return RGBColor(n, m, v); 00203 case 5: 00204 return RGBColor(v, m, n); 00205 } 00206 return RGBColor(1, 1, 1); 00207 } 00208 00209 00210 /****************************************************************************/ 00211