TGUI  0.9.1
Color.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 <TGUI/Vertex.hpp>
31 #include <cstdint>
32 #include <string>
33 #include <map>
34 
35 #if TGUI_HAS_BACKEND_SFML
36  #include <SFML/Graphics/Color.hpp>
37 #endif
38 
40 
41 namespace tgui
42 {
51  class TGUI_API Color
52  {
53  public:
54 
58  static TGUI_CONSTEXPR Color applyOpacity(const Color& color, float alpha)
59  {
60  return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
61  }
62 
63 
64  public:
65 
71  TGUI_CONSTEXPR Color() :
72  m_isSet{false},
73  m_red {0},
74  m_green{0},
75  m_blue {0},
76  m_alpha{0}
77  {
78  }
79 
80 #if TGUI_HAS_BACKEND_SFML
86  TGUI_CONSTEXPR Color(const sf::Color& color) :
87  m_isSet{true},
88  m_red {color.r},
89  m_green{color.g},
90  m_blue {color.b},
91  m_alpha{color.a}
92  {
93  }
94 #endif
95 
104  TGUI_CONSTEXPR Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
105  m_isSet{true},
106  m_red {red},
107  m_green{green},
108  m_blue {blue},
109  m_alpha{alpha}
110  {
111  }
112 
113 
121  Color(const String& string);
122 
123 
131  Color(const char* string);
132 
133 
139  TGUI_CONSTEXPR bool isSet() const
140  {
141  return m_isSet;
142  }
143 
144 #if TGUI_HAS_BACKEND_SFML
150  operator sf::Color() const
151  {
152  return sf::Color(m_red, m_green, m_blue, m_alpha);
153  }
154 #endif
155 
161  explicit operator Vertex::Color() const
162  {
163  return Vertex::Color{m_red, m_green, m_blue, m_alpha};
164  }
165 
166 
172  TGUI_CONSTEXPR std::uint8_t getRed() const
173  {
174  return m_red;
175  }
176 
177 
183  TGUI_CONSTEXPR std::uint8_t getGreen() const
184  {
185  return m_green;
186  }
187 
188 
194  TGUI_CONSTEXPR std::uint8_t getBlue() const
195  {
196  return m_blue;
197  }
198 
199 
205  TGUI_CONSTEXPR std::uint8_t getAlpha() const
206  {
207  return m_alpha;
208  }
209 
210 
214  TGUI_CONSTEXPR bool operator==(const Color& rhs) const
215  {
216  return (m_isSet == rhs.m_isSet)
217  && (m_red == rhs.m_red)
218  && (m_green == rhs.m_green)
219  && (m_blue == rhs.m_blue)
220  && (m_alpha == rhs.m_alpha);
221  }
222 
223 
227  TGUI_CONSTEXPR bool operator!=(const Color& right) const
228  {
229  return !(*this == right);
230  }
231 
232 
234  public:
235 
236  static const Color Black;
237  static const Color White;
238  static const Color Red;
239  static const Color Green;
240  static const Color Blue;
241  static const Color Yellow;
242  static const Color Magenta;
243  static const Color Cyan;
244  static const Color Transparent;
245 
246  static const std::map<String, Color> colorMap;
247 
248 
250  private:
251 
252  bool m_isSet;
253  std::uint8_t m_red;
254  std::uint8_t m_green;
255  std::uint8_t m_blue;
256  std::uint8_t m_alpha;
257  };
258 
260 }
261 
263 
264 #endif // TGUI_COLOR_HPP
Wrapper for colors.
Definition: Color.hpp:52
static const Color White
White predefined color.
Definition: Color.hpp:237
TGUI_CONSTEXPR std::uint8_t getGreen() const
Returns the green component of the color.
Definition: Color.hpp:183
static const Color Cyan
Cyan predefined color.
Definition: Color.hpp:243
static const Color Red
Red predefined color.
Definition: Color.hpp:238
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:58
TGUI_CONSTEXPR std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition: Color.hpp:205
TGUI_CONSTEXPR bool operator==(const Color &rhs) const
Compares the color with another one.
Definition: Color.hpp:214
TGUI_CONSTEXPR bool operator!=(const Color &right) const
Compares the color with another one.
Definition: Color.hpp:227
static const Color Black
Black predefined color.
Definition: Color.hpp:236
static const Color Magenta
Magenta predefined color.
Definition: Color.hpp:242
TGUI_CONSTEXPR Color()
Creates the object without a color.
Definition: Color.hpp:71
Color(const char *string)
Creates the object from a string.
static const Color Transparent
Transparent (black) predefined color.
Definition: Color.hpp:244
TGUI_CONSTEXPR std::uint8_t getRed() const
Returns the red component of the color.
Definition: Color.hpp:172
static const Color Yellow
Yellow predefined color.
Definition: Color.hpp:241
static const Color Blue
Blue predefined color.
Definition: Color.hpp:240
static const Color Green
Green predefined color.
Definition: Color.hpp:239
TGUI_CONSTEXPR std::uint8_t getBlue() const
Returns the blue component of the color.
Definition: Color.hpp:194
TGUI_CONSTEXPR bool isSet() const
Checks if a color was set.
Definition: Color.hpp:139
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:104
Color(const String &string)
Creates the object from a string.
Wrapper class to store strings.
Definition: String.hpp:74
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Vertex.hpp:39