36e6ee4fbfb26efa3fef9587aff29eda12eb1c51
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / gradient / gradient.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_GRADIENT_H__
2 #define __DALI_TOOLKIT_INTERNAL_GRADIENT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 //EXTERNAL INCLUDES
22 #include <dali/public-api/math/matrix3.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/images/buffer-image.h>
26
27 namespace Dali
28 {
29
30 class Vector4;
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 /**
39  * Gradients consist of continuously smooth color transitions along a vector from one color to another,
40  * possibly followed by additional transitions along the same vector to other colors.
41  */
42 class Gradient : public RefObject
43 {
44 public:
45   /**
46    * Defines the coordinate system of the attributes
47    *     (start and end position for linear gradient, circle center and radius for radial gradient)
48    */
49   enum GradientUnits
50   {
51     USER_SPACE_ON_USE,
52     OBJECT_BOUNDING_BOX
53   };
54
55   /**
56    * Indicates what happens if the gradient starts or ends inside the bounds of the object being painted by the gradient.
57    */
58   enum SpreadMethod
59   {
60     PAD,      // use the terminal colors of the gradient to fill the remainder of the target region
61     REPEAT,   // reflect the gradient pattern start-to-end, end-to-start, start-to-end, etc. continuously until the target rectangle is filled
62     REFLECT   // repeat the gradient pattern start-to-end, start-to-end, start-to-end, etc. continuously until the target region is filled
63   };
64
65   /**
66    * The stop node tells the gradient what color it should be at certain position.
67    */
68   struct GradientStop
69   {
70     GradientStop( float offset, const Vector4& color )
71     : mOffset( offset ), mStopColor( color )
72     {}
73
74     bool operator<(const GradientStop& rhs) const
75     {
76       return mOffset < rhs.mOffset;
77     }
78
79     float   mOffset;     // A value ranging from 0 to 1 to indicate where the gradient stop is placed.
80     Vector4 mStopColor;  // The color to use at this gradient stop
81   };
82
83 public:
84
85   /**
86    * Add a gradient stop.
87    *
88    * @param[in] offset The position to place the stop.
89    * @param[in] color  The color to use at this stop.
90    */
91   void AddStop(float offset, const Vector4& color);
92
93   /**
94    * Get the gradient stops.
95    * @return The vector of gradient stops.
96    */
97   const Vector<GradientStop>& GetStops();
98
99   /**
100    * Set the coordinate system used by the gradient attributes.
101    * @param[in] gradientUnits The the attributes are defined using the current user coordinate system or the bounding box of the shape.
102    */
103   void SetGradientUnits( GradientUnits gradientUnits );
104
105   /**
106    * Get the coordinate system used by the gradient attributes.
107    * @return USER_SPACE_ON_USE or OBJECT_BOUNDING_BOX
108    */
109   GradientUnits GetGradientUnits() const;
110
111   /**
112    * Indicates what happens if the gradient starts or ends inside the bounds of the target rectangle.
113    * If not specified, the effect is as if a value of 'pad' were specified
114    *
115    * @param[in] spread The method to fill the remainder of target region which is outside the gradient bounds
116    */
117   void SetSpreadMethod( SpreadMethod spread );
118
119   /**
120    * Get the filling method for the the remainder of target region which is outside the gradient boun.
121    * @return PAD, REFLECT or REPEAT
122    */
123   SpreadMethod GetSpreadMethod() const;
124
125   /**
126    * Get the transformation matrix to align the vertices with the gradient line/circle
127    * @ return the aligning transformation matrix
128    */
129   const Matrix3& GetAlignmentTransform() const;
130
131   /**
132    * Generate the lookup texture with the gradient stops.
133    * @return the lookup texture which transit smoothly between stops.
134    */
135   BufferImage GenerateLookupTexture();
136
137 private:
138
139   /**
140    * Estimate the resolution of the lookup texture.
141    * Note: Only call this function after the gradient stops are sorted in order.
142    */
143   unsigned int EstimateTextureResolution();
144
145 protected:
146
147   /**
148    * Construct a new Gradient object
149    * Called in the constructor of subclasses
150    */
151   Gradient();
152
153   /**
154    * @brief A reference counted object may only be deleted by calling Unreference().
155    */
156   virtual ~Gradient();
157
158   // Undefined
159   Gradient( const Gradient& gradient );
160
161   // Undefined
162   Gradient& operator=( const Gradient& handle );
163
164 protected:
165
166   Vector<GradientStop>      mGradientStops;
167   Matrix3                   mAlignmentTransform;
168   GradientUnits             mGradientUnits;
169   SpreadMethod              mSpreadMethod;
170
171 };
172
173 } // namespace Internal
174
175 } // namespace Toolkit
176
177 } // namespace Dali
178
179 #endif /* __DALI_TOOLKIT_INTERNAL_GRADIENT_RENDERER_H__ */