[4838] | 1 | /*! |
---|
[5009] | 2 | * @file color.h |
---|
| 3 | * @brief Definition of color-calculations |
---|
| 4 | * |
---|
[8986] | 5 | * TODO CONVERT THE VECTORS to COLORS!! |
---|
| 6 | * |
---|
[5009] | 7 | * code borrowed from: |
---|
| 8 | * http://www.easyrgb.com/math.php |
---|
[3245] | 9 | */ |
---|
[1853] | 10 | |
---|
[5009] | 11 | #ifndef _COLOR_H |
---|
| 12 | #define _COLOR_H |
---|
[1853] | 13 | |
---|
[7195] | 14 | #include "vector.h" |
---|
[1853] | 15 | |
---|
[9869] | 16 | //! A Class that handles Colors. |
---|
| 17 | /** |
---|
| 18 | * A Color is a collection of 4 values: |
---|
| 19 | * <ul> |
---|
| 20 | * <li>Red</li> |
---|
| 21 | * <li>Green</li> |
---|
| 22 | * <li>Blue</li> |
---|
| 23 | * <li>Alpha</li> |
---|
| 24 | * </ul> |
---|
| 25 | * With these four values any color of the entire spectrum can be defined. |
---|
| 26 | * |
---|
| 27 | * By default a Color lies between 0 and 1 for each component. |
---|
| 28 | * for example [1,0,0,.5] means red and half visible. |
---|
| 29 | */ |
---|
[5009] | 30 | class Color |
---|
| 31 | { |
---|
[7195] | 32 | public: |
---|
[9869] | 33 | /** @param r red, @param g green @param b blue @param a alpha @brief constructs a Color. */ |
---|
[8448] | 34 | Color(float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f) { _rgba[0] = r; _rgba[1] = g; _rgba[2] = b; _rgba[3] = a; }; |
---|
[9869] | 35 | /** @param c Color @brief copy constructor */ |
---|
[8376] | 36 | Color(const Color& c) { _rgba[0] = c.r(); _rgba[1] = c.g(); _rgba[2] = c.b(); _rgba[3] = c.a(); } |
---|
[8145] | 37 | |
---|
[9869] | 38 | /** @param c the Color to set to this color @returns the copied color */ |
---|
[8448] | 39 | inline const Color& operator=(const Color& c) { _rgba[0] = c.r(); _rgba[1] = c.g(); _rgba[2] = c.b(); _rgba[3] = c.a(); return *this; }; |
---|
[9869] | 40 | /** @param c the color to compare @returns true on match. @brief compares two colors */ |
---|
[8448] | 41 | inline bool operator==(const Color& c) const { return (r() == c.r() && g() == c.g() && b() == c.b() && a() == c.a()); }; |
---|
[8145] | 42 | |
---|
[9869] | 43 | /** @returns the i'th Value of the Color @param i part of the color 0:r, 1:g, 2:b, 3:a */ |
---|
[8448] | 44 | inline float& operator[](unsigned int i) { return _rgba[i]; } |
---|
[9869] | 45 | /** @returns a Constant Value of the color. @param i part of the color 0:r, 1:g, 2:b, 3:a */ |
---|
[8448] | 46 | inline const float& operator[](unsigned int i) const { return _rgba[i]; } |
---|
[8376] | 47 | |
---|
[9869] | 48 | /** @returns the red part. */ |
---|
[8448] | 49 | inline float r() const { return _rgba[0]; } |
---|
[9869] | 50 | /** @returns the reference to the red part */ |
---|
[8448] | 51 | inline float& r() { return _rgba[0]; } |
---|
[9869] | 52 | /** @returns the green part. */ |
---|
[8448] | 53 | inline float g() const { return _rgba[1]; } |
---|
[9869] | 54 | /** @returns the reference to the green part */ |
---|
[8448] | 55 | inline float& g() { return _rgba[1]; } |
---|
[9869] | 56 | /** @returns the blue part */ |
---|
[8448] | 57 | inline float b() const { return _rgba[2]; } |
---|
[9869] | 58 | /** @returns the reference to the blue part */ |
---|
[8448] | 59 | inline float& b() { return _rgba[2]; } |
---|
[9869] | 60 | /** @returns the alpha part */ |
---|
[8448] | 61 | inline float a() const { return _rgba[3]; } |
---|
[9869] | 62 | /** @returns the reference to the alpha part */ |
---|
[8448] | 63 | inline float& a() { return _rgba[3]; } |
---|
[8376] | 64 | |
---|
[8448] | 65 | |
---|
[9869] | 66 | /** @param r red, @param g green @param b blue @param a alpha @brief sets the color. */ |
---|
[8448] | 67 | void setColor(float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f) { _rgba[0] = r; _rgba[1] = g; _rgba[2] = b; _rgba[3] = a; }; |
---|
[9869] | 68 | /** @param c the color to set. @brief sets the color. */ |
---|
[8448] | 69 | void setColor(const Color& c) { r() = c.r(); g()= c.g(); b() = c.b(); a() = c.a(); }; |
---|
| 70 | |
---|
[9869] | 71 | /** @returns the distance to the color @param c the color to calculate the distance to. */ |
---|
[8448] | 72 | inline float dist(const Color& c) const { return (sqrt((r()-c.r())*(r()-c.r()) + (g()-c.g())*(g()-c.g()) + (b()-c.b())*(b()-c.b()) + (a()-c.a())*(a()-c.a()))); } |
---|
| 73 | /// Maths |
---|
[9869] | 74 | /** @param c the color to add to this one @returns the two added colors */ |
---|
[8448] | 75 | inline const Color& operator+=(const Color& c) { r()+=c.r(); g()+=c.g(); b()+=c.b(); a()+=c.a(); return *this; }; |
---|
[9869] | 76 | /** @returns the result of the added colors @param c the color to add */ |
---|
[8448] | 77 | inline Color operator+(const Color& c) const { return Color(r()+c.r(), g()+c.g(), b()+c.b(), a()+c.a()); }; |
---|
[9869] | 78 | /** @param c the color to substract to this one @returns the two substracted colors */ |
---|
[8448] | 79 | inline const Color& operator-=(const Color& c) { r()-=c.r(); g()-=c.g(); b()-=c.b(); a()-=c.a(); return *this; }; |
---|
[9869] | 80 | /** @returns the result of the substracted colors @param c the color to substract */ |
---|
[8448] | 81 | inline Color operator-(const Color& c) const { return Color(r()-c.r(), g()-c.g(), b()-c.b(), a()-c.a()); }; |
---|
[9869] | 82 | /** @param v the multiplier @returns the Color multiplied by v */ |
---|
[9656] | 83 | inline const Color& operator*=(float v) { r()*=v, g()*=v, b()*=v, a()*=v; return *this; }; |
---|
[9869] | 84 | /** @param v the multiplier @returns a multiplied color */ |
---|
[8448] | 85 | inline Color operator*(float v) const { return Color(r()*v, g()*v, b()*v, a()*v); }; |
---|
| 86 | |
---|
[9869] | 87 | /** @param c the color to slerp to @param v how much to slerp [0:1] @brief moves the color into the direction of another color */ |
---|
[8448] | 88 | void slerp(const Color& c, float v) { *this += (c - *this) * v; }; |
---|
[8986] | 89 | void slerpHSV(const Color& c, float v); |
---|
| 90 | static Color slerpHSVColor(const Color& from, const Color& to, float v); |
---|
| 91 | |
---|
[8448] | 92 | void debug() const; |
---|
| 93 | |
---|
[8376] | 94 | /// STATIC TRANSFORMATIONS |
---|
[8145] | 95 | public: |
---|
[5010] | 96 | static Vector RGBtoHSV (const Vector& RGB); |
---|
[7195] | 97 | static void RGBtoHSV (const Vector& RGB, Vector& HSV); |
---|
[5010] | 98 | static Vector HSVtoRGB (const Vector& HSV); |
---|
[7195] | 99 | static void HSVtoRGB (const Vector& HSV, Vector& RGB); |
---|
[5010] | 100 | |
---|
[7195] | 101 | private: |
---|
[5010] | 102 | static float minrgb(float r, float g, float b); |
---|
| 103 | static float maxrgb(float r, float g, float b); |
---|
[8145] | 104 | |
---|
[8986] | 105 | public: |
---|
| 106 | static const Color red; |
---|
| 107 | static const Color green; |
---|
| 108 | static const Color blue; |
---|
| 109 | static const Color white; |
---|
| 110 | static const Color black; |
---|
| 111 | |
---|
[8376] | 112 | private: |
---|
[9869] | 113 | float _rgba[4]; //!< Color Values [r,g,b,a] (red green blue alpha) |
---|
[1853] | 114 | }; |
---|
| 115 | |
---|
[5009] | 116 | #endif /* _COLOR_H */ |
---|