-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolor.hpp
314 lines (253 loc) · 6.25 KB
/
color.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef COLOR_HPP
#define COLOR_HPP
//===========================================================================
//
// Colors
//
//===========================================================================
class Color3;
//--------------------------
// 4D Color
//--------------------------
class Color4 {
public:
Color4();
explicit Color4( unsigned int argb );
explicit Color4( const float* );
Color4( const Color3&, float a );
Color4( float r, float g, float b, float a );
void set( float r, float g, float b, float a );
// access grants
float& operator [] ( int i ) { return (&r)[i]; }
float operator [] ( int i ) const { return (&r)[i]; }
void getFloat4 ( float* pf ) const { pf[0] = r; pf[1] = g; pf[2] = b; pf[3] = a; }
// casting (float* for vertex shader constants; uint32 for render states & co)
//operator float* () { return &r; }
//operator const float* () const { return &r; }
// returns a D3DCOLOR [i.e. a color in bgra (= D3D 'argb') order, not rgba!]
operator unsigned int () const;
Color3 rgb () const;
// no arithmetic operators defined for Color4 (but for Color3, see below)!
bool operator == ( const Color4& ) const;
bool operator != ( const Color4& ) const;
float r;
float g;
float b;
float a;
private: // (not allowed!)
// unary operators
Color4 operator + () const;
Color4 operator - () const;
// binary operators
Color4 operator + ( const Color4& ) const;
Color4 operator - ( const Color4& ) const;
Color4 operator * ( const Color4& ) const;
Color4 operator * ( float ) const;
Color4 operator / ( float ) const;
friend Color4 operator * ( float, const Color4& );
};
//--------------------------
// 3D Color // (i.e. alpha component is always 1)
//--------------------------
class Color3 : public Color4 {
public:
Color3() {}
explicit Color3( unsigned int rgb );
Color3( float r, float g, float b ) : Color4(r, g, b, 1) {}
void set( float r, float g, float b );
// access grants
void getFloat3 ( float* pf ) const { pf[0] = r; pf[1] = g; pf[2] = b; }
// assignment operators
Color3& operator += ( const Color3& );
Color3& operator -= ( const Color3& );
Color3& operator *= ( const Color3& ); // (componentwise multiply!)
Color3& operator *= ( float );
Color3& operator /= ( float );
// unary operators
Color3 operator + () const { return *this; }
Color3 operator - () const;
// binary operators
Color3 operator + ( const Color3& ) const;
Color3 operator - ( const Color3& ) const;
Color3 operator * ( const Color3& ) const; // (componentwise multiply!)
Color3 operator * ( float ) const;
Color3 operator / ( float ) const;
friend Color3 operator * ( float, const Color3& );
};
//--------------------------
// 4D Color
//--------------------------
inline
Color4::Color4( unsigned int argb )
{
const float f = 1.0f / 255.0f;
r = f * (float) (unsigned char) (argb >> 16);
g = f * (float) (unsigned char) (argb >> 8);
b = f * (float) (unsigned char) (argb >> 0);
a = f * (float) (unsigned char) (argb >> 24);
}
inline
Color4::Color4( const float* pf )
:
r(pf[0]),
g(pf[1]),
b(pf[2]),
a(pf[3])
{}
inline
Color4::Color4( const Color3& c, float fa )
:
r(c.r),
g(c.g),
b(c.b),
a(fa)
{}
inline
Color4::Color4( float fr, float fg, float fb, float fa )
:
r(fr),
g(fg),
b(fb),
a(fa)
{}
inline
Color4::Color4()
:
r(0),
g(0),
b(0),
a(1)
{}
inline
void Color4::set( float fr, float fg, float fb, float fa )
{
r = fr;
g = fg;
b = fb;
a = fa;
}
// casting
inline
Color4::operator unsigned int () const
{
unsigned int dwR = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : (unsigned int) (r * 255.0f + 0.5f);
unsigned int dwG = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : (unsigned int) (g * 255.0f + 0.5f);
unsigned int dwB = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : (unsigned int) (b * 255.0f + 0.5f);
unsigned int dwA = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : (unsigned int) (a * 255.0f + 0.5f);
return (dwA << 24) | (dwR << 16) | (dwG << 8) | dwB;
}
inline Color3
Color4::rgb () const
{
return Color3(r, g, b);
}
inline bool
Color4::operator == ( const Color4& c ) const
{
return r == c.r && g == c.g && b == c.b && a == c.a;
}
inline bool
Color4::operator != ( const Color4& c ) const
{
return r != c.r || g != c.g || b != c.b || a != c.a;
}
//--------------------------
// 3D Color
//--------------------------
inline
Color3::Color3( unsigned int rgb )
{
const float f = 1.0f / 255.0f;
r = f * (float) (unsigned char) (rgb >> 16);
g = f * (float) (unsigned char) (rgb >> 8);
b = f * (float) (unsigned char) (rgb >> 0);
a = 1;
}
inline
void Color3::set( float fr, float fg, float fb )
{
r = fr;
g = fg;
b = fb;
}
// assignment operators
inline Color3&
Color3::operator += ( const Color3& c )
{
r += c.r;
g += c.g;
b += c.b;
return *this;
}
inline Color3&
Color3::operator -= ( const Color3& c )
{
r -= c.r;
g -= c.g;
b -= c.b;
return *this;
}
inline Color3&
Color3::operator *= ( const Color3& c )
{
r *= c.r;
g *= c.g;
b *= c.b;
return *this;
}
inline Color3&
Color3::operator *= ( float f )
{
r *= f;
g *= f;
b *= f;
return *this;
}
inline Color3&
Color3::operator /= ( float f )
{
float fInv = 1.0f / f;
r *= fInv;
g *= fInv;
b *= fInv;
return *this;
}
// unary operators
inline Color3
Color3::operator - () const
{
return Color3(-r, -g, -b);
}
// binary operators
inline Color3
Color3::operator + ( const Color3& c ) const
{
return Color3(r + c.r, g + c.g, b + c.b);
}
inline Color3
Color3::operator - ( const Color3& c ) const
{
return Color3(r - c.r, g - c.g, b - c.b);
}
inline Color3
Color3::operator * ( const Color3& c ) const
{
return Color3(r * c.r, g * c.g, b * c.b);
}
inline Color3
Color3::operator * ( float f ) const
{
return Color3(r * f, g * f, b * f);
}
inline Color3
Color3::operator / ( float f ) const
{
float fInv = 1.0f / f;
return Color3(r * fInv, g * fInv, b * fInv);
}
inline Color3
operator * ( float f, const Color3& c )
{
return Color3(f * c.r, f * c.g, f * c.b);
}
#endif