Color Class Reference

#include <math.h>

Class Description

Represents a color with four components: red, green, blue, alpha.

Each component is represented by a 32 bit float value. The value 0 for the alpha means full transparency, while 1 means fully opaque.

+ Examples:

Definition at line 674 of file math.h.

Public Member Functions

 Color (float fRed=1.0f, float fGreen=1.0f, float fBlue=1.0f, float fAlpha=1.0f)
 Constructs a color object with specified values. More...
 
void Set (float fRed, float fGreen, float fBlue, float fAlpha=1.0f)
 Sets the current value of the color. More...
 
 operator const float * (void) const
 Returns a pointer to the color data, which consists of four floats (R, G, B, and A). More...
 
float & operator[] (unsigned int iChannel)
 Returns a color component based on its index. More...
 
 operator unsigned int (void) const
 Converts the color value into a singe 32 bit integer, where each component takes eight bits. More...
 
Coloroperator*= (const float f)
 Multiplies each component of the color with a single scalar value. More...
 
Color operator* (float f) const
 Multiplies the color by a scalar value. More...
 
Color operator/ (float f) const
 Divides the color by a scalar value. More...
 
Color operator* (const Color &c) const
 Multiplies two colors and returns the result. More...
 
Coloroperator*= (const Color &c)
 Mutliplies two colors, puts the result into the current object, and returns it. More...
 
Color operator+ (const Color &c) const
 Adds two colors and returns the result. More...
 
Color operator- (const Color &c) const
 Subtracts two colors and returns the result. More...
 
Coloroperator+= (const Color &c)
 Adds two colors, puts the result into the current object, and returns it. More...
 
bool operator== (const Color &c) const
 Returns true if the two colors are identical, false otherwise. More...
 
bool operator!= (const Color &c) const
 Returns true if the two colors are different, false otherwise. More...
 
Color Mix (const Color &c, float f) const
 Mix two colors based on a scalar value which should be within the range 0 and 1. More...
 
float Luminance () const
 Returns the luminance value for the color. More...
 
void toHSV (float &hue, float &sat, float &val) const
 
void fromHSV (float hue, float sat, float val)
 
void toCMYK (float &c, float &m, float &y, float &k) const
 
void fromCMYK (float c, float m, float y, float k)
 

Public Attributes

union {
   struct {
      float   m_fRed
 
      float   m_fGreen
 
      float   m_fBlue
 
      float   m_fAlpha
 
   } 
 
   struct {
      float   r
 
      float   g
 
      float   b
 
      float   a
 
   } 
 
   float   m_fData [4]
 
   double   m_dAlignDummy
 
   __m128   m_vAlignDummy
 
}; 
 

Static Public Attributes

static Color black
 
static Color white
 
static Color red
 
static Color green
 
static Color blue
 
static Color gray
 
static Color transparent
 

Constructor & Destructor Documentation

Color ( float  fRed = 1.0f,
float  fGreen = 1.0f,
float  fBlue = 1.0f,
float  fAlpha = 1.0f 
)
inline

Constructs a color object with specified values.

If no parameters used, the color will be white.

Definition at line 681 of file math.h.

682  { m_fRed = fRed; m_fGreen = fGreen; m_fBlue = fBlue, m_fAlpha = fAlpha; };
float m_fRed
Definition: math.h:782
float m_fBlue
Definition: math.h:782
float m_fGreen
Definition: math.h:782
float m_fAlpha
Definition: math.h:782

Member Function Documentation

void Set ( float  fRed,
float  fGreen,
float  fBlue,
float  fAlpha = 1.0f 
)
inline

Sets the current value of the color.

Definition at line 695 of file math.h.

696  { r = fRed; g = fGreen; b = fBlue; a = fAlpha; };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
operator const float * ( void  ) const
inline

Returns a pointer to the color data, which consists of four floats (R, G, B, and A).

Do not delete this pointer.

Definition at line 701 of file math.h.

701 { return &m_fRed; };
float m_fRed
Definition: math.h:782
float& operator[] ( unsigned int  iChannel)
inline

Returns a color component based on its index.

Definition at line 704 of file math.h.

705  { MB_ASSERT( iChannel < 4 ); return m_fData[iChannel]; };
#define MB_ASSERT(condition)
Definition: mudbox.h:73
float m_fData[4]
Definition: math.h:784
operator unsigned int ( void  ) const
inline

Converts the color value into a singe 32 bit integer, where each component takes eight bits.

This format is used by some external libraries.

Definition at line 709 of file math.h.

710  { return (unsigned int)(0xff*m_fRed)+(((unsigned int)(0xff*m_fGreen))<<8)+
711  (((unsigned int)(0xff*m_fBlue))<<16)+(((unsigned int)(0xff*m_fAlpha))<<24); };
float m_fRed
Definition: math.h:782
float m_fBlue
Definition: math.h:782
float m_fGreen
Definition: math.h:782
float m_fAlpha
Definition: math.h:782
Color& operator*= ( const float  f)
inline

Multiplies each component of the color with a single scalar value.

The alpha value is unchanged. The result is returned.

Definition at line 716 of file math.h.

716 { r*=f; g*=f; b*=f; return *this; };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
GLclampf f
Definition: GLee.h:9303
Color operator* ( float  f) const
inline

Multiplies the color by a scalar value.

