MyGUI  3.0.1
MyGUI_Colour.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_COLOUR_H__
24 #define __MYGUI_COLOUR_H__
25 
26 #include "MyGUI_Prerequest.h"
27 #include "MyGUI_Types.h"
28 
29 namespace MyGUI
30 {
31 
33  {
34  float red, green, blue, alpha;
35 
36  static const Colour Zero;
37  static const Colour Black;
38  static const Colour White;
39  static const Colour Red;
40  static const Colour Green;
41  static const Colour Blue;
42 
43  Colour() : red( 1 ), green( 1 ), blue( 1 ), alpha( 1 ) { }
44  Colour( float _red, float _green, float _blue, float _alpha = 1 ) : red( _red ), green( _green ), blue( _blue ), alpha( _alpha ) { }
45  explicit Colour(const std::string& _value) { *this = parse(_value); }
46 
47 
48  Colour& operator=( Colour const& _value )
49  {
50  red = _value.red;
51  green = _value.green;
52  blue = _value.blue;
53  alpha = _value.alpha;
54  return *this;
55  }
56 
57  bool operator==( Colour const& _value ) const
58  {
59  return ((red == _value.red) && (green == _value.green) && (blue == _value.blue) && (alpha == _value.alpha));
60  }
61 
62  bool operator!=( Colour const& _value ) const
63  {
64  return ! (*this == _value);
65  }
66 
67  void set( float _red, float _green, float _blue, float _alpha = 1 )
68  {
69  red = _red;
70  green = _green;
71  blue = _blue;
72  alpha = _alpha;
73  }
74 
75  void clear()
76  {
77  red = green = blue = alpha = 0;
78  }
79 
80  std::string print() const
81  {
82  std::ostringstream stream;
83  stream << *this;
84  return stream.str();
85  }
86 
87  static Colour parse(const std::string& _value)
88  {
89  if (!_value.empty())
90  {
91  if (_value[0] == '#')
92  {
93  std::istringstream stream(_value.substr(1));
94  int result = 0;
95  stream >> std::hex >> result;
96  if (!stream.fail())
97  {
98  return Colour( (unsigned char)( result >> 16 ) / 256.0f, (unsigned char)( result >> 8 ) / 256.0f, (unsigned char)( result ) / 256.0f );
99  }
100  }
101  else
102  {
103  float red, green, blue, alpha = 1;
104  std::istringstream stream(_value);
105  stream >> red >> green >> blue;
106  if (!stream.fail())
107  {
108  if (!stream.eof())
109  stream >> alpha;
110  return Colour(red, green, blue, alpha);
111  }
112  }
113  }
114  return Colour::Zero;
115  }
116 
117  friend std::ostream& operator << ( std::ostream& _stream, const Colour& _value )
118  {
119  _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha;
120  return _stream;
121  }
122 
123  friend std::istream& operator >> ( std::istream& _stream, Colour& _value )
124  {
125  _value.clear();
126 
127  std::string value;
128  _stream >> value;
129 
130  if (value.empty())
131  return _stream;
132 
133  if (value[0] == '#')
134  {
135  _value = Colour::parse(value);
136  }
137  else
138  {
139  std::istringstream stream(value);
140  stream >> _value.red;
141  if (stream.fail())
142  _value.clear();
143  else
144  {
145  _stream >> _value.green >> _value.blue;
146  if (!_stream.eof())
147  _stream >> _value.alpha;
148  else
149  _value.alpha = 1;
150 
151  if (_stream.fail())
152  _value.clear();
153  }
154  }
155 
156  return _stream;
157  }
158 
159  };
160 
161 } // namespace MyGUI
162 
163 #endif // __MYGUI_COLOUR_H__