CUGL 1.3
Cornell University Game Library
CUColor4.h
1 //
2 // CUColor4.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a standard colors. It provides both a
6 // float based color solution and a byte-based color solution. The former
7 // is better for blending and calculations. The later is better for storage.
8 //
9 // Because math objects are intended to be on the stack, we do not provide
10 // any shared pointer support in this class.
11 //
12 // This module is based on an original file from GamePlay3D: http://gameplay3d.org.
13 // It has been modified to support the CUGL framework.
14 //
15 // CUGL MIT License:
16 // This software is provided 'as-is', without any express or implied
17 // warranty. In no event will the authors be held liable for any damages
18 // arising from the use of this software.
19 //
20 // Permission is granted to anyone to use this software for any purpose,
21 // including commercial applications, and to alter it and redistribute it
22 // freely, subject to the following restrictions:
23 //
24 // 1. The origin of this software must not be misrepresented; you must not
25 // claim that you wrote the original software. If you use this software
26 // in a product, an acknowledgment in the product documentation would be
27 // appreciated but is not required.
28 //
29 // 2. Altered source versions must be plainly marked as such, and must not
30 // be misrepresented as being the original software.
31 //
32 // 3. This notice may not be removed or altered from any source distribution.
33 //
34 // Author: Walker White
35 // Version: 5/30/16
36 #ifndef __CU_COLOR4_H__
37 #define __CU_COLOR4_H__
38 
39 #include <math.h>
40 #include <functional>
41 #include <cugl/math/CUMathBase.h>
42 #include <cugl/base/CUEndian.h>
43 
45 #define COLOR_BYTE_TO_FLOAT(x) ( static_cast<float>(x)/255.0f )
46 
47 #define COLOR_FLOAT_TO_BYTE(x) ( static_cast<int>(roundf(x*255.0f)) )
48 
49 namespace cugl {
50 
51 // Forward references to the classes we convert to
52 class Color4;
53 class Vec3;
54 class Vec4;
55 
56 #pragma mark -
57 #pragma mark Color with Float Attributes
58 
73 class Color4f {
74 
75 #pragma mark Values
76 public:
78  GLfloat r;
80  GLfloat g;
82  GLfloat b;
84  GLfloat a;
85 
87  static const Color4f CLEAR;
89  static const Color4f WHITE;
91  static const Color4f BLACK;
93  static const Color4f YELLOW;
95  static const Color4f BLUE;
97  static const Color4f GREEN;
99  static const Color4f RED;
101  static const Color4f MAGENTA;
103  static const Color4f CYAN;
105  static const Color4f ORANGE;
107  static const Color4f GRAY;
109  static const Color4f CORNFLOWER;
111  static const Color4f PAPYRUS;
112 
113 #pragma mark Constructors
114 public:
118  Color4f() : r(0), g(0), b(0), a(0) {}
119 
130  Color4f(float r, float g, float b, float a = 1);
131 
140  Color4f(GLuint color);
141 
149  Color4f(const float* array);
150 
154  ~Color4f() {}
155 
156 
157 #pragma mark Setters
158 
168  Color4f& operator=(unsigned int color) {
169  return set(color);
170  }
171 
181  Color4f& operator=(const float* array) {
182  return set(array);
183  }
184 
197  Color4f& set(float r, float g, float b, float a = 1);
198 
208  Color4f& set(const float* array);
209 
220  Color4f& set(GLuint color);
221 
229  Color4f& set(const Color4f& c) {
230  r = c.r; g = c.g; b = c.b; a = c.a;
231  return *this;
232  }
233 
234 
235 #pragma mark Arithmetic
236 
244  Color4f& clamp(const Color4f& min, const Color4f& max);
245 
256  Color4f getClamp(const Color4f& min, const Color4f& max) const {
257  return Color4f(clampf(r,min.r,max.r), clampf(g,min.g,max.g),
258  clampf(b,min.b,max.b), clampf(a,min.a,max.a));
259  }
260 
272  Color4f& add(const Color4f& c, bool alpha = false) {
273  r = clampf(r+c.r,0.0,1.0);
274  g = clampf(g+c.g,0.0,1.0);
275  b = clampf(b+c.b,0.0,1.0);
276  a = (alpha ? clampf(a+c.a,0.0,1.0) : a);
277  return *this;
278  }
279 
292  Color4f& add(float r, float g, float b, float a = 0) {
293  this->r = clampf(this->r+r,0.0,1.0);
294  this->g = clampf(this->g+g,0.0,1.0);
295  this->b = clampf(this->b+b,0.0,1.0);
296  this->a = clampf(this->a+a,0.0,1.0);
297  return *this;
298  }
299 
311  Color4f& subtract(const Color4f& c, bool alpha = false) {
312  r = clampf(r-c.r,0.0,1.0);
313  g = clampf(g-c.g,0.0,1.0);
314  b = clampf(b-c.b,0.0,1.0);
315  a = (alpha ? clampf(a-c.a,0.0,1.0) : a);
316  return *this;
317  }
318 
331  Color4f& subtract(float r, float g, float b, float a = 0) {
332  this->r = clampf(this->r-r,0.0,1.0);
333  this->g = clampf(this->g-g,0.0,1.0);
334  this->b = clampf(this->b-b,0.0,1.0);
335  this->a = clampf(this->a-a,0.0,1.0);
336  return *this;
337  }
338 
349  Color4f& scale(float s, bool alpha = false) {
350  r = clampf(r*s,0.0,1.0);
351  g = clampf(g*s,0.0,1.0);
352  b = clampf(b*s,0.0,1.0);
353  a = (alpha ? clampf(a*s,0.0,1.0) : a);
354  return *this;
355  }
356 
369  Color4f& scale(float sr, float sg, float sb, float sa = 1) {
370  r = clampf(r*sr,0.0,1.0);
371  g = clampf(g*sg,0.0,1.0);
372  b = clampf(b*sb,0.0,1.0);
373  a = clampf(a*sa,0.0,1.0);
374  return *this;
375  }
376 
387  Color4f& scale(const Color4f& c, bool alpha = false) {
388  r *= c.r; g *= c.g; b *= c.b;
389  a = (alpha ? a*c.a : a);
390  return *this;
391  }
392 
405  Color4f& map(std::function<float(float)> func, bool alpha = false) {
406  r = clampf(func(r),0,1); g = clampf(func(g),0,1); b = clampf(func(b),0,1);
407  a = (alpha ? clampf(func(a),0,1) : a);
408  return *this;
409  }
410 
422  Color4f getMap(std::function<float(float)> func, bool alpha = false) const {
423  return Color4f(clampf(func(r),0,1), clampf(func(g),0,1),
424  clampf(func(b),0,1), (alpha ? clampf(func(a),0,1) : a));
425  }
426 
427 
428 #pragma mark Color Operations
429 
438  Color4f& complement(bool alpha = false) {
439  r = 1-r; g = 1-g; b = 1-b; a = (alpha ? 1-a : a);
440  return *this;
441  }
442 
454  Color4f getComplement(bool alpha = false) const {
455  return Color4f(1-r, 1-g, 1-b, (alpha ? 1-a : a));
456  }
457 
473  Color4f& lerp(const Color4f& other, float alpha) {
474  float x = clampf(alpha,0,1);
475  *this *= (1.f - x);
476  return *this += other * x;
477  }
478 
489  Color4f& blend(const Color4f& other) {
490  float a1 = a*(1-other.a);
491  float a2 = other.a+a1;
492  r = (other.r*other.a+r*a1)/a2;
493  g = (other.g*other.a+g*a1)/a2;
494  b = (other.b*other.a+b*a1)/a2;
495  a = a2;
496  return *this;
497  }
498 
509  Color4f& blendPre(const Color4f& other) {
510  a = other.a+a*(1-other.a);
511  r = other.r+r*(1-other.a);
512  g = other.g+g*(1-other.a);
513  b = other.b+b*(1-other.a);
514  return *this;
515  }
516 
527  r *= a; g *= a; b *= a;
528  return *this;
529  }
530 
543  if (a > 0) {
544  r /= a; g /= a; b /= a;
545  }
546  return *this;
547  }
548 
566  Color4f getLerp(const Color4f& other, float alpha) const {
567  float x = clampf(alpha,0,1);
568  return *this * (1.f - x) + other * x;
569  }
570 
583  Color4f getBlend(const Color4f& other) const {
584  float a1 = a*(1-other.a);
585  float a2 = other.a+a1;
586  return Color4f((other.r*other.a+r*a1)/a2,
587  (other.g*other.a+g*a1)/a2,
588  (other.b*other.a+b*a1)/a2,
589  a2);
590  }
591 
604  Color4f getBlendPre(const Color4f& other) const {
605  float oa = other.a+a*(1-other.a);
606  return Color4f((other.r+r*(1-other.a)), (other.g+g*(1-other.a)),
607  (other.b+b*(1-other.a)), oa);
608  }
609 
622  return Color4f(r*a,g*a,b*a,a);
623  }
624 
639  if (a > 0) {
640  return Color4f(r/a,g/a,b/a,a);
641  }
642  return *this;
643  }
644 
662  static Color4f* lerp(const Color4f& c1, const Color4f& c2, float alpha, Color4f* dst);
663 
677  static Color4f* blend(const Color4f& c1, const Color4f& c2, Color4f* dst);
678 
692  static Color4f* blendPre(const Color4f& c1, const Color4f& c2, Color4f* dst);
693 
705  GLuint getRGBA() const;
706 
707 #pragma mark Operators
708 
721  r = clampf(r+c.r,0.0,1.0);
722  g = clampf(g+c.g,0.0,1.0);
723  b = clampf(b+c.b,0.0,1.0);
724  a = clampf(a+c.a,0.0,1.0);
725  return *this;
726  }
727 
741  r = clampf(r-c.r,0.0,1.0);
742  g = clampf(g-c.g,0.0,1.0);
743  b = clampf(b-c.b,0.0,1.0);
744  a = clampf(a-c.a,0.0,1.0);
745  return *this;
746  }
747 
759  Color4f& operator*=(float s) {
760  r = clampf(r*s,0.0,1.0);
761  g = clampf(g*s,0.0,1.0);
762  b = clampf(b*s,0.0,1.0);
763  a = clampf(a*s,0.0,1.0);
764  return *this;
765  }
766 
777  r *= c.r; g *= c.g; b *= c.b; a *= c.a;
778  return *this;
779  }
780 
795  const Color4f operator+(const Color4f& c) const {
796  Color4f result(*this);
797  return result += c;
798  }
799 
814  const Color4f operator-(const Color4f& c) const {
815  Color4f result(*this);
816  return result -= c;
817  }
818 
830  const Color4f operator*(float s) const {
831  Color4f result(*this);
832  return result *= s;
833  }
834 
846  const Color4f operator*(const Color4f& c) const {
847  Color4f result(*this);
848  return result *= c;
849  }
850 
851 
852 #pragma mark Comparisons
853 
864  bool operator<(const Color4f& c) const;
865 
877  bool operator<=(const Color4f& c) const {
878  return *this < c || *this == c;
879  }
880 
892  bool operator>(const Color4f& c) const;
893 
905  bool operator>=(const Color4f& c) const {
906  return *this > c || *this == c;
907  }
908 
919  bool operator==(const Color4f& c) const {
920  return r == c.r && g == c.g && b == c.b && a == c.a;
921  }
922 
933  bool operator!=(const Color4f& c) const {
934  return r != c.r || g != c.g || b != c.b || a != c.a;
935  }
936 
948  bool darkerThan(const Color4f& c) const {
949  return r <= c.r && g <= c.g && b <= c.b && c.a <= a;
950  }
951 
964  bool lighterThan(const Color4f& c) const {
965  return r >= c.r && g >= c.g && b >= c.b && c.a >= a;
966  }
967 
978  bool equals(const Color4f& color, float variance=CU_MATH_EPSILON) const {
979  return (fabsf(r-color.r) < variance && fabsf(g-color.g) < variance &&
980  fabsf(b-color.b) < variance && fabsf(a-color.a) < variance);
981  }
982 
983 
984 #pragma mark Conversion Methods
985 public:
996  std::string toString(bool verbose = false) const;
997 
999  operator std::string() const { return toString(); }
1000 
1002  operator Vec4() const;
1003 
1011  explicit Color4f(const Vec4& vector);
1012 
1022  Color4f& operator= (const Vec4& vector);
1023 
1025  operator Vec3() const;
1026 
1034  explicit Color4f(const Vec3& vector);
1035 
1045  Color4f& operator= (const Vec3& vector);
1046 
1048  operator Color4() const;
1049 
1058  explicit Color4f(Color4 color);
1059 
1070  Color4f& operator= (Color4 color);
1071 };
1072 
1073 
1074 #pragma mark -
1075 #pragma mark Color with Byte Attributes
1076 
1084 class Color4 {
1085 
1086 #pragma mark Values
1087 public:
1088 union {
1089  struct {
1091  GLubyte r;
1093  GLubyte g;
1095  GLubyte b;
1097  GLubyte a;
1098  };
1100  Uint32 rgba;
1101 };
1102 
1104  static const Color4 CLEAR;
1106  static const Color4 WHITE;
1108  static const Color4 BLACK;
1110  static const Color4 YELLOW;
1112  static const Color4 BLUE;
1114  static const Color4 GREEN;
1116  static const Color4 RED;
1118  static const Color4 MAGENTA;
1120  static const Color4 CYAN;
1122  static const Color4 ORANGE;
1124  static const Color4 GRAY;
1126  static const Color4 CORNFLOWER;
1128  static const Color4 PAPYRUS;
1129 
1130 
1131 #pragma mark Constructors
1132 public:
1136  Color4() : rgba(0) {}
1137 
1146  Color4(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 255) {
1147  this->r = r; this->g = g; this->b = b; this->a = a;
1148  }
1149 
1159  Color4(GLuint color) { set(color); }
1160 
1169  Color4(const float* array);
1170 
1171 
1172 #pragma mark Setters
1173 
1184  Color4& operator=(GLuint color) {
1185  return set(color);
1186  }
1187 
1198  Color4& operator=(const float* array) {
1199  return set(array);
1200  }
1201 
1212  Color4& set(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 255) {
1213  this->r = r; this->g = g; this->b = b; this->a = a;
1214  return *this;
1215  }
1216 
1227  Color4& set(const float* array);
1228 
1239  Color4& set(GLuint color) {
1240  rgba = marshall(color);
1241  return *this;
1242  }
1243 
1251  Color4& set(const Color4& c) {
1252  rgba = c.rgba;
1253  return *this;
1254  }
1255 
1256 
1257 #pragma mark Arithmetic
1258 
1266  Color4& clamp(Color4 min, Color4 max);
1267 
1278  Color4 getClamp(Color4 min, Color4 max) const {
1279  return Color4(clampb(r,min.r,max.r), clampb(g,min.g,max.g),
1280  clampb(b,min.b,max.b), clampb(a,min.a,max.a));
1281  }
1282 
1283 
1295  Color4& add(Color4 c, bool alpha = false) {
1296  r = clampb(r+c.r,0,255);
1297  g = clampb(g+c.g,0,255);
1298  b = clampb(b+c.b,0,255);
1299  a = (alpha ? clampb(a+c.a,0,255) : a);
1300  return *this;
1301  }
1302 
1315  Color4& add(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 0) {
1316  this->r = clampb(this->r+r,0,255);
1317  this->g = clampb(this->g+g,0,255);
1318  this->b = clampb(this->b+b,0,255);
1319  this->a = clampb(this->a+a,0,255);
1320  return *this;
1321  }
1322 
1334  Color4& subtract(Color4 c, bool alpha = false) {
1335  r = clampb(r-c.r,0,255);
1336  g = clampb(g-c.g,0,255);
1337  b = clampb(b-c.b,0,255);
1338  a = (alpha ? clampb(a-c.a,0,255) : a);
1339  return *this;
1340  }
1341 
1354  Color4& subtract(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 0) {
1355  this->r = clampb(this->r-r,0,255);
1356  this->g = clampb(this->g-g,0,255);
1357  this->b = clampb(this->b-b,0,255);
1358  this->a = clampb(this->a-a,0,255);
1359  return *this;
1360  }
1361 
1372  Color4& scale(float s, bool alpha = false) {
1373  r = clampb(static_cast<GLubyte>(r*s),0,255);
1374  g = clampb(static_cast<GLubyte>(g*s),0,255);
1375  b = clampb(static_cast<GLubyte>(b*s),0,255);
1376  a = (alpha ? clampb(static_cast<GLubyte>(a*s),0,255) : a);
1377  return *this;
1378  }
1379 
1392  Color4& scale(float sr, float sg, float sb, float sa = 1) {
1393  r = clampb(static_cast<GLuint>(r*sr),0,255);
1394  g = clampb(static_cast<GLuint>(g*sg),0,255);
1395  b = clampb(static_cast<GLuint>(b*sb),0,255);
1396  a = clampb(static_cast<GLuint>(a*sa),0,255);
1397  return *this;
1398  }
1399 
1410  Color4& scale(Color4 c, bool alpha = false) {
1411  r = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.r)*r),0,255);
1412  g = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.g)*g),0,255);
1413  b = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.b)*b),0,255);
1414  a = (alpha ? clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.a)*a),0,255) : a);
1415  return *this;
1416  }
1417 
1430  Color4& map(std::function<GLubyte(GLubyte)> func, bool alpha = false) {
1431  r = func(r); g = func(g); b = func(b);
1432  a = (alpha ? func(a) : a);
1433  return *this;
1434  }
1435 
1448  Color4 getMap(std::function<GLubyte(GLubyte)> func, bool alpha = false) const {
1449  return Color4(func(r),func(g),func(b),(alpha ? func(a) : a));
1450  }
1451 
1452 
1453 #pragma mark Color4 Operations
1454 
1463  Color4& complement(bool alpha = false) {
1464  r = 255-r; g = 255-g; b = 255-b; a = (alpha ? 255-a : a);
1465  return *this;
1466  }
1467 
1479  Color4 getComplement(bool alpha = false) const {
1480  return Color4(255-r, 255-g, 255-b, (alpha ? 255-a : a));
1481  }
1482 
1498  Color4& lerp(const Color4f& other, float alpha) {
1499  float x = clampf(alpha,0,1);
1500  *this *= (1.f - x);
1501  return *this += other * x;
1502  }
1503 
1514  Color4& blend(Color4 other) {
1515  float srca = COLOR_BYTE_TO_FLOAT(other.a);
1516  float a1 = COLOR_BYTE_TO_FLOAT(a)*(1-srca);
1517  float a2 = srca+a1;
1518  r = clampb((GLubyte)((other.r*srca+r*a1)/a2),0,255);
1519  g = clampb((GLubyte)((other.g*srca+g*a1)/a2),0,255);
1520  b = clampb((GLubyte)((other.b*srca+b*a1)/a2),0,255);
1521  a = COLOR_FLOAT_TO_BYTE(a2);
1522  return *this;
1523  }
1524 
1536  float srca = COLOR_BYTE_TO_FLOAT(other.a);
1537  float a1 = srca+COLOR_BYTE_TO_FLOAT(a)*(1-srca);
1538  r = clampb((GLubyte)(other.r+r*(1-srca)),0,255);
1539  g = clampb((GLubyte)(other.g+g*(1-srca)),0,255);
1540  b = clampb((GLubyte)(other.b+b*(1-srca)),0,255);
1541  a = COLOR_FLOAT_TO_BYTE(a1);
1542  return *this;
1543  }
1544 
1555  scale(COLOR_BYTE_TO_FLOAT(a),false);
1556  return *this;
1557  }
1558 
1571  if (a > 0) {
1572  float a1 = 1/COLOR_BYTE_TO_FLOAT(a);
1573  scale(a1,false);
1574  }
1575  return *this;
1576  }
1577 
1595  Color4 getLerp(Color4 other, float alpha) const {
1596  float x = clampf(alpha,0,1);
1597  return *this * (1.f - x) + other * x;
1598  }
1599 
1612  Color4 getBlend(Color4 other) const {
1613  float srca = COLOR_BYTE_TO_FLOAT(other.a);
1614  float a1 = COLOR_BYTE_TO_FLOAT(a)*(1-srca);
1615  float a2 = srca+a1;
1616  return Color4(clampb((GLubyte)((other.r*srca+r*a1)/a2), 0, 255),
1617  clampb((GLubyte)((other.g*srca+g*a1)/a2), 0, 255),
1618  clampb((GLubyte)((other.b*srca+b*a1)/a2), 0, 255),
1619  COLOR_FLOAT_TO_BYTE(a2));
1620  }
1621 
1634  Color4f getBlendPre(Color4 other) const {
1635  float srca = COLOR_BYTE_TO_FLOAT(other.a);
1636  float dsta = COLOR_BYTE_TO_FLOAT(a);
1637  float a1 = srca+dsta*(1-srca);
1638  return Color4(clampb((GLubyte)(other.r+r*(1-srca)),0,255),
1639  clampb((GLubyte)(other.g+g*(1-srca)),0,255),
1640  clampb((GLubyte)(other.b+b*(1-srca)),0,255),
1641  COLOR_FLOAT_TO_BYTE(a1));
1642  }
1643 
1656  Color4 result(r,g,b,255);
1657  result *= COLOR_BYTE_TO_FLOAT(a);
1658  return result;
1659  }
1660 
1675  if (a > 0) {
1676  float a1 = 1/COLOR_BYTE_TO_FLOAT(a);
1677  Color4 result(*this);
1678  return result.scale(a1,false);
1679  }
1680  return *this;
1681  }
1682 
1700  static Color4* lerp(Color4 c1, Color4 c2, float alpha, Color4* dst);
1701 
1715  static Color4* blend(Color4 c1, Color4 c2, Color4* dst);
1716 
1730  static Color4* blendPre(Color4 c1, Color4 c2, Color4* dst);
1731 
1744  GLuint getRGBA() const {
1745  return marshall(rgba);
1746  }
1747 
1748 #pragma mark Operators
1749 
1762  r = clampb(r+c.r,0,255);
1763  g = clampb(g+c.g,0,255);
1764  b = clampb(b+c.b,0,255);
1765  a = clampb(a+c.a,0,255);
1766  return *this;
1767  }
1768 
1782  r = clampb(r-c.r,0,255);
1783  g = clampb(g-c.g,0,255);
1784  b = clampb(b-c.b,0,255);
1785  a = clampb(a-c.a,0,255);
1786  return *this;
1787  }
1788 
1800  Color4& operator*=(float s) {
1801  r = clampb(static_cast<GLubyte>(r*s),0,255);
1802  g = clampb(static_cast<GLubyte>(g*s),0,255);
1803  b = clampb(static_cast<GLubyte>(b*s),0,255);
1804  a = clampb(static_cast<GLubyte>(a*s),0,255);
1805  return *this;
1806  }
1807 
1818  r = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.r)*r),0,255);
1819  g = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.g)*g),0,255);
1820  b = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.b)*b),0,255);
1821  a = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.a)*a),0,255);
1822  return *this;
1823  }
1824 
1839  const Color4 operator+(Color4 c) const {
1840  Color4 result(*this);
1841  return result += c;
1842  }
1843 
1858  const Color4 operator-(Color4 c) const {
1859  Color4 result(*this);
1860  return result -= c;
1861  }
1862 
1876  const Color4 operator*(float s) const {
1877  Color4 result(*this);
1878  return result *= s;
1879  }
1880 
1892  const Color4 operator*(Color4 c) const {
1893  Color4 result(*this);
1894  return result *= c;
1895  }
1896 
1897 
1898 #pragma mark Comparisons
1899 
1910  bool operator<(Color4 c) const;
1911 
1923  bool operator<=(Color4 c) const {
1924  return *this < c || rgba == c.rgba;
1925  }
1926 
1938  bool operator>(Color4 c) const;
1939 
1951  bool operator>=(Color4 c) const {
1952  return *this > c || rgba == c.rgba;
1953  }
1954 
1962  bool operator==(Color4 c) const {
1963  return rgba == c.rgba;
1964  }
1965 
1976  bool operator!=(Color4 c) const {
1977  return rgba != c.rgba;
1978  }
1979 
1991  bool darkerThan(Color4 c) const {
1992  return r <= c.r && g <= c.g && b <= c.b && c.a <= a;
1993  }
1994 
2007  bool lighterThan(Color4 c) const {
2008  return r >= c.r && g >= c.g && b >= c.b && c.a >= a;
2009  }
2010 
2011 #pragma mark Conversion Methods
2012 public:
2023  std::string toString(bool verbose = false) const;
2024 
2026  operator std::string() const { return toString(); }
2027 
2033  operator Vec4() const;
2034 
2043  explicit Color4(const Vec4& vector);
2044 
2055  Color4& operator= (const Vec4& vector);
2056 
2062  operator Vec3() const;
2063 
2072  explicit Color4(const Vec3& vector);
2073 
2084  Color4& operator= (const Vec3& vector);
2085 
2087  operator Color4f() const;
2088 
2097  explicit Color4(const Color4f& color);
2098 
2109  Color4& operator= (const Color4f& color);
2110 };
2111 
2112 #pragma mark -
2113 #pragma mark Friend Operations
2114 
2127 inline const Color4f operator*(float s, const Color4f& c) {
2128  Color4f result(c);
2129  return result *= s;
2130 }
2131 
2144 inline const Color4 operator*(float s, Color4 c) {
2145  Color4 result(c);
2146  return result *= s;
2147 }
2148 
2149 }
2150 
2151 #endif /* __CU_COLOR4_H__ */
cugl::Color4f::getBlendPre
Color4f getBlendPre(const Color4f &other) const
Definition: CUColor4.h:604
cugl::Color4f::equals
bool equals(const Color4f &color, float variance=CU_MATH_EPSILON) const
Definition: CUColor4.h:978
cugl::Color4f::BLUE
static const Color4f BLUE
Definition: CUColor4.h:95
cugl::Color4::operator*=
Color4 & operator*=(Color4 c)
Definition: CUColor4.h:1817
cugl::Color4f::premultiply
Color4f & premultiply()
Definition: CUColor4.h:526
cugl::Color4f::getClamp
Color4f getClamp(const Color4f &min, const Color4f &max) const
Definition: CUColor4.h:256
cugl::Color4f::b
GLfloat b
Definition: CUColor4.h:82
cugl::Color4f::getMap
Color4f getMap(std::function< float(float)> func, bool alpha=false) const
Definition: CUColor4.h:422
cugl::Color4::operator*
const Color4 operator*(float s) const
Definition: CUColor4.h:1876
cugl::Color4f::add
Color4f & add(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:272
cugl::Color4f::operator=
Color4f & operator=(unsigned int color)
Definition: CUColor4.h:168
cugl::Color4f::operator*
const Color4f operator*(float s) const
Definition: CUColor4.h:830
cugl::Color4f::complement
Color4f & complement(bool alpha=false)
Definition: CUColor4.h:438
cugl::Vec4
Definition: CUVec4.h:64
cugl::Color4::add
Color4 & add(Color4 c, bool alpha=false)
Definition: CUColor4.h:1295
cugl::Color4f::MAGENTA
static const Color4f MAGENTA
Definition: CUColor4.h:101
cugl::Color4f::Color4f
Color4f()
Definition: CUColor4.h:118
cugl::Color4f::subtract
Color4f & subtract(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:311
cugl::Color4f::clamp
Color4f & clamp(const Color4f &min, const Color4f &max)
cugl::Color4::a
GLubyte a
Definition: CUColor4.h:1097
cugl::Color4f::scale
Color4f & scale(float s, bool alpha=false)
Definition: CUColor4.h:349
cugl::Color4f::operator+
const Color4f operator+(const Color4f &c) const
Definition: CUColor4.h:795
cugl::Color4::set
Color4 & set(const Color4 &c)
Definition: CUColor4.h:1251
cugl::Color4f::getPremultiplied
Color4f getPremultiplied() const
Definition: CUColor4.h:621
cugl::Color4::scale
Color4 & scale(float sr, float sg, float sb, float sa=1)
Definition: CUColor4.h:1392
cugl::Color4::lighterThan
bool lighterThan(Color4 c) const
Definition: CUColor4.h:2007
cugl::Color4f::WHITE
static const Color4f WHITE
Definition: CUColor4.h:89
cugl::Color4f::blend
Color4f & blend(const Color4f &other)
Definition: CUColor4.h:489
cugl::Color4f::toString
std::string toString(bool verbose=false) const
cugl::Color4::operator<=
bool operator<=(Color4 c) const
Definition: CUColor4.h:1923
cugl::Color4f::subtract
Color4f & subtract(float r, float g, float b, float a=0)
Definition: CUColor4.h:331
cugl::Color4::RED
static const Color4 RED
Definition: CUColor4.h:1116
cugl::Color4f::RED
static const Color4f RED
Definition: CUColor4.h:99
cugl::Color4f::CLEAR
static const Color4f CLEAR
Definition: CUColor4.h:87
cugl::Color4::MAGENTA
static const Color4 MAGENTA
Definition: CUColor4.h:1118
cugl::Color4f::GREEN
static const Color4f GREEN
Definition: CUColor4.h:97
cugl::Color4
Definition: CUColor4.h:1084
cugl::Color4::CLEAR
static const Color4 CLEAR
Definition: CUColor4.h:1104
cugl::Color4f::operator!=
bool operator!=(const Color4f &c) const
Definition: CUColor4.h:933
cugl::Color4::operator>=
bool operator>=(Color4 c) const
Definition: CUColor4.h:1951
cugl::Color4f::operator>=
bool operator>=(const Color4f &c) const
Definition: CUColor4.h:905
cugl::Color4::GRAY
static const Color4 GRAY
Definition: CUColor4.h:1124
cugl::Color4f::ORANGE
static const Color4f ORANGE
Definition: CUColor4.h:105
cugl::Color4::Color4
Color4()
Definition: CUColor4.h:1136
cugl::Color4::rgba
Uint32 rgba
Definition: CUColor4.h:1100
cugl::Color4::getBlend
Color4 getBlend(Color4 other) const
Definition: CUColor4.h:1612
cugl::Color4::operator=
Color4 & operator=(GLuint color)
Definition: CUColor4.h:1184
cugl::Color4::premultiply
Color4 & premultiply()
Definition: CUColor4.h:1554
cugl::Color4::CYAN
static const Color4 CYAN
Definition: CUColor4.h:1120
cugl::Color4f::set
Color4f & set(const Color4f &c)
Definition: CUColor4.h:229
cugl::Color4f::r
GLfloat r
Definition: CUColor4.h:78
cugl::Color4::getRGBA
GLuint getRGBA() const
Definition: CUColor4.h:1744
cugl::Color4f::CORNFLOWER
static const Color4f CORNFLOWER
Definition: CUColor4.h:109
cugl::Color4::g
GLubyte g
Definition: CUColor4.h:1093
cugl::Color4::BLACK
static const Color4 BLACK
Definition: CUColor4.h:1108
cugl::Color4::operator*=
Color4 & operator*=(float s)
Definition: CUColor4.h:1800
cugl::Color4::darkerThan
bool darkerThan(Color4 c) const
Definition: CUColor4.h:1991
cugl::Color4::scale
Color4 & scale(Color4 c, bool alpha=false)
Definition: CUColor4.h:1410
cugl::Color4::getPremultiplied
Color4 getPremultiplied() const
Definition: CUColor4.h:1655
cugl::Color4::set
Color4 & set(GLubyte r, GLubyte g, GLubyte b, GLubyte a=255)
Definition: CUColor4.h:1212
cugl::Color4::PAPYRUS
static const Color4 PAPYRUS
Definition: CUColor4.h:1128
cugl::Color4::operator+
const Color4 operator+(Color4 c) const
Definition: CUColor4.h:1839
cugl::Color4f::operator<=
bool operator<=(const Color4f &c) const
Definition: CUColor4.h:877
cugl::Color4::complement
Color4 & complement(bool alpha=false)
Definition: CUColor4.h:1463
cugl::Color4::YELLOW
static const Color4 YELLOW
Definition: CUColor4.h:1110
cugl::Color4::GREEN
static const Color4 GREEN
Definition: CUColor4.h:1114
cugl::Color4f::getRGBA
GLuint getRGBA() const
cugl::Color4f::lighterThan
bool lighterThan(const Color4f &c) const
Definition: CUColor4.h:964
cugl::Color4f::BLACK
static const Color4f BLACK
Definition: CUColor4.h:91
cugl::Color4f::getComplement
Color4f getComplement(bool alpha=false) const
Definition: CUColor4.h:454
cugl::Color4f::scale
Color4f & scale(float sr, float sg, float sb, float sa=1)
Definition: CUColor4.h:369
cugl::Color4::getMap
Color4 getMap(std::function< GLubyte(GLubyte)> func, bool alpha=false) const
Definition: CUColor4.h:1448
cugl::Color4::Color4
Color4(GLubyte r, GLubyte g, GLubyte b, GLubyte a=255)
Definition: CUColor4.h:1146
cugl::Color4::operator-=
Color4 & operator-=(Color4 c)
Definition: CUColor4.h:1781
cugl::Color4f::getLerp
Color4f getLerp(const Color4f &other, float alpha) const
Definition: CUColor4.h:566
cugl::Color4f::getBlend
Color4f getBlend(const Color4f &other) const
Definition: CUColor4.h:583
cugl::Color4::getLerp
Color4 getLerp(Color4 other, float alpha) const
Definition: CUColor4.h:1595
cugl::Color4f::g
GLfloat g
Definition: CUColor4.h:80
cugl::Color4f::operator>
bool operator>(const Color4f &c) const
cugl::Color4f::scale
Color4f & scale(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:387
cugl::Color4f::operator<
bool operator<(const Color4f &c) const
cugl::Color4f
Definition: CUColor4.h:73
cugl::Color4::operator==
bool operator==(Color4 c) const
Definition: CUColor4.h:1962
cugl::Color4::getComplement
Color4 getComplement(bool alpha=false) const
Definition: CUColor4.h:1479
cugl::Color4f::operator=
Color4f & operator=(const float *array)
Definition: CUColor4.h:181
cugl::Color4::getClamp
Color4 getClamp(Color4 min, Color4 max) const
Definition: CUColor4.h:1278
cugl::Color4f::operator*
const Color4f operator*(const Color4f &c) const
Definition: CUColor4.h:846
cugl::Color4::operator=
Color4 & operator=(const float *array)
Definition: CUColor4.h:1198
cugl::Color4::blendPre
Color4 & blendPre(Color4 other)
Definition: CUColor4.h:1535
cugl::Color4f::operator*=
Color4f & operator*=(float s)
Definition: CUColor4.h:759
cugl::Color4::subtract
Color4 & subtract(GLubyte r, GLubyte g, GLubyte b, GLubyte a=0)
Definition: CUColor4.h:1354
cugl::Color4f::getUnpremultiplied
Color4f getUnpremultiplied() const
Definition: CUColor4.h:638
cugl::Color4::WHITE
static const Color4 WHITE
Definition: CUColor4.h:1106
cugl::Color4f::GRAY
static const Color4f GRAY
Definition: CUColor4.h:107
cugl::Color4::operator*
const Color4 operator*(Color4 c) const
Definition: CUColor4.h:1892
cugl::Color4f::YELLOW
static const Color4f YELLOW
Definition: CUColor4.h:93
cugl::Color4f::a
GLfloat a
Definition: CUColor4.h:84
cugl::Color4::getUnpremultiplied
Color4 getUnpremultiplied() const
Definition: CUColor4.h:1674
cugl::Color4f::map
Color4f & map(std::function< float(float)> func, bool alpha=false)
Definition: CUColor4.h:405
cugl::Color4::r
GLubyte r
Definition: CUColor4.h:1091
cugl::Color4f::add
Color4f & add(float r, float g, float b, float a=0)
Definition: CUColor4.h:292
cugl::Color4::getBlendPre
Color4f getBlendPre(Color4 other) const
Definition: CUColor4.h:1634
cugl::Color4::operator-
const Color4 operator-(Color4 c) const
Definition: CUColor4.h:1858
cugl::Color4f::lerp
Color4f & lerp(const Color4f &other, float alpha)
Definition: CUColor4.h:473
cugl::Color4f::operator-
const Color4f operator-(const Color4f &c) const
Definition: CUColor4.h:814
cugl::Color4f::CYAN
static const Color4f CYAN
Definition: CUColor4.h:103
cugl::Color4::unpremultiply
Color4 & unpremultiply()
Definition: CUColor4.h:1570
cugl::Color4::toString
std::string toString(bool verbose=false) const
cugl::Color4f::set
Color4f & set(float r, float g, float b, float a=1)
cugl::Color4::operator<
bool operator<(Color4 c) const
cugl::Color4f::operator==
bool operator==(const Color4f &c) const
Definition: CUColor4.h:919
cugl::Color4::add
Color4 & add(GLubyte r, GLubyte g, GLubyte b, GLubyte a=0)
Definition: CUColor4.h:1315
cugl::Color4::Color4
Color4(GLuint color)
Definition: CUColor4.h:1159
cugl::Color4f::blendPre
Color4f & blendPre(const Color4f &other)
Definition: CUColor4.h:509
cugl::Color4f::~Color4f
~Color4f()
Definition: CUColor4.h:154
cugl::Color4::operator!=
bool operator!=(Color4 c) const
Definition: CUColor4.h:1976
cugl::Color4::blend
Color4 & blend(Color4 other)
Definition: CUColor4.h:1514
cugl::Color4::ORANGE
static const Color4 ORANGE
Definition: CUColor4.h:1122
cugl::Color4::subtract
Color4 & subtract(Color4 c, bool alpha=false)
Definition: CUColor4.h:1334
cugl::Color4f::operator-=
Color4f & operator-=(const Color4f &c)
Definition: CUColor4.h:740
cugl::Vec3
Definition: CUVec3.h:61
cugl::Color4::scale
Color4 & scale(float s, bool alpha=false)
Definition: CUColor4.h:1372
cugl::Color4f::darkerThan
bool darkerThan(const Color4f &c) const
Definition: CUColor4.h:948
cugl::Color4f::PAPYRUS
static const Color4f PAPYRUS
Definition: CUColor4.h:111
cugl::Color4::operator+=
Color4 & operator+=(Color4 c)
Definition: CUColor4.h:1761
cugl::Color4::b
GLubyte b
Definition: CUColor4.h:1095
cugl::Color4::lerp
Color4 & lerp(const Color4f &other, float alpha)
Definition: CUColor4.h:1498
cugl::Color4::map
Color4 & map(std::function< GLubyte(GLubyte)> func, bool alpha=false)
Definition: CUColor4.h:1430
cugl::Color4::set
Color4 & set(GLuint color)
Definition: CUColor4.h:1239
cugl::Color4f::unpremultiply
Color4f & unpremultiply()
Definition: CUColor4.h:542
cugl::Color4::BLUE
static const Color4 BLUE
Definition: CUColor4.h:1112
cugl::Color4::clamp
Color4 & clamp(Color4 min, Color4 max)
cugl::Color4f::operator+=
Color4f & operator+=(const Color4f &c)
Definition: CUColor4.h:720
cugl::Color4f::operator*=
Color4f & operator*=(const Color4f &c)
Definition: CUColor4.h:776
cugl::Color4::operator>
bool operator>(Color4 c) const
cugl::Color4::CORNFLOWER
static const Color4 CORNFLOWER
Definition: CUColor4.h:1126