1 | /*! |
---|
2 | * @file color.h |
---|
3 | * @brief Definition of color-calculations |
---|
4 | * |
---|
5 | * TODO CONVERT THE VECTORS to COLORS!! |
---|
6 | * |
---|
7 | * code borrowed from: |
---|
8 | * http://www.easyrgb.com/math.php |
---|
9 | */ |
---|
10 | |
---|
11 | #ifndef _COLOR_H |
---|
12 | #define _COLOR_H |
---|
13 | |
---|
14 | #include "vector.h" |
---|
15 | |
---|
16 | //! a very abstract Class that helps transforming Colors into different Systems |
---|
17 | class Color |
---|
18 | { |
---|
19 | public: |
---|
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; }; |
---|
21 | Color(const Color& c) { _rgba[0] = c.r(); _rgba[1] = c.g(); _rgba[2] = c.b(); _rgba[3] = c.a(); } |
---|
22 | |
---|
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()); }; |
---|
25 | |
---|
26 | inline float& operator[](unsigned int i) { return _rgba[i]; } |
---|
27 | inline const float& operator[](unsigned int i) const { return _rgba[i]; } |
---|
28 | |
---|
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]; } |
---|
37 | |
---|
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; }; |
---|
53 | void slerpHSV(const Color& c, float v); |
---|
54 | |
---|
55 | static Color slerpHSVColor(const Color& from, const Color& to, float v); |
---|
56 | |
---|
57 | void debug() const; |
---|
58 | |
---|
59 | /// STATIC TRANSFORMATIONS |
---|
60 | public: |
---|
61 | static Vector RGBtoHSV (const Vector& RGB); |
---|
62 | static void RGBtoHSV (const Vector& RGB, Vector& HSV); |
---|
63 | static Vector HSVtoRGB (const Vector& HSV); |
---|
64 | static void HSVtoRGB (const Vector& HSV, Vector& RGB); |
---|
65 | |
---|
66 | private: |
---|
67 | static float minrgb(float r, float g, float b); |
---|
68 | static float maxrgb(float r, float g, float b); |
---|
69 | |
---|
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 | |
---|
78 | private: |
---|
79 | float _rgba[4]; //!< Color Values |
---|
80 | |
---|
81 | /* float _r; //!< Red Value. |
---|
82 | float _g; //!< Green Value. |
---|
83 | float _b; //!< Blue Value. |
---|
84 | float _a; //!< Alpha Value.*/ |
---|
85 | }; |
---|
86 | |
---|
87 | #endif /* _COLOR_H */ |
---|