TGUI  1.0-alpha
Color.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
36 #include <SFML/Graphics/Color.hpp>
37#endif
38
40
41namespace tgui
42{
43 class Color;
44 namespace priv
45 {
46 TGUI_API Color constructColorFromString(String string);
47 }
48
49
58#if TGUI_COMPILED_WITH_CPP_VER >= 20
59 class Color
60#else
61 class TGUI_API Color
62#endif
63 {
64 public:
65
69 static TGUI_CONSTEXPR Color applyOpacity(const Color& color, float alpha)
70 {
71 return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
72 }
73
74
75 public:
76
82 TGUI_CONSTEXPR Color() :
83 m_isSet{false},
84 m_red {0},
85 m_green{0},
86 m_blue {0},
87 m_alpha{0}
88 {
89 }
90
91#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
97 TGUI_CONSTEXPR Color(const sf::Color& color) :
98 m_isSet{true},
99 m_red {color.r},
100 m_green{color.g},
101 m_blue {color.b},
102 m_alpha{color.a}
103 {
104 }
105#endif
106
115 TGUI_CONSTEXPR Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
116 m_isSet{true},
117 m_red {red},
118 m_green{green},
119 m_blue {blue},
120 m_alpha{alpha}
121 {
122 }
123
124
125#if defined (_MSC_VER)
126# pragma warning(push)
127# pragma warning(disable: 26495) // Ignore "Variable is uninitialized" warning to surpress incorrect code analysis
128#endif
136 Color(const String& string) :
137 Color{priv::constructColorFromString(string)}
138 {
139 }
140#if defined (_MSC_VER)
141# pragma warning(pop)
142#endif
143
151 Color(const char* string) :
152 Color{String{string}}
153 {
154 }
155
156
162 TGUI_CONSTEXPR bool isSet() const
163 {
164 return m_isSet;
165 }
166
167#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
173 operator sf::Color() const
174 {
175 return sf::Color(m_red, m_green, m_blue, m_alpha);
176 }
177#endif
178
184 explicit operator Vertex::Color() const
185 {
186 return Vertex::Color{m_red, m_green, m_blue, m_alpha};
187 }
188
189
195 TGUI_CONSTEXPR std::uint8_t getRed() const
196 {
197 return m_red;
198 }
199
200
206 TGUI_CONSTEXPR std::uint8_t getGreen() const
207 {
208 return m_green;
209 }
210
211
217 TGUI_CONSTEXPR std::uint8_t getBlue() const
218 {
219 return m_blue;
220 }
221
222
228 TGUI_CONSTEXPR std::uint8_t getAlpha() const
229 {
230 return m_alpha;
231 }
232
233
237 TGUI_CONSTEXPR bool operator==(const Color& rhs) const
238 {
239 return (m_isSet == rhs.m_isSet)
240 && (m_red == rhs.m_red)
241 && (m_green == rhs.m_green)
242 && (m_blue == rhs.m_blue)
243 && (m_alpha == rhs.m_alpha);
244 }
245
246
250 TGUI_CONSTEXPR bool operator!=(const Color& right) const
251 {
252 return !(*this == right);
253 }
254
255
257 public:
258
259 static const Color Black;
260 static const Color White;
261 static const Color Red;
262 static const Color Green;
263 static const Color Blue;
264 static const Color Yellow;
265 static const Color Magenta;
266 static const Color Cyan;
267 static const Color Transparent;
268
269 static const std::array<std::pair<StringView, Color>, 9> colorNamesMap;
270
271
273 private:
274
275 bool m_isSet = false;
276 std::uint8_t m_red = 0;
277 std::uint8_t m_green = 0;
278 std::uint8_t m_blue = 0;
279 std::uint8_t m_alpha = 0;
280 };
281
283
284 namespace priv
285 {
300 TGUI_API Color constructColorFromString(String string);
301 }
302
304
305
306#if TGUI_COMPILED_WITH_CPP_VER >= 20 // Although technically available since c++17, we put the bar higher to allow building TGUI with c++17 in VS2017
307 inline constexpr const Color Color::Black { 0, 0, 0};
308 inline constexpr const Color Color::White {255, 255, 255};
309 inline constexpr const Color Color::Red {255, 0, 0};
310 inline constexpr const Color Color::Green { 0, 255, 0};
311 inline constexpr const Color Color::Blue { 0, 0, 255};
312 inline constexpr const Color Color::Yellow {255, 255, 0};
313 inline constexpr const Color Color::Magenta {255, 0, 255};
314 inline constexpr const Color Color::Cyan { 0, 255, 255};
315 inline constexpr const Color Color::Transparent{ 0, 0, 0, 0};
316
317 inline constexpr const std::array<std::pair<StringView, Color>, 9> Color::colorNamesMap
318 {
319 {{U"black", Color::Black},
320 {U"white", Color::White},
321 {U"red", Color::Red},
322 {U"yellow", Color::Yellow},
323 {U"green", Color::Green},
324 {U"cyan", Color::Cyan},
325 {U"blue", Color::Blue},
326 {U"magenta", Color::Magenta},
327 {U"transparent", Color::Transparent}}
328 };
329#endif
330
331}
332
334
335#endif // TGUI_COLOR_HPP
Wrapper for colors.
Definition: Color.hpp:63
TGUI_CONSTEXPR Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition: Color.hpp:97
static const Color Transparent
Transparent (black) predefined color.
Definition: Color.hpp:267
TGUI_CONSTEXPR std::uint8_t getGreen() const
Returns the green component of the color.
Definition: Color.hpp:206
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:69
TGUI_CONSTEXPR std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition: Color.hpp:228
static const Color Black
Black predefined color.
Definition: Color.hpp:259
static const Color Green
Green predefined color.
Definition: Color.hpp:262
TGUI_CONSTEXPR bool operator==(const Color &rhs) const
Compares the color with another one.
Definition: Color.hpp:237
TGUI_CONSTEXPR bool operator!=(const Color &right) const
Compares the color with another one.
Definition: Color.hpp:250
static const Color White
White predefined color.
Definition: Color.hpp:260
TGUI_CONSTEXPR Color()
Creates the object without a color.
Definition: Color.hpp:82
Color(const char *string)
Creates the object from a string.
Definition: Color.hpp:151
TGUI_CONSTEXPR std::uint8_t getRed() const
Returns the red component of the color.
Definition: Color.hpp:195
static const Color Cyan
Cyan predefined color.
Definition: Color.hpp:266
static const Color Magenta
Magenta predefined color.
Definition: Color.hpp:265
TGUI_CONSTEXPR std::uint8_t getBlue() const
Returns the blue component of the color.
Definition: Color.hpp:217
static const Color Yellow
Yellow predefined color.
Definition: Color.hpp:264
static const Color Red
Red predefined color.
Definition: Color.hpp:261
static const Color Blue
Blue predefined color.
Definition: Color.hpp:263
TGUI_CONSTEXPR bool isSet() const
Checks if a color was set.
Definition: Color.hpp:162
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:115
Color(const String &string)
Creates the object from a string.
Definition: Color.hpp:136
Wrapper class to store strings.
Definition: String.hpp:79
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Vertex.hpp:39