TGUI  1.3.0
Loading...
Searching...
No Matches
Color.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <cstdint>
34 #include <string>
35 #include <map>
36#endif
37
38#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
39 #include <SFML/Graphics/Color.hpp>
40#endif
41
43
44TGUI_MODULE_EXPORT namespace tgui
45{
46 class Color;
47}
48
49namespace tgui
50{
51 namespace priv
52 {
53 TGUI_API Color constructColorFromString(const String& string);
54 }
55}
56
57TGUI_MODULE_EXPORT namespace tgui
58{
67#if defined(TGUI_SYSTEM_WINDOWS) && TGUI_COMPILED_WITH_CPP_VER >= 17
68 // dllimport doesn't work for the static members when they are defined as inline constexpr outside the class,
69 // so we aren't allowed to put TGUI_API here on Windows.
70 class Color
71#else
72 class TGUI_API Color
73#endif
74 {
75 public:
76
80 TGUI_NODISCARD static constexpr Color applyOpacity(const Color& color, float alpha)
81 {
82 return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
83 }
84
85
86 public:
87
93 constexpr Color() :
94 m_isSet{false},
95 m_red {0},
96 m_green{0},
97 m_blue {0},
98 m_alpha{0}
99 {
100 }
101
102#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
108 constexpr Color(const sf::Color& color) :
109 m_isSet{true},
110 m_red {color.r},
111 m_green{color.g},
112 m_blue {color.b},
113 m_alpha{color.a}
114 {
115 }
116#endif
117
126 constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
127 m_isSet{true},
128 m_red {red},
129 m_green{green},
130 m_blue {blue},
131 m_alpha{alpha}
132 {
133 }
134
135
136#if defined (_MSC_VER)
137# pragma warning(push)
138# pragma warning(disable: 26495) // Ignore "Variable is uninitialized" warning to surpress incorrect code analysis
139#endif
147 Color(const String& string) :
148 Color{priv::constructColorFromString(string)}
149 {
150 }
151#if defined (_MSC_VER)
152# pragma warning(pop)
153#endif
154
162 Color(const char* string) :
163 Color{String{string}}
164 {
165 }
166
167
173 TGUI_NODISCARD constexpr bool isSet() const
174 {
175 return m_isSet;
176 }
177
178#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
184 operator sf::Color() const
185 {
186 return {m_red, m_green, m_blue, m_alpha};
187 }
188#endif
189
195 explicit operator Vertex::Color() const
196 {
197 return Vertex::Color{m_red, m_green, m_blue, m_alpha};
198 }
199
200
206 TGUI_NODISCARD constexpr std::uint8_t getRed() const
207 {
208 return m_red;
209 }
210
211
217 TGUI_NODISCARD constexpr std::uint8_t getGreen() const
218 {
219 return m_green;
220 }
221
222
228 TGUI_NODISCARD constexpr std::uint8_t getBlue() const
229 {
230 return m_blue;
231 }
232
233
239 TGUI_NODISCARD constexpr std::uint8_t getAlpha() const
240 {
241 return m_alpha;
242 }
243
244
248 TGUI_NODISCARD constexpr bool operator==(const Color& rhs) const
249 {
250 return (m_isSet == rhs.m_isSet)
251 && (m_red == rhs.m_red)
252 && (m_green == rhs.m_green)
253 && (m_blue == rhs.m_blue)
254 && (m_alpha == rhs.m_alpha);
255 }
256
257
261 TGUI_NODISCARD constexpr bool operator!=(const Color& right) const
262 {
263 return !(*this == right);
264 }
265
266
268 public:
269
270 static const Color Black;
271 static const Color White;
272 static const Color Red;
273 static const Color Green;
274 static const Color Blue;
275 static const Color Yellow;
276 static const Color Magenta;
277 static const Color Cyan;
278 static const Color Transparent;
279
280 static const std::array<std::pair<StringView, Color>, 9> colorNamesMap;
281
282
284 private:
285
286 bool m_isSet = false;
287 std::uint8_t m_red = 0;
288 std::uint8_t m_green = 0;
289 std::uint8_t m_blue = 0;
290 std::uint8_t m_alpha = 0;
291 };
292
294
295
296#if TGUI_COMPILED_WITH_CPP_VER >= 17
297 inline constexpr const Color Color::Black { 0, 0, 0};
298 inline constexpr const Color Color::White {255, 255, 255};
299 inline constexpr const Color Color::Red {255, 0, 0};
300 inline constexpr const Color Color::Green { 0, 255, 0};
301 inline constexpr const Color Color::Blue { 0, 0, 255};
302 inline constexpr const Color Color::Yellow {255, 255, 0};
303 inline constexpr const Color Color::Magenta {255, 0, 255};
304 inline constexpr const Color Color::Cyan { 0, 255, 255};
305 inline constexpr const Color Color::Transparent{ 0, 0, 0, 0};
306
307 inline constexpr const std::array<std::pair<StringView, Color>, 9> Color::colorNamesMap
308 {
309 {{U"black"sv, Color::Black},
310 {U"white"sv, Color::White},
311 {U"red"sv, Color::Red},
312 {U"yellow"sv, Color::Yellow},
313 {U"green"sv, Color::Green},
314 {U"cyan"sv, Color::Cyan},
315 {U"blue"sv, Color::Blue},
316 {U"magenta"sv, Color::Magenta},
317 {U"transparent"sv, Color::Transparent}}
318 };
319#endif
320}
321
323
324namespace tgui
325{
326 namespace priv
327 {
342 TGUI_NODISCARD TGUI_API Color constructColorFromString(const String& string);
343 }
344}
345
347
348#endif // TGUI_COLOR_HPP
Wrapper for colors.
Definition Color.hpp:74
static TGUI_NODISCARD constexpr Color applyOpacity(const Color &color, float alpha)
Returns the color with its alpha channel multiplied with the alpha parameter.
Definition Color.hpp:80
static const Color Transparent
Transparent (black) predefined color.
Definition Color.hpp:278
TGUI_NODISCARD constexpr bool isSet() const
Checks if a color was set.
Definition Color.hpp:173
TGUI_NODISCARD constexpr bool operator!=(const Color &right) const
Compares the color with another one.
Definition Color.hpp:261
constexpr Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition Color.hpp:108
TGUI_NODISCARD constexpr std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition Color.hpp:239
static const Color Black
Black predefined color.
Definition Color.hpp:270
static const Color Green
Green predefined color.
Definition Color.hpp:273
TGUI_NODISCARD constexpr std::uint8_t getRed() const
Returns the red component of the color.
Definition Color.hpp:206
constexpr Color()
Creates the object without a color.
Definition Color.hpp:93
TGUI_NODISCARD constexpr std::uint8_t getBlue() const
Returns the blue component of the color.
Definition Color.hpp:228
static const Color White
White predefined color.
Definition Color.hpp:271
Color(const char *string)
Creates the object from a string.
Definition Color.hpp:162
TGUI_NODISCARD constexpr bool operator==(const Color &rhs) const
Compares the color with another one.
Definition Color.hpp:248
static const Color Cyan
Cyan predefined color.
Definition Color.hpp:277
static const Color Magenta
Magenta predefined color.
Definition Color.hpp:276
static const Color Yellow
Yellow predefined color.
Definition Color.hpp:275
static const Color Red
Red predefined color.
Definition Color.hpp:272
static const Color Blue
Blue predefined color.
Definition Color.hpp:274
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:126
TGUI_NODISCARD constexpr std::uint8_t getGreen() const
Returns the green component of the color.
Definition Color.hpp:217
Color(const String &string)
Creates the object from a string.
Definition Color.hpp:147
Wrapper class to store strings.
Definition String.hpp:101
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Definition Vertex.hpp:42