1 /* === S Y N F I G ========================================================= */ 2 /*! \file 3 ** \brief CairoColorAccumulator Class Implementation 4 ** 5 ** $Id$ 6 ** 7 ** \legal 8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley 9 ** Copyright (c) 2007, 2008 Chris Moore 10 ** Copyright (c) 2012-2013 Carlos López 11 ** Copyright (c) 2015 Diego Barrios Romero 12 ** 13 ** This package is free software; you can redistribute it and/or 14 ** modify it under the terms of the GNU General Public License as 15 ** published by the Free Software Foundation; either version 2 of 16 ** the License, or (at your option) any later version. 17 ** 18 ** This package is distributed in the hope that it will be useful, 19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 ** General Public License for more details. 22 ** \endlegal 23 */ 24 /* ========================================================================= */ 25 26 #ifndef __SYNFIG_COLOR_CAIROCOLORACUMULATOR_H 27 #define __SYNFIG_COLOR_CAIROCOLORACUMULATOR_H 28 29 #include <synfig/color/common.h> 30 31 namespace synfig { 32 33 class CairoColor; 34 35 class CairoColorAccumulator 36 { 37 friend class CairoColor; 38 public: 39 typedef float value_type; 40 41 private: 42 value_type a_, r_, g_, b_; 43 44 public: 45 46 CairoColorAccumulator & 47 operator+=(const CairoColorAccumulator &rhs) 48 { 49 r_+=rhs.r_; 50 g_+=rhs.g_; 51 b_+=rhs.b_; 52 a_+=rhs.a_; 53 return *this; 54 } 55 56 CairoColorAccumulator & 57 operator-=(const CairoColorAccumulator &rhs) 58 { 59 r_-=rhs.r_; 60 g_-=rhs.g_; 61 b_-=rhs.b_; 62 a_-=rhs.a_; 63 return *this; 64 } 65 66 CairoColorAccumulator & 67 operator*=(const float &rhs) 68 { 69 r_*=rhs; 70 g_*=rhs; 71 b_*=rhs; 72 a_*=rhs; 73 return *this; 74 } 75 76 CairoColorAccumulator & 77 operator/=(const float &rhs) 78 { 79 const float temp(value_type(1)/rhs); 80 r_*=temp; 81 g_*=temp; 82 b_*=temp; 83 a_*=temp; 84 return *this; 85 } 86 87 CairoColorAccumulator 88 operator+(const CairoColorAccumulator &rhs)const 89 { return CairoColorAccumulator(*this)+=rhs; } 90 91 CairoColorAccumulator 92 operator-(const CairoColorAccumulator &rhs)const 93 { return CairoColorAccumulator(*this)-=rhs; } 94 95 CairoColorAccumulator 96 operator*(const float &rhs)const 97 { return CairoColorAccumulator(*this)*=rhs; } 98 99 CairoColorAccumulator 100 operator/(const float &rhs)const 101 { return CairoColorAccumulator(*this)/=rhs; } 102 103 bool 104 operator==(const CairoColorAccumulator &rhs)const 105 { return r_==rhs.r_ && g_==rhs.g_ && b_==rhs.b_ && a_!=rhs.a_; } 106 107 bool 108 operator!=(const CairoColorAccumulator &rhs)const 109 { return r_!=rhs.r_ || g_!=rhs.g_ || b_!=rhs.b_ || a_!=rhs.a_; } 110 111 CairoColorAccumulator 112 operator-()const 113 { return CairoColorAccumulator(-r_,-g_,-b_,-a_); } 114 is_valid()115 bool is_valid()const 116 { return !std::isnan(r_) && !std::isnan(g_) && !std::isnan(b_) && !std::isnan(a_); } 117 118 public: CairoColorAccumulator()119 CairoColorAccumulator(): a_(), r_(), g_(), b_() { } 120 121 /*! \param R Red 122 ** \param G Green 123 ** \param B Blue 124 ** \param A Opacity(alpha) */ 125 CairoColorAccumulator(const value_type& R, const value_type& G, const value_type& B, const value_type& A=1): a_(A)126 a_(A), 127 r_(R), 128 g_(G), 129 b_(B) { } 130 131 //! Copy constructor CairoColorAccumulator(const CairoColorAccumulator & c)132 CairoColorAccumulator(const CairoColorAccumulator& c): 133 a_(c.a_), 134 r_(c.r_), 135 g_(c.g_), 136 b_(c.b_) { } 137 138 //! Converter CairoColorAccumulator(const CairoColor & c)139 CairoColorAccumulator(const CairoColor& c): 140 a_(c.get_a()/CairoColor::range), 141 r_(c.get_r()/CairoColor::range), 142 g_(c.get_g()/CairoColor::range), 143 b_(c.get_b()/CairoColor::range) { } 144 145 //! Converter CairoColorAccumulator(int c)146 CairoColorAccumulator(int c): a_(c),r_(c), g_(c), b_(c) { } 147 148 //! Returns the RED component get_r()149 const value_type& get_r()const { return r_; } 150 151 //! Returns the GREEN component get_g()152 const value_type& get_g()const { return g_; } 153 154 //! Returns the BLUE component get_b()155 const value_type& get_b()const { return b_; } 156 157 //! Returns the amount of opacity (alpha) get_a()158 const value_type& get_a()const { return a_; } 159 160 //! Synonym for get_a(). \see get_a() get_alpha()161 const value_type& get_alpha()const { return get_a(); } 162 163 //! Sets the RED component to \a x set_r(const value_type & x)164 CairoColorAccumulator& set_r(const value_type& x) { r_ = x; return *this; } 165 166 //! Sets the GREEN component to \a x set_g(const value_type & x)167 CairoColorAccumulator& set_g(const value_type& x) { g_ = x; return *this; } 168 169 //! Sets the BLUE component to \a x set_b(const value_type & x)170 CairoColorAccumulator& set_b(const value_type& x) { b_ = x; return *this; } 171 172 //! Sets the opacity (alpha) to \a x set_a(const value_type & x)173 CairoColorAccumulator& set_a(const value_type& x) { a_ = x; return *this; } 174 175 //! Synonym for set_a(). \see set_a() set_alpha(const value_type & x)176 CairoColorAccumulator& set_alpha(const value_type& x) { return set_a(x); } 177 }; 178 179 180 } // synfig namespace 181 182 #endif // __SYNFIG_COLOR_CAIROCOLORACUMULATOR_H 183