Definition at line 719 of file math.h.

720  { return Color( r*f, g*f, b*f, a*f ); };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
GLclampf f
Definition: GLee.h:9303
Color(float fRed=1.0f, float fGreen=1.0f, float fBlue=1.0f, float fAlpha=1.0f)
Constructs a color object with specified values.
Definition: math.h:681
Color operator/ ( float  f) const
inline

Divides the color by a scalar value.

Definition at line 723 of file math.h.

724  { return operator *( 1/f ); };
Color operator*(float f) const
Multiplies the color by a scalar value.
Definition: math.h:719
GLclampf f
Definition: GLee.h:9303
Color operator* ( const Color c) const
inline

Multiplies two colors and returns the result.

Definition at line 727 of file math.h.

728  { return Color( r*c.r, g*c.g, b*c.b, a*c.a ); };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
Color(float fRed=1.0f, float fGreen=1.0f, float fBlue=1.0f, float fAlpha=1.0f)
Constructs a color object with specified values.
Definition: math.h:681
Color& operator*= ( const Color c)
inline

Mutliplies two colors, puts the result into the current object, and returns it.

Definition at line 732 of file math.h.

733  { r *= c.r; g *= c.g; b *= c.b; a *= c.a; return *this; };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
Color operator+ ( const Color c) const
inline

Adds two colors and returns the result.

Definition at line 736 of file math.h.

737  { return Color( r+c.r, g+c.g, b+c.b, a+c.a ); };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
Color(float fRed=1.0f, float fGreen=1.0f, float fBlue=1.0f, float fAlpha=1.0f)
Constructs a color object with specified values.
Definition: math.h:681
Color operator- ( const Color c) const
inline

Subtracts two colors and returns the result.

Definition at line 740 of file math.h.

741  { return Color( r-c.r, g-c.g, b-c.b, a-c.a ); };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
Color(float fRed=1.0f, float fGreen=1.0f, float fBlue=1.0f, float fAlpha=1.0f)
Constructs a color object with specified values.
Definition: math.h:681
Color& operator+= ( const Color c)
inline

Adds two colors, puts the result into the current object, and returns it.

Definition at line 744 of file math.h.

745  { r += c.r; g += c.g; b += c.b; a += c.a; return *this; };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
bool operator== ( const Color c) const
inline

Returns true if the two colors are identical, false otherwise.

Definition at line 748 of file math.h.

749  { return r == c.r && g == c.g && b == c.b && a == c.a; };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
bool operator!= ( const Color c) const
inline

Returns true if the two colors are different, false otherwise.

Definition at line 752 of file math.h.

752 { return !operator ==( c ); };
bool operator==(const Color &c) const
Returns true if the two colors are identical, false otherwise.
Definition: math.h:748
const GLubyte * c
Definition: GLee.h:5419
Color Mix ( const Color c,
float  f 
) const
inline

Mix two colors based on a scalar value which should be within the range 0 and 1.

Return the result.

The scalar value controls how much of this color, and how much of the passed-in color are included in the mix. Setting the f value closer to 1 biases the result towards this color; setting the f value closer to zero biases the result towards the passed-in colour.

Definition at line 762 of file math.h.

763  { return Color( r*f+c.r*(1-f), g*f+c.g*(1-f), b*f+c.b*(1-f), a*f+c.a*(1-f) ); };
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLubyte g
Definition: GLee.h:5404
GLubyte GLubyte b
Definition: GLee.h:5404
const GLubyte * c
Definition: GLee.h:5419
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
GLclampf f
Definition: GLee.h:9303
Color(float fRed=1.0f, float fGreen=1.0f, float fBlue=1.0f, float fAlpha=1.0f)
Constructs a color object with specified values.
Definition: math.h:681
float Luminance ( ) const
inline

Returns the luminance value for the color.

Definition at line 766 of file math.h.

767  { return 0.299f * r + 0.587f * g + 0.114f * b; }
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
float b
Definition: math.h:783
GLubyte g
Definition: GLee.h:5404
void toHSV ( float &  hue,
float &  sat,
float &  val 
) const
void fromHSV ( float  hue,
float  sat,
float  val 
)
void toCMYK ( float &  c,
float &  m,
float &  y,
float &  k 
) const
void fromCMYK ( float  c,
float  m,
float  y,
float  k 
)

Member Data Documentation

float m_fRed

Definition at line 782 of file math.h.

float m_fGreen

Definition at line 782 of file math.h.

float m_fBlue

Definition at line 782 of file math.h.

float m_fAlpha

Definition at line 782 of file math.h.

float r

Definition at line 783 of file math.h.

float g

Definition at line 783 of file math.h.

float b

Definition at line 783 of file math.h.

float a

Definition at line 783 of file math.h.

float m_fData[4]

Definition at line 784 of file math.h.

double m_dAlignDummy

Definition at line 786 of file math.h.

__m128 m_vAlignDummy

Definition at line 788 of file math.h.

union { ... }
Color black
static

Definition at line 793 of file math.h.

Color white
static

Definition at line 794 of file math.h.

Color red
static

Definition at line 795 of file math.h.

Color green
static

Definition at line 796 of file math.h.

Color blue
static

Definition at line 797 of file math.h.

Color gray
static

Definition at line 798 of file math.h.

Color transparent
static

Definition at line 799 of file math.h.


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