| 1 | #ifndef FG_GUI_COLOR_HXX |
| 2 | #define FG_GUI_COLOR_HXX |
| 3 | |
| 4 | // forward decls |
| 5 | class SGPropertyNode; |
| 6 | |
| 7 | class FGColor { |
| 8 | public: |
| 9 | FGColor() { clear(); } |
| 10 | FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); } |
| 11 | FGColor(const SGPropertyNode *prop) { set(prop); } |
| 12 | FGColor(FGColor *c) { |
| 13 | if (c) set(c->_red, c->_green, c->_blue, c->_alpha); |
| 14 | } |
| 15 | |
| 16 | inline void clear() { _red = _green = _blue = _alpha = -1.0f; } |
| 17 | // merges in non-negative components from property with children <red> etc. |
| 18 | bool merge(const SGPropertyNode *prop); |
| 19 | bool merge(const FGColor *color); |
| 20 | |
| 21 | bool set(const SGPropertyNode *prop) { clear(); return merge(prop); }; |
| 22 | bool set(const FGColor *color) { clear(); return merge(color); } |
| 23 | bool set(float r, float g, float b, float a = 1.0f) { |
| 24 | _red = r, _green = g, _blue = b, _alpha = a; |
| 25 | return true; |
| 26 | } |
| 27 | bool isValid() const { |
| 28 | return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0; |
| 29 | } |
| 30 | void print() const; |
| 31 | |
| 32 | inline void setRed(float red) { _red = red; } |
| 33 | inline void setGreen(float green) { _green = green; } |
| 34 | inline void setBlue(float blue) { _blue = blue; } |
| 35 | inline void setAlpha(float alpha) { _alpha = alpha; } |
| 36 | |
| 37 | inline float red() const { return clamp(_red); } |
| 38 | inline float green() const { return clamp(_green); } |
| 39 | inline float blue() const { return clamp(_blue); } |
| 40 | inline float alpha() const { return _alpha < 0.0 ? 1.0f : clamp(_alpha); } |
| 41 | |
| 42 | protected: |
| 43 | float _red, _green, _blue, _alpha; |
| 44 | |
| 45 | private: |
| 46 | float clamp(float f) const { return f < 0.0 ? 0.0f : f > 1.0 ? 1.0f : f; } |
| 47 | }; |
| 48 | |
| 49 | #endif |