5e74ddca94e7f921a874aab69a570cfc5a0291b8
[synfig.git] / synfig-core / trunk / src / synfig / gradient.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file gradient.h
3 **      \brief Color Gradient Class
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_GRADIENT_H
26 #define __SYNFIG_GRADIENT_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include "real.h"
31 #include "color.h"
32 #include <vector>
33 #include <utility>
34 #include "uniqueid.h"
35
36 /* === M A C R O S ========================================================= */
37
38 /* === T Y P E D E F S ===================================================== */
39
40 /* === C L A S S E S & S T R U C T S ======================================= */
41
42 namespace synfig {
43
44 /*! \struct GradientCPoint
45 **      \brief \todo
46 */
47 struct GradientCPoint : public UniqueID
48 {
49         Real pos;
50         Color color;
51
52         bool operator<(const GradientCPoint &rhs)const { return pos<rhs.pos; }
53         bool operator<(const Real &rhs)const { return pos<rhs; }
54
55         GradientCPoint() { }
56         GradientCPoint(const Real &pos, const Color &color):pos(pos),color(color) { }
57 }; // END of class GradientCPoint
58
59
60 /*! \class Gradient
61 **      \brief Color Gradient Class
62 */
63 using namespace std;
64 class Gradient
65 {
66 public:
67         typedef GradientCPoint CPoint;
68         typedef vector<CPoint> CPointList;
69         typedef CPointList::const_iterator                      const_iterator;
70         typedef CPointList::iterator                            iterator;
71         typedef CPointList::const_reverse_iterator      const_reverse_iterator;
72         typedef CPointList::reverse_iterator            reverse_iterator;
73 private:
74         CPointList cpoints;
75 public:
76         Gradient() { }
77
78         //! Two-Tone Color Gradient Convenience Constructor
79         Gradient(const Color &c1, const Color &c2);
80
81         //! Three-Tone Color Gradient Convenience Constructor
82         Gradient(const Color &c1, const Color &c2, const Color &c3);
83
84         //! Alias for sort (Implemented for consistency)
85         void sync() { sort(); }
86
87         //! You should call this function after changing stuff.
88         void sort();
89
90         void push_back(const CPoint cpoint) { cpoints.push_back(cpoint); }
91         iterator erase(iterator iter) { return cpoints.erase(iter); }
92         bool empty()const { return cpoints.empty(); }
93         size_t size()const { return cpoints.size(); }
94
95         iterator begin() { return cpoints.begin(); }
96         iterator end() { return cpoints.end(); }
97         reverse_iterator rbegin() { return cpoints.rbegin(); }
98         reverse_iterator rend() { return cpoints.rend(); }
99         const_iterator begin()const { return cpoints.begin(); }
100         const_iterator end()const { return cpoints.end(); }
101         const_reverse_iterator rbegin()const { return cpoints.rbegin(); }
102         const_reverse_iterator rend()const { return cpoints.rend(); }
103
104         Gradient &operator+=(const Gradient &rhs);
105         Gradient &operator-=(const Gradient &rhs);
106         Gradient &operator*=(const float    &rhs);
107         Gradient &operator/=(const float    &rhs);
108
109         Gradient operator+(const Gradient &rhs)const { return Gradient(*this)+=rhs; }
110         Gradient operator-(const Gradient &rhs)const { return Gradient(*this)-=rhs; }
111         Gradient operator*(const float    &rhs)const { return Gradient(*this)*=rhs; }
112         Gradient operator/(const float    &rhs)const { return Gradient(*this)/=rhs; }
113
114         Color operator()(const Real &x, float supersample=0)const;
115
116         //! Returns the iterator of the CPoint closest to \a x
117         iterator proximity(const Real &x);
118
119         //! Returns the const_iterator of the CPoint closest to \a x
120         const_iterator proximity(const Real &x)const;
121
122         //! Returns the iterator of the CPoint with UniqueID \a id
123         iterator find(const UniqueID &id);
124
125         //! Returns the const_iterator of the CPoint with UniqueID \a id
126         const_iterator find(const UniqueID &id)const;
127 }; // END of class Gradient
128
129 }; // END of namespace synfig
130
131 /* === E N D =============================================================== */
132
133 #endif