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