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 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 | */ |
---|
30 | class Color |
---|
31 | { |
---|
32 | public: |
---|
33 | /** @param r red, @param g green @param b blue @param a alpha @brief constructs a Color. */ |
---|
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; }; |
---|
35 | /** @param c Color @brief copy constructor */ |
---|
36 | Color(const Color& c) { _rgba[0] = c.r(); _rgba[1] = c.g(); _rgba[2] = c.b(); _rgba[3] = c.a(); } |
---|
37 | |
---|
38 | /** @param c the Color to set to this color @returns the copied color */ |
---|
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; }; |
---|
40 | /** @param c the color to compare @returns true on match. @brief compares two colors */ |
---|
41 | inline bool operator==(const Color& c) const { return (r() == c.r() && g() == c.g() && b() == c.b() && a() == c.a()); }; |
---|
42 | |
---|
43 | /** @returns the i'th Value of the Color @param i part of the color 0:r, 1:g, 2:b, 3:a */ |
---|
44 | inline float& operator[](unsigned int i) { return _rgba[i]; } |
---|
45 | /** @returns a Constant Value of the color. @param i part of the color 0:r, 1:g, 2:b, 3:a */ |
---|
46 | inline const float& operator[](unsigned int i) const { return _rgba[i]; } |
---|
47 | |
---|
48 | /** @returns the red part. */ |
---|
49 | inline float r() const { return _rgba[0]; } |
---|
50 | /** @returns the reference to the red part */ |
---|
51 | inline float& r() { return _rgba[0]; } |
---|
52 | /** @returns the green part. */ |
---|
53 | inline float g() const { return _rgba[1]; } |
---|
54 | /** @returns the reference to the green part */ |
---|
55 | inline float& g() { return _rgba[1]; } |
---|
56 | /** @returns the blue part */ |
---|
57 | inline float b() const { return _rgba[2]; } |
---|
58 | /** @returns the reference to the blue part */ |
---|
59 | inline float& b() { return _rgba[2]; } |
---|
60 | /** @returns the alpha part */ |
---|
61 | inline float a() const { return _rgba[3]; } |
---|
62 | /** @returns the reference to the alpha part */ |
---|
63 | inline float& a() { return _rgba[3]; } |
---|
64 | |
---|
65 | |
---|
66 | /** @param r red, @param g green @param b blue @param a alpha @brief sets the color. */ |
---|
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; }; |
---|
68 | /** @param c the color to set. @brief sets the color. */ |
---|
69 | void setColor(const Color& c) { r() = c.r(); g()= c.g(); b() = c.b(); a() = c.a(); }; |
---|
70 | |
---|
71 | /** @returns the distance to the color @param c the color to calculate the distance to. */ |
---|
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 |
---|
74 | /** @param c the color to add to this one @returns the two added colors */ |
---|
75 | inline const Color& operator+=(const Color& c) { r()+=c.r(); g()+=c.g(); b()+=c.b(); a()+=c.a(); return *this; }; |
---|
76 | /** @returns the result of the added colors @param c the color to add */ |
---|
77 | inline Color operator+(const Color& c) const { return Color(r()+c.r(), g()+c.g(), b()+c.b(), a()+c.a()); }; |
---|
78 | /** @param c the color to substract to this one @returns the two substracted colors */ |
---|
79 | inline const Color& operator-=(const Color& c) { r()-=c.r(); g()-=c.g(); b()-=c.b(); a()-=c.a(); return *this; }; |
---|
80 | /** @returns the result of the substracted colors @param c the color to substract */ |
---|
81 | inline Color operator-(const Color& c) const { return Color(r()-c.r(), g()-c.g(), b()-c.b(), a()-c.a()); }; |
---|
82 | /** @param v the multiplier @returns the Color multiplied by v */ |
---|
83 | inline const Color& operator*=(float v) { r()*=v, g()*=v, b()*=v, a()*=v; return *this; }; |
---|
84 | /** @param v the multiplier @returns a multiplied color */ |
---|
85 | inline Color operator*(float v) const { return Color(r()*v, g()*v, b()*v, a()*v); }; |
---|
86 | |
---|
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 */ |
---|
88 | void slerp(const Color& c, float v) { *this += (c - *this) * v; }; |
---|
89 | void slerpHSV(const Color& c, float v); |
---|
90 | static Color slerpHSVColor(const Color& from, const Color& to, float v); |
---|
91 | |
---|
92 | void debug() const; |
---|
93 | |
---|
94 | /// STATIC TRANSFORMATIONS |
---|
95 | public: |
---|
96 | static Vector RGBtoHSV (const Vector& RGB); |
---|
97 | static void RGBtoHSV (const Vector& RGB, Vector& HSV); |
---|
98 | static Vector HSVtoRGB (const Vector& HSV); |
---|
99 | static void HSVtoRGB (const Vector& HSV, Vector& RGB); |
---|
100 | |
---|
101 | private: |
---|
102 | static float minrgb(float r, float g, float b); |
---|
103 | static float maxrgb(float r, float g, float b); |
---|
104 | |
---|
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 | |
---|
112 | private: |
---|
113 | float _rgba[4]; //!< Color Values [r,g,b,a] (red green blue alpha) |
---|
114 | }; |
---|
115 | |
---|
116 | #endif /* _COLOR_H */ |
---|