[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 | |
---|
[5011] | 16 | //! a very abstract Class that helps transforming Colors into different Systems |
---|
[5009] | 17 | class Color |
---|
| 18 | { |
---|
[7195] | 19 | public: |
---|
[8448] | 20 | 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; }; |
---|
[8376] | 21 | Color(const Color& c) { _rgba[0] = c.r(); _rgba[1] = c.g(); _rgba[2] = c.b(); _rgba[3] = c.a(); } |
---|
[8145] | 22 | |
---|
[8448] | 23 | 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; }; |
---|
| 24 | inline bool operator==(const Color& c) const { return (r() == c.r() && g() == c.g() && b() == c.b() && a() == c.a()); }; |
---|
[8145] | 25 | |
---|
[8448] | 26 | inline float& operator[](unsigned int i) { return _rgba[i]; } |
---|
| 27 | inline const float& operator[](unsigned int i) const { return _rgba[i]; } |
---|
[8376] | 28 | |
---|
[8448] | 29 | inline float r() const { return _rgba[0]; } |
---|
| 30 | inline float& r() { return _rgba[0]; } |
---|
| 31 | inline float g() const { return _rgba[1]; } |
---|
| 32 | inline float& g() { return _rgba[1]; } |
---|
| 33 | inline float b() const { return _rgba[2]; } |
---|
| 34 | inline float& b() { return _rgba[2]; } |
---|
| 35 | inline float a() const { return _rgba[3]; } |
---|
| 36 | inline float& a() { return _rgba[3]; } |
---|
[8376] | 37 | |
---|
[8448] | 38 | |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | 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; }; |
---|
| 42 | void setColor(const Color& c) { r() = c.r(); g()= c.g(); b() = c.b(); a() = c.a(); }; |
---|
| 43 | |
---|
| 44 | 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()))); } |
---|
| 45 | /// Maths |
---|
| 46 | inline const Color& operator+=(const Color& c) { r()+=c.r(); g()+=c.g(); b()+=c.b(); a()+=c.a(); return *this; }; |
---|
| 47 | inline Color operator+(const Color& c) const { return Color(r()+c.r(), g()+c.g(), b()+c.b(), a()+c.a()); }; |
---|
| 48 | inline const Color& operator-=(const Color& c) { r()-=c.r(); g()-=c.g(); b()-=c.b(); a()-=c.a(); return *this; }; |
---|
| 49 | inline Color operator-(const Color& c) const { return Color(r()-c.r(), g()-c.g(), b()-c.b(), a()-c.a()); }; |
---|
| 50 | inline Color operator*(float v) const { return Color(r()*v, g()*v, b()*v, a()*v); }; |
---|
| 51 | |
---|
| 52 | void slerp(const Color& c, float v) { *this += (c - *this) * v; }; |
---|
[8986] | 53 | void slerpHSV(const Color& c, float v); |
---|
[8448] | 54 | |
---|
[8986] | 55 | static Color slerpHSVColor(const Color& from, const Color& to, float v); |
---|
| 56 | |
---|
[8448] | 57 | void debug() const; |
---|
| 58 | |
---|
[8376] | 59 | /// STATIC TRANSFORMATIONS |
---|
[8145] | 60 | public: |
---|
[5010] | 61 | static Vector RGBtoHSV (const Vector& RGB); |
---|
[7195] | 62 | static void RGBtoHSV (const Vector& RGB, Vector& HSV); |
---|
[5010] | 63 | static Vector HSVtoRGB (const Vector& HSV); |
---|
[7195] | 64 | static void HSVtoRGB (const Vector& HSV, Vector& RGB); |
---|
[5010] | 65 | |
---|
[7195] | 66 | private: |
---|
[5010] | 67 | static float minrgb(float r, float g, float b); |
---|
| 68 | static float maxrgb(float r, float g, float b); |
---|
[8145] | 69 | |
---|
[8986] | 70 | public: |
---|
| 71 | static const Color red; |
---|
| 72 | static const Color green; |
---|
| 73 | static const Color blue; |
---|
| 74 | static const Color white; |
---|
| 75 | static const Color black; |
---|
| 76 | static const Color orx; |
---|
| 77 | |
---|
[8376] | 78 | private: |
---|
| 79 | float _rgba[4]; //!< Color Values |
---|
| 80 | |
---|
| 81 | /* float _r; //!< Red Value. |
---|
[8145] | 82 | float _g; //!< Green Value. |
---|
| 83 | float _b; //!< Blue Value. |
---|
[8376] | 84 | float _a; //!< Alpha Value.*/ |
---|
[1853] | 85 | }; |
---|
| 86 | |
---|
[5009] | 87 | #endif /* _COLOR_H */ |
---|