MyGUI 3.4.3
MyGUI_Colour.cpp
Go to the documentation of this file.
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
10namespace 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
20 Colour::Colour(float _red, float _green, float _blue, float _alpha) :
21 red(_red),
22 green(_green),
23 blue(_blue),
24 alpha(_alpha)
25 {
26 }
27
28 Colour::Colour(std::string_view _value)
29 {
30 *this = parse(_value);
31 }
32
33 bool Colour::operator==(Colour const& _value) const
34 {
35 return ((red == _value.red) && (green == _value.green) && (blue == _value.blue) && (alpha == _value.alpha));
36 }
37
38 bool Colour::operator!=(Colour const& _value) const
39 {
40 return !(*this == _value);
41 }
42
43 void Colour::set(float _red, float _green, float _blue, float _alpha)
44 {
45 red = _red;
46 green = _green;
47 blue = _blue;
48 alpha = _alpha;
49 }
50
52 {
53 red = green = blue = alpha = 0;
54 }
55
56 std::string Colour::print() const
57 {
58 std::ostringstream stream;
59 stream << *this;
60 return stream.str();
61 }
62
63 Colour Colour::parse(std::string_view _value)
64 {
65 if (!_value.empty())
66 {
67 if (_value[0] == '#')
68 {
69 std::stringstream stream;
70 stream << _value.substr(1);
71 int result = 0;
72 stream >> std::hex >> result;
73 if (!stream.fail())
74 {
75 return {
76 (unsigned char)(result >> 16) / 256.0f,
77 (unsigned char)(result >> 8) / 256.0f,
78 (unsigned char)(result) / 256.0f};
79 }
80 }
81 else
82 {
83 float red;
84 float green;
85 float blue;
86 std::stringstream stream;
87 stream << _value;
88 stream >> red >> green >> blue;
89 if (!stream.fail())
90 {
91 float alpha = ALPHA_MAX;
92 if (!stream.eof())
93 stream >> alpha;
94 return {red, green, blue, alpha};
95 }
96 }
97 }
98 return Colour::Zero;
99 }
100
101 std::ostream& Colour::operatorShiftLeft(std::ostream& _stream, const Colour& _value)
102 {
103 _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha;
104 return _stream;
105 }
106
107 std::istream& Colour::operatorShiftRight(std::istream& _stream, Colour& _value)
108 {
109 _value.clear();
110
111 std::string value;
112 _stream >> value;
113
114 if (value.empty())
115 return _stream;
116
117 if (value[0] == '#')
118 {
119 _value = parse(value);
120 }
121 else
122 {
123 std::istringstream stream(value);
124 stream >> _value.red;
125 if (stream.fail())
126 _value.clear();
127 else
128 {
129 _stream >> _value.green >> _value.blue;
130 if (!_stream.eof())
131 _stream >> _value.alpha;
132 else
133 _value.alpha = 1;
134
135 if (_stream.fail())
136 _value.clear();
137 }
138 }
139
140 return _stream;
141 }
142
143} // namespace MyGUI
constexpr float ALPHA_MAX
static Colour parse(std::string_view _value)
bool operator!=(Colour const &_value) const
std::string print() const
static const Colour Green
static const Colour Zero
static std::istream & operatorShiftRight(std::istream &_stream, Colour &_value)
static const Colour Red
void set(float _red, float _green, float _blue, float _alpha=1)
static const Colour Blue
Colour()=default
static const Colour White
static std::ostream & operatorShiftLeft(std::ostream &_stream, const Colour &_value)
bool operator==(Colour const &_value) const
static const Colour Black