TGUI  0.8.5
Color.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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/Config.hpp>
30 #include <SFML/Graphics/Color.hpp>
31 #include <SFML/System/String.hpp>
32 #include <cstdint>
33 #include <string>
34 #include <map>
35 
37 
38 namespace tgui
39 {
48  class TGUI_API Color
49  {
50  public:
51 
55  static Color calcColorOpacity(const Color& color, float alpha)
56  {
57  return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
58  }
59 
60 
61  public:
62 
68  Color() :
69  m_isSet{false}
70  {
71  }
72 
73 
79  Color(const sf::Color& color) :
80  m_isSet{true},
81  m_color{color}
82  {
83  }
84 
85 
94  Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
95  m_isSet{true},
96  m_color{red, green, blue, alpha}
97  {
98  }
99 
100 
108  Color(const sf::String& string);
109 
110 
118  Color(const std::string& string);
119 
120 
128  Color(const char* string);
129 
130 
136  bool isSet() const;
137 
138 
144  operator sf::Color() const;
145 
146 
152  std::uint8_t getRed() const;
153 
154 
160  std::uint8_t getGreen() const;
161 
162 
168  std::uint8_t getBlue() const;
169 
170 
176  std::uint8_t getAlpha() const;
177 
178 
182  bool operator==(const Color& right) const;
183 
184 
188  bool operator!=(const Color& right) const;
189 
190 
194  bool operator==(const sf::Color& right) const;
195 
196 
200  bool operator!=(const sf::Color& right) const;
201 
202 
204  public:
205 
206  static const Color Black;
207  static const Color White;
208  static const Color Red;
209  static const Color Green;
210  static const Color Blue;
211  static const Color Yellow;
212  static const Color Magenta;
213  static const Color Cyan;
214  static const Color Transparent;
215 
216  static const std::map<std::string, Color> colorMap;
217 
218 
220  private:
221 
222  bool m_isSet;
223  sf::Color m_color;
224  };
225 
226 #ifdef TGUI_USE_CPP17
227  // Can't be inlined in the Color class itself because Color would still be an incomplete type
228  inline const Color Color::Black { 0, 0, 0};
229  inline const Color Color::White {255, 255, 255};
230  inline const Color Color::Red {255, 0, 0};
231  inline const Color Color::Green { 0, 255, 0};
232  inline const Color Color::Blue { 0, 0, 255};
233  inline const Color Color::Yellow {255, 255, 0};
234  inline const Color Color::Magenta {255, 0, 255};
235  inline const Color Color::Cyan { 0, 255, 255};
236  inline const Color Color::Transparent{ 0, 0, 0, 0};
237 
238  inline const std::map<std::string, Color> Color::colorMap{
239  {"black", Color::Black},
240  {"white", Color::White},
241  {"red", Color::Red},
242  {"yellow", Color::Yellow},
243  {"green", Color::Green},
244  {"cyan", Color::Cyan},
245  {"blue", Color::Blue},
246  {"magenta", Color::Magenta},
247  {"transparent", Color::Transparent}
248  };
249 #endif
250 
252 }
253 
255 
256 #endif // TGUI_COLOR_HPP
257 
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
std::uint8_t getRed() const
Returns the red component of the color.
std::uint8_t getGreen() const
Returns the green component of the color.
Wrapper for colors.
Definition: Color.hpp:48
static const Color Cyan
Cyan predefined color.
Definition: Color.hpp:213
std::uint8_t getBlue() const
Returns the blue component of the color.
Color()
Creates the object without a color.
Definition: Color.hpp:68
static const Color Magenta
Magenta predefined color.
Definition: Color.hpp:212
static const Color Blue
Blue predefined color.
Definition: Color.hpp:210
static const Color Red
Red predefined color.
Definition: Color.hpp:208
std::uint8_t getAlpha() const
Returns the alpha component of the color.
TGUI_CONSTEXPR bool operator!=(const Vector2f &left, const Vector2f &right)
Overload of binary operator ==.
Definition: Vector2f.hpp:224
static const Color Black
Black predefined color.
Definition: Color.hpp:206
TGUI_CONSTEXPR bool operator==(const Vector2f &left, const Vector2f &right)
Overload of binary operator ==.
Definition: Vector2f.hpp:216
Color(const sf::Color &color)
Creates the object from an sf::Color.
Definition: Color.hpp:79
static const Color Transparent
Transparent (black) predefined color.
Definition: Color.hpp:214
static Color calcColorOpacity(const Color &color, float alpha)
Returns the color with its alpha channel multiplied with the alpha parameter.
Definition: Color.hpp:55
static const Color White
White predefined color.
Definition: Color.hpp:207
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:94
static const Color Yellow
Yellow predefined color.
Definition: Color.hpp:211
static const Color Green
Green predefined color.
Definition: Color.hpp:209