color.h
#ifndef CMU462_COLOR_H
#define CMU462_COLOR_H
#include "CMU462.h"
#include <string>
namespace CMU462 {
class Color {
public:
// Components.
float r;
float g;
float b;
float a;
// constants
static const Color White;
static const Color Black;
Color( float r = 0, float g = 0, float b = 0, float a = 1.0 )
: r( r ), g( g ), b( b ), a( a ) { }
Color( const unsigned char* arr );
// Addition. Alpha is ignored.
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;
}
// Vector multiplication. Alpha is ignored.
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;
}
// Scalar multiplication.
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;
}
// comparison
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 );
}
static Color fromHex( const char* s );
std::string toHex( ) const;
}; // class Color
// 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 );
} // namespace CMU462
#endif
CMU462
Definition: CMU462.h:8
CMU462::Color::a
float a
value of alpha chanel
Definition: color.h:22
CMU462::Color::fromHex
static Color fromHex(const char *s)
Construct a Color object from a hexadecimal (8-bit per component) ASCII string.
Definition: color.cpp:24
CMU462::Color::r
float r
value of red chanel
Definition: color.h:19
CMU462::Color::g
float g
value of green chanel
Definition: color.h:20
CMU462::Color::toHex
std::string toHex() const
Returns a hexadecimal string rrggbb encoding this color.
Definition: color.cpp:55
CMU462::Color::b
float b
value of blue chanel
Definition: color.h:21
CMU462::Color
Encodes a color via additive red, green, and blue chanel values.
Definition: color.h:15
CMU462::Color::Color
Color(float r=0, float g=0, float b=0, float a=1.0)
Constructor.
Definition: color.h:37