1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#ifndef COLOR_H_D3124023
#define COLOR_H_D3124023
#include <string>
/**
* Encodes a color via additive red, green, and blue chanel values.
* Each color chanel value is in the range [0,1]. The alpha value
* defines the transparency of the color and is also in [0,1].
*/
class Color {
public:
// Components.
float r; /**< value of red chanel */
float g; /**< value of green chanel */
float b; /**< value of blue chanel */
float a; /**< value of alpha chanel */
// constants
static const Color White;
static const Color Black;
/**
* Constructor.
* Initialize from component values.
* By default, alpha is initialized to 1.
* \param r Value of the red chanel.
* \param g Value of the green chanel.
* \param b Value of the blue chanel.
* \param a Value of the alpha chanel.
*/
Color( float r = 0, float g = 0, float b = 0, float a = 1.0 )
: r( r ), g( g ), b( b ), a( a ) { }
/**
* Constructor.
* Initialize from array of 8-bit component values (RGB only).
* \param arr Array containing component values.
*/
Color( const unsigned char* arr );
inline Color operator+(const Color& rhs) const
{
return Color(r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
}
inline Color& operator+=(const Color& rhs)
{
r += rhs.r;
g += rhs.g;
b += rhs.b;
a += rhs.a;
return *this;
}
inline Color operator*(const Color& rhs) const
{
return Color(r * rhs.r, g * rhs.g, b * rhs.b, a * rhs.a);
}
inline Color& operator*=(const Color& rhs)
{
r *= rhs.r;
g *= rhs.g;
b *= rhs.b;
a *= rhs.a;
return *this;
}
inline Color operator*(float s) const
{
return Color(r * s, g * s, b * s, a * s);
}
inline Color& operator*=(float s)
{
r *= s;
g *= s;
b *= s;
a *= s;
return *this;
}
inline bool operator==(const Color& rhs) const
{
return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
}
inline bool operator!=(const Color& rhs) const
{
return !operator==( rhs );
}
/**
* Construct a Color object from a hexadecimal (8-bit per
* component) ASCII string.
* Since hexademical strings are typically not used to encode
* alpha values, the alpha is set to 1 (opaque) by default.
* This method also accepts the string "none", in which case
* it returns a color value with alpha zero (transparent).
* \return Color constructed from the input hex encoding.
*/
static Color fromHex(const char* s);
/**
* Returns a hexadecimal string #rrggbb encoding this color.
* \return the hexadecimal encoding of the color.
*/
std::string toHex() const;
};
// Commutable scalar multiplication.
inline Color operator*(float s, const Color& c)
{
return c * s;
}
// Prints components.
std::ostream& operator<<(std::ostream& os, const Color& c);
#endif /* end of include guard: COLOR_H_D3124023 */
|