CMU462 Library  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 "CMU462.h"
5 
6 #include <string>
7 
8 namespace CMU462 {
9 
15 class Color {
16  public:
17 
18  // Components.
19  float r;
20  float g;
21  float b;
22  float a;
24  // constants
25  static const Color White;
26  static const Color Black;
27 
37  Color( float r = 0, float g = 0, float b = 0, float a = 1.0 )
38  : r( r ), g( g ), b( b ), a( a ) { }
39 
45  Color( const unsigned char* arr );
46 
47  // Addition. Alpha is ignored.
48  inline Color operator+( const Color& rhs ) const {
49  return Color( r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
50  }
51 
52  inline Color& operator+=( const Color& rhs ) {
53  r += rhs.r; g += rhs.g; b += rhs.b; 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; g *= rhs.g; b *= rhs.b; a *= rhs.a;
64  return *this;
65  }
66 
67  // Scalar multiplication.
68  inline Color operator*( float s ) const {
69  return Color( r * s, g * s, b * s, a * s );
70  }
71 
72  inline Color& operator*=( float s ) {
73  r *= s; g *= s; b *= s; a *= s;
74  return *this;
75  }
76 
77  // comparison
78  inline bool operator==( const Color& rhs ) const {
79  return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
80  }
81 
82  inline bool operator!=( const Color& rhs ) const {
83  return !operator==( rhs );
84  }
85 
95  static Color fromHex( const char* s );
96 
101  std::string toHex( ) const;
102 
103 
104 }; // class Color
105 
106 
107 // Commutable scalar multiplication.
108 inline Color operator*( float s, const Color& c ) {
109  return c * s;
110 }
111 
112 // Prints components.
113 std::ostream& operator<<( std::ostream& os, const Color& c );
114 
115 } // namespace CMU462
116 
117 #endif
Definition: CMU462.h:8
float a
value of alpha chanel
Definition: color.h:22
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:19
float g
value of green chanel
Definition: color.h:20
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:21
Encodes a color via additive red, green, and blue chanel values.
Definition: color.h:15
Color(float r=0, float g=0, float b=0, float a=1.0)
Constructor.
Definition: color.h:37