TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Color3.h
Go to the documentation of this file.
1 
17 #ifndef G3D_Color3_h
18 #define G3D_Color3_h
19 
20 #include "G3D/platform.h"
21 #include "G3D/g3dmath.h"
22 #include "G3D/HashTrait.h"
23 #include "G3D/Color1.h"
24 #include <string>
25 
26 namespace G3D {
27 class Any;
28 
33 class Color3 {
34 private:
35  // Hidden operators
36  bool operator<(const Color3&) const;
37  bool operator>(const Color3&) const;
38  bool operator<=(const Color3&) const;
39  bool operator>=(const Color3&) const;
40 
41 public:
45  Color3() : r(0), g(0), b(0) {}
46 
47  bool nonZero() const {
48  return (r != 0) || (g != 0) || (b != 0);
49  }
50 
65  explicit Color3(const Any& any);
66 
67  Color3& operator=(const Any& a);
68 
70  Any toAny() const;
71 
72  explicit Color3(class BinaryInput& bi);
73 
74  Color3(float r, float g, float b);
75 
77  explicit Color3(float v) : r(v), g(v), b(v) {}
78 
79  explicit Color3(const class Vector3& v);
80 
81  explicit Color3(const float value[3]);
82 
84  const Color3& rgb() const {
85  return *this;
86  }
87 
91  Color3 (const Color3& other);
92 
93  Color3 (const class Color3unorm8& other);
94 
95  inline bool isZero() const {
96  return (r == 0.0f) && (g == 0.0f) && (b == 0.0f);
97  }
98 
99  inline bool isOne() const {
100  return (r == 1.0f) && (g == 1.0f) && (b == 1.0f);
101  }
102 
103  bool isFinite() const;
104 
108  static Color3 fromARGB(uint32);
109 
114  static Color3 fromASRGB(uint32);
115 
118  static const Color3& wheelRandom();
119 
122  static Color3 ansiMap(uint32 i);
123 
134  static Color3 pastelMap(uint32 i);
135 
139  float r, g, b;
140 
141  void serialize(class BinaryOutput& bo) const;
142  void deserialize(class BinaryInput& bi);
143 
144  // access vector V as V[0] = V.r, V[1] = V.g, V[2] = V.b
145  //
146  // WARNING. These member functions rely on
147  // (1) Color3 not having virtual functions
148  // (2) the data packed in a 3*sizeof(float) memory block
149  const float& operator[] (int i) const;
150  float& operator[] (int i);
151 
152  // assignment and comparison
153  Color3& operator= (const Color3& rkVector);
154  bool operator== (const Color3& rkVector) const;
155  bool operator!= (const Color3& rkVector) const;
156  size_t hashCode() const;
157 
158  // arithmetic operations
159  Color3 operator+ (const Color3& rkVector) const;
160  Color3 operator- (const Color3& rkVector) const;
161  inline Color3 operator* (float s) const {
162  return Color3(r * s, g * s, b * s);
163  }
164  inline Color3 operator/ (const Color3& rkVector) const {
165  return Color3(r / rkVector.r, g / rkVector.g, b / rkVector.b);
166  }
167 
168  Color3 operator* (const Color3& rkVector) const;
169  inline Color3 operator/ (float fScalar) const {
170  return (*this) * (1.0f / fScalar);
171  }
172 
173  Color3 operator- () const;
174 
175  // arithmetic updates
176  Color3& operator+= (const Color3& rkVector);
177  Color3& operator-= (const Color3& rkVector);
178  Color3& operator*= (const Color3& rkVector);
179  Color3& operator*= (float fScalar);
180  Color3& operator/= (float fScalar);
181 
182  bool fuzzyEq(const Color3& other) const;
183  bool fuzzyNe(const Color3& other) const;
184 
185  // vector operations
186  float length () const;
187  Color3 direction() const;
188  float squaredLength () const;
189  float dot (const Color3& rkVector) const;
190  float unitize (float fTolerance = 1e-06f);
191  Color3 cross (const Color3& rkVector) const;
192  Color3 unitCross (const Color3& rkVector) const;
193 
194  inline Color3 pow(const Color3& other) const {
195  return Color3(::pow(r, other.r), ::pow(g, other.g), ::pow(b, other.b));
196  }
197 
198  inline Color3 pow(float other) const {
199  return Color3(::pow(r, other), ::pow(g, other), ::pow(b, other));
200  }
201 
202  inline Color3 max(const Color3& other) const {
203  return Color3(G3D::max(r, other.r), G3D::max(g, other.g), G3D::max(b, other.b));
204  }
205 
206  inline Color3 min(const Color3& other) const {
207  return Color3(G3D::min(r, other.r), G3D::min(g, other.g), G3D::min(b, other.b));
208  }
209 
211  inline float min() const {
212  return G3D::min(G3D::min(r, g), b);
213  }
214 
216  inline float max() const {
217  return G3D::max(G3D::max(r, g), b);
218  }
219 
220  inline Color3 lerp(const Color3& other, float a) const {
221  return (*this) + (other - *this) * a;
222 
223  }
224 
225  inline float sum() const {
226  return r + g + b;
227  }
228 
229  inline float average() const {
230  return sum() / 3.0f;
231  }
232 
233 
239  static Color3 fromHSV(const Vector3& _hsv);
240  static Vector3 toHSV(const Color3& _rgb);
241 
243  static Color3 jetColorMap(const float& val);
244 
246  static Color3 rainbowColorMap(float hue);
247 
248  std::string toString() const;
249 
251  static Color3 random();
252 
253  // Special values.
254  // Intentionally not inlined: see Matrix3::identity() for details.
255  static const Color3& red();
256  static const Color3& green();
257  static const Color3& blue();
258  static const Color3& purple();
259  static const Color3& cyan();
260  static const Color3& yellow();
261  static const Color3& brown();
262  static const Color3& orange();
263  static const Color3& black();
264  static const Color3& gray();
265  static const Color3& white();
266 
267  static const Color3& zero();
268  static const Color3& one();
269 
270  inline Color3 bgr() const {
271  return Color3(b, g, r);
272  }
273 };
274 
275 inline G3D::Color3 operator* (float s, const G3D::Color3& c) {
276  return c * s;
277 }
278 
280  return c * s.value;
281 }
282 
284  return c * s.value;
285 }
286 
287 inline G3D::Color3 operator/ (float s, const G3D::Color3& c) {
288  return c * (1.0f/s);
289 }
290 
291 
292 //----------------------------------------------------------------------------
293 
294 inline Color3::Color3(float fX, float fY, float fZ) {
295  r = fX;
296  g = fY;
297  b = fZ;
298 }
299 
300 //----------------------------------------------------------------------------
301 inline Color3::Color3(const float afCoordinate[3]) {
302  r = afCoordinate[0];
303  g = afCoordinate[1];
304  b = afCoordinate[2];
305 }
306 
307 //----------------------------------------------------------------------------
308 inline Color3::Color3 (const Color3& rkVector) {
309  r = rkVector.r;
310  g = rkVector.g;
311  b = rkVector.b;
312 }
313 
314 //----------------------------------------------------------------------------
315 inline float& Color3::operator[] (int i) {
316  return ((float*)this)[i];
317 }
318 
319 //----------------------------------------------------------------------------
320 
321 inline const float& Color3::operator[] (int i) const {
322  return ((float*)this)[i];
323 }
324 
325 //----------------------------------------------------------------------------
326 
327 inline bool Color3::fuzzyEq(const Color3& other) const {
328  return G3D::fuzzyEq((*this - other).squaredLength(), 0);
329 }
330 
331 //----------------------------------------------------------------------------
332 
333 inline bool Color3::fuzzyNe(const Color3& other) const {
334  return G3D::fuzzyNe((*this - other).squaredLength(), 0);
335 }
336 
337 
338 //----------------------------------------------------------------------------
339 inline Color3& Color3::operator= (const Color3& rkVector) {
340  r = rkVector.r;
341  g = rkVector.g;
342  b = rkVector.b;
343  return *this;
344 }
345 
346 //----------------------------------------------------------------------------
347 inline bool Color3::operator== (const Color3& rkVector) const {
348  return ( r == rkVector.r && g == rkVector.g && b == rkVector.b );
349 }
350 
351 //----------------------------------------------------------------------------
352 inline bool Color3::operator!= (const Color3& rkVector) const {
353  return ( r != rkVector.r || g != rkVector.g || b != rkVector.b );
354 }
355 
356 //----------------------------------------------------------------------------
357 inline Color3 Color3::operator+ (const Color3& rkVector) const {
358  return Color3(r + rkVector.r, g + rkVector.g, b + rkVector.b);
359 }
360 
361 //----------------------------------------------------------------------------
362 inline Color3 Color3::operator- (const Color3& rkVector) const {
363  return Color3(r -rkVector.r, g - rkVector.g, b - rkVector.b);
364 }
365 
366 //----------------------------------------------------------------------------
367 inline Color3 Color3::operator* (const Color3& rkVector) const {
368  return Color3(r * rkVector.r, g * rkVector.g, b * rkVector.b);
369 }
370 
371 //----------------------------------------------------------------------------
372 inline Color3 Color3::operator- () const {
373  return Color3( -r, -g, -b);
374 }
375 
376 //----------------------------------------------------------------------------
377 inline Color3& Color3::operator+= (const Color3& rkVector) {
378  r += rkVector.r;
379  g += rkVector.g;
380  b += rkVector.b;
381  return *this;
382 }
383 
384 //----------------------------------------------------------------------------
385 inline Color3& Color3::operator-= (const Color3& rkVector) {
386  r -= rkVector.r;
387  g -= rkVector.g;
388  b -= rkVector.b;
389  return *this;
390 }
391 
392 //----------------------------------------------------------------------------
393 inline Color3& Color3::operator*= (float fScalar) {
394  r *= fScalar;
395  g *= fScalar;
396  b *= fScalar;
397  return *this;
398 }
399 
400 //----------------------------------------------------------------------------
401 inline Color3& Color3::operator*= (const Color3& rkVector) {
402  r *= rkVector.r;
403  g *= rkVector.g;
404  b *= rkVector.b;
405  return *this;
406 }
407 //----------------------------------------------------------------------------
408 inline float Color3::squaredLength () const {
409  return r*r + g*g + b*b;
410 }
411 
412 //----------------------------------------------------------------------------
413 inline float Color3::length () const {
414  return sqrtf(r*r + g*g + b*b);
415 }
416 
417 //----------------------------------------------------------------------------
418 inline Color3 Color3::direction () const {
419  float lenSquared = r * r + g * g + b * b;
420 
421  if (lenSquared != 1.0f) {
422  return *this / sqrtf(lenSquared);
423  } else {
424  return *this;
425  }
426 }
427 
428 //----------------------------------------------------------------------------
429 inline float Color3::dot (const Color3& rkVector) const {
430  return r*rkVector.r + g*rkVector.g + b*rkVector.b;
431 }
432 
433 //----------------------------------------------------------------------------
434 inline Color3 Color3::cross (const Color3& rkVector) const {
435  return Color3(g*rkVector.b - b*rkVector.g, b*rkVector.r - r*rkVector.b,
436  r*rkVector.g - g*rkVector.r);
437 }
438 
439 //----------------------------------------------------------------------------
440 inline Color3 Color3::unitCross (const Color3& rkVector) const {
441  Color3 kCross(g*rkVector.b - b*rkVector.g, b*rkVector.r - r*rkVector.b,
442  r*rkVector.g - g*rkVector.r);
443  return kCross.direction();
444 }
445 
446 
449 
452 
455 
457 typedef Color3 Energy3;
458 
461 
463 typedef Color3 Power3;
464 
465 #if 0 // Disabled to avoid taking these useful names from the namespace
466 typedef float Power;
467 typedef float Biradiance;
468 typedef float Radiance;
469 typedef float Radiosity;
470 typedef float Energy;
471 typedef float Irradiance;
472 #endif
473 } // namespace
474 
475 
476 template <> struct HashTrait<G3D::Color3> {
477  static size_t hashCode(const G3D::Color3& key) {
478  return key.hashCode();
479  }
480 };
481 
482 
483 #endif
float dot(const Color3 &rkVector) const
Definition: Color3.h:429
bool operator>(const Color3 &) const
float max() const
Definition: Color3.h:216
float average() const
Definition: Color3.h:229
Color3 pow(const Color3 &other) const
Definition: Color3.h:194
static const Color3 & white()
Definition: Color3.cpp:192
static Color3 fromASRGB(uint32)
Definition: Color3.cpp:256
Color3()
Initializes to all zero.
Definition: Color3.h:45
bool operator>=(const Color3 &) const
Color3 & operator=(const Any &a)
Definition: Color3.cpp:25
Definition: BinaryInput.h:69
bool fuzzyNe(const Color3 &other) const
Definition: Color3.h:333
float length() const
Definition: Color3.h:413
static Color3 random()
Definition: Color3.cpp:263
Color3 cross(const Color3 &rkVector) const
Definition: Color3.h:434
Color3 Radiance3
Definition: Color3.h:451
static Color3 jetColorMap(const float &val)
Definition: Color3.cpp:372
bool operator!=(const Color3 &rkVector) const
Definition: Color3.h:352
Color3 operator*(float s) const
Definition: Color3.h:161
static const Color3 & gray()
Definition: Color3.cpp:186
static const Color3 & purple()
Definition: Color3.cpp:139
Definition: HashTrait.h:105
Color3 Irradiance3
Definition: Color3.h:460
Definition: AABox.h:25
float squaredLength() const
Definition: Color3.h:408
bool any(float x)
Definition: g3dmath.h:424
bool fuzzyEq(const Color3 &other) const
Definition: Color3.h:327
Color3 Power3
Definition: Color3.h:463
bool operator<=(const Color3 &) const
static const Color3 & green()
Definition: Color3.cpp:127
bool fuzzyNe(double a, double b)
Definition: g3dmath.h:861
const Color3 & rgb() const
Definition: Color3.h:84
G3D::Color3 operator/(float s, const G3D::Color3 &c)
Definition: Color3.h:287
Color3 & operator/=(float fScalar)
Definition: Color3.cpp:270
static const Color3 & orange()
Definition: Color3.cpp:163
Color3 unitCross(const Color3 &rkVector) const
Definition: Color3.h:440
std::string toString() const
Definition: Color3.cpp:394
T max(const T &x, const T &y)
Definition: g3dmath.h:320
Definition: Vector3.h:58
static size_t hashCode(const G3D::Color3 &key)
Definition: Color3.h:477
static const Color3 & cyan()
Definition: Color3.cpp:145
Color3 direction() const
Definition: Color3.h:418
Color3(float v)
Initializes all channels to v.
Definition: Color3.h:77
Color3 Biradiance3
Definition: Color3.h:448
Color3 lerp(const Color3 &other, float a) const
Definition: Color3.h:220
static const Color3 & yellow()
Definition: Color3.cpp:151
bool isOne() const
Definition: Color3.h:99
T min(const T &x, const T &y)
Definition: g3dmath.h:305
Easy loading and saving of human-readable configuration files.
Definition: Any.h:184
float g
Definition: Color3.h:139
static Color3 fromHSV(const Vector3 &_hsv)
Definition: Color3.cpp:302
static Color3 ansiMap(uint32 i)
Definition: Color3.cpp:100
static const Color3 & blue()
Definition: Color3.cpp:133
Any toAny() const
Definition: Color3.cpp:93
static const Color3 & one()
Definition: Color3.cpp:180
static const Color3 & zero()
Definition: Color3.cpp:174
static const Color3 & wheelRandom()
Definition: Color3.cpp:222
void serialize(class BinaryOutput &bo) const
Definition: Color3.cpp:215
size_t hashCode() const
Definition: Color3.cpp:232
static const Color3 & red()
Definition: Color3.cpp:121
float unitize(float fTolerance=1e-06f)
Definition: Color3.cpp:286
Color3 & operator-=(const Color3 &rkVector)
Definition: Color3.h:385
Color3 Energy3
Definition: Color3.h:457
static Color3 fromARGB(uint32)
Definition: Color3.cpp:252
float r
Definition: Color3.h:139
const float & operator[](int i) const
Definition: Color3.h:321
static Vector3 toHSV(const Color3 &_rgb)
Definition: Color3.cpp:337
Color3 operator/(const Color3 &rkVector) const
Definition: Color3.h:164
float sum() const
Definition: Color3.h:225
Color3 & operator*=(const Color3 &rkVector)
Definition: Color3.h:401
float min() const
Definition: Color3.h:211
Definition: Color1.h:28
Definition: Color3.h:33
Definition: BinaryOutput.h:52
Color3 max(const Color3 &other) const
Definition: Color3.h:202
Color3 operator-() const
Definition: Color3.h:372
bool isFinite() const
Definition: Color3.cpp:198
Color3 pow(float other) const
Definition: Color3.h:198
static const Color3 & black()
Definition: Color3.cpp:169
const FieldDescriptor value
Definition: descriptor.h:1522
bool operator==(const Color3 &rkVector) const
Definition: Color3.h:347
uint32_t uint32
Definition: g3dmath.h:168
bool nonZero() const
Definition: Color3.h:47
static Color3 pastelMap(uint32 i)
Definition: Color3.cpp:111
Color3 & operator+=(const Color3 &rkVector)
Definition: Color3.h:377
void deserialize(class BinaryInput &bi)
Definition: Color3.cpp:208
Color3 Radiosity3
Definition: Color3.h:454
float value
Definition: Color1.h:37
Color3 bgr() const
Definition: Color3.h:270
bool isZero() const
Definition: Color3.h:95
Color3 min(const Color3 &other) const
Definition: Color3.h:206
bool operator<(const Color3 &) const
bool fuzzyEq(double a, double b)
Definition: g3dmath.h:857
G3D::Color3 operator*(float s, const G3D::Color3 &c)
Definition: Color3.h:275
static const Color3 & brown()
Definition: Color3.cpp:157
static Color3 rainbowColorMap(float hue)
Definition: Color3.cpp:400
Color3 operator+(const Color3 &rkVector) const
Definition: Color3.h:357
float b
Definition: Color3.h:139