Brush.h
1 /* Copyright (C) 2006-2017 Marc Boris Duerner
2  Copyright (C) 2017-2017 Aloysius Indrayanto
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 
30 #ifndef PT_GFX_BRUSH_H
31 #define PT_GFX_BRUSH_H
32 
33 #include <Pt/Gfx/Image.h>
34 #include <Pt/SmartPtr.h>
35 
36 namespace Pt {
37 
38 namespace Gfx {
39 
40 class BrushData;
41 
42 class ColorStop
43 {
44  public:
45  ColorStop(float position, const Color& color)
46  : _position(position)
47  , _color(color)
48  {}
49 
50  float position() const
51  { return _position; }
52 
53  const Color& color() const
54  { return _color; }
55 
56  private:
57  float _position;
58  Color _color;
59 };
60 
61 
62 class ColorStops
63 {
64  public:
65  ColorStops()
66  {}
67 
68  ~ColorStops()
69  {}
70 
71  bool empty() const
72  { return _stops.empty(); }
73 
74  std::size_t size() const
75  { return _stops.size(); }
76 
77  void clear()
78  { _stops.clear(); }
79 
80  // TODO: Throw exception if the position < 0.0 or position > 1.0
81  // TODO: Throw exception if the positions are mixed up
82  void add(float position, const Color& color)
83  { _stops.push_back( ColorStop(position, color) ); }
84 
85  const ColorStop& operator[] (std::size_t n) const
86  { return _stops[n]; }
87 
88  const ColorStop& front() const
89  { return _stops.front(); }
90 
91  const ColorStop& back() const
92  { return _stops.back(); }
93 
94  void calculateInterpolatedColor(Color& res, const float position) const;
95 
96 
97  private:
98  std::vector<ColorStop> _stops;
99 };
100 
101 
102 class PT_GFX_API Brush
103 {
104  public:
105  enum FillStyle
106  {
107  Solid = 0,
108  Texture = 1,
109  Gradient = 2,
110  };
111 
112  enum PositionMode
113  {
114  Absolute = 0,
115  Relative = 1
116  };
117 
118  enum GradientStyle
119  {
120  Horizontal = 0, // only for old painters
121  Vertical = 1, // only for old painters
122  Linear = 2,
123  Radial = 3
124  };
125 
126  public:
129  Brush();
130 
131  Brush(const Color& color);
132 
133  Brush(const Image& texture, Pt::int32_t offX = 0, Pt::int32_t offY = 0);
134 
135  static Brush verticalGradient(const Color& from, const Color& to);
136 
137  static Brush horizontalGradient(const Color& from, const Color& to);
138 
139  static Brush verticalGradient(const ColorStops& colorStops);
140 
141  static Brush horizontalGradient(const ColorStops& colorStops);
142 
145  static Brush linearGradient(const PointF& begin, const PointF& end,
146  const ColorStops& colorStops);
147 
150  static Brush linearGradient(float beginX, float beginY,
151  float endX, float endY,
152  const ColorStops& colorStops);
153 
156  static Brush radialGradient(const PointF& begin, float beginRadius,
157  const PointF& end, float endRadius,
158  const ColorStops& colorStops);
159 
162  static Brush radialGradient(float beginX, float beginY, float beginRadius,
163  float endX, float endY, float endRadius,
164  const ColorStops& colorStops);
165 
168  FillStyle fillStyle() const;
169 
170  PositionMode positionMode() const;
171 
172  void setColor(const Color& color);
173 
174  const Color& color() const;
175 
178  GradientStyle gradient() const;
179 
180  // remove when linear gradients use color stops
181  const Color& gradientColor() const;
182 
185  const ColorStops& gradientStops() const;
186 
189  const PointF& gradientBegin() const;
190 
193  float gradientBeginRadius() const;
194 
197  const PointF& gradientEnd() const;
198 
201  float gradientEndRadius() const;
202 
203  void setTexture(const Image& texture,
204  Pt::int32_t offX = 0, Pt::int32_t offY = 0);
205 
206  const Image& texture() const;
207 
208  // TODO: offset for textures is the origin and needs to be
209  // handled diferently in the painters
210 
211  Pt::int32_t offsetX() const;
212 
213  Pt::int32_t offsetY() const;
214 
215  bool isGradient() const;
216 
217  bool isTexture() const;
218 
219  bool isNull() const;
220 
221  private:
222  Brush(BrushData* data);
223 
224  private:
225  SmartPtr<BrushData> _brushData;
226 };
227 
228 
229 class BrushData
230 {
231  public:
232  BrushData()
233  : _isNull(true)
234  , _fillStyle(Brush::Solid)
235  , _color(0, 0, 0)
236  , _gradient(Brush::Horizontal)
237  {}
238 
239  BrushData(const Color& color)
240  : _isNull(false)
241  , _fillStyle(Brush::Solid)
242  , _color(color)
243  , _gradient(Brush::Horizontal)
244  {}
245 
246  BrushData(const Image& texture,
247  Pt::int32_t offsetX, Pt::int32_t offsetY);
248 
249  // only for old Painter
250  BrushData(const Color& from, const Color& to,
251  Brush::GradientStyle g);
252 
253  ~BrushData()
254  {}
255 
256  Brush::FillStyle fillStyle() const
257  { return _fillStyle; }
258 
259  Brush::PositionMode positionMode() const
260  { return _positionMode; }
261 
262  void setSolid(const Color& color);
263 
264  const Color& color() const
265  { return _gradientStops.empty() ? _color : _gradientStops.front().color(); }
266 
267  // 1D gradient
268  void set1DGradient(Brush::GradientStyle g, const ColorStops& colorStops);
269 
270  // absolute positioned linear gradient
271  void setLinearGradient(const PointF& begin, const PointF& end,
272  const ColorStops& colorStops);
273 
274  // relative positioned linear gradient
275  void setLinearGradient(float beginX, float beginY,
276  float endX, float endY,
277  const ColorStops& colorStops);
278 
279 
280  // absolute positioned radial gradient
281  void setRadialGradient(const PointF& begin, float beginRadius,
282  const PointF& end, float endRadius,
283  const ColorStops& colorStops);
284 
285  // relative positioned radial gradient
286  void setRadialGradient(float beginX, float beginY, float beginRadius,
287  float endX, float endY, float endRadius,
288  const ColorStops& colorStops);
289 
290  Brush::GradientStyle gradient() const
291  { return _gradient; }
292 
293  // remove when linear gradients use color stops
294  const Color& gradientColor() const
295  { return _gradientStops.empty() ? _color : _gradientStops.back().color(); }
296 
297  const ColorStops& gradientStops() const
298  { return _gradientStops; }
299 
300  const PointF& gradientBegin() const
301  { return _gradientBegin; }
302 
303  float gradientBeginRadius() const
304  { return _gradientBeginRadius; }
305 
306  const PointF& gradientEnd() const
307  { return _gradientEnd; }
308 
309  float gradientEndRadius() const
310  { return _gradientEndRadius; }
311 
312  void setTexture(const Image& texture,
313  Pt::int32_t offsetX, Pt::int32_t offsetY);
314 
315  const Image& texture() const
316  { return _texture; }
317 
318  Pt::int32_t offsetX() const
319  { return _ofsX; }
320 
321  Pt::int32_t offsetY() const
322  { return _ofsY; }
323 
324  bool isGradient() const
325  { return _fillStyle == Brush::Gradient; }
326 
327  bool isTexture() const
328  { return _fillStyle == Brush::Texture; }
329 
330  bool isNull() const
331  { return _isNull; }
332 
333  private:
334  bool _isNull;
335  Brush::FillStyle _fillStyle;
336  Brush::PositionMode _positionMode;
337  Color _color;
338 
339  Brush::GradientStyle _gradient;
340  ColorStops _gradientStops;
341  PointF _gradientBegin;
342  float _gradientBeginRadius;
343  PointF _gradientEnd;
344  float _gradientEndRadius;
345 
346  Pt::int32_t _ofsX;
347  Pt::int32_t _ofsY;
348  Image _texture;
349 };
350 
351 } // namespace
352 
353 } // namespace
354 
355 #endif
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36