MyGUI 3.5.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
51 void Colour::clear()
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{std::string{_value.substr(1)}};
70 int result = 0;
71 stream >> std::hex >> result;
72 if (!stream.fail())
73 {
74 return {
75 (unsigned char)(result >> 16) / 256.0f,
76 (unsigned char)(result >> 8) / 256.0f,
77 (unsigned char)(result) / 256.0f};
78 }
79 }
80 else
81 {
82 float red;
83 float green;
84 float blue;
85 std::stringstream stream{std::string{_value}};
86 stream >> red >> green >> blue;
87 if (!stream.fail())
88 {
89 float alpha = ALPHA_MAX;
90 if (!stream.eof())
91 stream >> alpha;
92 return {red, green, blue, alpha};
93 }
94 }
95 }
96 return Colour::Zero;
97 }
98
99 std::ostream& Colour::operatorShiftLeft(std::ostream& _stream, const Colour& _value)
100 {
101 _stream << _value.red << " " << _value.green << " " << _value.blue << " " << _value.alpha;
102 return _stream;
103 }
104
105 std::istream& Colour::operatorShiftRight(std::istream& _stream, Colour& _value)
106 {
107 _value.clear();
108
109 std::string value;
110 _stream >> value;
111
112 if (value.empty())
113 return _stream;
114
115 if (value[0] == '#')
116 {
117 _value = parse(value);
118 }
119 else
120 {
121 std::istringstream stream(value);
122 stream >> _value.red;
123 if (stream.fail())
124 _value.clear();
125 else
126 {
127 _stream >> _value.green >> _value.blue;
128 if (!_stream.eof())
129 _stream >> _value.alpha;
130 else
131 _value.alpha = 1;
132
133 if (_stream.fail())
134 _value.clear();
135 }
136 }
137
138 return _stream;
139 }
140
141} // namespace MyGUI
constexpr float ALPHA_MAX