CMU462 Library Documentation  1.0
15-462/15-662: Computer Graphics (Fall 2015)
color.h
1 #ifndef CMU462_COLOR_H
2 #define CMU462_COLOR_H
3 
4 #include <string>
5 
6 namespace CMU462 {
7 
13 class Color {
14  public:
15 
16  // Components.
17  float r;
18  float g;
19  float b;
20  float a;
22  // constants
23  static const Color White;
24  static const Color Black;
25 
35  Color( float r = 0, float g = 0, float b = 0, float a = 1.0 )
36  : r( r ), g( g ), b( b ), a( a ) { }
37 
43  Color( const unsigned char* arr );
44 
45  // Addition. Alpha is ignored.
46  inline Color operator+( const Color& rhs ) const {
47  return Color( r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
48  }
49 
50  inline Color& operator+=( const Color& rhs ) {
51  r += rhs.r; g += rhs.g; b += rhs.b; a += rhs.a;
52  return *this;
53  }
54 
55  // Vector multiplication. Alpha is ignored.
56  inline Color operator*( const Color& rhs ) const {
57  return Color( r * rhs.r, g * rhs.g, b * rhs.b, a * rhs.a);
58  }
59 
60  inline Color& operator*=( const Color& rhs ) {
61  r *= rhs.r; g *= rhs.g; b *= rhs.b; a *= rhs.a;
62  return *this;
63  }
64 
65  // Scalar multiplication.
66  inline Color operator*( float s ) const {
67  return Color( r * s, g * s, b * s, a * s );
68  }
69 
70  inline Color& operator*=( float s ) {
71  r *= s; g *= s; b *= s; a *= s;
72  return *this;
73  }
74 
75  // comparison
76  inline bool operator==( const Color& rhs ) const {
77  return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
78  }
79 
80  inline bool operator!=( const Color& rhs ) const {
81  return !operator==( rhs );
82  }
83 
93  static Color fromHex( const char* s );
94 
99  std::string toHex( ) const;
100 
101 
102 }; // class Color
103 
104 
105 // Commutable scalar multiplication.
106 inline Color operator*( float s, const Color& c ) {
107  return c * s;
108 }
109 
110 // Prints components.
111 std::ostream& operator<<( std::ostream& os, const Color& c );
112 
113 } // namespace CMU462
114 
115 #endif
Definition: color.cpp:10
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