ManaPlus
Public Member Functions | Data Fields
Color Class Reference

#include <color.h>

Public Member Functions

 Color ()
 
 Color (const unsigned int color)
 
 Color (const unsigned int ar, const unsigned int ag, const unsigned int ab, const unsigned int aa)
 
Color operator+ (const Color &color) const
 
Color operator- (const Color &color) const
 
Color operator* (const float value) const
 
bool operator== (const Color &color) const
 
bool operator!= (const Color &color) const
 

Data Fields

unsigned int r
 
unsigned int g
 
unsigned int b
 
unsigned int a
 

Detailed Description

Represents a color with red, green, blue and alpha components.

Definition at line 75 of file color.h.

Constructor & Destructor Documentation

◆ Color() [1/3]

Color::Color ( )
inline

Constructor. Initializes the color to black.

Definition at line 81 of file color.h.

81  :
82  r(0U),
83  g(0U),
84  b(0U),
85  a(255U)
86  {
87  }
unsigned int a
Definition: color.h:251
unsigned int b
Definition: color.h:245
unsigned int r
Definition: color.h:235
unsigned int g
Definition: color.h:240

◆ Color() [2/3]

Color::Color ( const unsigned int  color)
inlineexplicit

Constructor. Constructs a color from the bytes in an integer. Call it with a hexadecimal constant for HTML-style color representation. The alpha component is 255 by default.

EXAMPLE: Color(0xff50a0) constructs a very nice pinkish color.

NOTE: Because of this constructor, integers will be automatically casted to a color by your compiler.

Parameters
colorThe color to initialise the object with.

Definition at line 102 of file color.h.

102  :
103  r((color >> 16) & 0xFFU),
104  g((color >> 8) & 0xFFU),
105  b(color & 0xFFU),
106  a(255U)
107  {
108  }

◆ Color() [3/3]

Color::Color ( const unsigned int  ar,
const unsigned int  ag,
const unsigned int  ab,
const unsigned int  aa 
)
inline

Constructor. The default alpha value is 255.

Parameters
arRed color component (range 0-255).
agGreen color component (range 0-255).
abBlue color component (range 0-255).
aaAlpha, used for transparency. A value of 0 means totaly transparent, 255 is totaly opaque.

Definition at line 119 of file color.h.

122  :
123  r(ar),
124  g(ag),
125  b(ab),
126  a(aa)
127  {
128  }

Member Function Documentation

◆ operator!=()

bool Color::operator!= ( const Color color) const
inline

Compares two colors.

Returns
True if the two colors have different RGBA components, false otherwise.

Definition at line 224 of file color.h.

225  {
226  return !(r == color.r &&
227  g == color.g &&
228  b == color.b &&
229  a == color.a);
230  }

References a, b, g, and r.

◆ operator*()

Color Color::operator* ( const float  value) const
inline

Multiplies the RGB values of a color with a float value. The values will be clamped if they go out of range.

Parameters
valueThe value to multiply the color with.
Returns
The multiplied colors. The alpha value will, unlike the add and subtract operations, be multiplied as well.

Definition at line 190 of file color.h.

191  {
192  Color result(CAST_U32(static_cast<float>(r) * value),
193  CAST_U32(static_cast<float>(g) * value),
194  CAST_U32(static_cast<float>(b) * value),
195  a);
196 
197  result.r = (result.r > 255U ? 255U : result.r);
198  result.g = (result.g > 255U ? 255U : result.g);
199  result.b = (result.b > 255U ? 255U : result.b);
200 
201  return result;
202  }
#define CAST_U32
Definition: cast.h:31
Definition: color.h:76

References a, b, CAST_U32, g, and r.

◆ operator+()

Color Color::operator+ ( const Color color) const
inline

Adds the RGB values of two colors together. The values will be clamped if they go out of range.

WARNING: This function will reset the alpha value of the returned color to 255.

Parameters
colorA color to add to this color.
Returns
The added colors with an alpha value set to 255.

Definition at line 143 of file color.h.

144  {
145  Color result(r + color.r,
146  g + color.g,
147  b + color.b,
148  255U);
149 
150  result.r = (result.r > 255U ? 255U : result.r);
151  result.g = (result.g > 255U ? 255U : result.g);
152  result.b = (result.b > 255U ? 255U : result.b);
153 
154  return result;
155  }

References b, g, and r.

◆ operator-()

Color Color::operator- ( const Color color) const
inline

Subtracts the RGB values of one color from another. The values will be clamped if they go out of range.

WARNING: This function will reset the alpha value of the returned color to 255.

Parameters
colorA color to subtract from this color.
Returns
The subtracted colors with an alpha value set to 255.

Definition at line 167 of file color.h.

168  {
169  Color result(r - color.r,
170  g - color.g,
171  b - color.b,
172  255U);
173 
174  result.r = (result.r > 255U ? 255U : result.r);
175  result.g = (result.g > 255U ? 255U : result.g);
176  result.b = (result.b > 255U ? 255U : result.b);
177 
178  return result;
179  }

References b, g, and r.

◆ operator==()

bool Color::operator== ( const Color color) const
inline

Compares two colors.

Returns
True if the two colors have the same RGBA components false otherwise.

Definition at line 210 of file color.h.

211  {
212  return r == color.r &&
213  g == color.g &&
214  b == color.b &&
215  a == color.a;
216  }

References a, b, g, and r.

Field Documentation

◆ a

unsigned int Color::a

◆ b

unsigned int Color::b

◆ g

unsigned int Color::g

◆ r

unsigned int Color::r

The documentation for this class was generated from the following file: