TGUI  0.9-dev
Color.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_COLOR_HPP
27 #define TGUI_COLOR_HPP
28 
29 #include <TGUI/String.hpp>
30 #include <SFML/Graphics/Color.hpp>
31 #include <cstdint>
32 #include <string>
33 #include <map>
34 
36 
37 namespace tgui
38 {
47  class TGUI_API Color
48  {
49  public:
50 
54  static TGUI_CONSTEXPR Color applyOpacity(const Color& color, float alpha)
55  {
56  return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
57  }
58 
59 
60  public:
61 
67  TGUI_CONSTEXPR Color() :
68  m_isSet{false},
69  m_red {0},
70  m_green{0},
71  m_blue {0},
72  m_alpha{255}
73  {
74  }
75 
76 
82  TGUI_CONSTEXPR Color(const sf::Color& color) :
83  m_isSet{true},
84  m_red {color.r},
85  m_green{color.g},
86  m_blue {color.b},
87  m_alpha{color.a}
88  {
89  }
90 
91 
100  TGUI_CONSTEXPR Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
101  m_isSet{true},
102  m_red {red},
103  m_green{green},
104  m_blue {blue},
105  m_alpha{alpha}
106  {
107  }
108 
109 
117  Color(const String& string);
118 
119 
127  Color(const char* string);
128 
129 
135  TGUI_CONSTEXPR bool isSet() const
136  {
137  return m_isSet;
138  }
139 
140 
146  operator sf::Color() const
147  {
148  return sf::Color(m_red, m_green, m_blue, m_alpha);
149  }
150 
151 
157  TGUI_CONSTEXPR std::uint8_t getRed() const
158  {
159  return m_red;
160  }
161 
162 
168  TGUI_CONSTEXPR std::uint8_t getGreen() const
169  {
170  return m_green;
171  }
172 
173 
179  TGUI_CONSTEXPR std::uint8_t getBlue() const
180  {
181  return m_blue;
182  }
183 
184 
190  TGUI_CONSTEXPR std::uint8_t getAlpha() const
191  {
192  return m_alpha;
193  }
194 
195 
199  TGUI_CONSTEXPR bool operator==(const Color& rhs) const
200  {
201  return (m_isSet == rhs.m_isSet)
202  && (m_red == rhs.m_red)
203  && (m_green == rhs.m_green)
204  && (m_blue == rhs.m_blue)
205  && (m_alpha == rhs.m_alpha);
206  }
207 
208 
212  TGUI_CONSTEXPR bool operator!=(const Color& right) const
213  {
214  return !(*this == right);
215  }
216 
217 
219  public:
220 
221  static const Color Black;
222  static const Color White;
223  static const Color Red;
224  static const Color Green;
225  static const Color Blue;
226  static const Color Yellow;
227  static const Color Magenta;
228  static const Color Cyan;
229  static const Color Transparent;
230 
231  static const std::map<String, Color> colorMap;
232 
233 
235  private:
236 
237  bool m_isSet;
238  std::uint8_t m_red;
239  std::uint8_t m_green;
240  std::uint8_t m_blue;
241  std::uint8_t m_alpha;
242  };
243 
245 }
246 
248 
249 #endif // TGUI_COLOR_HPP
TGUI_CONSTEXPR std::uint8_t getRed() const
Returns the red component of the color.
Definition: Color.hpp:157
TGUI_CONSTEXPR Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition: Color.hpp:82
static const Color Magenta
Magenta predefined color.
Definition: Color.hpp:227
TGUI_CONSTEXPR Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha=255)
Creates the object from an the RGB or RGBA values.
Definition: Color.hpp:100
static const Color Blue
Blue predefined color.
Definition: Color.hpp:225
TGUI_CONSTEXPR std::uint8_t getGreen() const
Returns the green component of the color.
Definition: Color.hpp:168
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
TGUI_CONSTEXPR std::uint8_t getBlue() const
Returns the blue component of the color.
Definition: Color.hpp:179
static TGUI_CONSTEXPR Color applyOpacity(const Color &color, float alpha)
Returns the color with its alpha channel multiplied with the alpha parameter.
Definition: Color.hpp:54
Wrapper for colors.
Definition: Color.hpp:47
static const Color Cyan
Cyan predefined color.
Definition: Color.hpp:228
TGUI_CONSTEXPR bool operator!=(const Color &right) const
Compares the color with another one.
Definition: Color.hpp:212
static const Color White
White predefined color.
Definition: Color.hpp:222
TGUI_CONSTEXPR Color()
Creates the object without a color.
Definition: Color.hpp:67
TGUI_CONSTEXPR bool operator==(const Color &rhs) const
Compares the color with another one.
Definition: Color.hpp:199
static const Color Green
Green predefined color.
Definition: Color.hpp:224
static const Color Yellow
Yellow predefined color.
Definition: Color.hpp:226
static const Color Transparent
Transparent (black) predefined color.
Definition: Color.hpp:229
TGUI_CONSTEXPR bool isSet() const
Checks if a color was set.
Definition: Color.hpp:135
static const Color Red
Red predefined color.
Definition: Color.hpp:223
static const Color Black
Black predefined color.
Definition: Color.hpp:221
TGUI_CONSTEXPR std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition: Color.hpp:190