TGUI 1.11
Loading...
Searching...
No Matches
Color.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#ifndef TGUI_COLOR_HPP
26#define TGUI_COLOR_HPP
27
28#include <TGUI/String.hpp>
29#include <TGUI/Vertex.hpp>
30
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}
45
46namespace tgui
47{
48 namespace priv
49 {
50 TGUI_API Color constructColorFromString(const String& string);
51 }
52}
53
54namespace tgui
55{
64#if defined(TGUI_SYSTEM_WINDOWS) && TGUI_COMPILED_WITH_CPP_VER >= 17
65 // dllimport doesn't work for the static members when they are defined as inline constexpr outside the class,
66 // so we aren't allowed to put TGUI_API here on Windows.
67 class Color
68#else
69 class TGUI_API Color
70#endif
71 {
72 public:
73
77 TGUI_NODISCARD static constexpr Color applyOpacity(const Color& color, float alpha)
78 {
79 return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
80 }
81
82 public:
83
89 constexpr Color() :
90 m_isSet{false},
91 m_red {0},
92 m_green{0},
93 m_blue {0},
94 m_alpha{0}
95 {
96 }
97
98#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
104 constexpr Color(const sf::Color& color) :
105 m_isSet{true},
106 m_red {color.r},
107 m_green{color.g},
108 m_blue {color.b},
109 m_alpha{color.a}
110 {
111 }
112#endif
113
122 constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
123 m_isSet{true},
124 m_red {red},
125 m_green{green},
126 m_blue {blue},
127 m_alpha{alpha}
128 {
129 }
130
131#if defined (_MSC_VER)
132# pragma warning(push)
133# pragma warning(disable: 26495) // Ignore "Variable is uninitialized" warning to surpress incorrect code analysis
134#endif
142 Color(const String& string) :
143 Color{priv::constructColorFromString(string)}
144 {
145 }
146#if defined (_MSC_VER)
147# pragma warning(pop)
148#endif
149
157 Color(const char* string) :
158 Color{String{string}}
159 {
160 }
161
167 TGUI_NODISCARD constexpr bool isSet() const
168 {
169 return m_isSet;
170 }
171
172#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
178 operator sf::Color() const
179 {
180 return {m_red, m_green, m_blue, m_alpha};
181 }
182#endif
183
189 explicit operator Vertex::Color() const
190 {
191 return Vertex::Color{m_red, m_green, m_blue, m_alpha};
192 }
193
199 TGUI_NODISCARD constexpr std::uint8_t getRed() const
200 {
201 return m_red;
202 }
203
209 TGUI_NODISCARD constexpr std::uint8_t getGreen() const
210 {
211 return m_green;
212 }
213
219 TGUI_NODISCARD constexpr std::uint8_t getBlue() const
220 {
221 return m_blue;
222 }
223
229 TGUI_NODISCARD constexpr std::uint8_t getAlpha() const
230 {
231 return m_alpha;
232 }
233
237 TGUI_NODISCARD 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
249 TGUI_NODISCARD constexpr bool operator!=(const Color& right) const
250 {
251 return !(*this == right);
252 }
253
255 public:
256
257 static const Color Black;
258 static const Color White;
259 static const Color Red;
260 static const Color Green;
261 static const Color Blue;
262 static const Color Yellow;
263 static const Color Magenta;
264 static const Color Cyan;
265 static const Color Transparent;
266
267 static const std::array<std::pair<StringView, Color>, 9> colorNamesMap;
268
270 private:
271
272 bool m_isSet = false;
273 std::uint8_t m_red = 0;
274 std::uint8_t m_green = 0;
275 std::uint8_t m_blue = 0;
276 std::uint8_t m_alpha = 0;
277 };
278
280
281#if TGUI_COMPILED_WITH_CPP_VER >= 17
282 inline constexpr const Color Color::Black { 0, 0, 0};
283 inline constexpr const Color Color::White {255, 255, 255};
284 inline constexpr const Color Color::Red {255, 0, 0};
285 inline constexpr const Color Color::Green { 0, 255, 0};
286 inline constexpr const Color Color::Blue { 0, 0, 255};
287 inline constexpr const Color Color::Yellow {255, 255, 0};
288 inline constexpr const Color Color::Magenta {255, 0, 255};
289 inline constexpr const Color Color::Cyan { 0, 255, 255};
290 inline constexpr const Color Color::Transparent{ 0, 0, 0, 0};
291
292 inline constexpr const std::array<std::pair<StringView, Color>, 9> Color::colorNamesMap
293 {
294 {{U"black"sv, Color::Black},
295 {U"white"sv, Color::White},
296 {U"red"sv, Color::Red},
297 {U"yellow"sv, Color::Yellow},
298 {U"green"sv, Color::Green},
299 {U"cyan"sv, Color::Cyan},
300 {U"blue"sv, Color::Blue},
301 {U"magenta"sv, Color::Magenta},
302 {U"transparent"sv, Color::Transparent}}
303 };
304#endif
305}
306
308
309namespace tgui
310{
311 namespace priv
312 {
327 TGUI_NODISCARD TGUI_API Color constructColorFromString(const String& string);
328 }
329}
330
332
333#endif // TGUI_COLOR_HPP
Wrapper for colors.
Definition Color.hpp:71
constexpr std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition Color.hpp:229
static const Color Transparent
Transparent (black) predefined color.
Definition Color.hpp:265
constexpr Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition Color.hpp:104
constexpr std::uint8_t getGreen() const
Returns the green component of the color.
Definition Color.hpp:209
static constexpr Color applyOpacity(const Color &color, float alpha)
Returns the color with its alpha channel multiplied with the alpha parameter.
Definition Color.hpp:77
static const Color Black
Black predefined color.
Definition Color.hpp:257
static const Color Green
Green predefined color.
Definition Color.hpp:260
constexpr Color()
Creates the object without a color.
Definition Color.hpp:89
constexpr std::uint8_t getBlue() const
Returns the blue component of the color.
Definition Color.hpp:219
static const Color White
White predefined color.
Definition Color.hpp:258
constexpr bool operator!=(const Color &right) const
Compares the color with another one.
Definition Color.hpp:249
Color(const char *string)
Creates the object from a string.
Definition Color.hpp:157
constexpr std::uint8_t getRed() const
Returns the red component of the color.
Definition Color.hpp:199
static const Color Cyan
Cyan predefined color.
Definition Color.hpp:264
constexpr bool isSet() const
Checks if a color was set.
Definition Color.hpp:167
constexpr bool operator==(const Color &rhs) const
Compares the color with another one.
Definition Color.hpp:237
static const Color Magenta
Magenta predefined color.
Definition Color.hpp:263
static const Color Yellow
Yellow predefined color.
Definition Color.hpp:262
static const Color Red
Red predefined color.
Definition Color.hpp:259
static const Color Blue
Blue predefined color.
Definition Color.hpp:261
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:122
Color(const String &string)
Creates the object from a string.
Definition Color.hpp:142
Wrapper class to store strings.
Definition String.hpp:94
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Definition Vertex.hpp:39