1 //////////////////////////////////////////////////////////////////////////////////////////
3 // Class declaration for an RGBA color
4 // Downloaded from: www.paulsprojects.net
5 // Created: 20th July 2002
6 // Modified: 7th November 2002 - Some speed improvements
7 // - Removed clamping after adds etc. Do it yourself!
8 // To enable use with floating point color buffers
9 // - Corrected lerp (reversed f and 1-f)
10 // 13th December 2002 - Added default parameter to alpha of Set()
11 // - Added red, green, blue constant COLORs
13 // Copyright (c) 2006, Paul Baker
14 // Distributed under the New BSD Licence. (See accompanying file License.txt or copy at
15 // http://www.paulsprojects.net/NewBSDLicense.txt)
16 //////////////////////////////////////////////////////////////////////////////////////////
28 COLOR(float newR
, float newG
, float newB
, float newA
=0.0f
)
29 { r
=newR
; g
=newG
; b
=newB
; a
=newA
; }
31 COLOR(const float * rhs
)
32 { r
=*rhs
; g
=*(rhs
+1); b
=*(rhs
+2); a
=*(rhs
+3); }
34 COLOR(const COLOR
& rhs
)
35 { r
=rhs
.r
; g
=rhs
.g
; b
=rhs
.b
; a
=rhs
.a
;}
39 void Set(float newR
, float newG
, float newB
, float newA
=0.0f
)
40 { r
=newR
; g
=newG
; b
=newB
; a
=newA
; }
42 //accessors kept for compatability
43 void SetR(float newR
) {r
= newR
;}
44 void SetG(float newG
) {g
= newG
;}
45 void SetB(float newB
) {b
= newB
;}
46 void SetA(float newA
) {a
= newA
;}
48 float GetR() const {return r
;} //public accessor functions
49 float GetG() const {return g
;} //inline, const
50 float GetB() const {return b
;}
51 float GetA() const {return a
;}
53 void ClampTo01(void); //clamp all components to [0,1]
55 void SetBlack(void) {r
=g
=b
=a
=1.0f
;}
56 void SetWhite(void) {r
=g
=b
=a
=0.0f
;}
57 void SetGrey(float shade
) {r
=g
=b
=a
=shade
;}
60 COLOR
lerp(const COLOR
& c2
, float factor
)
61 { return (*this)*(1.0f
-factor
) + c2
*factor
; }
64 COLOR
operator+(const COLOR
& rhs
) const
65 { return COLOR(r
+rhs
.r
, g
+rhs
.g
, b
+rhs
.b
, a
+rhs
.a
); }
67 COLOR
operator-(const COLOR
& rhs
) const
68 { return COLOR(r
-rhs
.r
, g
-rhs
.g
, b
-rhs
.b
, a
-rhs
.a
); }
70 COLOR
operator*(const COLOR
& rhs
) const
71 { return COLOR(r
*rhs
.r
, g
*rhs
.g
, b
*rhs
.b
, a
*rhs
.a
); }
73 COLOR
operator/(const COLOR
& rhs
) const
74 { return COLOR(r
/rhs
.r
, g
/rhs
.g
, b
/rhs
.b
, a
/rhs
.a
); }
76 COLOR
operator*(const float rhs
) const
77 { return COLOR(r
*rhs
, g
*rhs
, b
*rhs
, a
*rhs
); }
79 COLOR
operator/(const float rhs
) const
80 { return COLOR(r
/rhs
, g
/rhs
, b
/rhs
, a
/rhs
); }
82 //multiply by a float, eg 3*c
83 friend COLOR
operator*(float scaleFactor
, const COLOR
& rhs
);
85 bool operator==(const COLOR
& rhs
) const;
86 bool operator!=(const COLOR
& rhs
) const
87 { return !((*this)==rhs
); }
90 COLOR
operator+=(const COLOR
& rhs
)
91 { (*this)=(*this)+rhs
; return (*this); }
93 COLOR
operator-=(const COLOR
& rhs
)
94 { (*this)=(*this)-rhs
; return (*this); }
96 COLOR
operator*=(const COLOR
& rhs
)
97 { (*this)=(*this)*rhs
; return (*this); }
99 COLOR
operator/=(const COLOR
& rhs
)
100 { (*this)=(*this)/rhs
; return (*this); }
102 COLOR
operator*=(const float rhs
)
103 { (*this)=(*this)*rhs
; return (*this); }
105 COLOR
operator/=(const float rhs
)
106 { (*this)=(*this)/rhs
; return (*this); }
109 COLOR
operator-(void) const {return COLOR(-r
,-g
, -b
, -a
);}
110 COLOR
operator+(void) const {return (*this);}
112 //cast to pointer to float for glColor4fv etc
113 operator float* () const {return (float*) this;}
114 operator const float* () const {return (const float*) this;}
123 const COLOR
white(1.0f
, 1.0f
, 1.0f
, 1.0f
);
124 const COLOR
black(0.0f
, 0.0f
, 0.0f
, 0.0f
);
126 const COLOR
red(1.0f
, 0.0f
, 0.0f
, 1.0f
);
127 const COLOR
green(0.0f
, 1.0f
, 0.0f
, 1.0f
);
128 const COLOR
blue(0.0f
, 0.0f
, 1.0f
, 1.0f
);