TGUI 1.13
Loading...
Searching...
No Matches
Color.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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
33#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
34 #include <SFML/Graphics/Color.hpp>
35#endif
36
38
39namespace tgui
40{
41 class Color;
42
43 namespace priv
44 {
45 TGUI_API Color constructColorFromString(const String& string);
46 }
47
56#if defined(TGUI_SYSTEM_WINDOWS)
57 // dllimport doesn't work for the static members when they are defined as inline constexpr outside the class,
58 // so we aren't allowed to put TGUI_API here on Windows.
59 class Color
60#else
61 class TGUI_API Color
62#endif
63 {
64 public:
68 [[nodiscard]] static constexpr Color applyOpacity(const Color& color, float alpha)
69 {
70 return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
71 }
72
73 public:
79 constexpr Color() :
80 m_isSet{false},
81 m_red{0},
82 m_green{0},
83 m_blue{0},
84 m_alpha{0}
85 {
86 }
87
88#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
94 constexpr Color(const sf::Color& color) :
95 m_isSet{true},
96 m_red{color.r},
97 m_green{color.g},
98 m_blue{color.b},
99 m_alpha{color.a}
100 {
101 }
102#endif
103
112 constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
113 m_isSet{true},
114 m_red{red},
115 m_green{green},
116 m_blue{blue},
117 m_alpha{alpha}
118 {
119 }
120
121#if defined(_MSC_VER)
122 #pragma warning(push)
123 #pragma warning(disable : 26495) // Ignore "Variable is uninitialized" warning to surpress incorrect code analysis
124#endif
132 Color(const String& string) :
133 Color{priv::constructColorFromString(string)}
134 {
135 }
136#if defined(_MSC_VER)
137 #pragma warning(pop)
138#endif
139
147 Color(const char* string) :
148 Color{String{string}}
149 {
150 }
151
157 [[nodiscard]] constexpr bool isSet() const
158 {
159 return m_isSet;
160 }
161
162#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
168 operator sf::Color() const
169 {
170 return {m_red, m_green, m_blue, m_alpha};
171 }
172#endif
173
179 explicit operator Vertex::Color() const
180 {
181 return Vertex::Color{m_red, m_green, m_blue, m_alpha};
182 }
183
189 [[nodiscard]] constexpr std::uint8_t getRed() const
190 {
191 return m_red;
192 }
193
199 [[nodiscard]] constexpr std::uint8_t getGreen() const
200 {
201 return m_green;
202 }
203
209 [[nodiscard]] constexpr std::uint8_t getBlue() const
210 {
211 return m_blue;
212 }
213
219 [[nodiscard]] constexpr std::uint8_t getAlpha() const
220 {
221 return m_alpha;
222 }
223
227 [[nodiscard]] constexpr bool operator==(const Color& rhs) const
228 {
229 return (m_isSet == rhs.m_isSet) && (m_red == rhs.m_red) && (m_green == rhs.m_green) && (m_blue == rhs.m_blue)
230 && (m_alpha == rhs.m_alpha);
231 }
232
236 [[nodiscard]] constexpr bool operator!=(const Color& right) const
237 {
238 return !(*this == right);
239 }
240
242
243 public:
244 static const Color Black;
245 static const Color White;
246 static const Color Red;
247 static const Color Green;
248 static const Color Blue;
249 static const Color Yellow;
250 static const Color Magenta;
251 static const Color Cyan;
252 static const Color Transparent;
253
254 static const std::array<std::pair<StringView, Color>, 9> colorNamesMap;
255
257
258 private:
259 bool m_isSet = false;
260 std::uint8_t m_red = 0;
261 std::uint8_t m_green = 0;
262 std::uint8_t m_blue = 0;
263 std::uint8_t m_alpha = 0;
264 };
265
267
268 inline constexpr Color Color::Black{0, 0, 0};
269 inline constexpr Color Color::White{255, 255, 255};
270 inline constexpr Color Color::Red{255, 0, 0};
271 inline constexpr Color Color::Green{0, 255, 0};
272 inline constexpr Color Color::Blue{0, 0, 255};
273 inline constexpr Color Color::Yellow{255, 255, 0};
274 inline constexpr Color Color::Magenta{255, 0, 255};
275 inline constexpr Color Color::Cyan{0, 255, 255};
276 inline constexpr Color Color::Transparent{0, 0, 0, 0};
277
278 inline constexpr std::array<std::pair<StringView, Color>, 9> Color::colorNamesMap{
279 {{U"black"sv, Color::Black},
280 {U"white"sv, Color::White},
281 {U"red"sv, Color::Red},
282 {U"yellow"sv, Color::Yellow},
283 {U"green"sv, Color::Green},
284 {U"cyan"sv, Color::Cyan},
285 {U"blue"sv, Color::Blue},
286 {U"magenta"sv, Color::Magenta},
287 {U"transparent"sv, Color::Transparent}}};
288
290
291 namespace priv
292 {
307 [[nodiscard]] TGUI_API Color constructColorFromString(const String& string);
308 } // namespace priv
309} // namespace tgui
310
311#endif // TGUI_COLOR_HPP
Wrapper for colors.
Definition Color.hpp:63
constexpr std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition Color.hpp:219
static const Color Red
Red predefined color.
Definition Color.hpp:246
static const Color Transparent
Transparent (black) predefined color.
Definition Color.hpp:252
static const Color Blue
Blue predefined color.
Definition Color.hpp:248
constexpr Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition Color.hpp:94
constexpr std::uint8_t getGreen() const
Returns the green component of the color.
Definition Color.hpp:199
static constexpr Color applyOpacity(const Color &color, float alpha)
Returns the color with its alpha channel multiplied with the alpha parameter.
Definition Color.hpp:68
static const Color Yellow
Yellow predefined color.
Definition Color.hpp:249
static const Color Green
Green predefined color.
Definition Color.hpp:247
static const Color Black
Black predefined color.
Definition Color.hpp:244
constexpr Color()
Creates the object without a color.
Definition Color.hpp:79
constexpr std::uint8_t getBlue() const
Returns the blue component of the color.
Definition Color.hpp:209
static const Color White
White predefined color.
Definition Color.hpp:245
constexpr bool operator!=(const Color &right) const
Compares the color with another one.
Definition Color.hpp:236
Color(const char *string)
Creates the object from a string.
Definition Color.hpp:147
constexpr std::uint8_t getRed() const
Returns the red component of the color.
Definition Color.hpp:189
constexpr bool isSet() const
Checks if a color was set.
Definition Color.hpp:157
constexpr bool operator==(const Color &rhs) const
Compares the color with another one.
Definition Color.hpp:227
static const Color Magenta
Magenta predefined color.
Definition Color.hpp:250
static const Color Cyan
Cyan predefined color.
Definition Color.hpp:251
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:112
Color(const String &string)
Creates the object from a string.
Definition Color.hpp:132
Wrapper class to store strings.
Definition String.hpp:94
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Definition Vertex.hpp:39