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