1 ////////////////////////////////////////////////////////////
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
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.
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:
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.
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
21 // 3. This notice may not be removed or altered from any source distribution.
23 ////////////////////////////////////////////////////////////
25 #ifndef SFML_COLOR_HPP
26 #define SFML_COLOR_HPP
28 ////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Graphics/Export.hpp>
36 ////////////////////////////////////////////////////////////
37 /// \brief Utility class for manipulating RGBA colors
39 ////////////////////////////////////////////////////////////
40 class SFML_GRAPHICS_API Color
44 ////////////////////////////////////////////////////////////
45 /// \brief Default constructor
47 /// Constructs an opaque black color. It is equivalent to
48 /// sf::Color(0, 0, 0, 255).
50 ////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////
54 /// \brief Construct the color from its 4 RGBA components
56 /// \param red Red component (in the range [0, 255])
57 /// \param green Green component (in the range [0, 255])
58 /// \param blue Blue component (in the range [0, 255])
59 /// \param alpha Alpha (opacity) component (in the range [0, 255])
61 ////////////////////////////////////////////////////////////
62 Color(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha = 255);
64 ////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////
67 static const Color Black; ///< Black predefined color
68 static const Color White; ///< White predefined color
69 static const Color Red; ///< Red predefined color
70 static const Color Green; ///< Green predefined color
71 static const Color Blue; ///< Blue predefined color
72 static const Color Yellow; ///< Yellow predefined color
73 static const Color Magenta; ///< Magenta predefined color
74 static const Color Cyan; ///< Cyan predefined color
75 static const Color Transparent; ///< Transparent (black) predefined color
77 ////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////
80 Uint8 r; ///< Red component
81 Uint8 g; ///< Green component
82 Uint8 b; ///< Blue component
83 Uint8 a; ///< Alpha (opacity) component
86 ////////////////////////////////////////////////////////////
88 /// \brief Overload of the == operator
90 /// This operator compares two colors and check if they are equal.
92 /// \param left Left operand
93 /// \param right Right operand
95 /// \return True if colors are equal, false if they are different
97 ////////////////////////////////////////////////////////////
98 SFML_GRAPHICS_API bool operator ==(const Color& left, const Color& right);
100 ////////////////////////////////////////////////////////////
102 /// \brief Overload of the != operator
104 /// This operator compares two colors and check if they are different.
106 /// \param left Left operand
107 /// \param right Right operand
109 /// \return True if colors are different, false if they are equal
111 ////////////////////////////////////////////////////////////
112 SFML_GRAPHICS_API bool operator !=(const Color& left, const Color& right);
114 ////////////////////////////////////////////////////////////
116 /// \brief Overload of the binary + operator
118 /// This operator returns the component-wise sum of two colors.
119 /// Components that exceed 255 are clamped to 255.
121 /// \param left Left operand
122 /// \param right Right operand
124 /// \return Result of \a left + \a right
126 ////////////////////////////////////////////////////////////
127 SFML_GRAPHICS_API Color operator +(const Color& left, const Color& right);
129 ////////////////////////////////////////////////////////////
131 /// \brief Overload of the binary - operator
133 /// This operator returns the component-wise subtraction of two colors.
134 /// Components below 0 are clamped to 0.
136 /// \param left Left operand
137 /// \param right Right operand
139 /// \return Result of \a left - \a right
141 ////////////////////////////////////////////////////////////
142 SFML_GRAPHICS_API Color operator -(const Color& left, const Color& right);
144 ////////////////////////////////////////////////////////////
146 /// \brief Overload of the binary * operator
148 /// This operator returns the component-wise multiplication
149 /// (also called "modulation") of two colors.
150 /// Components are then divided by 255 so that the result is
151 /// still in the range [0, 255].
153 /// \param left Left operand
154 /// \param right Right operand
156 /// \return Result of \a left * \a right
158 ////////////////////////////////////////////////////////////
159 SFML_GRAPHICS_API Color operator *(const Color& left, const Color& right);
161 ////////////////////////////////////////////////////////////
163 /// \brief Overload of the binary += operator
165 /// This operator computes the component-wise sum of two colors,
166 /// and assigns the result to the left operand.
167 /// Components that exceed 255 are clamped to 255.
169 /// \param left Left operand
170 /// \param right Right operand
172 /// \return Reference to \a left
174 ////////////////////////////////////////////////////////////
175 SFML_GRAPHICS_API Color& operator +=(Color& left, const Color& right);
177 ////////////////////////////////////////////////////////////
179 /// \brief Overload of the binary -= operator
181 /// This operator computes the component-wise subtraction of two colors,
182 /// and assigns the result to the left operand.
183 /// Components below 0 are clamped to 0.
185 /// \param left Left operand
186 /// \param right Right operand
188 /// \return Reference to \a left
190 ////////////////////////////////////////////////////////////
191 SFML_GRAPHICS_API Color& operator -=(Color& left, const Color& right);
193 ////////////////////////////////////////////////////////////
195 /// \brief Overload of the binary *= operator
197 /// This operator returns the component-wise multiplication
198 /// (also called "modulation") of two colors, and assigns
199 /// the result to the left operand.
200 /// Components are then divided by 255 so that the result is
201 /// still in the range [0, 255].
203 /// \param left Left operand
204 /// \param right Right operand
206 /// \return Reference to \a left
208 ////////////////////////////////////////////////////////////
209 SFML_GRAPHICS_API Color& operator *=(Color& left, const Color& right);
214 #endif // SFML_COLOR_HPP
217 ////////////////////////////////////////////////////////////
219 /// \ingroup graphics
221 /// sf::Color is a simple color class composed of 4 components:
225 /// \li Alpha (opacity)
227 /// Each component is a public member, an unsigned integer in
228 /// the range [0, 255]. Thus, colors can be constructed and
229 /// manipulated very easily:
232 /// sf::Color color(255, 0, 0); // red
233 /// color.r = 0; // make it black
234 /// color.b = 128; // make it dark blue
237 /// The fourth component of colors, named "alpha", represents
238 /// the opacity of the color. A color with an alpha value of
239 /// 255 will be fully opaque, while an alpha value of 0 will
240 /// make a color fully transparent, whatever the value of the
241 /// other components is.
243 /// The most common colors are already defined as static variables:
245 /// sf::Color black = sf::Color::Black;
246 /// sf::Color white = sf::Color::White;
247 /// sf::Color red = sf::Color::Red;
248 /// sf::Color green = sf::Color::Green;
249 /// sf::Color blue = sf::Color::Blue;
250 /// sf::Color yellow = sf::Color::Yellow;
251 /// sf::Color magenta = sf::Color::Magenta;
252 /// sf::Color cyan = sf::Color::Cyan;
253 /// sf::Color transparent = sf::Color::Transparent;
256 /// Colors can also be added and modulated (multiplied) using the
257 /// overloaded operators + and *.
259 ////////////////////////////////////////////////////////////