1 /* 2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/ 3 * Distributed under the MIT License 4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT) 5 */ 6 7 #include "MyGUI_Precompiled.h" 8 #include "MyGUI_Colour.h" 9 10 namespace MyGUI 11 { 12 13 const Colour Colour::Zero = Colour(0, 0, 0, 0); 14 const Colour Colour::Black = Colour(0, 0, 0, 1); 15 const Colour Colour::White = Colour(1, 1, 1, 1); 16 const Colour Colour::Red = Colour(1, 0, 0, 1); 17 const Colour Colour::Green = Colour(0, 1, 0, 1); 18 const Colour Colour::Blue = Colour(0, 0, 1, 1); 19 Colour()20 Colour::Colour() : 21 red(1), 22 green(1), 23 blue(1), 24 alpha(1) 25 { 26 } 27 Colour(float _red,float _green,float _blue,float _alpha)28 Colour::Colour( float _red, float _green, float _blue, float _alpha) : 29 red(_red), 30 green(_green), 31 blue(_blue), 32 alpha(_alpha) 33 { 34 } 35 Colour(const std::string & _value)36 Colour::Colour(const std::string& _value) 37 { 38 *this = parse(_value); 39 } 40 operator =(Colour const & _value)41 Colour& Colour::operator = (Colour const& _value) 42 { 43 red = _value.red; 44 green = _value.green; 45 blue = _value.blue; 46 alpha = _value.alpha; 47 return *this; 48 } 49 operator ==(Colour const & _value) const50 bool Colour::operator == (Colour const& _value) const 51 { 52 return ((red == _value.red) && (green == _value.green) && (blue == _value.blue) && (alpha == _value.alpha)); 53 } 54 operator !=(Colour const & _value) const55 bool Colour::operator != (Colour const& _value) const 56 { 57 return ! (*this == _value); 58 } 59 set(float _red,float _green,float _blue,float _alpha)60 void Colour::set(float _red, float _green, float _blue, float _alpha) 61 { 62 red = _red; 63 green = _green; 64 blue = _blue; 65 alpha = _alpha; 66 } 67 clear()68 void Colour::clear() 69 { 70 red = green = blue = alpha = 0; 71 } 72 print() const73 std::string Colour::print() const 74 { 75 std::ostringstream stream; 76 stream << *this; 77 return stream.str(); 78 } 79 parse(const std::string & _value)80 Colour Colour::parse(const std::string& _value) 81 { 82 if (!_value.empty()) 83 { 84 if (_value[0] == '#') 85 { 86 std::istringstream stream(_value.substr(1)); 87 int result = 0; 88 stream >> std::hex >> result; 89 if (!stream.fail()) 90 { 91 return Colour( (unsigned char)( result >> 16 ) / 256.0f, (unsigned char)( result >> 8 ) / 256.0f, (unsigned char)( result ) / 256.0f ); 92 } 93 } 94 else 95 { 96 float red, green, blue; 97 std::istringstream stream(_value); 98 stream >> red >> green >> blue; 99 if (!stream.fail()) 100 { 101 float alpha = ALPHA_MAX; 102 if (!stream.eof()) 103 stream >> alpha; 104 return Colour(red, green, blue, alpha); 105 } 106 } 107 } 108 return Colour::Zero; 109 } 110 operatorShiftLeft(std::ostream & _stream,const Colour & _value)111 std::ostream& Colour::operatorShiftLeft(std::ostream& _stream, const Colour& _value) 112 { 113 _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha; 114 return _stream; 115 } 116 operatorShiftRight(std::istream & _stream,Colour & _value)117 std::istream& Colour::operatorShiftRight(std::istream& _stream, Colour& _value) 118 { 119 _value.clear(); 120 121 std::string value; 122 _stream >> value; 123 124 if (value.empty()) 125 return _stream; 126 127 if (value[0] == '#') 128 { 129 _value = parse(value); 130 } 131 else 132 { 133 std::istringstream stream(value); 134 stream >> _value.red; 135 if (stream.fail()) 136 _value.clear(); 137 else 138 { 139 _stream >> _value.green >> _value.blue; 140 if (!_stream.eof()) 141 _stream >> _value.alpha; 142 else 143 _value.alpha = 1; 144 145 if (_stream.fail()) 146 _value.clear(); 147 } 148 } 149 150 return _stream; 151 } 152 153 } // namespace MyGUI 154