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=255)
 
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 74 of file color.h.

Constructor & Destructor Documentation

◆ Color() [1/3]

Color::Color ( )
inline

Constructor. Initializes the color to black.

Definition at line 80 of file color.h.

80  :
81  r(0U),
82  g(0U),
83  b(0U),
84  a(255U)
85  {
86  }
unsigned int a
Definition: color.h:250
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
unsigned int r
Definition: color.h:234

◆ 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 101 of file color.h.

101  :
102  r((color >> 16) & 0xFFU),
103  g((color >> 8) & 0xFFU),
104  b(color & 0xFFU),
105  a(255U)
106  {
107  }
unsigned int a
Definition: color.h:250
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
unsigned int r
Definition: color.h:234

◆ Color() [3/3]

Color::Color ( const unsigned int  ar,
const unsigned int  ag,
const unsigned int  ab,
const unsigned int  aa = 255 
)
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 118 of file color.h.

References A_DEFAULT_COPY.

121  :
122  r(ar),
123  g(ag),
124  b(ab),
125  a(aa)
126  {
127  }
unsigned int a
Definition: color.h:250
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
unsigned int r
Definition: color.h:234

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 223 of file color.h.

References a, b, g, and r.

224  {
225  return !(r == color.r &&
226  g == color.g &&
227  b == color.b &&
228  a == color.a);
229  }
unsigned int a
Definition: color.h:250
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
unsigned int r
Definition: color.h:234

◆ 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 189 of file color.h.

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

190  {
191  Color result(CAST_U32(static_cast<float>(r) * value),
192  CAST_U32(static_cast<float>(g) * value),
193  CAST_U32(static_cast<float>(b) * value),
194  a);
195 
196  result.r = (result.r > 255U ? 255U : result.r);
197  result.g = (result.g > 255U ? 255U : result.g);
198  result.b = (result.b > 255U ? 255U : result.b);
199 
200  return result;
201  }
#define CAST_U32
Definition: cast.h:30
unsigned int a
Definition: color.h:250
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
Definition: color.h:74
unsigned int r
Definition: color.h:234

◆ 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 142 of file color.h.

References b, g, and r.

143  {
144  Color result(r + color.r,
145  g + color.g,
146  b + color.b,
147  255U);
148 
149  result.r = (result.r > 255U ? 255U : result.r);
150  result.g = (result.g > 255U ? 255U : result.g);
151  result.b = (result.b > 255U ? 255U : result.b);
152 
153  return result;
154  }
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
Definition: color.h:74
unsigned int r
Definition: color.h:234

◆ 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 166 of file color.h.

References b, g, and r.

167  {
168  Color result(r - color.r,
169  g - color.g,
170  b - color.b,
171  255U);
172 
173  result.r = (result.r > 255U ? 255U : result.r);
174  result.g = (result.g > 255U ? 255U : result.g);
175  result.b = (result.b > 255U ? 255U : result.b);
176 
177  return result;
178  }
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
Definition: color.h:74
unsigned int r
Definition: color.h:234

◆ 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 209 of file color.h.

References a, b, g, and r.

210  {
211  return r == color.r &&
212  g == color.g &&
213  b == color.b &&
214  a == color.a;
215  }
unsigned int a
Definition: color.h:250
unsigned int g
Definition: color.h:239
unsigned int b
Definition: color.h:244
unsigned int r
Definition: color.h:234

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: