A class used to represent a color value.
This class stores colors as a glm::vec4 (e.g. as four floating-point values). Setters and accessors are provided to set/get color components both as floating point values (between 0.0f and 1.0f), and as byte values (between 0 and 255).
The application of a CYIColor is determined by the containing class as well as the shader program in effect.
For platforms that support C++/CX, the CYIColorCXUtilities class contains utilities for converting to and from WinRT color classes.
When dealing with colors packed into 32-bit integers, a byte ordering can be specified for the individual color components. By default, the ABGR ordering is used.
Pre-defined colors are available both as static objects and by name (through the FromString function).
#include <utility/YiColor.h>
Public Types | |
enum | ByteOrder { ByteOrder::RGBA = 0, ByteOrder::ARGB, ByteOrder::BGRA, ByteOrder::ABGR } |
Public Member Functions | |
CYIColor () | |
Default constructor. More... | |
CYIColor (const CYIColor &other) | |
Copy constructor. More... | |
CYIColor (CYIColor &&other) noexcept | |
Move constructor. More... | |
CYIColor (const CYIColor &color, float alpha) | |
Constructs a new color from color, while using the transparency value alpha. More... | |
CYIColor (double red, double green, double blue, double alpha=1.0) | |
Double-precision floating-point Constructor. More... | |
CYIColor (float red, float green, float blue, float alpha=1.0f) | |
Floating-point Constructor. More... | |
CYIColor (const glm::vec3 &rgb) | |
GLM Vector3D Constructor. More... | |
CYIColor (const glm::vec4 &rgba) | |
GLM Vector4D Constructor. More... | |
CYIColor (uint32_t packed, ByteOrder byteOrder=ByteOrder::ABGR) | |
Packed 4x8-bit Constructor. More... | |
CYIColor (CYIStringView name) | |
Named color constructor. More... | |
CYIColor & | operator= (const CYIColor &color) |
Copy assignment operator. More... | |
CYIColor & | operator= (CYIColor &&color) |
Move assignment operator. More... | |
CYIColor & | operator= (const glm::vec3 &rgb) |
GLM Vector3D assignment operator. More... | |
CYIColor & | operator= (const glm::vec4 &rgba) |
GLM Vector4D assignment operator. More... | |
void | SetColor (const CYIColor &color) |
Sets the color to the same color of another RGB instance. More... | |
void | SetColor (float red, float green, float blue, float alpha=1.0f) |
Sets the color to a given set floating-point values. More... | |
void | SetColorInteger (int32_t red, int32_t green, int32_t blue, int32_t alpha=255) |
Sets the color to a given set of signed integer values. More... | |
void | SetColor (const glm::vec3 &rgb) |
Sets the color to a given GLM Vector3D. More... | |
void | SetColor (const glm::vec4 &rgba) |
Sets the color to a given GLM Vector4D. More... | |
void | SetColor (uint32_t packed, ByteOrder byteOrder=ByteOrder::ABGR) |
Sets the color to a given packed 4x8-bit value. More... | |
void | SetRed (double red) |
void | SetRed (float red) |
void | SetRedInteger (int32_t red) |
void | SetGreen (double green) |
void | SetGreen (float green) |
void | SetGreenInteger (int32_t green) |
void | SetBlue (double blue) |
void | SetBlue (float blue) |
void | SetBlueInteger (int32_t blue) |
void | SetAlpha (double alpha) |
void | SetAlpha (float alpha) |
void | SetAlphaInteger (int32_t alpha) |
void | SetHSLHue (float hue) |
void | SetHSLHueInteger (int32_t hue) |
void | SetHSLSaturation (float saturation) |
void | SetHSLSaturationInteger (int32_t saturation) |
void | SetHSLLightness (float lightness) |
void | SetHSLLightnessInteger (int32_t lightness) |
float | GetRed () const |
float | GetGreen () const |
float | GetBlue () const |
float | GetAlpha () const |
float | GetHSLHue () const |
float | GetHSLSaturation () const |
float | GetHSLLightness () const |
bool | IsVisible () const |
uint8_t | GetRedByte () const |
uint8_t | GetGreenByte () const |
uint8_t | GetBlueByte () const |
uint8_t | GetAlphaByte () const |
uint32_t | GetPackedColor (ByteOrder byteOrder=ByteOrder::ABGR) const |
CYIColor | GetInverted () const |
CYIColor | GetPremultiplied () const |
CYIColor | GetClamped () const |
CYIString | ToHexString (ByteOrder byteOrder=ByteOrder::RGBA) const |
const glm::vec4 & | GetColorVector () const |
glm::vec4 | GetHSLAColorVector () const |
template<typename T = bool> | |
CYIColor (int32_t r, int32_t g, int32_t b, int32_t a=255) | |
template<typename T = bool> | |
CYIColor (uint8_t r, uint8_t g, uint8_t b, uint8_t a=255) | |
template<typename T = bool> | |
void | SetColor (int32_t r, int32_t g, int32_t b, int32_t a=255) |
template<typename T = bool> | |
void | SetRed (int32_t r) |
template<typename T = bool> | |
void | SetRed (uint8_t r) |
template<typename T = bool> | |
void | SetGreen (int32_t g) |
template<typename T = bool> | |
void | SetGreen (uint8_t g) |
template<typename T = bool> | |
void | SetBlue (int32_t b) |
template<typename T = bool> | |
void | SetBlue (uint8_t b) |
template<typename T = bool> | |
void | SetAlpha (int32_t a) |
template<typename T = bool> | |
void | SetAlpha (uint8_t a) |
Static Public Member Functions | |
static const CYINamedColors & | Named () |
static const CYINamedColors & | GetNamedColors () |
static CYIColor | GetColorByName (CYIStringView colorName, bool *pError=nullptr) |
static CYIColor | FromString (CYIStringView colorName, bool *pError=nullptr) |
static CYIColor | FromColor (const CYIColor &color, float alpha) |
static CYIColor | FromRGB (float red, float green, float blue, float alpha=1.0f) |
static CYIColor | FromRGBInteger (int32_t red, int32_t green, int32_t blue, int32_t alpha=255) |
static CYIColor | FromRGB (const glm::vec3 &rgbVector) |
static CYIColor | FromRGBA (const glm::vec4 &rgbaVector) |
static CYIColor | FromPackedRGBA (uint32_t packed, ByteOrder byteOrder=ByteOrder::ABGR) |
static CYIColor | FromHSL (float hue, float saturation, float luminance, float alpha=1.0f) |
static CYIColor | FromHSL (const glm::vec3 &hslVector) |
static CYIColor | FromHSLA (const glm::vec4 &hslaVector) |
static uint32_t | GetPackedColor (uint8_t red, uint32_t green, uint32_t blue, uint32_t alpha, ByteOrder byteOrder=ByteOrder::ABGR) |
Friends | |
bool | operator== (const CYIColor &lhs, const CYIColor &rhs) |
bool | operator!= (const CYIColor &lhs, const CYIColor &rhs) |
bool | operator< (const CYIColor &lhs, const CYIColor &rhs) |
bool | operator<= (const CYIColor &lhs, const CYIColor &rhs) |
bool | operator> (const CYIColor &lhs, const CYIColor &rhs) |
bool | operator>= (const CYIColor &lhs, const CYIColor &rhs) |
|
strong |
CYIColor::CYIColor | ( | ) |
Default constructor.
Initializes the color to Transparent.
CYIColor::CYIColor | ( | const CYIColor & | other | ) |
Copy constructor.
|
noexcept |
Move constructor.
CYIColor::CYIColor | ( | const CYIColor & | color, |
float | alpha | ||
) |
Constructs a new color from color, while using the transparency value alpha.
color | the color to copy. |
alpha | the opacity to use |
CYIColor::CYIColor | ( | double | red, |
double | green, | ||
double | blue, | ||
double | alpha = 1.0 |
||
) |
Double-precision floating-point Constructor.
red | the red component. Value should be between 0.0 to 1.0. |
green | the green component. Value should be between 0.0 to 1.0. |
blue | the blue component. Value should be between 0.0 to 1.0. |
alpha | the alpha component. Value should be between 0.0 to 1.0. |
CYIColor::CYIColor | ( | float | red, |
float | green, | ||
float | blue, | ||
float | alpha = 1.0f |
||
) |
Floating-point Constructor.
red | the red component. Value should be between 0.0f to 1.0f. |
green | the green component. Value should be between 0.0f to 1.0f. |
blue | the blue component. Value should be between 0.0f to 1.0f. |
alpha | the alpha component. Value should be between 0.0f to 1.0f. |
CYIColor::CYIColor | ( | const glm::vec3 & | rgb | ) |
GLM Vector3D Constructor.
rgb | the red, blue and green components. Values should be between glm::vec3(0.0f) and glm::vec3(1.0f). |
CYIColor::CYIColor | ( | const glm::vec4 & | rgba | ) |
GLM Vector4D Constructor.
rgba | the red, blue, green and alpha components. Values should be between glm::vec4(0.0f) and glm::vec4(1.0f). |
|
explicit |
Packed 4x8-bit Constructor.
packed | packed 4x8-bit values between the range of 0 and 255. |
byteOrder | defines the order in which bytes are read from the packed parameter. The default order is CYIColor::ByteOrder::ABGR. |
|
explicit |
CYIColor::CYIColor | ( | int32_t | r, |
int32_t | g, | ||
int32_t | b, | ||
int32_t | a = 255 |
||
) |
CYIColor::CYIColor | ( | uint8_t | r, |
uint8_t | g, | ||
uint8_t | b, | ||
uint8_t | a = 255 |
||
) |
Returns a new color created from color, while using the transparency value alpha.
|
static |
Creates a color object from HSLA (Hue/Saturation/Lightness/Alpha) color information. All values must be normalized between 0.0 and 1.0.
|
static |
Creates a color object from HSL (Hue/Saturation/Lightness) color information. All values must be normalized between 0.0 and 1.0.
The first component of the vector is expected to be the hue, the second component is expected to be the saturation, and the third component is expected to the lightness.
|
static |
Creates a color object from a glm::vec4 containing a HSLA (Hue/Saturation/Lightness/Alpha) color information.
The first component of the vector is expected to be the hue, the second component is expected to be the saturation, the third component is expected to the lightness, and the last component is expected to be the alpha. All values must be normalized between 0.0 and 1.0.
|
static |
Returns a new color created from the provided packed integer. A byte order can be provided – if absent, CYIColor::ByteOrder::ABGR is assumed.
|
static |
Returns a new color created from the provided red, green and blue components. A transparency value can optionally be provided. All values should be between 0.0 and 1.0.
|
static |
Returns a new color created from the provided red, green and blue components. A transparency value of 1.0 will be assumed. All values should be between 0.0 and 1.0.
|
static |
Returns a new color created from the provided red, green, blue and alpha components. All values should be between 0.0 and 1.0.
|
static |
Returns a new color created from the provided red, green and blue components. A transparency value can optionally be provided. All values should be between 0 and 255 inclusively.
|
static |
This function returns a color instance by name. CSS 3 named colors, hexadecimal values, rgb expressions and hsla expressions are supported. The following string types are supported:
lightsalmon
). Named colors, with the exception of transparent
, have an alpha value of 1.0. CSS 3 and W3C color names are supported.#0A2
or #00AA2B
). This assumes an alpha value of 1.0.#0A28
or #00AA2B80
).rgb(0, 255, 128)
). This assumes an alpha value of 1.0.rgba(0, 255, 128, 0.5)
).hsl(210, 50%, 25%)
).hsla(210, 50%, 25%, 0.5)
).CYIColor::FromString("Crimson")
and CYIColor::FromString("crimson")
will return same color.If pError is non-null, its pointed-to variable will be set to true if an error is encountered (and no warning will be logged).
float CYIColor::GetAlpha | ( | ) | const |
Returns the value of the alpha channel. The value should be between 0.0f and 1.0f
uint8_t CYIColor::GetAlphaByte | ( | ) | const |
Returns the value of the alpha channel. The value is clamped between 0 and 255
float CYIColor::GetBlue | ( | ) | const |
Returns the value of the blue channel. The value should be between 0.0f and 1.0f
uint8_t CYIColor::GetBlueByte | ( | ) | const |
Returns the value of the blue channel. The value is clamped between 0 and 255
CYIColor CYIColor::GetClamped | ( | ) | const |
Returns this color with all components clamped between 0.0f and 1.0f.
|
static |
Returns a color instance by name.
const glm::vec4& CYIColor::GetColorVector | ( | ) | const |
Returns a glm::vec4 containing the RGBA color data.
float CYIColor::GetGreen | ( | ) | const |
Returns the value of the green channel. The value should be between 0.0f and 1.0f
uint8_t CYIColor::GetGreenByte | ( | ) | const |
Returns the value of the green channel. The value is clamped between 0 and 255
glm::vec4 CYIColor::GetHSLAColorVector | ( | ) | const |
Returns a glm::vec4 containing the HSLA (Hue/Saturation/Lightness/Alpha) representation of the color data. All values are normalized between 0.0 and 1.0.
float CYIColor::GetHSLHue | ( | ) | const |
Returns the hue of this color (as represented in the HSL color space). The value will be normalized between 0.0f and 1.0f.
float CYIColor::GetHSLLightness | ( | ) | const |
Returns the lightness of this color (as represented in the HSL color space). The value will be normalized between 0.0f and 1.0f.
float CYIColor::GetHSLSaturation | ( | ) | const |
Returns the saturation of this color (as represented in the HSL color space). The value will be normalized between 0.0f and 1.0f.
CYIColor CYIColor::GetInverted | ( | ) | const |
Returns the inverse of this color. Only the red, green and blue channels are inverted – not the transparency.
|
static |
uint32_t CYIColor::GetPackedColor | ( | ByteOrder | byteOrder = ByteOrder::ABGR | ) | const |
Returns the packed 4x8-bit representation of this color. The order of the bytes in the returned integer will depend on the value of byteOrder. By default, an order of CYIColor::ByteOrder::ABGR is used.
|
inlinestatic |
This function 'packs' a color, represented as four byte components, into a single 32-bit word. The order of the bytes is specified through the byteOrder parameter. By default, the color is packed as 0xAABBGGRR.
CYIColor CYIColor::GetPremultiplied | ( | ) | const |
float CYIColor::GetRed | ( | ) | const |
Returns the value of the red channel. The value should be between 0.0f and 1.0f
uint8_t CYIColor::GetRedByte | ( | ) | const |
Returns the value of the red channel. The value is clamped between 0 and 255
bool CYIColor::IsVisible | ( | ) | const |
Returns true if this color has a non-0 alpha value.
|
static |
Returns a struct containing predefined named colors. Predefined named colors are accessed like this: const CYIColor &red = CYIColor::Named().Red;
Predefined named colors can safely be stored as references in static const variables without risking static initialization order issues.
Copy assignment operator.
color | the color to copy. |
Move assignment operator.
color | the color to move. |
CYIColor& CYIColor::operator= | ( | const glm::vec3 & | rgb | ) |
GLM Vector3D assignment operator.
rgb | the red, blue and green components. Values should be between glm::vec3(0.0f) and glm::vec3(1.0f). |
CYIColor& CYIColor::operator= | ( | const glm::vec4 & | rgba | ) |
GLM Vector4D assignment operator.
rgba | the red, blue, green and alpha components. Values should be between glm::vec4(0.0f) and glm::vec4(1.0f). |
void CYIColor::SetAlpha | ( | double | alpha | ) |
Sets the alpha color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetAlpha | ( | float | alpha | ) |
Sets the red color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetAlpha | ( | int32_t | a | ) |
void CYIColor::SetAlpha | ( | uint8_t | a | ) |
void CYIColor::SetAlphaInteger | ( | int32_t | alpha | ) |
Sets the alpha color channel. Value should be between 0 and 255.
void CYIColor::SetBlue | ( | double | blue | ) |
Sets the blue color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetBlue | ( | float | blue | ) |
Sets the blue color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetBlue | ( | int32_t | b | ) |
void CYIColor::SetBlue | ( | uint8_t | b | ) |
void CYIColor::SetBlueInteger | ( | int32_t | blue | ) |
Sets the blue color channel. Value should be between 0 and 255.
void CYIColor::SetColor | ( | const CYIColor & | color | ) |
Sets the color to the same color of another RGB instance.
color | the color to copy. |
void CYIColor::SetColor | ( | float | red, |
float | green, | ||
float | blue, | ||
float | alpha = 1.0f |
||
) |
Sets the color to a given set floating-point values.
red | the red component. Value should be between 0.0f to 1.0f. |
green | the green component. Value should be between 0.0f to 1.0f. |
blue | the blue component. Value should be between 0.0f to 1.0f. |
alpha | the alpha component. Value should be between 0.0f to 1.0f. |
void CYIColor::SetColor | ( | const glm::vec3 & | rgb | ) |
Sets the color to a given GLM Vector3D.
rgb | the red, blue and green components. Values should be between glm::vec3(0.0f) and glm::vec3(1.0f). |
void CYIColor::SetColor | ( | const glm::vec4 & | rgba | ) |
Sets the color to a given GLM Vector4D.
rgba | the red, blue, green and alpha components. Values should be between glm::vec4(0.0f) and glm::vec4(1.0f). |
void CYIColor::SetColor | ( | uint32_t | packed, |
ByteOrder | byteOrder = ByteOrder::ABGR |
||
) |
Sets the color to a given packed 4x8-bit value.
packed | packed 4x8-bit values between the range of 0 and 255. |
byteOrder | defines the order in which bytes are read from the packed parameter. The default order is CYIColor::ByteOrder::ABGR. |
void CYIColor::SetColor | ( | int32_t | r, |
int32_t | g, | ||
int32_t | b, | ||
int32_t | a = 255 |
||
) |
void CYIColor::SetColorInteger | ( | int32_t | red, |
int32_t | green, | ||
int32_t | blue, | ||
int32_t | alpha = 255 |
||
) |
Sets the color to a given set of signed integer values.
red | the red component. Value should be between 0 to 255. |
green | the green component. Value should be between 0 to 255. |
blue | the blue component. Value should be between 0 to 255. |
alpha | the alpha component. Value should be between 0 to 255. |
void CYIColor::SetGreen | ( | double | green | ) |
Sets the green color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetGreen | ( | float | green | ) |
Sets the green color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetGreen | ( | int32_t | g | ) |
void CYIColor::SetGreen | ( | uint8_t | g | ) |
void CYIColor::SetGreenInteger | ( | int32_t | green | ) |
Sets the green color channel. Value should be between 0 and 255.
void CYIColor::SetHSLHue | ( | float | hue | ) |
Sets the hue of this color to hue. The value should be between 0.0f and 1.0f.
void CYIColor::SetHSLHueInteger | ( | int32_t | hue | ) |
Sets the hue of this color to hue. Value should be between 0 and 255.
void CYIColor::SetHSLLightness | ( | float | lightness | ) |
Sets the lightness of this color to l. The value should be between 0.0f and 1.0f.
void CYIColor::SetHSLLightnessInteger | ( | int32_t | lightness | ) |
Sets the lightness of this color to lightness. Value should be between 0 and 255.
void CYIColor::SetHSLSaturation | ( | float | saturation | ) |
Sets the saturation of this color to saturation. The value should be between 0.0f and 1.0f.
void CYIColor::SetHSLSaturationInteger | ( | int32_t | saturation | ) |
Sets the saturation of this color to saturation. Value should be between 0 and 255.
void CYIColor::SetRed | ( | double | red | ) |
Sets the red color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetRed | ( | float | red | ) |
Sets the red color channel. Value should be between 0.0f and 1.0f
void CYIColor::SetRed | ( | int32_t | r | ) |
void CYIColor::SetRed | ( | uint8_t | r | ) |
void CYIColor::SetRedInteger | ( | int32_t | red | ) |
Sets the red color channel. Value should be between 0 and 255.
CYIString CYIColor::ToHexString | ( | ByteOrder | byteOrder = ByteOrder::RGBA | ) | const |
Returns a string representation of this color, formatted as "#RRGGBBAA". The byteOrder parameter can be used to request a different byte order.