36 #ifndef __CU_COLOR4_H__
37 #define __CU_COLOR4_H__
41 #include "CUMathBase.h"
42 #include "../base/CUEndian.h"
45 #define COLOR_BYTE_TO_FLOAT(x) ( static_cast<float>(x)/255.0f )
47 #define COLOR_FLOAT_TO_BYTE(x) ( static_cast<int>(roundf(x*255.0f)) )
57 #pragma mark Color with Float Attributes
113 #pragma mark Constructors
130 Color4f(
float r,
float g,
float b,
float a = 1);
148 r = copy.
r; g = copy.
g; b = copy.
b; a = copy.
a;
217 Color4f&
set(
float r,
float g,
float b,
float a = 1);
250 r = c.
r; g = c.
g; b = c.
b; a = c.
a;
255 #pragma mark Arithmetic
277 return Color4f(clampf(r,min.
r,max.
r), clampf(g,min.
g,max.
g),
278 clampf(b,min.
b,max.
b), clampf(a,min.
a,max.
a));
293 r = clampf(r+c.
r,0.0,1.0);
294 g = clampf(g+c.
g,0.0,1.0);
295 b = clampf(b+c.
b,0.0,1.0);
296 a = (alpha ? clampf(a+c.
a,0.0,1.0) :
a);
313 this->r = clampf(this->r+r,0.0,1.0);
314 this->g = clampf(this->g+g,0.0,1.0);
315 this->b = clampf(this->b+b,0.0,1.0);
316 this->a = clampf(this->a+a,0.0,1.0);
332 r = clampf(r-c.
r,0.0,1.0);
333 g = clampf(g-c.
g,0.0,1.0);
334 b = clampf(b-c.
b,0.0,1.0);
335 a = (alpha ? clampf(a-c.
a,0.0,1.0) :
a);
352 this->r = clampf(this->r-r,0.0,1.0);
353 this->g = clampf(this->g-g,0.0,1.0);
354 this->b = clampf(this->b-b,0.0,1.0);
355 this->a = clampf(this->a-a,0.0,1.0);
370 r = clampf(r*s,0.0,1.0);
371 g = clampf(g*s,0.0,1.0);
372 b = clampf(b*s,0.0,1.0);
373 a = (alpha ? clampf(a*s,0.0,1.0) :
a);
390 r = clampf(r*sr,0.0,1.0);
391 g = clampf(g*sg,0.0,1.0);
392 b = clampf(b*sb,0.0,1.0);
393 a = clampf(a*sa,0.0,1.0);
408 r *= c.
r; g *= c.
g; b *= c.
b;
409 a = (alpha ? a*c.
a :
a);
425 Color4f&
map(std::function<
float(
float)> func,
bool alpha =
false) {
426 r = clampf(func(r),0,1); g = clampf(func(g),0,1); b = clampf(func(b),0,1);
427 a = (alpha ? clampf(func(a),0,1) :
a);
442 Color4f getMap(std::function<
float(
float)> func,
bool alpha =
false)
const {
443 return Color4f(clampf(func(r),0,1), clampf(func(g),0,1),
444 clampf(func(b),0,1), (alpha ? clampf(func(a),0,1) : a));
448 #pragma mark Color Operations
459 r = 1-
r; g = 1-
g; b = 1-
b; a = (alpha ? 1-a :
a);
475 return Color4f(1-r, 1-g, 1-b, (alpha ? 1-a : a));
494 float x = clampf(alpha,0,1);
496 return *
this += other * x;
510 float a1 = a*(1-other.
a);
511 float a2 = other.
a+a1;
512 r = (other.
r*other.
a+r*a1)/a2;
513 g = (other.
g*other.
a+g*a1)/a2;
514 b = (other.
b*other.
a+b*a1)/a2;
530 a = other.
a+a*(1-other.
a);
531 r = other.
r+r*(1-other.
a);
532 g = other.
g+g*(1-other.
a);
533 b = other.
b+b*(1-other.
a);
547 r *=
a; g *=
a; b *=
a;
564 r /=
a; g /=
a; b /=
a;
587 float x = clampf(alpha,0,1);
588 return *
this * (1.f - x) + other * x;
604 float a1 = a*(1-other.
a);
605 float a2 = other.
a+a1;
606 return Color4f((other.
r*other.
a+r*a1)/a2,
607 (other.
g*other.
a+g*a1)/a2,
608 (other.
b*other.
a+b*a1)/a2,
625 float oa = other.
a+a*(1-other.
a);
626 return Color4f((other.
r+r*(1-other.
a)), (other.
g+g*(1-other.
a)),
627 (other.
b+b*(1-other.
a)), oa);
727 #pragma mark Operators
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);
761 r = clampf(r-c.
r,0.0,1.0);
762 g = clampf(g-c.
g,0.0,1.0);
763 b = clampf(b-c.
b,0.0,1.0);
764 a = clampf(a-c.
a,0.0,1.0);
780 r = clampf(r*s,0.0,1.0);
781 g = clampf(g*s,0.0,1.0);
782 b = clampf(b*s,0.0,1.0);
783 a = clampf(a*s,0.0,1.0);
797 r *= c.
r; g *= c.
g; b *= c.
b; a *= c.
a;
872 #pragma mark Comparisons
898 return *
this < c || *
this == c;
926 return *
this > c || *
this == c;
940 return r == c.
r && g == c.
g && b == c.
b && a == c.
a;
954 return r != c.
r || g != c.
g || b != c.
b || a != c.
a;
969 return r <= c.
r && g <= c.
g && b <= c.
b && c.
a <=
a;
985 return r >= c.
r && g >= c.
g && b >= c.
b && c.
a >=
a;
999 return (fabsf(r-color.
r) < variance && fabsf(g-color.
g) < variance &&
1000 fabsf(b-color.
b) < variance && fabsf(a-color.
a) < variance);
1004 #pragma mark Conversion Methods
1016 std::string
toString(
bool verbose =
false)
const;
1022 operator Vec4()
const;
1045 operator Vec3()
const;
1095 #pragma mark Color with Byte Attributes
1151 #pragma mark Constructors
1166 Color4(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 255) {
1167 this->r =
r; this->g =
g; this->b =
b; this->a =
a;
1189 Color4(
const float* array);
1197 #pragma mark Setters
1237 Color4&
set(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 255) {
1238 this->r =
r; this->g =
g; this->b =
b; this->a =
a;
1282 #pragma mark Arithmetic
1304 return Color4(clampb(r,min.
r,max.
r), clampb(g,min.
g,max.
g),
1305 clampb(b,min.
b,max.
b), clampb(a,min.
a,max.
a));
1321 r = clampb(r+c.
r,0,255);
1322 g = clampb(g+c.
g,0,255);
1323 b = clampb(b+c.
b,0,255);
1324 a = (alpha ? clampb(a+c.
a,0,255) :
a);
1340 Color4&
add(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 0) {
1341 this->r = clampb(this->r+r,0,255);
1342 this->g = clampb(this->g+g,0,255);
1343 this->b = clampb(this->b+b,0,255);
1344 this->a = clampb(this->a+a,0,255);
1360 r = clampb(r-c.
r,0,255);
1361 g = clampb(g-c.
g,0,255);
1362 b = clampb(b-c.
b,0,255);
1363 a = (alpha ? clampb(a-c.
a,0,255) :
a);
1380 this->r = clampb(this->r-r,0,255);
1381 this->g = clampb(this->g-g,0,255);
1382 this->b = clampb(this->b-b,0,255);
1383 this->a = clampb(this->a-a,0,255);
1398 r = clampb(static_cast<GLubyte>(r*s),0,255);
1399 g = clampb(static_cast<GLubyte>(g*s),0,255);
1400 b = clampb(static_cast<GLubyte>(b*s),0,255);
1401 a = (alpha ? clampb(static_cast<GLubyte>(a*s),0,255) :
a);
1418 r = clampb(static_cast<GLuint>(r*sr),0,255);
1419 g = clampb(static_cast<GLuint>(g*sg),0,255);
1420 b = clampb(static_cast<GLuint>(b*sb),0,255);
1421 a = clampb(static_cast<GLuint>(a*sa),0,255);
1436 r = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
r)*r),0,255);
1437 g = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
g)*g),0,255);
1438 b = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
b)*b),0,255);
1439 a = (alpha ? clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
a)*a),0,255) :
a);
1455 Color4&
map(std::function<GLubyte(GLubyte)> func,
bool alpha =
false) {
1456 r = func(r); g = func(g); b = func(b);
1457 a = (alpha ? func(a) :
a);
1473 Color4 getMap(std::function<GLubyte(GLubyte)> func,
bool alpha =
false)
const {
1474 return Color4(func(r),func(g),func(b),(alpha ? func(a) : a));
1478 #pragma mark Color4 Operations
1489 r = 255-
r; g = 255-
g; b = 255-
b; a = (alpha ? 255-a :
a);
1505 return Color4(255-r, 255-g, 255-b, (alpha ? 255-a : a));
1524 float x = clampf(alpha,0,1);
1526 return *
this += other * x;
1540 float srca = COLOR_BYTE_TO_FLOAT(other.
a);
1541 float a1 = COLOR_BYTE_TO_FLOAT(a)*(1-srca);
1543 r = clampb((GLubyte)((other.
r*srca+r*a1)/a2),0,255);
1544 g = clampb((GLubyte)((other.
g*srca+g*a1)/a2),0,255);
1545 b = clampb((GLubyte)((other.
b*srca+b*a1)/a2),0,255);
1546 a = COLOR_FLOAT_TO_BYTE(a2);
1561 float srca = COLOR_BYTE_TO_FLOAT(other.
a);
1562 float a1 = srca+COLOR_BYTE_TO_FLOAT(a)*(1-srca);
1563 r = clampb((GLubyte)(other.
r+r*(1-srca)),0,255);
1564 g = clampb((GLubyte)(other.
g+g*(1-srca)),0,255);
1565 b = clampb((GLubyte)(other.
b+b*(1-srca)),0,255);
1566 a = COLOR_FLOAT_TO_BYTE(a1);
1580 scale(COLOR_BYTE_TO_FLOAT(a),
false);
1597 float a1 = 1/COLOR_BYTE_TO_FLOAT(a);
1621 float x = clampf(alpha,0,1);
1622 return *
this * (1.f - x) + other * x;
1638 float srca = COLOR_BYTE_TO_FLOAT(other.
a);
1639 float a1 = COLOR_BYTE_TO_FLOAT(a)*(1-srca);
1641 return Color4(clampb((GLubyte)((other.
r*srca+r*a1)/a2), 0, 255),
1642 clampb((GLubyte)((other.
g*srca+g*a1)/a2), 0, 255),
1643 clampb((GLubyte)((other.
b*srca+b*a1)/a2), 0, 255),
1644 COLOR_FLOAT_TO_BYTE(a2));
1660 float srca = COLOR_BYTE_TO_FLOAT(other.
a);
1661 float dsta = COLOR_BYTE_TO_FLOAT(a);
1662 float a1 = srca+dsta*(1-srca);
1663 return Color4(clampb((GLubyte)(other.
r+r*(1-srca)),0,255),
1664 clampb((GLubyte)(other.
g+g*(1-srca)),0,255),
1665 clampb((GLubyte)(other.
b+b*(1-srca)),0,255),
1666 COLOR_FLOAT_TO_BYTE(a1));
1681 Color4 result(r,g,b,255);
1682 result *= COLOR_BYTE_TO_FLOAT(a);
1701 float a1 = 1/COLOR_BYTE_TO_FLOAT(a);
1703 return result.
scale(a1,
false);
1773 #pragma mark Operators
1787 r = clampb(r+c.
r,0,255);
1788 g = clampb(g+c.
g,0,255);
1789 b = clampb(b+c.
b,0,255);
1790 a = clampb(a+c.
a,0,255);
1807 r = clampb(r-c.
r,0,255);
1808 g = clampb(g-c.
g,0,255);
1809 b = clampb(b-c.
b,0,255);
1810 a = clampb(a-c.
a,0,255);
1826 r = clampb(static_cast<GLubyte>(r*s),0,255);
1827 g = clampb(static_cast<GLubyte>(g*s),0,255);
1828 b = clampb(static_cast<GLubyte>(b*s),0,255);
1829 a = clampb(static_cast<GLubyte>(a*s),0,255);
1843 r = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
r)*r),0,255);
1844 g = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
g)*g),0,255);
1845 b = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
b)*b),0,255);
1846 a = clampb(static_cast<GLuint>(COLOR_BYTE_TO_FLOAT(c.
a)*a),0,255);
1923 #pragma mark Comparisons
1949 return *
this < c ||
rgba == c.
rgba;
1977 return *
this > c ||
rgba == c.
rgba;
2017 return r <= c.
r && g <= c.
g && b <= c.
b && c.
a <=
a;
2033 return r >= c.
r && g >= c.
g && b >= c.
b && c.
a >=
a;
2036 #pragma mark Conversion Methods
2048 std::string
toString(
bool verbose =
false)
const;
2058 operator Vec4()
const;
2087 operator Vec3()
const;
2138 #pragma mark Friend Operations
static const Color4 CORNFLOWER
Definition: CUColor4.h:1146
Color4f & clamp(const Color4f &min, const Color4f &max)
static const Color4f BLACK
Definition: CUColor4.h:91
Color4 & subtract(GLubyte r, GLubyte g, GLubyte b, GLubyte a=0)
Definition: CUColor4.h:1379
Color4f getPremultiplied() const
Definition: CUColor4.h:641
Color4 & set(GLuint color)
Definition: CUColor4.h:1264
Color4f getClamp(const Color4f &min, const Color4f &max) const
Definition: CUColor4.h:276
Color4 getBlend(Color4 other) const
Definition: CUColor4.h:1637
Color4f & blendPre(const Color4f &other)
Definition: CUColor4.h:529
static const Color4 YELLOW
Definition: CUColor4.h:1130
Color4f getBlend(const Color4f &other) const
Definition: CUColor4.h:603
Color4 getUnpremultiplied() const
Definition: CUColor4.h:1699
Color4 & add(Color4 c, bool alpha=false)
Definition: CUColor4.h:1320
bool lighterThan(const Color4f &c) const
Definition: CUColor4.h:984
~Color4()
Definition: CUColor4.h:1194
Color4f & lerp(const Color4f &other, float alpha)
Definition: CUColor4.h:493
Color4 & set(const Color4 &c)
Definition: CUColor4.h:1276
bool operator==(Color4 c) const
Definition: CUColor4.h:1987
bool darkerThan(const Color4f &c) const
Definition: CUColor4.h:968
Color4(GLubyte r, GLubyte g, GLubyte b, GLubyte a=255)
Definition: CUColor4.h:1166
~Color4f()
Definition: CUColor4.h:163
const Color4f operator+(const Color4f &c) const
Definition: CUColor4.h:815
Color4f getMap(std::function< float(float)> func, bool alpha=false) const
Definition: CUColor4.h:442
const Color4f operator-(const Color4f &c) const
Definition: CUColor4.h:834
GLfloat g
Definition: CUColor4.h:80
static const Color4f ORANGE
Definition: CUColor4.h:105
Color4f & operator*=(const Color4f &c)
Definition: CUColor4.h:796
Color4f getBlendPre(const Color4f &other) const
Definition: CUColor4.h:624
GLfloat a
Definition: CUColor4.h:84
bool equals(const Color4f &color, float variance=CU_MATH_EPSILON) const
Definition: CUColor4.h:998
Color4f & map(std::function< float(float)> func, bool alpha=false)
Definition: CUColor4.h:425
static const Color4 BLACK
Definition: CUColor4.h:1128
Color4 & map(std::function< GLubyte(GLubyte)> func, bool alpha=false)
Definition: CUColor4.h:1455
GLubyte r
Definition: CUColor4.h:1111
Color4 & scale(Color4 c, bool alpha=false)
Definition: CUColor4.h:1435
bool operator==(const Color4f &c) const
Definition: CUColor4.h:939
SDL_FORCE_INLINE Uint16 marshall(Sint16 value)
Definition: CUEndian.h:53
bool darkerThan(Color4 c) const
Definition: CUColor4.h:2016
GLuint getRGBA() const
Definition: CUColor4.h:1769
bool operator!=(Color4 c) const
Definition: CUColor4.h:2001
Color4 & operator+=(Color4 c)
Definition: CUColor4.h:1786
Color4 & subtract(Color4 c, bool alpha=false)
Definition: CUColor4.h:1359
const Color4 operator*(float s) const
Definition: CUColor4.h:1901
Color4 & scale(float s, bool alpha=false)
Definition: CUColor4.h:1397
static const Color4f CYAN
Definition: CUColor4.h:103
Color4f getLerp(const Color4f &other, float alpha) const
Definition: CUColor4.h:586
const Color4 operator-(Color4 c) const
Definition: CUColor4.h:1883
Definition: CUColor4.h:73
Color4 & scale(float sr, float sg, float sb, float sa=1)
Definition: CUColor4.h:1417
Color4 & premultiply()
Definition: CUColor4.h:1579
static const Color4f GRAY
Definition: CUColor4.h:107
Color4(GLuint color)
Definition: CUColor4.h:1179
Color4f & operator=(const float *array)
Definition: CUColor4.h:201
static const Color4 RED
Definition: CUColor4.h:1136
static const Color4 WHITE
Definition: CUColor4.h:1126
bool operator>=(const Color4f &c) const
Definition: CUColor4.h:925
Color4f & complement(bool alpha=false)
Definition: CUColor4.h:458
Color4 & unpremultiply()
Definition: CUColor4.h:1595
Color4f(const Color4f ©)
Definition: CUColor4.h:147
Color4 & operator*=(Color4 c)
Definition: CUColor4.h:1842
Color4 & blendPre(Color4 other)
Definition: CUColor4.h:1560
Color4f & add(float r, float g, float b, float a=0)
Definition: CUColor4.h:312
static const Color4 CLEAR
Definition: CUColor4.h:1124
Color4f & unpremultiply()
Definition: CUColor4.h:562
Color4 & operator=(const float *array)
Definition: CUColor4.h:1223
Color4f & subtract(float r, float g, float b, float a=0)
Definition: CUColor4.h:351
static const Color4f GREEN
Definition: CUColor4.h:97
Color4 & operator-=(Color4 c)
Definition: CUColor4.h:1806
Color4 & operator=(GLuint color)
Definition: CUColor4.h:1209
Color4()
Definition: CUColor4.h:1156
std::string toString(bool verbose=false) const
Color4 & operator*=(float s)
Definition: CUColor4.h:1825
Color4f & premultiply()
Definition: CUColor4.h:546
Color4f & add(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:292
Color4 getMap(std::function< GLubyte(GLubyte)> func, bool alpha=false) const
Definition: CUColor4.h:1473
Color4f & operator=(unsigned int color)
Definition: CUColor4.h:188
bool operator>=(Color4 c) const
Definition: CUColor4.h:1976
Color4f & operator-=(const Color4f &c)
Definition: CUColor4.h:760
bool operator<=(const Color4f &c) const
Definition: CUColor4.h:897
bool operator>(Color4 c) const
Color4 getLerp(Color4 other, float alpha) const
Definition: CUColor4.h:1620
Color4 getComplement(bool alpha=false) const
Definition: CUColor4.h:1504
std::string toString(bool verbose=false) const
static const Color4 GRAY
Definition: CUColor4.h:1144
Color4f & operator=(const Color4f &c)
Definition: CUColor4.h:174
static const Color4 CYAN
Definition: CUColor4.h:1140
Color4 & add(GLubyte r, GLubyte g, GLubyte b, GLubyte a=0)
Definition: CUColor4.h:1340
Color4 & complement(bool alpha=false)
Definition: CUColor4.h:1488
bool lighterThan(Color4 c) const
Definition: CUColor4.h:2032
static const Color4f CLEAR
Definition: CUColor4.h:87
GLubyte b
Definition: CUColor4.h:1115
Color4f & subtract(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:331
Color4 & blend(Color4 other)
Definition: CUColor4.h:1539
static const Color4f BLUE
Definition: CUColor4.h:95
Color4f getComplement(bool alpha=false) const
Definition: CUColor4.h:474
Color4 getClamp(Color4 min, Color4 max) const
Definition: CUColor4.h:1303
Color4f & set(const Color4f &c)
Definition: CUColor4.h:249
static const Color4f CORNFLOWER
Definition: CUColor4.h:109
Color4f & operator+=(const Color4f &c)
Definition: CUColor4.h:740
GLfloat r
Definition: CUColor4.h:78
GLubyte g
Definition: CUColor4.h:1113
const Color4f operator*(const Color4f &c) const
Definition: CUColor4.h:866
Color4f & blend(const Color4f &other)
Definition: CUColor4.h:509
static const Color4 BLUE
Definition: CUColor4.h:1132
Color4f & operator*=(float s)
Definition: CUColor4.h:779
Color4f & scale(float sr, float sg, float sb, float sa=1)
Definition: CUColor4.h:389
int operator*(Font::Style value)
Definition: CUFont.h:1503
static const Color4f RED
Definition: CUColor4.h:99
Uint32 rgba
Definition: CUColor4.h:1120
Color4f getBlendPre(Color4 other) const
Definition: CUColor4.h:1659
Color4f & scale(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:407
const Color4 operator+(Color4 c) const
Definition: CUColor4.h:1864
static const Color4f WHITE
Definition: CUColor4.h:89
bool operator<=(Color4 c) const
Definition: CUColor4.h:1948
Definition: CUColor4.h:1104
const Color4f operator*(float s) const
Definition: CUColor4.h:850
static const Color4 PAPYRUS
Definition: CUColor4.h:1148
Definition: CUAnimationNode.h:52
GLfloat b
Definition: CUColor4.h:82
Color4 & clamp(Color4 min, Color4 max)
const Color4 operator*(Color4 c) const
Definition: CUColor4.h:1917
Color4f()
Definition: CUColor4.h:118
bool operator!=(const Color4f &c) const
Definition: CUColor4.h:953
Color4f & scale(float s, bool alpha=false)
Definition: CUColor4.h:369
bool operator<(const Color4f &c) const
Color4 getPremultiplied() const
Definition: CUColor4.h:1680
Color4f getUnpremultiplied() const
Definition: CUColor4.h:658
bool operator>(const Color4f &c) const
Color4 & lerp(const Color4f &other, float alpha)
Definition: CUColor4.h:1523
static const Color4 MAGENTA
Definition: CUColor4.h:1138
static const Color4f YELLOW
Definition: CUColor4.h:93
GLubyte a
Definition: CUColor4.h:1117
static const Color4 ORANGE
Definition: CUColor4.h:1142
static const Color4 GREEN
Definition: CUColor4.h:1134
Color4f & set(float r, float g, float b, float a=1)
static const Color4f MAGENTA
Definition: CUColor4.h:101
static const Color4f PAPYRUS
Definition: CUColor4.h:111
Color4 & set(GLubyte r, GLubyte g, GLubyte b, GLubyte a=255)
Definition: CUColor4.h:1237
bool operator<(Color4 c) const