FifeGUI 0.2.0
A C++ GUI library designed for games.
fcn::Color Class Reference

#include <color.hpp>

Public Member Functions

Color blendWith (Color const &other) const
 Color ()=default
 Color (int hexColor)
 Color (int r, int g, int b, int a)
 Color (std::string const &colorString)
 Color (uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Color darken (float percentage) const
Color lighten (float percentage) const
bool operator!= (Color const &color) const
Color operator* (float value) const
Coloroperator*= (float value)
Color operator+ (Color const &color) const
Color operator+ (float value) const
Coloroperator+= (Color const &color)
Color operator- (Color const &color) const
Color operator- (float value) const
Coloroperator-= (Color const &color)
bool operator== (Color const &color) const
Color toGrayScale () const
std::string toHexString () const
std::string toRGBAString () const
std::string toRGBString () const

Public Attributes

uint8_t a {255}
uint8_t b {0}
uint8_t g {0}
uint8_t r {0}

Friends

std::ostream & operator<< (std::ostream &out, Color const &color)

Detailed Description

Color.

Represents a color with red, green, blue and alpha components. The color components are in the range 0-255.

The alpha component is used for transparency. A value of 0 means fully transparent, 255 is fully opaque. The alpha component is 255 by default.

The color can be constructed from a hexadecimal color code or an RGB color code. The hexadecimal color code must be 6 characters long and prefixed with a '#'. The RGB color code must be in the format "rgb(r,g,b)".

The color can be lightened or darkened by a percentage. The percentage should be in the range 0-1. A percentage of 0.1 will return a color that is 10% lighter. A percentage of -0.1 will return a color that is 10% darker.

The color can be converted to grayscale. The color can be blended with another color using alpha blending.

The color can be output as a hexadecimal color code or an RGB color code. The color can be compared with another color. The color can be added, subtracted, multiplied, and divided with another color object. The color can be added, subtracted, multiplied, and divided with a float value. The color can be output to a stream. The color can be converted to a string. The color can be converted to a string in hexadecimal format. The color can be converted to a string in RGB format.

Definition at line 55 of file color.hpp.

Constructor & Destructor Documentation

◆ Color() [1/5]

◆ Color() [2/5]

fcn::Color::Color ( int hexColor)
explicit

Constructor.

Constructs a color from the bytes in an integer. The integer should be in the format 0xRRGGBB. The alpha component is 255 by default. *

Parameters
hexColorThe color to initialise the object with (format 0xRRGGBB).

Definition at line 14 of file color.cpp.

References b, g, and r.

◆ Color() [3/5]

fcn::Color::Color ( std::string const & colorString)
explicit

Constructor.

Constructs a color from a string. The string can be a hexadecimal color code or an RGB color code. The hexadecimal color code must be 6 characters long and prefixed with a '#'. The RGB color code must be in the format "rgb(r,g,b)". The alpha component is 255 by default.

Parameters
colorStringThe color to initialise the object with.

Definition at line 29 of file color.cpp.

◆ Color() [4/5]

fcn::Color::Color ( int r,
int g,
int b,
int a )

Constructor.

Constructs a color from the red, green, blue and alpha components. The default alpha value is 255.

Parameters
rRed color component (range 0-255).
gGreen color component (range 0-255).
bBlue color component (range 0-255).
aAlpha, used for transparency. A value of 0 means fully transparent, 255 is fully opaque.

Definition at line 17 of file color.cpp.

References a, b, g, and r.

◆ Color() [5/5]

fcn::Color::Color ( uint8_t r,
uint8_t g,
uint8_t b,
uint8_t a = 255 )

Constructor.

Constructs a color from the red, green, blue and alpha components. The default alpha value is 255.

Parameters
rRed color component (range 0-255).
gGreen color component (range 0-255).
bBlue color component (range 0-255).
aAlpha, used for transparency. A value of 0 means fully transparent, 255 is fully opaque.

Definition at line 26 of file color.cpp.

References a, b, g, and r.

Member Function Documentation

◆ blendWith()

Color fcn::Color::blendWith ( Color const & other) const

Blends the color with another color using alpha blending.

The alpha value changes. The resulting color's alpha will be a combination of both colors' alpha values.

Parameters
otherThe color to blend with.
Returns
The blended color.

Definition at line 265 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ darken()

Color fcn::Color::darken ( float percentage) const

Darkens the color by a percentage.

The alpha value will be kept as it is. The color will be clamped if it goes out of range. The percentage should be in the range 0-1. A percentage of 0 will return the same color. A percentage of 1 will return black. A percentage of -1 will return white. A percentage of 0.1 will return a color that is 10% darker. A percentage of -0.1 will return a color that is 10% lighter.

Parameters
percentageThe percentage to darken the color by.
Returns
A new darkened color object.

Definition at line 248 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ lighten()

Color fcn::Color::lighten ( float percentage) const

Lightens the color by a percentage.

The alpha value will be kept as it is. The color will be clamped if it goes out of range. The percentage should be in the range 0-1. A percentage of 0 will return the same color. A percentage of 1 will return white. A percentage of -1 will return black. A percentage of 0.1 will return a color that is 10% lighter. A percentage of -0.1 will return a color that is 10% darker.

Parameters
percentageThe percentage to lighten the color by.
Returns
A new lightened color object.

Definition at line 238 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ operator!=()

bool fcn::Color::operator!= ( Color const & color) const

Compares two colors.

Parameters
colorThe color to compare with.
Returns
True, if both colors have the same RGBA components, false otherwise.

Definition at line 301 of file color.cpp.

References Color().

Referenced by Color().

◆ operator*()

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

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 200 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ operator*=()

Color & fcn::Color::operator*= ( float value)

Multiplies the RGB values of this color with a float value.

The values will be clamped if they go out of range. The alpha value will be kept as it is.

Parameters
valueThe value to multiply the color with.
Returns
The modified object (the current instance).

Definition at line 228 of file color.cpp.

References b, Color(), g, and r.

Referenced by Color().

◆ operator+() [1/2]

Color fcn::Color::operator+ ( Color const & color) const

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 161 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ operator+() [2/2]

Color fcn::Color::operator+ ( float value) const

Adds a float value to the RGB values of a color.

The values will be clamped if they go out of range. The alpha value will be kept as it is.

Parameters
valueThe value to add to the color.
Returns
The added colors with an alpha value set to 255.

Definition at line 181 of file color.cpp.

References a, b, Color(), g, and r.

◆ operator+=()

Color & fcn::Color::operator+= ( Color const & color)

Adds the RGB values of another color to this color.

The values will be clamped if they go out of range. The alpha value will be kept as it is.

Parameters
colorThe color to add to this color.
Returns
The modified object (the current instance).

Definition at line 210 of file color.cpp.

References b, Color(), g, and r.

Referenced by Color().

◆ operator-() [1/2]

Color fcn::Color::operator- ( Color const & color) const

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 171 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ operator-() [2/2]

Color fcn::Color::operator- ( float value) const

Subtracts a float value from the RGB values of a color.

The values will be clamped if they go out of range. The alpha value will be kept as it is.

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

Definition at line 190 of file color.cpp.

References a, b, Color(), g, and r.

◆ operator-=()

Color & fcn::Color::operator-= ( Color const & color)

Subtracts the RGB values of another color from this color.

The values will be clamped if they go out of range. The alpha value will be kept as it is.

Parameters
colorThe color to subtract from this color.
Returns
The modified object (the current instance).

Definition at line 219 of file color.cpp.

References b, Color(), g, and r.

Referenced by Color().

◆ operator==()

bool fcn::Color::operator== ( Color const & color) const

Compares two colors.

Parameters
colorThe color to compare with.
Returns
True, if both colors have the same RGBA components, false otherwise.

Definition at line 296 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ toGrayScale()

Color fcn::Color::toGrayScale ( ) const

Converts the color to grayscale.

The alpha value will be kept as it is.

Returns
The grayscale color.

Definition at line 258 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by Color().

◆ toHexString()

std::string fcn::Color::toHexString ( ) const

Returns the color as a hexadecimal string in the form "#RRGGBB".

Returns
Hex string representation of the color (alpha not included).

Definition at line 313 of file color.cpp.

References b, g, and r.

Referenced by Color().

◆ toRGBAString()

std::string fcn::Color::toRGBAString ( ) const

Returns the color in the form "rgba(r,g,b,a)".

Returns
RGBA-style string representation of the color including alpha.

Definition at line 328 of file color.cpp.

References a, b, g, and r.

Referenced by Color().

◆ toRGBString()

std::string fcn::Color::toRGBString ( ) const

Returns the color in the form "rgb(r,g,b)".

Returns
RGB-style string representation of the color.

Definition at line 321 of file color.cpp.

References b, g, and r.

Referenced by Color().

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
Color const & color )
friend

Output operator for output.

Parameters
outThe stream to output to.
colorThe color to output.

Definition at line 306 of file color.cpp.

References a, b, Color(), g, operator<<, and r.

Referenced by Color(), and operator<<.

Member Data Documentation

◆ a

◆ b

◆ g

◆ r


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