From 330f75e663c22e1198a92fd134865ada98c3957b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 2 May 2016 22:57:13 -0400 Subject: Initial commit --- color.h | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 color.h (limited to 'color.h') diff --git a/color.h b/color.h new file mode 100644 index 0000000..ea5f9fe --- /dev/null +++ b/color.h @@ -0,0 +1,125 @@ +#ifndef COLOR_H_D3124023 +#define COLOR_H_D3124023 + +#include + +/** + * Encodes a color via additive red, green, and blue chanel values. + * Each color chanel value is in the range [0,1]. The alpha value + * defines the transparency of the color and is also in [0,1]. + */ +class Color { + public: + // Components. + float r; /**< value of red chanel */ + float g; /**< value of green chanel */ + float b; /**< value of blue chanel */ + float a; /**< value of alpha chanel */ + + // constants + static const Color White; + static const Color Black; + + /** + * Constructor. + * Initialize from component values. + * By default, alpha is initialized to 1. + * \param r Value of the red chanel. + * \param g Value of the green chanel. + * \param b Value of the blue chanel. + * \param a Value of the alpha chanel. + */ + Color( float r = 0, float g = 0, float b = 0, float a = 1.0 ) + : r( r ), g( g ), b( b ), a( a ) { } + + /** + * Constructor. + * Initialize from array of 8-bit component values (RGB only). + * \param arr Array containing component values. + */ + Color( const unsigned char* arr ); + + 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; + } + + 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; + } + + 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; + } + + 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 ); + } + + /** + * Construct a Color object from a hexadecimal (8-bit per + * component) ASCII string. + * Since hexademical strings are typically not used to encode + * alpha values, the alpha is set to 1 (opaque) by default. + * This method also accepts the string "none", in which case + * it returns a color value with alpha zero (transparent). + * \return Color constructed from the input hex encoding. + */ + static Color fromHex(const char* s); + + /** + * Returns a hexadecimal string #rrggbb encoding this color. + * \return the hexadecimal encoding of the color. + */ + std::string toHex() const; + +}; + +// 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); + +#endif /* end of include guard: COLOR_H_D3124023 */ -- cgit 1.4.1