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