TGUI  1.5
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#ifndef TGUI_COLOR_HPP
26#define TGUI_COLOR_HPP
27
28#include <TGUI/String.hpp>
29#include <TGUI/Vertex.hpp>
30
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <cstdint>
33 #include <string>
34 #include <map>
35#endif
36
37#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
38 #include <SFML/Graphics/Color.hpp>
39#endif
40
42
43TGUI_MODULE_EXPORT namespace tgui
44{
45 class Color;
46}
47
48namespace tgui
49{
50 namespace priv
51 {
52 TGUI_API Color constructColorFromString(const String& string);
53 }
54}
55
56TGUI_MODULE_EXPORT namespace tgui
57{
66#if defined(TGUI_SYSTEM_WINDOWS) && TGUI_COMPILED_WITH_CPP_VER >= 17
67 // dllimport doesn't work for the static members when they are defined as inline constexpr outside the class,
68 // so we aren't allowed to put TGUI_API here on Windows.
69 class Color
70#else
71 class TGUI_API Color
72#endif
73 {
74 public:
75
79 TGUI_NODISCARD static constexpr Color applyOpacity(const Color& color, float alpha)
80 {
81 return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
82 }
83
84 public:
85
91 constexpr Color() :
92 m_isSet{false},
93 m_red {0},
94 m_green{0},
95 m_blue {0},
96 m_alpha{0}
97 {
98 }
99
100#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
106 constexpr Color(const sf::Color& color) :
107 m_isSet{true},
108 m_red {color.r},
109 m_green{color.g},
110 m_blue {color.b},
111 m_alpha{color.a}
112 {
113 }
114#endif
115
124 constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
125 m_isSet{true},
126 m_red {red},
127 m_green{green},
128 m_blue {blue},
129 m_alpha{alpha}
130 {
131 }
132
133#if defined (_MSC_VER)
134# pragma warning(push)
135# pragma warning(disable: 26495) // Ignore "Variable is uninitialized" warning to surpress incorrect code analysis
136#endif
144 Color(const String& string) :
145 Color{priv::constructColorFromString(string)}
146 {
147 }
148#if defined (_MSC_VER)
149# pragma warning(pop)
150#endif
151
159 Color(const char* string) :
160 Color{String{string}}
161 {
162 }
163
169 TGUI_NODISCARD constexpr bool isSet() const
170 {
171 return m_isSet;
172 }
173
174#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_DISABLE_SFML_CONVERSIONS
180 operator sf::Color() const
181 {
182 return {m_red, m_green, m_blue, m_alpha};
183 }
184#endif
185
191 explicit operator Vertex::Color() const
192 {
193 return Vertex::Color{m_red, m_green, m_blue, m_alpha};
194 }
195
201 TGUI_NODISCARD constexpr std::uint8_t getRed() const
202 {
203 return m_red;
204 }
205
211 TGUI_NODISCARD constexpr std::uint8_t getGreen() const
212 {
213 return m_green;
214 }
215
221 TGUI_NODISCARD constexpr std::uint8_t getBlue() const
222 {
223 return m_blue;
224 }
225
231 TGUI_NODISCARD constexpr std::uint8_t getAlpha() const
232 {
233 return m_alpha;
234 }
235
239 TGUI_NODISCARD constexpr bool operator==(const Color& rhs) const
240 {
241 return (m_isSet == rhs.m_isSet)
242 && (m_red == rhs.m_red)
243 && (m_green == rhs.m_green)
244 && (m_blue == rhs.m_blue)
245 && (m_alpha == rhs.m_alpha);
246 }
247
251 TGUI_NODISCARD constexpr bool operator!=(const Color& right) const
252 {
253 return !(*this == right);
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
272 private:
273
274 bool m_isSet = false;
275 std::uint8_t m_red = 0;
276 std::uint8_t m_green = 0;
277 std::uint8_t m_blue = 0;
278 std::uint8_t m_alpha = 0;
279 };
280
282
283#if TGUI_COMPILED_WITH_CPP_VER >= 17
284 inline constexpr const Color Color::Black { 0, 0, 0};
285 inline constexpr const Color Color::White {255, 255, 255};
286 inline constexpr const Color Color::Red {255, 0, 0};
287 inline constexpr const Color Color::Green { 0, 255, 0};
288 inline constexpr const Color Color::Blue { 0, 0, 255};
289 inline constexpr const Color Color::Yellow {255, 255, 0};
290 inline constexpr const Color Color::Magenta {255, 0, 255};
291 inline constexpr const Color Color::Cyan { 0, 255, 255};
292 inline constexpr const Color Color::Transparent{ 0, 0, 0, 0};
293
294 inline constexpr const std::array<std::pair<StringView, Color>, 9> Color::colorNamesMap
295 {
296 {{U"black"sv, Color::Black},
297 {U"white"sv, Color::White},
298 {U"red"sv, Color::Red},
299 {U"yellow"sv, Color::Yellow},
300 {U"green"sv, Color::Green},
301 {U"cyan"sv, Color::Cyan},
302 {U"blue"sv, Color::Blue},
303 {U"magenta"sv, Color::Magenta},
304 {U"transparent"sv, Color::Transparent}}
305 };
306#endif
307}
308
310
311namespace tgui
312{
313 namespace priv
314 {
329 TGUI_NODISCARD TGUI_API Color constructColorFromString(const String& string);
330 }
331}
332
334
335#endif // TGUI_COLOR_HPP
Wrapper for colors.
Definition Color.hpp:73
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:79
static const Color Transparent
Transparent (black) predefined color.
Definition Color.hpp:267
TGUI_NODISCARD constexpr bool isSet() const
Checks if a color was set.
Definition Color.hpp:169
TGUI_NODISCARD constexpr bool operator!=(const Color &right) const
Compares the color with another one.
Definition Color.hpp:251
constexpr Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition Color.hpp:106
TGUI_NODISCARD constexpr std::uint8_t getAlpha() const
Returns the alpha component of the color.
Definition Color.hpp:231
static const Color Black
Black predefined color.
Definition Color.hpp:259
static const Color Green
Green predefined color.
Definition Color.hpp:262
TGUI_NODISCARD constexpr std::uint8_t getRed() const
Returns the red component of the color.
Definition Color.hpp:201
constexpr Color()
Creates the object without a color.
Definition Color.hpp:91
TGUI_NODISCARD constexpr std::uint8_t getBlue() const
Returns the blue component of the color.
Definition Color.hpp:221
static const Color White
White predefined color.
Definition Color.hpp:260
Color(const char *string)
Creates the object from a string.
Definition Color.hpp:159
TGUI_NODISCARD constexpr bool operator==(const Color &rhs) const
Compares the color with another one.
Definition Color.hpp:239
static const Color Cyan
Cyan predefined color.
Definition Color.hpp:266
static const Color Magenta
Magenta predefined color.
Definition Color.hpp:265
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
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:124
TGUI_NODISCARD constexpr std::uint8_t getGreen() const
Returns the green component of the color.
Definition Color.hpp:211
Color(const String &string)
Creates the object from a string.
Definition Color.hpp:144
Wrapper class to store strings.
Definition String.hpp:96
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition Vertex.hpp:41