MyGUI
3.0.3
|
00001 00007 /* 00008 This file is part of MyGUI. 00009 00010 MyGUI is free software: you can redistribute it and/or modify 00011 it under the terms of the GNU Lesser General Public License as published by 00012 the Free Software Foundation, either version 3 of the License, or 00013 (at your option) any later version. 00014 00015 MyGUI is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public License 00021 along with MyGUI. If not, see <http://www.gnu.org/licenses/>. 00022 */ 00023 #ifndef __MYGUI_COLOUR_H__ 00024 #define __MYGUI_COLOUR_H__ 00025 00026 #include "MyGUI_Prerequest.h" 00027 #include "MyGUI_Types.h" 00028 00029 namespace MyGUI 00030 { 00031 00032 struct MYGUI_EXPORT Colour 00033 { 00034 float red, green, blue, alpha; 00035 00036 static const Colour Zero; 00037 static const Colour Black; 00038 static const Colour White; 00039 static const Colour Red; 00040 static const Colour Green; 00041 static const Colour Blue; 00042 00043 Colour() : red( 1 ), green( 1 ), blue( 1 ), alpha( 1 ) { } 00044 Colour( float _red, float _green, float _blue, float _alpha = 1 ) : red( _red ), green( _green ), blue( _blue ), alpha( _alpha ) { } 00045 explicit Colour(const std::string& _value) { *this = parse(_value); } 00046 00047 00048 Colour& operator=( Colour const& _value ) 00049 { 00050 red = _value.red; 00051 green = _value.green; 00052 blue = _value.blue; 00053 alpha = _value.alpha; 00054 return *this; 00055 } 00056 00057 bool operator==( Colour const& _value ) const 00058 { 00059 return ((red == _value.red) && (green == _value.green) && (blue == _value.blue) && (alpha == _value.alpha)); 00060 } 00061 00062 bool operator!=( Colour const& _value ) const 00063 { 00064 return ! (*this == _value); 00065 } 00066 00067 void set( float _red, float _green, float _blue, float _alpha = 1 ) 00068 { 00069 red = _red; 00070 green = _green; 00071 blue = _blue; 00072 alpha = _alpha; 00073 } 00074 00075 void clear() 00076 { 00077 red = green = blue = alpha = 0; 00078 } 00079 00080 std::string print() const 00081 { 00082 std::ostringstream stream; 00083 stream << *this; 00084 return stream.str(); 00085 } 00086 00087 static Colour parse(const std::string& _value) 00088 { 00089 if (!_value.empty()) 00090 { 00091 if (_value[0] == '#') 00092 { 00093 std::istringstream stream(_value.substr(1)); 00094 int result = 0; 00095 stream >> std::hex >> result; 00096 if (!stream.fail()) 00097 { 00098 return Colour( (unsigned char)( result >> 16 ) / 256.0f, (unsigned char)( result >> 8 ) / 256.0f, (unsigned char)( result ) / 256.0f ); 00099 } 00100 } 00101 else 00102 { 00103 float red, green, blue, alpha = 1; 00104 std::istringstream stream(_value); 00105 stream >> red >> green >> blue; 00106 if (!stream.fail()) 00107 { 00108 if (!stream.eof()) 00109 stream >> alpha; 00110 return Colour(red, green, blue, alpha); 00111 } 00112 } 00113 } 00114 return Colour::Zero; 00115 } 00116 00117 friend std::ostream& operator << ( std::ostream& _stream, const Colour& _value ) 00118 { 00119 _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha; 00120 return _stream; 00121 } 00122 00123 friend std::istream& operator >> ( std::istream& _stream, Colour& _value ) 00124 { 00125 _value.clear(); 00126 00127 std::string value; 00128 _stream >> value; 00129 00130 if (value.empty()) 00131 return _stream; 00132 00133 if (value[0] == '#') 00134 { 00135 _value = Colour::parse(value); 00136 } 00137 else 00138 { 00139 std::istringstream stream(value); 00140 stream >> _value.red; 00141 if (stream.fail()) 00142 _value.clear(); 00143 else 00144 { 00145 _stream >> _value.green >> _value.blue; 00146 if (!_stream.eof()) 00147 _stream >> _value.alpha; 00148 else 00149 _value.alpha = 1; 00150 00151 if (_stream.fail()) 00152 _value.clear(); 00153 } 00154 } 00155 00156 return _stream; 00157 } 00158 00159 }; 00160 00161 } // namespace MyGUI 00162 00163 #endif // __MYGUI_COLOUR_H__