CUGL 2.3
Cornell University Game Library
|
#include <CUColor4.h>
Public Member Functions | |
Color4f () | |
Color4f (float r, float g, float b, float a=1) | |
Color4f (Uint32 color) | |
Color4f (const std::string color) | |
Color4f (const float *array) | |
~Color4f () | |
Color4f & | operator= (unsigned int color) |
Color4f & | operator= (const float *array) |
Color4f & | operator= (const std::string name) |
Color4f & | set (float r, float g, float b, float a=1) |
Color4f & | set (const float *array) |
Color4f & | set (Uint32 color) |
Color4f & | set (const Color4f c) |
Color4f & | set (const std::string name) |
Color4f & | setHSV (float h, float s, float v, float a=1) |
Color4f & | setHSL (float h, float s, float l, float a=1) |
Color4f & | clamp (const Color4f min, const Color4f max) |
Color4f | getClamp (const Color4f min, const Color4f max) const |
Color4f & | add (const Color4f c, bool alpha=false) |
Color4f & | add (float r, float g, float b, float a=0) |
Color4f & | subtract (const Color4f c, bool alpha=false) |
Color4f & | subtract (float r, float g, float b, float a=0) |
Color4f & | scale (float s, bool alpha=false) |
Color4f & | scale (float sr, float sg, float sb, float sa=1) |
Color4f & | scale (const Color4f c, bool alpha=false) |
Color4f & | map (std::function< float(float)> func, bool alpha=false) |
Color4f | getMap (std::function< float(float)> func, bool alpha=false) const |
Color4f & | complement (bool alpha=false) |
Color4f | getComplement (bool alpha=false) const |
Color4f & | lerp (const Color4f other, float alpha) |
Color4f & | blend (const Color4f other) |
Color4f & | blendPre (const Color4f other) |
Color4f & | premultiply () |
Color4f & | unpremultiply () |
Color4f | getLerp (const Color4f other, float alpha) const |
Color4f | getBlend (const Color4f other) const |
Color4f | getBlendPre (const Color4f other) const |
Color4f | getPremultiplied () const |
Color4f | getUnpremultiplied () const |
Color4f & | operator+= (const Color4f c) |
Color4f & | operator-= (const Color4f c) |
Color4f & | operator*= (float s) |
Color4f & | operator*= (const Color4f c) |
const Color4f | operator+ (const Color4f c) const |
const Color4f | operator- (const Color4f c) const |
const Color4f | operator* (float s) const |
const Color4f | operator* (const Color4f c) const |
bool | operator< (const Color4f c) const |
bool | operator<= (const Color4f c) const |
bool | operator> (const Color4f c) const |
bool | operator>= (const Color4f c) const |
bool | operator== (const Color4f c) const |
bool | operator!= (const Color4f c) const |
bool | darkerThan (const Color4f c) const |
bool | lighterThan (const Color4f c) const |
bool | equals (const Color4f color, float variance=CU_MATH_EPSILON) const |
std::string | toString (bool verbose=false) const |
operator std::string () const | |
operator Vec4 () const | |
Color4f (const Vec4 vector) | |
Color4f & | operator= (const Vec4 vector) |
operator Vec3 () const | |
Color4f (const Vec3 vector) | |
Color4f & | operator= (const Vec3 vector) |
operator Color4 () const | |
Color4f (Color4 color) | |
Color4f & | operator= (Color4 color) |
float * | get (float *array) const |
Uint32 | getRGBA () const |
Uint32 | getPacked () const |
Static Public Member Functions | |
static Color4f * | lerp (const Color4f c1, const Color4f c2, float alpha, Color4f *dst) |
static Color4f * | blend (const Color4f c1, const Color4f c2, Color4f *dst) |
static Color4f * | blendPre (const Color4f c1, const Color4f c2, Color4f *dst) |
Public Attributes | |
float | r |
float | g |
float | b |
float | a |
Static Public Attributes | |
static const Color4f | CLEAR |
static const Color4f | WHITE |
static const Color4f | BLACK |
static const Color4f | YELLOW |
static const Color4f | BLUE |
static const Color4f | GREEN |
static const Color4f | RED |
static const Color4f | MAGENTA |
static const Color4f | CYAN |
static const Color4f | ORANGE |
static const Color4f | GRAY |
static const Color4f | CORNFLOWER |
static const Color4f | PAPYRUS |
This class is a RGBA color composed of 4 floats.
This is the preferred transparent color class for when you need to do a lot of calculations. However, it is not a compact storage format and is not ideal for shipping to the GPU. Used Color4
for that instead.
This class is in standard layout with fields of uniform type. This means that it is safe to reinterpret_cast objects to float arrays and Vec4. In addition, it has cast support for the latter. However, this class contains arithmetic methods to keep these casts from being necessary. In addition, all of the arithmetic operations implicitly clamp to maintain the invariants on all of the color values.
|
inline |
Constructs a new clear color (all zeros).
cugl::Color4f::Color4f | ( | float | r, |
float | g, | ||
float | b, | ||
float | a = 1 |
||
) |
Constructs a new color initialized to the specified values.
The color values must all be in the range 0..1.
r | The red color. |
g | The green color. |
b | The blue color. |
a | The alpha value (optional). |
cugl::Color4f::Color4f | ( | Uint32 | color | ) |
Creates a new color from an integer interpreted as an RGBA value.
This constructor processes the integer in RGBA order. Hence, 0xff0000ff represents red or the color (1, 0, 0, 1). Because this representation is endian specific, it is converted to the native endianness.
color | The integer to interpret as an RGBA value. |
cugl::Color4f::Color4f | ( | const std::string | color | ) |
Creates a new color from an string represenation
A string representation may either be an explicit name or a (modified) HTML code. The supported color names are the classic TCL/TK colors, which are listed here:
https://www.tcl.tk/man/tcl8.6/TkCmd/colors.htm
An HTML code is a string that starts with #, followed by the characters 0-9, A-F. This is the hex representation of the color, as specified here:
We support 3, 4, 6, and 8 character codes (not including the #). The standard code is 6 characters, but that does not specify the alpha value.
If you specify eight characters, the last two characters specify the byte values for alpha, following the same rules for red, green, blue.
Finally, the 3 and 4 character versions are the abbreviated 6 and 8 character versions, respectively. In these versions, the byte values are repeated. So #fa2
becomes #ffaa22
and #fad3
becomes #ffaadd33
.
color | The integer to interpret as an RGBA value. |
cugl::Color4f::Color4f | ( | const float * | array | ) |
Constructs a new color from the values in the specified array.
The color values must all be in the range 0..1.
array | An array containing the color values in the order r, g, b, a. |
|
inline |
Destroys this color, releasing all resources
|
explicit |
Creates a color from the given vector.
The attributes are read in the order x,y,z,w.
vector | The vector to convert |
|
explicit |
Creates a color from the given vector.
The attributes are read in the order x,y,z. The alpha value is 1.
vector | The vector to convert |
|
explicit |
Creates a float-based color from the given byte-based color.
The attributes are read in the order r,g,b,a. They are all divided by 255.0f before assignment.
color | The color to convert |
Adds the given color to this one in place.
This operation is functionally identical to additive blending. The addition is clamped so that this remains a valid color.
c | The color to add |
alpha | Whether to add the alpha values (optional) |
|
inline |
Adds the given values to this color.
The addition is clamped so that this remains a valid color.
r | The red color to add. |
g | The green color to add. |
b | The blue color to add. |
a | The alpha value (optional). |
Blends the two colors c1 and c2, assuming they are not premultiplied.
The blending is the standard over operation with color c1 as the source and c2 as the destination. It assumes that the color values are not premultiplied.
c1 | The source color. |
c2 | The destination color. |
dst | The color to store the result in |
Blends this color with the other one, storing the new value in place.
The blending is the standard over operation with this color as the destination. It assumes that the color values are not premultiplied.
other | The color to interpolate with. |
|
static |
Blends the two colors c1 and c2, assuming they are premultiplied.
The blending is the standard over operation with color c1 as the source and c2 as the destination. It assumes that the color values are premultiplied.
c1 | The source color. |
c2 | The destination color. |
dst | The color to store the result in |
Blends this color with the other one, storing the new value in place.
The blending is the standard over operation with this color as the destination. It assumes that this color is premultiplied.
other | The color to interpolate with. |
Clamps this color within the given range.
min | The minimum value. |
max | The maximum value. |
|
inline |
Complements this color.
The complement of the color is the (1-v) for each value v.
alpha | Whether to complement the alpha value (optional) |
|
inline |
Returns true if this color is dominated by the given color.
Domination means that all components of the given color are greater than or equal to the components of this one. However, alpha is reversed for this computation, as a greater value means more opaque (and hence darker).
c | The color to compare against. |
|
inline |
Returns true if the color are within tolerance of each other.
The tolerance bounds each color attribute separately.
color | The color to compare against. |
variance | The comparison tolerance. |
float * cugl::Color4f::get | ( | float * | array | ) | const |
Writes the color values into a float array.
The attributes are read into the array in the order r,g,b,a. All values are between 0 and 1.
array | The array to store the color values. |
Returns a blend of this color with the other one.
The blending is the standard over operation with this color as the destination. It assumes that the color values are not premultiplied.
Note: this does not modify this color.
other | The color to interpolate with. |
Returns a blend of this color with the other one.
The blending is the standard over operation with this color as the destination. It assumes that this color is premultiplied.
Note: this does not modify this color.
other | The color to interpolate with. |
Returns a copy of this color clamped within the given range.
Note: this does not modify this color.
min | The minimum value. |
max | The maximum value. |
|
inline |
Returns the complement of this color.
The complement of the color is the (1-v) for each value v.
alpha | Whether to complement the alpha value (optional) |
Note: this does not modify this color.
Returns the linear interpolation of this color with other.
If alpha is 0, the color is unchanged. If alpha is 1, the color is other. Otherwise it is a value in between. If alpha is outside of the range 0 to 1, it is clamped to the nearest value.
This method just implements standard linear interpolation. It does not attempt to give any blending semantics to it.
Note: this does not modify this color.
other | The color to interpolate with. |
alpha | The interpolation value in 0..1 |
|
inline |
Returns a copy of this color with func applied to each component.
This method supports any function that has the signature float func(float); This includes many of the functions in math.h.
func | The function to map on the color values. |
alpha | Whether to modify the alpha value (optional) |
Uint32 cugl::Color4f::getPacked | ( | ) | const |
Returns the packed integer representation of this color
This representation returned is native to the platform. In other words, it is guaranteed to be the same value as if you were to access the rgba attribute directly. That makes this value the correct one to send to OpenGL (which always uses client order).
However, this value is not necessarily the same value that you assigned the constructor, because of endianness issues. To get that value, you should call getRGBA
instead.
|
inline |
Returns the premultiplied version of this color, using its current alpha.
This class does not store whether the color is already premultiplied. Hence premultiplying an already premultiplied color will have a compounding effect.
Note: this does not modify this color.
Uint32 cugl::Color4f::getRGBA | ( | ) | const |
Returns the canonical integer representation of this color
This method returns the color as an integer in network order, which matches the way web colors are specified. In this order, red will always be the highest order byte and alpha will always be the lowest order byte. Hence 0x00ff00ff represents green or the color (0, 1, 0, 1).
The value returned by this method is guaranteed to be the same value that you assigned the constructor, or via setter. However, it is not guaranteed to be a value recognizable by OpenGL. For that value you will need to call getPacked
instead.
|
inline |
Returns the unpremultiplied version of this color, using its current alpha.
This class does not store whether the color is already premultiplied. Hence unpremultiplying an non-premultiplied color will have a compounding effect.
If the alpha value is 0, the copy is equal to the original color.
Note: this does not modify this color.
|
static |
Interpolates the two colors c1 and c2, and stores the result in dst.
If alpha is 0, the result is c1. If alpha is 1, the color is c2. Otherwise it is a value in c1..c2. If alpha is outside of the range 0 to 1, it is clamped to the nearest value.
This method just implements standard linear interpolation. It does not attempt to give any blending semantics to it.
c1 | The first color. |
c2 | The second color. |
alpha | The interpolation value in 0..1 |
dst | The color to store the result in |
Modifies this color to be the linear interpolation with other.
If alpha is 0, the color is unchanged. If alpha is 1, the color is other. Otherwise it is a value in between. If alpha is outside of the range 0 to 1, it is clamped to the nearest value.
This method just implements standard linear interpolation. It does not attempt to give any blending semantics to it.
other | The color to interpolate with. |
alpha | The interpolation value in 0..1 |
|
inline |
Returns true if this color dominates the given color.
Domination means that all components of this color are greater than or equal to the components of the given color. However, alpha is reversed for this computation, as a lesser value means more transparent (and hence lighter).
c | The color to compare against. |
|
inline |
Maps the given function to the color values in place.
This method supports any function that has the signature float func(float); This includes many of the functions in math.h. However, the values are clamped to ensure that this remains a valid color.
func | The function to map on the color values. |
alpha | Whether to modify the alpha value (optional) |
|
inline |
Cast from Color4f to a string.
|
inline |
Returns true if this color is not equal to the given color.
Comparison is exact, which may be unreliable given that the attributes are floats.
c | The color to compare against. |
Returns the scalar product of this color with the given color.
This operation is functionally identical to multiplicative blending.
Note: this does not modify this color.
c | The color to scale by. |
|
inline |
Returns the scalar product of this color with the given color.
The scaling is clamped so that this remains a valid color.
Note: this does not modify this color.
s | The value to scale by. |
Scales this color nonuniformly by the given color.
This operation is functionally identical to multiplicative blending.
c | The color to scale by |
|
inline |
Scales this color in place by the given factor.
The scaling is clamped so that this remains a valid color.
This version of scaling always multiplies the alpha values.
s | The value to scale by |
Returns the sum of this color with the given color.
This operation is functionally identical to additive blending. The addition is clamped so that this remains a valid color.
This version of addition always adds alpha values.
Note: this does not modify this color.
c | The color to add. |
Adds the given color to this one in place.
This operation is functionally identical to additive blending. The addition is clamped so that this remains a valid color.
This version of addition always adds alpha values.
c | The color to add |
Returns the difference of this color with the given color.
This operation is functionally identical to subtractive blending. The subtraction is clamped so that this remains a valid color.
This version of subtraction always adds subtracts values.
Note: this does not modify this color.
c | The color to subtract. |
Subtracts the given color from this one in place.
This operation is functionally identical to subtractive blending. The subtraction is clamped so that this remains a valid color.
This version of subtraction always subtracts alpha values.
c | The color to subtract |
bool cugl::Color4f::operator< | ( | const Color4f | c | ) | const |
Returns true if this color is less than the given color.
This comparison uses lexicographical order of rgba. To test if all components in this color are less than those of c, use the method darkerThan().
c | The color to compare against. |
|
inline |
Returns true if this color is less than or equal the given color.
This comparison uses lexicographical order of rgba. To test if all components in this color are less than those of c, use the method darkerThan().
c | The color to compare against. |
Sets the attributes of this color from the given byte-based color.
The attributes are read in the order r,g,b,a. They are all divided by 255.0f before assignment.
color | The color to convert. |
|
inline |
Sets the elements of this color from the values in the specified array.
The color values must all be in the range 0..1.
array | An array containing the color values in the order r, g, b, a. |
|
inline |
Sets the elements of this color to match the string represenation
A string representation may either be an explicit name or a (modified) HTML code. The supported color names are the classic TCL/TK colors, which are listed here:
https://www.tcl.tk/man/tcl8.6/TkCmd/colors.htm
An HTML code is a string that starts with #, followed by the characters 0-9, A-F. This is the hex representation of the color, as specified here:
We support 3, 4, 6, and 8 character codes (not including the #). The standard code is 6 characters, but that does not specify the alpha value.
If you specify eight characters, the last two characters specify the byte values for alpha, following the same rules for red, green, blue.
Finally, the 3 and 4 character versions are the abbreviated 6 and 8 character versions, respectively. In these versions, the byte values are repeated. So #fa2
becomes #ffaa22
and #fad3
becomes #ffaadd33
.
name | The string name |
Sets the coordinates of this color to those of the given vector.
The attributes are read in the order x,y,z. The alpha value is 1.
vector | The vector to convert |
Sets the coordinates of this color to those of the given vector.
The attributes are read in the order x,y,z,w.
vector | The vector to convert |
|
inline |
Sets this color to an integer interpreted as an RGBA value.
This setter processes the integer in RGBA order. Hence, 0xff0000ff represents red or the color (1, 0, 0, 1).
color | The integer to interpret as an RGBA value. |
|
inline |
Returns true if this color is equal to the given color.
Comparison is exact, which may be unreliable given that the attributes are floats.
c | The color to compare against. |
bool cugl::Color4f::operator> | ( | const Color4f | c | ) | const |
Returns true if this color is greater than the given color.
This comparison uses lexicographical order of rgba. To test if all components in this color are greater than those of c, use the method lighterThan().
c | The color to compare against. |
|
inline |
Returns true if this color is greater than or equal the given color.
This comparison uses lexicographical order of rgba. To test if all components in this color are greater than those of c, use the method lighterThan().
c | The color to compare against. |
|
inline |
Premultiplies this color with its current alpha.
This class does not store whether the color is already premultiplied. Hence premultiplying an already premultiplied color will have a compounding effect.
Scales this color nonuniformly by the given color.
This operation is functionally identical to multiplicative blending.
c | The color to scale by |
alpha | Whether to scale the alpha value (optional) |
|
inline |
Scales this color in place by the given factor.
The scaling is clamped so that this remains a valid color.
s | The scalar to multiply by |
alpha | Whether to scale the alpha value (optional) |
|
inline |
Scales this color nonuniformly by the given factors.
The scaling is clamped so that this remains a valid color.
sr | The scalar to multiply the red value |
sg | The scalar to multiply the green value |
sb | The scalar to multiply the blue value |
sa | The scalar to multiply alpha value (optional). |
Sets the values of this color to those in the specified color.
c | The color to copy. |
Color4f & cugl::Color4f::set | ( | const float * | array | ) |
Sets the elements of this color from the values in the specified array.
The color values must all be in the range 0..1.
array | An array containing the color values in the order r, g, b, a. |
Color4f & cugl::Color4f::set | ( | const std::string | name | ) |
Sets the elements of this color to match the string represenation
A string representation may either be an explicit name or a (modified) HTML code. The supported color names are the classic TCL/TK colors, which are listed here:
https://www.tcl.tk/man/tcl8.6/TkCmd/colors.htm
An HTML code is a string that starts with #, followed by the characters 0-9, A-F. This is the hex representation of the color, as specified here:
We support 3, 4, 6, and 8 character codes (not including the #). The standard code is 6 characters, but that does not specify the alpha value.
If you specify eight characters, the last two characters specify the byte values for alpha, following the same rules for red, green, blue.
Finally, the 3 and 4 character versions are the abbreviated 6 and 8 character versions, respectively. In these versions, the byte values are repeated. So #fa2
becomes #ffaa22
and #fad3
becomes #ffaadd33
.
name | The string name |
Color4f & cugl::Color4f::set | ( | float | r, |
float | g, | ||
float | b, | ||
float | a = 1 |
||
) |
Sets the elements of this color to the specified values.
The color values must all be in the range 0..1.
r | The red color. |
g | The green color. |
b | The blue color. |
a | The alpha value (optional). |
Color4f & cugl::Color4f::set | ( | Uint32 | color | ) |
Sets this color to an integer interpreted as an RGBA value.
This setter processes the integer in RGBA order. Hence, 0xff0000ff represents red or the color (1, 0, 0, 1).
color | The integer to interpret as an RGBA value. |
Color4f & cugl::Color4f::setHSL | ( | float | h, |
float | s, | ||
float | l, | ||
float | a = 1 |
||
) |
Sets this color to have the given hue-saturation-lightness
This method will apply the formula given on this page:
https://en.wikipedia.org/wiki/HSL_and_HSV
h | The hue (in radians) |
s | The saturation (in range 0..1) |
l | The lightness (in range 0..1) |
a | The alpha component (in range 0..1) |
Color4f & cugl::Color4f::setHSV | ( | float | h, |
float | s, | ||
float | v, | ||
float | a = 1 |
||
) |
Sets this color to have the given hue-saturation-value
This method will convert to hue-staturation-lightness and then apply the formula given on this page:
https://en.wikipedia.org/wiki/HSL_and_HSV
h | The hue (in radians) |
s | The saturation (in range 0..1) |
v | The brightness value (in range 0..1) |
a | The alpha component (in range 0..1) |
Subtracts the given color from this one in place.
This operation is functionally identical to subtractive blending. The subtraction is clamped so that this remains a valid color.
c | The color to subtract |
alpha | Whether to subtract the alpha value (optional) |
|
inline |
Subtracts the given values from this color.
The subtraction is clamped so that this remains a valid color.
r | The red color to subtract. |
g | The green color to subtract. |
b | The blue color to subtract. |
a | The alpha value (optional). |
std::string cugl::Color4f::toString | ( | bool | verbose = false | ) | const |
Returns a string representation of this vector for debuggging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
verbose | Whether to include class information |
|
inline |
Undoes premultiplication this color with its current alpha.
This class does not store whether the color is already premultiplied. Hence unpremultiplying an non-premultiplied color will have a compounding effect.
If the alpha value is 0, the color is unchanged.
float cugl::Color4f::a |
The alpha value
float cugl::Color4f::b |
The blue value
|
static |
The Black color (0,0,0,1)
|
static |
The Blue color (0,0,1,1)
|
static |
The Clear color (0,0,0,0)
|
static |
The classic XNA color (0.392,0.584,0.93,1)
|
static |
The Cyan color (0,1,1,1)
float cugl::Color4f::g |
The green value
|
static |
The Gray color (0.65,0.65,0.65,1)
|
static |
The Green color (0,1,0,1)
|
static |
The Magenta color (1,0,1,1)
|
static |
The Orange color (1,0.5,0,1)
|
static |
The Playing Fields color (0.933f,0.99f,0.65f,1.0f)
float cugl::Color4f::r |
The red value
|
static |
The Red color (1,0,0,1)
|
static |
The White color (1,1,1,1)
|
static |
The Yellow color (1,1,0,1)