MyGUI
3.2.1
|
00001 /* 00002 * This source file is part of MyGUI. For the latest info, see http://mygui.info/ 00003 * Distributed under the MIT License 00004 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT) 00005 */ 00006 00007 #include "MyGUI_Precompiled.h" 00008 #include "MyGUI_Colour.h" 00009 00010 namespace MyGUI 00011 { 00012 00013 const Colour Colour::Zero = Colour(0, 0, 0, 0); 00014 const Colour Colour::Black = Colour(0, 0, 0, 1); 00015 const Colour Colour::White = Colour(1, 1, 1, 1); 00016 const Colour Colour::Red = Colour(1, 0, 0, 1); 00017 const Colour Colour::Green = Colour(0, 1, 0, 1); 00018 const Colour Colour::Blue = Colour(0, 0, 1, 1); 00019 00020 Colour::Colour() : 00021 red(1), 00022 green(1), 00023 blue(1), 00024 alpha(1) 00025 { 00026 } 00027 00028 Colour::Colour( float _red, float _green, float _blue, float _alpha) : 00029 red(_red), 00030 green(_green), 00031 blue(_blue), 00032 alpha(_alpha) 00033 { 00034 } 00035 00036 Colour::Colour(const std::string& _value) 00037 { 00038 *this = parse(_value); 00039 } 00040 00041 Colour& Colour::operator = (Colour const& _value) 00042 { 00043 red = _value.red; 00044 green = _value.green; 00045 blue = _value.blue; 00046 alpha = _value.alpha; 00047 return *this; 00048 } 00049 00050 bool Colour::operator == (Colour const& _value) const 00051 { 00052 return ((red == _value.red) && (green == _value.green) && (blue == _value.blue) && (alpha == _value.alpha)); 00053 } 00054 00055 bool Colour::operator != (Colour const& _value) const 00056 { 00057 return ! (*this == _value); 00058 } 00059 00060 void Colour::set(float _red, float _green, float _blue, float _alpha) 00061 { 00062 red = _red; 00063 green = _green; 00064 blue = _blue; 00065 alpha = _alpha; 00066 } 00067 00068 void Colour::clear() 00069 { 00070 red = green = blue = alpha = 0; 00071 } 00072 00073 std::string Colour::print() const 00074 { 00075 std::ostringstream stream; 00076 stream << *this; 00077 return stream.str(); 00078 } 00079 00080 Colour Colour::parse(const std::string& _value) 00081 { 00082 if (!_value.empty()) 00083 { 00084 if (_value[0] == '#') 00085 { 00086 std::istringstream stream(_value.substr(1)); 00087 int result = 0; 00088 stream >> std::hex >> result; 00089 if (!stream.fail()) 00090 { 00091 return Colour( (unsigned char)( result >> 16 ) / 256.0f, (unsigned char)( result >> 8 ) / 256.0f, (unsigned char)( result ) / 256.0f ); 00092 } 00093 } 00094 else 00095 { 00096 float red, green, blue; 00097 std::istringstream stream(_value); 00098 stream >> red >> green >> blue; 00099 if (!stream.fail()) 00100 { 00101 float alpha = ALPHA_MAX; 00102 if (!stream.eof()) 00103 stream >> alpha; 00104 return Colour(red, green, blue, alpha); 00105 } 00106 } 00107 } 00108 return Colour::Zero; 00109 } 00110 00111 std::ostream& Colour::operatorShiftLeft(std::ostream& _stream, const Colour& _value) 00112 { 00113 _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha; 00114 return _stream; 00115 } 00116 00117 std::istream& Colour::operatorShiftRight(std::istream& _stream, Colour& _value) 00118 { 00119 _value.clear(); 00120 00121 std::string value; 00122 _stream >> value; 00123 00124 if (value.empty()) 00125 return _stream; 00126 00127 if (value[0] == '#') 00128 { 00129 _value = parse(value); 00130 } 00131 else 00132 { 00133 std::istringstream stream(value); 00134 stream >> _value.red; 00135 if (stream.fail()) 00136 _value.clear(); 00137 else 00138 { 00139 _stream >> _value.green >> _value.blue; 00140 if (!_stream.eof()) 00141 _stream >> _value.alpha; 00142 else 00143 _value.alpha = 1; 00144 00145 if (_stream.fail()) 00146 _value.clear(); 00147 } 00148 } 00149 00150 return _stream; 00151 } 00152 00153 } // namespace MyGUI