TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Color4.h
Go to the documentation of this file.
1 
17 #ifndef G3D_Color4_h
18 #define G3D_Color4_h
19 
20 #include "G3D/platform.h"
21 #include "G3D/g3dmath.h"
22 #include "G3D/Color3.h"
23 #include <string>
24 
25 namespace G3D {
26 
27 class Any;
28 
33 class Color4 {
34 private:
35  // Hidden operators
36  bool operator<(const Color4&) const;
37  bool operator>(const Color4&) const;
38  bool operator<=(const Color4&) const;
39  bool operator>=(const Color4&) const;
40 
41 public:
42 
48  Color4(const Any& any);
49 
51  Any toAny() const;
52 
56  Color4() : r(0), g(0), b(0), a(0) {}
57 
58  Color4(const Color3& c3, float a = 1.0);
59 
60  Color4(const class Color4unorm8& c);
61 
62  Color4(class BinaryInput& bi);
63 
64  Color4(const class Vector4& v);
65 
66  Color4(float r, float g, float b, float a = 1.0);
67 
68  static const Color4& one();
69 
70  Color4(float value[4]);
71 
75  Color4(const Color4& other);
76 
77 
78  inline bool isZero() const {
79  return (r == 0.0f) && (g == 0.0f) && (b == 0.0f) && (a == 0.0f);
80  }
81 
82  inline bool isOne() const {
83  return (r == 1.0f) && (g == 1.0f) && (b == 1.0f) && (a == 1.0f);
84  }
85 
86  void serialize(class BinaryOutput& bo) const;
87  void deserialize(class BinaryInput& bi);
88 
92  static Color4 fromARGB(uint32);
93 
97  float r, g, b, a;
98 
99  inline Color3 rgb() const {
100  return Color3(r, g, b);
101  }
102 
103  // access vector V as V[0] = V.r, V[1] = V.g, V[2] = V.b, v[3] = V.a
104  //
105  // WARNING. These member functions rely on
106  // (1) Color4 not having virtual functions
107  // (2) the data packed in a 3*sizeof(float) memory block
108  float& operator[] (int i) const;
109 
110  // assignment and comparison
111  Color4& operator= (const Color4& rkVector);
112  bool operator== (const Color4& rkVector) const;
113  bool operator!= (const Color4& rkVector) const;
114  size_t hashCode() const;
115 
116  // arithmetic operations
117  Color4 operator+ (const Color4& rkVector) const;
118  Color4 operator- (const Color4& rkVector) const;
119  Color4 operator* (float fScalar) const;
120  Color4 operator* (const Color4& k) const {
121  return Color4(r*k.r, g*k.g, b*k.b, a * k.a);
122  }
123 
124  Color4& operator*= (const Color4& c) {
125  r *= c.r;
126  g *= c.g;
127  b *= c.b;
128  a *= c.a;
129  return *this;
130  }
131 
132  Color4 operator/ (float fScalar) const;
133  Color4 operator- () const;
134  friend Color4 operator* (double fScalar, const Color4& rkVector);
135 
136  // arithmetic updates
137  Color4& operator+= (const Color4& rkVector);
138  Color4& operator-= (const Color4& rkVector);
139  Color4& operator*= (float fScalar);
140  Color4& operator/= (float fScalar);
141 
142  bool fuzzyEq(const Color4& other) const;
143  bool fuzzyNe(const Color4& other) const;
144 
145  std::string toString() const;
146 
147  inline Color4 max(const Color4& other) const {
148  return Color4(G3D::max(r, other.r), G3D::max(g, other.g), G3D::max(b, other.b), G3D::max(a, other.a));
149  }
150 
151  inline Color4 min(const Color4& other) const {
152  return Color4(G3D::min(r, other.r), G3D::min(g, other.g), G3D::min(b, other.b), G3D::min(a, other.a));
153  }
154 
156  inline float sum() const {
157  return r + g + b + a;
158  }
159 
160  inline Color4 lerp(const Color4& other, float a) const {
161  return (*this) + (other - *this) * a;
162 
163  }
164 
165  // Special values.
166  // Intentionally not inlined: see Matrix3::identity() for details.
167  static const Color4& zero();
168  static const Color4& clear();
169 
170  static const Color4& inf();
171  static const Color4& nan();
172 
173  inline bool isFinite() const {
174  return G3D::isFinite(r) && G3D::isFinite(g) && G3D::isFinite(b) && G3D::isFinite(a);
175  }
176 
177  inline Color3 bgr() const {
178  return Color3(b, g, r);
179  }
180 };
181 
185 Color4 operator*(const Color3& c3, const Color4& c4);
186 
187 
188 inline Color4 operator*(const Color3& c3, const Color4& c4) {
189  return Color4(c3.r * c4.r, c3.g * c4.g, c3.b * c4.b, c4.a);
190 }
191 
192 //----------------------------------------------------------------------------
193 
194 
195 inline Color4::Color4(const Color3& c3, float a) {
196  r = c3.r;
197  g = c3.g;
198  b = c3.b;
199  this->a = a;
200 }
201 
202 //----------------------------------------------------------------------------
203 
205  float r,
206  float g,
207  float b,
208  float a) :
209  r(r), g(g), b(b), a(a) {
210 }
211 
212 //----------------------------------------------------------------------------
213 inline Color4::Color4 (float afCoordinate[4]) {
214  r = afCoordinate[0];
215  g = afCoordinate[1];
216  b = afCoordinate[2];
217  a = afCoordinate[3];
218 }
219 
220 //----------------------------------------------------------------------------
221 
223  const Color4& other) {
224 
225  r = other.r;
226  g = other.g;
227  b = other.b;
228  a = other.a;
229 }
230 
231 //----------------------------------------------------------------------------
232 
233 inline float& Color4::operator[] (int i) const {
234  return ((float*)this)[i];
235 }
236 
237 //----------------------------------------------------------------------------
238 
239 inline bool Color4::fuzzyEq(const Color4& other) const {
240  Color4 dif = (*this - other);
241  return G3D::fuzzyEq(dif.r * dif.r + dif.g * dif.g + dif.b * dif.b + dif.a * dif.a, 0);
242 }
243 
244 //----------------------------------------------------------------------------
245 
246 inline bool Color4::fuzzyNe(const Color4& other) const {
247  Color4 dif = (*this - other);
248  return G3D::fuzzyNe(dif.r * dif.r + dif.g * dif.g + dif.b * dif.b + dif.a * dif.a, 0);
249 }
250 
251 
252 //----------------------------------------------------------------------------
253 inline Color4& Color4::operator= (const Color4& other) {
254  r = other.r;
255  g = other.g;
256  b = other.b;
257  a = other.a;
258  return *this;
259 }
260 
261 //----------------------------------------------------------------------------
262 
263 inline bool Color4::operator== (const Color4& other) const {
264  return ( r == other.r && g == other.g && b == other.b && a == other.a);
265 }
266 
267 //----------------------------------------------------------------------------
268 
269 inline bool Color4::operator!= (const Color4& other) const {
270  return ( r != other.r || g != other.g || b != other.b || a != other.a);
271 }
272 
273 //----------------------------------------------------------------------------
274 inline Color4 Color4::operator+ (const Color4& other) const {
275  return Color4(r + other.r, g + other.g, b + other.b, a + other.a);
276 }
277 
278 //----------------------------------------------------------------------------
279 inline Color4 Color4::operator- (const Color4& other) const {
280  return Color4(r - other.r, g - other.g, b - other.b, a - other.a);
281 }
282 
283 //----------------------------------------------------------------------------
284 
285 inline Color4 Color4::operator* (float fScalar) const {
286  return Color4(fScalar * r, fScalar * g, fScalar * b, fScalar * a);
287 }
288 
289 //----------------------------------------------------------------------------
290 
291 inline Color4 Color4::operator- () const {
292  return Color4(-r, -g, -b, -a);
293 }
294 
295 //----------------------------------------------------------------------------
296 
297 inline Color4 operator* (float fScalar, const Color4& other) {
298  return Color4(fScalar * other.r, fScalar * other.g,
299  fScalar * other.b, fScalar * other.a);
300 }
301 
302 //----------------------------------------------------------------------------
303 
304 inline Color4& Color4::operator+= (const Color4& other) {
305  r += other.r;
306  g += other.g;
307  b += other.b;
308  a += other.a;
309  return *this;
310 }
311 
312 //----------------------------------------------------------------------------
313 
314 inline Color4& Color4::operator-= (const Color4& other) {
315  r -= other.r;
316  g -= other.g;
317  b -= other.b;
318  a -= other.a;
319  return *this;
320 }
321 
322 //----------------------------------------------------------------------------
323 
324 inline Color4& Color4::operator*= (float fScalar) {
325  r *= fScalar;
326  g *= fScalar;
327  b *= fScalar;
328  a *= fScalar;
329  return *this;
330 }
331 
332 } // namespace
333 
334 template <>
335 struct HashTrait<G3D::Color4> {
336  static size_t hashCode(const G3D::Color4& key) {
337  return key.hashCode();
338  }
339 };
340 
341 #endif
bool operator>=(const Color4 &) const
Color4 lerp(const Color4 &other, float a) const
Definition: Color4.h:160
bool operator==(const Color4 &rkVector) const
Definition: Color4.h:263
Color4 operator+(const Color4 &rkVector) const
Definition: Color4.h:274
Color4 & operator+=(const Color4 &rkVector)
Definition: Color4.h:304
Color4 operator/(float fScalar) const
Definition: Color4.cpp:144
static const Color4 & inf()
Definition: Color4.cpp:75
float a
Definition: Color4.h:97
Definition: BinaryInput.h:69
Color4 & operator=(const Color4 &rkVector)
Definition: Color4.h:253
static size_t hashCode(const G3D::Color4 &key)
Definition: Color4.h:336
bool fuzzyEq(const Color4 &other) const
Definition: Color4.h:239
Color4 & operator-=(const Color4 &rkVector)
Definition: Color4.h:314
Any toAny() const
Definition: Color4.cpp:56
Definition: HashTrait.h:105
Definition: AABox.h:25
bool any(float x)
Definition: g3dmath.h:424
bool isOne() const
Definition: Color4.h:82
size_t hashCode() const
Definition: Color4.cpp:103
bool fuzzyNe(double a, double b)
Definition: g3dmath.h:861
T max(const T &x, const T &y)
Definition: g3dmath.h:320
Color4()
Definition: Color4.h:56
float b
Definition: Color4.h:97
T min(const T &x, const T &y)
Definition: g3dmath.h:305
float & operator[](int i) const
Definition: Color4.h:233
Easy loading and saving of human-readable configuration files.
Definition: Any.h:184
float g
Definition: Color3.h:139
float r
Definition: Color4.h:97
Color4 min(const Color4 &other) const
Definition: Color4.h:151
bool operator<(const Color4 &) const
bool fuzzyNe(const Color4 &other) const
Definition: Color4.h:246
bool operator!=(const Color4 &rkVector) const
Definition: Color4.h:269
Color4 & operator/=(float fScalar)
Definition: Color4.cpp:163
void serialize(class BinaryOutput &bo) const
Definition: Color4.cpp:134
Definition: Vector4.h:39
static const Color4 & one()
Definition: Color4.cpp:63
static const Color4 & nan()
Definition: Color4.cpp:81
float r
Definition: Color3.h:139
Color4 & operator*=(const Color4 &c)
Definition: Color4.h:124
static Color4 fromARGB(uint32)
Definition: Color4.cpp:112
static const Color4 & clear()
Definition: Color4.cpp:87
bool operator<=(const Color4 &) const
bool isFinite() const
Definition: Color4.h:173
Definition: Color3.h:33
float g
Definition: Color4.h:97
Definition: BinaryOutput.h:52
Color4 max(const Color4 &other) const
Definition: Color4.h:147
bool isZero() const
Definition: Color4.h:78
Color3 rgb() const
Definition: Color4.h:99
Color4 operator-() const
Definition: Color4.h:291
const FieldDescriptor value
Definition: descriptor.h:1522
uint32_t uint32
Definition: g3dmath.h:168
std::string toString() const
Definition: Color4.cpp:182
void deserialize(class BinaryInput &bi)
Definition: Color4.cpp:126
static const Color4 & zero()
Definition: Color4.cpp:69
Color4 operator*(float fScalar) const
Definition: Color4.h:285
bool fuzzyEq(double a, double b)
Definition: g3dmath.h:857
G3D::Color3 operator*(float s, const G3D::Color3 &c)
Definition: Color3.h:275
Definition: Color4.h:33
bool isFinite(double x)
Definition: g3dmath.h:525
float sum() const
Definition: Color4.h:156
bool operator>(const Color4 &) const
Color3 bgr() const
Definition: Color4.h:177
float b
Definition: Color3.h:139