CMU462 Library  1.0
15-462/15-662: Computer Graphics (Fall 2015)
irradiance.h
1 #ifndef CMU462_COLOR_H
2 #define CMU462_COLOR_H
3 
4 #include <string>
5 
6 namespace CMU462 {
7 
14 class Color {
15  public:
16  // Components.
17  float r;
18  float g;
19  float b;
21  // constants
22  static const Color White;
23  static const Color Black;
24 
34  Color(float r = 0, float g = 0, float b = 0, float a = 1.0)
35  : r(r), g(g), b(b), a(a) {}
36 
42  Color(const unsigned char *arr);
43 
44  // Addition. Alpha is ignored.
45  inline Color operator+(const Color &rhs) const {
46  return Color(r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
47  }
48 
49  inline Color &operator+=(const Color &rhs) {
50  r += rhs.r;
51  g += rhs.g;
52  b += rhs.b;
53  a += rhs.a;
54  return *this;
55  }
56 
57  // Vector multiplication. Alpha is ignored.
58  inline Color operator*(const Color &rhs) const {
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  r *= rhs.r;
64  g *= rhs.g;
65  b *= rhs.b;
66  a *= rhs.a;
67  return *this;
68  }
69 
70  // Scalar multiplication.
71  inline Color operator*(float s) const {
72  return Color(r * s, g * s, b * s, a * s);
73  }
74 
75  inline Color &operator*=(float s) {
76  r *= s;
77  g *= s;
78  b *= s;
79  a *= s;
80  return *this;
81  }
82 
83  // comparison
84  inline bool operator==(const Color &rhs) const {
85  return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
86  }
87 
88  inline bool operator!=(const Color &rhs) const { return !operator==(rhs); }
89 
99  static Color fromHex(const char *s);
100 
105  std::string toHex() const;
106 
107 }; // class Color
108 
109 // Commutable scalar multiplication.
110 inline Color operator*(float s, const Color &c) { return c * s; }
111 
112 // Prints components.
113 std::ostream &operator<<(std::ostream &os, const Color &c);
114 
115 } // namespace CMU462
116 
117 #endif
Definition: color.h:6
float a
value of alpha chanel
Definition: color.h:20
static Color fromHex(const char *s)
Construct a Color object from a hexadecimal (8-bit per component) ASCII string.
Definition: color.cpp:24
float r
value of red chanel
Definition: color.h:17
float g
value of green chanel
Definition: color.h:18
std::string toHex() const
Returns a hexadecimal string rrggbb encoding this color.
Definition: color.cpp:55
float b
value of blue chanel
Definition: color.h:19
Encodes a color via additive red, green, and blue chanel values.
Definition: color.h:13
Color(float r=0, float g=0, float b=0, float a=1.0)
Constructor.
Definition: color.h:35