|
CUGL 1.3
Cornell University Game Library
|
36 #ifndef __CU_COLOR4_H__
37 #define __CU_COLOR4_H__
41 #include <cugl/math/CUMathBase.h>
42 #include <cugl/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
235 #pragma mark Arithmetic
258 clampf(
b,min.
b,max.
b), clampf(
a,min.
a,max.
a));
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);
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);
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);
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);
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);
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);
388 r *= c.
r;
g *= c.
g;
b *= c.
b;
389 a = (alpha ?
a*c.
a :
a);
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);
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));
428 #pragma mark Color Operations
439 r = 1-
r;
g = 1-
g;
b = 1-
b;
a = (alpha ? 1-
a :
a);
474 float x = clampf(alpha,0,1);
476 return *
this += other * x;
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;
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);
567 float x = clampf(alpha,0,1);
568 return *
this * (1.f - x) + other * x;
584 float a1 =
a*(1-other.
a);
585 float a2 = other.
a+a1;
587 (other.
g*other.
a+
g*a1)/a2,
588 (other.
b*other.
a+
b*a1)/a2,
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);
707 #pragma mark Operators
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);
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);
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);
777 r *= c.
r;
g *= c.
g;
b *= c.
b;
a *= c.
a;
852 #pragma mark Comparisons
878 return *
this < c || *
this == c;
906 return *
this > c || *
this == c;
920 return r == c.
r &&
g == c.
g &&
b == c.
b &&
a == c.
a;
934 return r != c.
r ||
g != c.
g ||
b != c.
b ||
a != c.
a;
949 return r <= c.
r &&
g <= c.
g &&
b <= c.
b && c.
a <=
a;
965 return r >= c.
r &&
g >= c.
g &&
b >= c.
b && c.
a >=
a;
979 return (fabsf(
r-color.
r) < variance && fabsf(
g-color.
g) < variance &&
980 fabsf(
b-color.
b) < variance && fabsf(
a-color.
a) < variance);
984 #pragma mark Conversion Methods
996 std::string
toString(
bool verbose =
false)
const;
999 operator std::string()
const {
return toString(); }
1002 operator Vec4()
const;
1025 operator Vec3()
const;
1075 #pragma mark Color with Byte Attributes
1131 #pragma mark Constructors
1147 this->r =
r; this->g =
g; this->b =
b; this->a =
a;
1169 Color4(
const float* array);
1172 #pragma mark Setters
1213 this->r =
r; this->g =
g; this->b =
b; this->a =
a;
1240 rgba = marshall(color);
1257 #pragma mark Arithmetic
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));
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);
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);
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);
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);
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);
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);
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);
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);
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));
1453 #pragma mark Color4 Operations
1464 r = 255-
r;
g = 255-
g;
b = 255-
b;
a = (alpha ? 255-
a :
a);
1480 return Color4(255-
r, 255-
g, 255-
b, (alpha ? 255-
a :
a));
1499 float x = clampf(alpha,0,1);
1501 return *
this += other * x;
1515 float srca = COLOR_BYTE_TO_FLOAT(other.
a);
1516 float a1 = COLOR_BYTE_TO_FLOAT(
a)*(1-srca);
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);
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);
1555 scale(COLOR_BYTE_TO_FLOAT(
a),
false);
1572 float a1 = 1/COLOR_BYTE_TO_FLOAT(
a);
1596 float x = clampf(alpha,0,1);
1597 return *
this * (1.f - x) + other * x;
1613 float srca = COLOR_BYTE_TO_FLOAT(other.
a);
1614 float a1 = COLOR_BYTE_TO_FLOAT(
a)*(1-srca);
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));
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));
1657 result *= COLOR_BYTE_TO_FLOAT(
a);
1676 float a1 = 1/COLOR_BYTE_TO_FLOAT(
a);
1678 return result.
scale(a1,
false);
1745 return marshall(
rgba);
1748 #pragma mark Operators
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);
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);
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);
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);
1898 #pragma mark Comparisons
1924 return *
this < c ||
rgba == c.
rgba;
1952 return *
this > c ||
rgba == c.
rgba;
1992 return r <= c.
r &&
g <= c.
g &&
b <= c.
b && c.
a <=
a;
2008 return r >= c.
r &&
g >= c.
g &&
b >= c.
b && c.
a >=
a;
2011 #pragma mark Conversion Methods
2023 std::string
toString(
bool verbose =
false)
const;
2033 operator Vec4()
const;
2062 operator Vec3()
const;
2113 #pragma mark Friend Operations
2127 inline const Color4f operator*(
float s,
const Color4f& c) {
2144 inline const Color4 operator*(
float s, Color4 c) {
Color4f getBlendPre(const Color4f &other) const
Definition: CUColor4.h:604
bool equals(const Color4f &color, float variance=CU_MATH_EPSILON) const
Definition: CUColor4.h:978
static const Color4f BLUE
Definition: CUColor4.h:95
Color4 & operator*=(Color4 c)
Definition: CUColor4.h:1817
Color4f & premultiply()
Definition: CUColor4.h:526
Color4f getClamp(const Color4f &min, const Color4f &max) const
Definition: CUColor4.h:256
GLfloat b
Definition: CUColor4.h:82
Color4f getMap(std::function< float(float)> func, bool alpha=false) const
Definition: CUColor4.h:422
const Color4 operator*(float s) const
Definition: CUColor4.h:1876
Color4f & add(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:272
Color4f & operator=(unsigned int color)
Definition: CUColor4.h:168
const Color4f operator*(float s) const
Definition: CUColor4.h:830
Color4f & complement(bool alpha=false)
Definition: CUColor4.h:438
Color4 & add(Color4 c, bool alpha=false)
Definition: CUColor4.h:1295
static const Color4f MAGENTA
Definition: CUColor4.h:101
Color4f()
Definition: CUColor4.h:118
Color4f & subtract(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:311
Color4f & clamp(const Color4f &min, const Color4f &max)
GLubyte a
Definition: CUColor4.h:1097
Color4f & scale(float s, bool alpha=false)
Definition: CUColor4.h:349
const Color4f operator+(const Color4f &c) const
Definition: CUColor4.h:795
Color4 & set(const Color4 &c)
Definition: CUColor4.h:1251
Color4f getPremultiplied() const
Definition: CUColor4.h:621
Color4 & scale(float sr, float sg, float sb, float sa=1)
Definition: CUColor4.h:1392
bool lighterThan(Color4 c) const
Definition: CUColor4.h:2007
static const Color4f WHITE
Definition: CUColor4.h:89
Color4f & blend(const Color4f &other)
Definition: CUColor4.h:489
std::string toString(bool verbose=false) const
bool operator<=(Color4 c) const
Definition: CUColor4.h:1923
Color4f & subtract(float r, float g, float b, float a=0)
Definition: CUColor4.h:331
static const Color4 RED
Definition: CUColor4.h:1116
static const Color4f RED
Definition: CUColor4.h:99
static const Color4f CLEAR
Definition: CUColor4.h:87
static const Color4 MAGENTA
Definition: CUColor4.h:1118
static const Color4f GREEN
Definition: CUColor4.h:97
Definition: CUColor4.h:1084
static const Color4 CLEAR
Definition: CUColor4.h:1104
bool operator!=(const Color4f &c) const
Definition: CUColor4.h:933
bool operator>=(Color4 c) const
Definition: CUColor4.h:1951
bool operator>=(const Color4f &c) const
Definition: CUColor4.h:905
static const Color4 GRAY
Definition: CUColor4.h:1124
static const Color4f ORANGE
Definition: CUColor4.h:105
Color4()
Definition: CUColor4.h:1136
Uint32 rgba
Definition: CUColor4.h:1100
Color4 getBlend(Color4 other) const
Definition: CUColor4.h:1612
Color4 & operator=(GLuint color)
Definition: CUColor4.h:1184
Color4 & premultiply()
Definition: CUColor4.h:1554
static const Color4 CYAN
Definition: CUColor4.h:1120
Color4f & set(const Color4f &c)
Definition: CUColor4.h:229
GLfloat r
Definition: CUColor4.h:78
GLuint getRGBA() const
Definition: CUColor4.h:1744
static const Color4f CORNFLOWER
Definition: CUColor4.h:109
GLubyte g
Definition: CUColor4.h:1093
static const Color4 BLACK
Definition: CUColor4.h:1108
Color4 & operator*=(float s)
Definition: CUColor4.h:1800
bool darkerThan(Color4 c) const
Definition: CUColor4.h:1991
Color4 & scale(Color4 c, bool alpha=false)
Definition: CUColor4.h:1410
Color4 getPremultiplied() const
Definition: CUColor4.h:1655
Color4 & set(GLubyte r, GLubyte g, GLubyte b, GLubyte a=255)
Definition: CUColor4.h:1212
static const Color4 PAPYRUS
Definition: CUColor4.h:1128
const Color4 operator+(Color4 c) const
Definition: CUColor4.h:1839
bool operator<=(const Color4f &c) const
Definition: CUColor4.h:877
Color4 & complement(bool alpha=false)
Definition: CUColor4.h:1463
static const Color4 YELLOW
Definition: CUColor4.h:1110
static const Color4 GREEN
Definition: CUColor4.h:1114
bool lighterThan(const Color4f &c) const
Definition: CUColor4.h:964
static const Color4f BLACK
Definition: CUColor4.h:91
Color4f getComplement(bool alpha=false) const
Definition: CUColor4.h:454
Color4f & scale(float sr, float sg, float sb, float sa=1)
Definition: CUColor4.h:369
Color4 getMap(std::function< GLubyte(GLubyte)> func, bool alpha=false) const
Definition: CUColor4.h:1448
Color4(GLubyte r, GLubyte g, GLubyte b, GLubyte a=255)
Definition: CUColor4.h:1146
Color4 & operator-=(Color4 c)
Definition: CUColor4.h:1781
Color4f getLerp(const Color4f &other, float alpha) const
Definition: CUColor4.h:566
Color4f getBlend(const Color4f &other) const
Definition: CUColor4.h:583
Color4 getLerp(Color4 other, float alpha) const
Definition: CUColor4.h:1595
GLfloat g
Definition: CUColor4.h:80
bool operator>(const Color4f &c) const
Color4f & scale(const Color4f &c, bool alpha=false)
Definition: CUColor4.h:387
bool operator<(const Color4f &c) const
Definition: CUColor4.h:73
bool operator==(Color4 c) const
Definition: CUColor4.h:1962
Color4 getComplement(bool alpha=false) const
Definition: CUColor4.h:1479
Color4f & operator=(const float *array)
Definition: CUColor4.h:181
Color4 getClamp(Color4 min, Color4 max) const
Definition: CUColor4.h:1278
const Color4f operator*(const Color4f &c) const
Definition: CUColor4.h:846
Color4 & operator=(const float *array)
Definition: CUColor4.h:1198
Color4 & blendPre(Color4 other)
Definition: CUColor4.h:1535
Color4f & operator*=(float s)
Definition: CUColor4.h:759
Color4 & subtract(GLubyte r, GLubyte g, GLubyte b, GLubyte a=0)
Definition: CUColor4.h:1354
Color4f getUnpremultiplied() const
Definition: CUColor4.h:638
static const Color4 WHITE
Definition: CUColor4.h:1106
static const Color4f GRAY
Definition: CUColor4.h:107
const Color4 operator*(Color4 c) const
Definition: CUColor4.h:1892
static const Color4f YELLOW
Definition: CUColor4.h:93
GLfloat a
Definition: CUColor4.h:84
Color4 getUnpremultiplied() const
Definition: CUColor4.h:1674
Color4f & map(std::function< float(float)> func, bool alpha=false)
Definition: CUColor4.h:405
GLubyte r
Definition: CUColor4.h:1091
Color4f & add(float r, float g, float b, float a=0)
Definition: CUColor4.h:292
Color4f getBlendPre(Color4 other) const
Definition: CUColor4.h:1634
const Color4 operator-(Color4 c) const
Definition: CUColor4.h:1858
Color4f & lerp(const Color4f &other, float alpha)
Definition: CUColor4.h:473
const Color4f operator-(const Color4f &c) const
Definition: CUColor4.h:814
static const Color4f CYAN
Definition: CUColor4.h:103
Color4 & unpremultiply()
Definition: CUColor4.h:1570
std::string toString(bool verbose=false) const
Color4f & set(float r, float g, float b, float a=1)
bool operator<(Color4 c) const
bool operator==(const Color4f &c) const
Definition: CUColor4.h:919
Color4 & add(GLubyte r, GLubyte g, GLubyte b, GLubyte a=0)
Definition: CUColor4.h:1315
Color4(GLuint color)
Definition: CUColor4.h:1159
Color4f & blendPre(const Color4f &other)
Definition: CUColor4.h:509
~Color4f()
Definition: CUColor4.h:154
bool operator!=(Color4 c) const
Definition: CUColor4.h:1976
Color4 & blend(Color4 other)
Definition: CUColor4.h:1514
static const Color4 ORANGE
Definition: CUColor4.h:1122
Color4 & subtract(Color4 c, bool alpha=false)
Definition: CUColor4.h:1334
Color4f & operator-=(const Color4f &c)
Definition: CUColor4.h:740
Color4 & scale(float s, bool alpha=false)
Definition: CUColor4.h:1372
bool darkerThan(const Color4f &c) const
Definition: CUColor4.h:948
static const Color4f PAPYRUS
Definition: CUColor4.h:111
Color4 & operator+=(Color4 c)
Definition: CUColor4.h:1761
GLubyte b
Definition: CUColor4.h:1095
Color4 & lerp(const Color4f &other, float alpha)
Definition: CUColor4.h:1498
Color4 & map(std::function< GLubyte(GLubyte)> func, bool alpha=false)
Definition: CUColor4.h:1430
Color4 & set(GLuint color)
Definition: CUColor4.h:1239
Color4f & unpremultiply()
Definition: CUColor4.h:542
static const Color4 BLUE
Definition: CUColor4.h:1112
Color4 & clamp(Color4 min, Color4 max)
Color4f & operator+=(const Color4f &c)
Definition: CUColor4.h:720
Color4f & operator*=(const Color4f &c)
Definition: CUColor4.h:776
bool operator>(Color4 c) const
static const Color4 CORNFLOWER
Definition: CUColor4.h:1126