source: GTP/trunk/Lib/Illum/GPUObscurancesGT/include/COLOR.h @ 930

Revision 930, 3.9 KB checked in by igarcia, 18 years ago (diff)
Line 
1//////////////////////////////////////////////////////////////////////////////////////////
2//      COLOR.h
3//      Class declaration for an RGBA color
4//      You may use this code however you wish, but if you do, please credit me and
5//      provide a link to my website in a readme file or similar
6//      Downloaded from: www.paulsprojects.net
7//      Created:        20th July 2002
8//      Modified:       7th November 2002       -       Some speed improvements
9//                                                                      -       Removed clamping after adds etc. Do it yourself!
10//                                                                              To enable use with floating point color buffers
11//                                                                      -       Corrected lerp (reversed f and 1-f)
12//                              13th December 2002      -       Added default parameter to alpha of Set()
13//                                                                      -       Added red, green, blue constant COLORs
14//////////////////////////////////////////////////////////////////////////////////////////     
15
16#ifndef COLOR_H
17#define COLOR_H
18
19class COLOR
20{
21public:
22        //constructors
23        COLOR()
24        {       r=g=b=a=0.0f;   }
25
26        COLOR(float newR, float newG, float newB, float newA=0.0f)
27        {       r=newR; g=newG; b=newB; a=newA; }
28
29        COLOR(const float * rhs)
30        {       r=*rhs; g=*(rhs+1);     b=*(rhs+2); a=*(rhs+3); }
31
32        COLOR(const COLOR & rhs)
33        {       r=rhs.r;        g=rhs.g;        b=rhs.b;        a=rhs.a;}
34
35        ~COLOR() {}     //empty
36
37        void Set(float newR, float newG, float newB, float newA=0.0f)
38        {       r=newR; g=newG; b=newB; a=newA; }
39       
40        //accessors kept for compatability
41        void SetR(float newR) {r = newR;}
42        void SetG(float newG) {g = newG;}
43        void SetB(float newB) {b = newB;}
44        void SetA(float newA) {a = newA;}
45       
46        float GetR() const {return r;}  //public accessor functions
47        float GetG() const {return g;}  //inline, const
48        float GetB() const {return b;}
49        float GetA() const {return a;}
50
51        void ClampTo01(void);                   //clamp all components to [0,1]
52
53        void SetBlack(void) {r=g=b=a=1.0f;}
54        void SetWhite(void) {r=g=b=a=0.0f;}
55        void SetGrey(float shade) {r=g=b=a=shade;}
56
57        //linear interpolate
58        COLOR lerp(const COLOR & c2, float factor)
59        {       return (*this)*(1.0f-factor) + c2*factor;       }
60
61        //binary operators
62        COLOR operator+(const COLOR & rhs) const
63        {       return COLOR(r+rhs.r, g+rhs.g, b+rhs.b, a+rhs.a);       }
64
65        COLOR operator-(const COLOR & rhs) const
66        {       return COLOR(r-rhs.r, g-rhs.g, b-rhs.b, a-rhs.a);       }
67
68        COLOR operator*(const COLOR & rhs) const
69        {       return COLOR(r*rhs.r, g*rhs.g, b*rhs.b, a*rhs.a);       }
70
71        COLOR operator/(const COLOR & rhs) const
72        {       return COLOR(r/rhs.r, g/rhs.g, b/rhs.b, a/rhs.a);       }
73
74        COLOR operator*(const float rhs) const
75        {       return COLOR(r*rhs, g*rhs, b*rhs, a*rhs);       }
76
77        COLOR operator/(const float rhs) const
78        {       return COLOR(r/rhs, g/rhs, b/rhs, a/rhs);       }
79
80        //multiply by a float, eg 3*c
81        friend COLOR operator*(float scaleFactor, const COLOR & rhs);
82
83        bool operator==(const COLOR & rhs) const;
84        bool operator!=(const COLOR & rhs) const
85        {       return !((*this)==rhs); }
86
87        //self-add etc
88        COLOR operator+=(const COLOR & rhs)
89        {       (*this)=(*this)+rhs;    return (*this); }
90
91        COLOR operator-=(const COLOR & rhs)
92        {       (*this)=(*this)-rhs;    return (*this); }
93
94        COLOR operator*=(const COLOR & rhs)
95        {       (*this)=(*this)*rhs;    return (*this); }
96
97        COLOR operator/=(const COLOR & rhs)
98        {       (*this)=(*this)/rhs;    return (*this); }
99
100        COLOR operator*=(const float rhs)
101        {       (*this)=(*this)*rhs;    return (*this); }
102
103        COLOR operator/=(const float rhs)
104        {       (*this)=(*this)/rhs;    return (*this); }
105
106        //unary operators
107        COLOR operator-(void) const {return COLOR(-r,-g, -b, -a);}
108        COLOR operator+(void) const {return (*this);}
109
110        //cast to pointer to float for glColor4fv etc
111        operator float* () const {return (float*) this;}
112        operator const float* () const {return (const float*) this;}
113
114        //member variables
115        float r;
116        float g;
117        float b;
118        float a;
119};
120
121const COLOR white(1.0f, 1.0f, 1.0f, 1.0f);
122const COLOR black(0.0f, 0.0f, 0.0f, 0.0f);
123
124const COLOR red(1.0f, 0.0f, 0.0f, 1.0f);
125const COLOR green(0.0f, 1.0f, 0.0f, 1.0f);
126const COLOR blue(0.0f, 0.0f, 1.0f, 1.0f);
127
128const COLOR cyan(0.0f, 1.0f, 1.0f, 1.0f);
129const COLOR magenta(1.0f, 0.0f, 1.0f, 1.0f);
130const COLOR yellow(1.0f, 1.0f, 0.0f, 1.0f);
131
132#endif  //COLOR_H
Note: See TracBrowser for help on using the repository browser.