TGUI  0.8.2
Color.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 <cstdint>
32 #include <string>
33 #include <map>
34 
36 
37 namespace tgui
38 {
47  class TGUI_API Color
48  {
49  public:
50 
54  static Color calcColorOpacity(const Color& color, float alpha)
55  {
56  return {color.getRed(), color.getGreen(), color.getBlue(), static_cast<std::uint8_t>(color.getAlpha() * alpha)};
57  }
58 
59 
60  public:
61 
67  Color() :
68  m_isSet{false}
69  {
70  }
71 
72 
78  Color(const sf::Color& color) :
79  m_isSet{true},
80  m_color{color}
81  {
82  }
83 
84 
93  Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) :
94  m_isSet{true},
95  m_color{red, green, blue, alpha}
96  {
97  }
98 
99 
107  Color(const std::string& string);
108 
109 
117  Color(const char* string);
118 
119 
125  bool isSet() const;
126 
127 
133  operator sf::Color() const;
134 
135 
141  std::uint8_t getRed() const;
142 
143 
149  std::uint8_t getGreen() const;
150 
151 
157  std::uint8_t getBlue() const;
158 
159 
165  std::uint8_t getAlpha() const;
166 
167 
171  bool operator==(const Color& right) const;
172 
173 
177  bool operator!=(const Color& right) const;
178 
179 
183  bool operator==(const sf::Color& right) const;
184 
185 
189  bool operator!=(const sf::Color& right) const;
190 
191 
193  public:
194 
195  static const Color Black;
196  static const Color White;
197  static const Color Red;
198  static const Color Green;
199  static const Color Blue;
200  static const Color Yellow;
201  static const Color Magenta;
202  static const Color Cyan;
203  static const Color Transparent;
204 
205  static const std::map<std::string, Color> colorMap;
206 
207 
209  private:
210 
211  bool m_isSet;
212  sf::Color m_color;
213  };
214 
215 #ifdef TGUI_USE_CPP17
216  // Can't be inlined in the Color class itself because Color would still be an incomplete type
217  inline const Color Color::Black { 0, 0, 0};
218  inline const Color Color::White {255, 255, 255};
219  inline const Color Color::Red {255, 0, 0};
220  inline const Color Color::Green { 0, 255, 0};
221  inline const Color Color::Blue { 0, 0, 255};
222  inline const Color Color::Yellow {255, 255, 0};
223  inline const Color Color::Magenta {255, 0, 255};
224  inline const Color Color::Cyan { 0, 255, 255};
225  inline const Color Color::Transparent{ 0, 0, 0, 0};
226 
227  inline const std::map<std::string, Color> Color::colorMap{
228  {"black", Color::Black},
229  {"white", Color::White},
230  {"red", Color::Red},
231  {"yellow", Color::Yellow},
232  {"green", Color::Green},
233  {"cyan", Color::Cyan},
234  {"blue", Color::Blue},
235  {"magenta", Color::Magenta},
236  {"transparent", Color::Transparent}
237  };
238 #endif
239 
241 }
242 
244 
245 #endif // TGUI_COLOR_HPP
246 
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:47
static const Color Cyan
Cyan predefined color.
Definition: Color.hpp:202
std::uint8_t getBlue() const
Returns the blue component of the color.
Color()
Creates the object without a color.
Definition: Color.hpp:67
static const Color Magenta
Magenta predefined color.
Definition: Color.hpp:201
static const Color Blue
Blue predefined color.
Definition: Color.hpp:199
static const Color Red
Red predefined color.
Definition: Color.hpp:197
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:195
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:78
static const Color Transparent
Transparent (black) predefined color.
Definition: Color.hpp:203
static Color calcColorOpacity(const Color &color, float alpha)
Returns the color with its alpha channel multiplied with the alpha parameter.
Definition: Color.hpp:54
static const Color White
White predefined color.
Definition: Color.hpp:196
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:93
static const Color Yellow
Yellow predefined color.
Definition: Color.hpp:200
static const Color Green
Green predefined color.
Definition: Color.hpp:198