ARGoS
3
A parallel, multi-engine simulator for swarm robotics
|
00001 00011 #ifndef COLOR_H 00012 #define COLOR_H 00013 00014 #include <argos3/core/utility/datatypes/datatypes.h> 00015 #include <argos3/core/utility/string_utilities.h> 00016 #include <string.h> 00017 #include <iostream> 00018 00019 namespace argos { 00020 00025 class CColor { 00026 00027 public: 00028 00029 static CColor BLACK; 00030 static CColor WHITE; 00031 static CColor RED; 00032 static CColor GREEN; 00033 static CColor BLUE; 00034 static CColor MAGENTA; 00035 static CColor CYAN; 00036 static CColor YELLOW; 00037 static CColor ORANGE; 00038 static CColor GRAY10; 00039 static CColor GRAY20; 00040 static CColor GRAY30; 00041 static CColor GRAY40; 00042 static CColor GRAY50; 00043 static CColor GRAY60; 00044 static CColor GRAY70; 00045 static CColor GRAY80; 00046 static CColor GRAY90; 00047 00051 CColor() {} 00052 00056 explicit CColor(const UInt8 un_red, 00057 const UInt8 un_green, 00058 const UInt8 un_blue, 00059 const UInt8 un_alpha = 255) throw() : 00060 m_tChannels(un_red, un_green, un_blue, un_alpha) {} 00061 00066 inline Real ToGrayScale() const throw() { 00067 return 00068 0.299f * m_tChannels.m_unRed + 00069 0.587f * m_tChannels.m_unGreen + 00070 0.114f * m_tChannels.m_unBlue; 00071 } 00072 00077 inline UInt8 GetRed() const throw() { return m_tChannels.m_unRed; } 00082 inline void SetRed(const UInt8 un_red) throw() { m_tChannels.m_unRed = un_red; } 00083 00088 inline UInt8 GetGreen() const throw() { return m_tChannels.m_unGreen; } 00093 inline void SetGreen(const UInt8 un_green) throw() { m_tChannels.m_unGreen = un_green; } 00094 00099 inline UInt8 GetBlue() const throw() { return m_tChannels.m_unBlue; } 00104 inline void SetBlue(const UInt8 un_blue) throw() { m_tChannels.m_unBlue = un_blue; } 00105 00110 inline UInt8 GetAlpha() const throw() { return m_tChannels.m_unAlpha; } 00115 inline void SetAlpha(const UInt8 un_alpha) throw() { m_tChannels.m_unAlpha = un_alpha; } 00116 00124 inline void Set(const UInt8 un_red, 00125 const UInt8 un_green, 00126 const UInt8 un_blue, 00127 const UInt8 un_alpha = 255) throw() { 00128 SetRed(un_red); 00129 SetGreen(un_green); 00130 SetBlue(un_blue); 00131 SetAlpha(un_alpha); 00132 } 00133 00141 inline void Set(const std::string& str_color) { 00142 try { 00143 if (str_color == "black") *this = CColor::BLACK; 00144 else if (str_color == "white") *this = CColor::WHITE; 00145 else if (str_color == "red") *this = CColor::RED; 00146 else if (str_color == "green") *this = CColor::GREEN; 00147 else if (str_color == "blue") *this = CColor::BLUE; 00148 else if (str_color == "magenta") *this = CColor::MAGENTA; 00149 else if (str_color == "cyan") *this = CColor::CYAN; 00150 else if (str_color == "yellow") *this = CColor::YELLOW; 00151 else if (str_color == "orange") *this = CColor::ORANGE; 00152 else if (str_color == "gray10") *this = CColor::GRAY10; 00153 else if (str_color == "gray20") *this = CColor::GRAY20; 00154 else if (str_color == "gray30") *this = CColor::GRAY30; 00155 else if (str_color == "gray40") *this = CColor::GRAY40; 00156 else if (str_color == "gray50") *this = CColor::GRAY50; 00157 else if (str_color == "gray60") *this = CColor::GRAY60; 00158 else if (str_color == "gray70") *this = CColor::GRAY70; 00159 else if (str_color == "gray80") *this = CColor::GRAY80; 00160 else if (str_color == "gray90") *this = CColor::GRAY90; 00161 else { 00162 UInt8 unValues[4]; 00163 ParseValues<UInt8>(str_color, 4, unValues, ','); 00164 Set(unValues[0], unValues[1], unValues[2], unValues[3]); 00165 } 00166 } 00167 catch(CARGoSException& ex) { 00168 THROW_ARGOSEXCEPTION_NESTED("Error while parsing color input string", ex); 00169 } 00170 } 00171 00176 inline operator UInt32() { 00177 return *reinterpret_cast<UInt32*>(&m_tChannels); 00178 } 00179 00185 inline bool operator==(const CColor& c_color2) const throw() { 00186 return m_tChannels == c_color2.m_tChannels; 00187 } 00188 00194 inline bool operator!=(const CColor& c_color2) const throw() { 00195 return m_tChannels != c_color2.m_tChannels; 00196 } 00197 00209 friend std::ostream& operator<<(std::ostream& os, 00210 const CColor& c_color) { 00211 if (c_color == CColor::BLACK) os << "black"; 00212 else if (c_color == CColor::WHITE) os << "white"; 00213 else if (c_color == CColor::RED) os << "red"; 00214 else if (c_color == CColor::GREEN) os << "green"; 00215 else if (c_color == CColor::BLUE) os << "blue"; 00216 else if (c_color == CColor::MAGENTA) os << "magenta"; 00217 else if (c_color == CColor::CYAN) os << "cyan"; 00218 else if (c_color == CColor::YELLOW) os << "yellow"; 00219 else if (c_color == CColor::ORANGE) os << "orange"; 00220 else if (c_color == CColor::GRAY10) os << "gray10"; 00221 else if (c_color == CColor::GRAY20) os << "gray20"; 00222 else if (c_color == CColor::GRAY30) os << "gray30"; 00223 else if (c_color == CColor::GRAY40) os << "gray40"; 00224 else if (c_color == CColor::GRAY50) os << "gray50"; 00225 else if (c_color == CColor::GRAY60) os << "gray60"; 00226 else if (c_color == CColor::GRAY70) os << "gray70"; 00227 else if (c_color == CColor::GRAY80) os << "gray80"; 00228 else if (c_color == CColor::GRAY90) os << "gray90"; 00229 else { 00230 os << c_color.m_tChannels.m_unRed 00231 << "," << c_color.m_tChannels.m_unGreen 00232 << "," << c_color.m_tChannels.m_unBlue 00233 << "," << c_color.m_tChannels.m_unAlpha; 00234 } 00235 return os; 00236 } 00237 00247 friend std::istream& operator>>(std::istream& is, 00248 CColor& c_color) { 00249 std::string strColor; 00250 is >> strColor; 00251 c_color.Set(strColor); 00252 return is; 00253 } 00254 00255 00256 private: 00257 00258 struct TChannels { 00259 UInt8 m_unRed; 00260 UInt8 m_unGreen; 00261 UInt8 m_unBlue; 00262 UInt8 m_unAlpha; 00263 00264 TChannels() : 00265 m_unRed(0), 00266 m_unGreen(0), 00267 m_unBlue(0), 00268 m_unAlpha(255) {} 00269 00270 TChannels(const UInt8 un_red, 00271 const UInt8 un_green, 00272 const UInt8 un_blue, 00273 const UInt8 un_alpha = 255) : 00274 m_unRed(un_red), 00275 m_unGreen(un_green), 00276 m_unBlue(un_blue), 00277 m_unAlpha(un_alpha) {} 00278 00279 inline bool operator==(const TChannels& t_channels) const { 00280 return 00281 (m_unRed == t_channels.m_unRed) && 00282 (m_unGreen == t_channels.m_unGreen) && 00283 (m_unBlue == t_channels.m_unBlue) && 00284 (m_unAlpha == t_channels.m_unAlpha); 00285 } 00286 00287 inline bool operator!=(const TChannels& t_channels) const { 00288 return 00289 (m_unRed != t_channels.m_unRed) || 00290 (m_unGreen != t_channels.m_unGreen) || 00291 (m_unBlue != t_channels.m_unBlue) || 00292 (m_unAlpha != t_channels.m_unAlpha); 00293 } 00294 00295 } m_tChannels; 00296 00297 }; 00298 00299 } 00300 00301 #endif