• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_Colour.h

Go to the documentation of this file.
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                             stream >> alpha;
00109                             return Colour(red, green, blue, alpha);
00110                         }
00111                     }
00112                 }
00113                 return Colour(0, 0, 0, 0);
00114             }
00115 
00116             friend std::ostream& operator << ( std::ostream& _stream, const Colour&  _value )
00117             {
00118                 _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha;
00119                 return _stream;
00120             }
00121 
00122             friend std::istream& operator >> ( std::istream& _stream, Colour&  _value )
00123             {
00124                 _stream >> _value.red >> _value.green >> _value.blue >> _value.alpha;
00125                 if (_stream.fail()) _value.clear();
00126                 return _stream;
00127             }
00128 
00129         };
00130 
00131 } // namespace MyGUI
00132 
00133 #endif // __MYGUI_COLOUR_H__

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1