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/Color.h>
34 #include <Pt/Gfx/Point.h>
35 #include <Pt/Gfx/Image.h>
36 #include <Pt/SmartPtr.h>
37 
38 namespace Pt {
39 
40 namespace Gfx {
41 
42 class BrushData;
43 class Bitmap;
44 
48 class ColorStop
49 {
50  public:
53  ColorStop(float position, const Color& color)
54  : _position(position)
55  , _color(color)
56  {}
57 
60  float position() const
61  { return _position; }
62 
65  const Color& color() const
66  { return _color; }
67 
68  private:
69  float _position;
70  Color _color;
71 };
72 
73 
78 {
79  public:
83  {}
84 
88  {}
89 
92  bool empty() const
93  { return _stops.empty(); }
94 
97  std::size_t size() const
98  { return _stops.size(); }
99 
102  void clear()
103  { _stops.clear(); }
104 
105  // TODO: Throw exception if the position < 0.0 or position > 1.0
106  // TODO: Throw exception if the positions are mixed up
109  void add(float position, const Color& color)
110  { _stops.push_back( ColorStop(position, color) ); }
111 
114  const ColorStop& operator[] (std::size_t n) const
115  { return _stops[n]; }
116 
119  const ColorStop& front() const
120  { return _stops.front(); }
121 
124  const ColorStop& back() const
125  { return _stops.back(); }
126 
129  void calculateInterpolatedColor(Color& res, const float position) const;
130 
131 
132  private:
133  std::vector<ColorStop> _stops;
134 };
135 
136 
144 class PT_GFX_API Brush
145 {
146  public:
150  {
151  Solid = 0,
152  Texture = 1,
153  Gradient = 2,
154  };
155 
159  {
160  Absolute = 0,
161  Relative = 1
162  };
163 
167  {
168  Horizontal = 0, // only for old painters
169  Vertical = 1, // only for old painters
170  Linear = 2,
171  Radial = 3
172  };
173 
174  public:
177  Brush();
178 
181  Brush(const Color& color);
182 
185  Brush(const Image& texture, Pt::int32_t offX = 0, Pt::int32_t offY = 0);
186 
189  static Brush verticalGradient(const Color& from, const Color& to);
190 
193  static Brush horizontalGradient(const Color& from, const Color& to);
194 
197  static Brush verticalGradient(const ColorStops& colorStops);
198 
201  static Brush horizontalGradient(const ColorStops& colorStops);
202 
205  static Brush linearGradient(const PointF& begin, const PointF& end,
206  const ColorStops& colorStops);
207 
210  static Brush linearGradient(float beginX, float beginY,
211  float endX, float endY,
212  const ColorStops& colorStops);
213 
216  static Brush radialGradient(const PointF& begin, float beginRadius,
217  const PointF& end, float endRadius,
218  const ColorStops& colorStops);
219 
222  static Brush radialGradient(float beginX, float beginY, float beginRadius,
223  float endX, float endY, float endRadius,
224  const ColorStops& colorStops);
225 
229 
233 
236  void setColor(const Color& color);
237 
240  const Color& color() const;
241 
245 
246  // remove when linear gradients use color stops
249  const Color& gradientColor() const;
250 
253  const ColorStops& gradientStops() const;
254 
257  const PointF& gradientBegin() const;
258 
261  float gradientBeginRadius() const;
262 
265  const PointF& gradientEnd() const;
266 
269  float gradientEndRadius() const;
270 
273  void setTexture(const Image& texture,
274  Pt::int32_t offX = 0, Pt::int32_t offY = 0);
275 
278  const Image& texture() const;
279 
280  // TODO: offset for textures is the origin and needs to be
281  // handled diferently in the painters
282 
286 
290 
293  bool isGradient() const;
294 
297  bool isTexture() const;
298 
301  bool isNull() const;
302 
303  private:
304  Brush(BrushData* data);
305 
306  private:
307  SmartPtr<BrushData> _brushData;
308 };
309 
310 
311 class BrushData
312 {
313  public:
314  BrushData()
315  : _isNull(true)
316  , _fillStyle(Brush::Solid)
317  , _color(0, 0, 0)
318  , _gradient(Brush::Horizontal)
319  , _ofsX(0)
320  , _ofsY(0)
321  , _texture(0)
322  {}
323 
324  BrushData(const Color& color)
325  : _isNull(false)
326  , _fillStyle(Brush::Solid)
327  , _color(color)
328  , _gradient(Brush::Horizontal)
329  , _ofsX(0)
330  , _ofsY(0)
331  , _texture(0)
332  {}
333 
334  BrushData(const Image& texture,
335  Pt::int32_t offsetX, Pt::int32_t offsetY);
336 
337  // only for old Painter
338  BrushData(const Color& from, const Color& to,
340 
341  ~BrushData();
342 
343  Brush::FillStyle fillStyle() const
344  { return _fillStyle; }
345 
346  Brush::PositionMode positionMode() const
347  { return _positionMode; }
348 
349  void setSolid(const Color& color);
350 
351  const Color& color() const
352  { return _gradientStops.empty() ? _color : _gradientStops.front().color(); }
353 
354  // 1D gradient
355  void set1DGradient(Brush::GradientStyle g, const ColorStops& colorStops);
356 
357  // absolute positioned linear gradient
358  void setLinearGradient(const PointF& begin, const PointF& end,
359  const ColorStops& colorStops);
360 
361  // relative positioned linear gradient
362  void setLinearGradient(float beginX, float beginY,
363  float endX, float endY,
364  const ColorStops& colorStops);
365 
366 
367  // absolute positioned radial gradient
368  void setRadialGradient(const PointF& begin, float beginRadius,
369  const PointF& end, float endRadius,
370  const ColorStops& colorStops);
371 
372  // relative positioned radial gradient
373  void setRadialGradient(float beginX, float beginY, float beginRadius,
374  float endX, float endY, float endRadius,
375  const ColorStops& colorStops);
376 
377  Brush::GradientStyle gradient() const
378  { return _gradient; }
379 
380  // remove when linear gradients use color stops
381  const Color& gradientColor() const
382  { return _gradientStops.empty() ? _color : _gradientStops.back().color(); }
383 
384  const ColorStops& gradientStops() const
385  { return _gradientStops; }
386 
387  const PointF& gradientBegin() const
388  { return _gradientBegin; }
389 
390  float gradientBeginRadius() const
391  { return _gradientBeginRadius; }
392 
393  const PointF& gradientEnd() const
394  { return _gradientEnd; }
395 
396  float gradientEndRadius() const
397  { return _gradientEndRadius; }
398 
399  void setTexture(const Image& texture,
400  Pt::int32_t offsetX, Pt::int32_t offsetY);
401 
402  const Image& texture() const;
403 
404  Pt::int32_t offsetX() const
405  { return _ofsX; }
406 
407  Pt::int32_t offsetY() const
408  { return _ofsY; }
409 
410  bool isGradient() const
411  { return _fillStyle == Brush::Gradient; }
412 
413  bool isTexture() const
414  { return _fillStyle == Brush::Texture; }
415 
416  bool isNull() const
417  { return _isNull; }
418 
419  private:
420  bool _isNull;
421  Brush::FillStyle _fillStyle;
422  Brush::PositionMode _positionMode;
423  Color _color;
424 
425  Brush::GradientStyle _gradient;
426  ColorStops _gradientStops;
427  PointF _gradientBegin;
428  float _gradientBeginRadius;
429  PointF _gradientEnd;
430  float _gradientEndRadius;
431 
432  Pt::int32_t _ofsX;
433  Pt::int32_t _ofsY;
434  Bitmap* _texture;
435 };
436 
437 } // namespace
438 
439 } // namespace
440 
441 #endif
Core module.
Definition: pt-gfx-images.dox:14
bool empty() const
Returns true if no stops are stored.
Definition: Brush.h:92
const ColorStops & gradientStops() const
Color stops of a gradient.
Brush(const Image &texture, Pt::int32_t offX=0, Pt::int32_t offY=0)
Constructs a texture brush.
bool isNull() const
Returns true if the brush is null.
const Color & color() const
Returns the solid or first gradient color.
static Brush verticalGradient(const Color &from, const Color &to)
Constructs a vertical gradient brush from two colors.
float position() const
Returns the normalized stop position.
Definition: Brush.h:60
static Brush radialGradient(float beginX, float beginY, float beginRadius, float endX, float endY, float endRadius, const ColorStops &colorStops)
Constructs a relative positioned radial gradient.
const ColorStop & operator[](std::size_t n) const
Returns the stop at the given index.
Definition: Brush.h:114
const Color & gradientColor() const
Returns the legacy secondary gradient color.
const Color & color() const
Returns the stop color.
Definition: Brush.h:65
PositionMode
Identifies how brush coordinates are interpreted.
Definition: Brush.h:159
bool isTexture() const
Returns true if the brush uses a texture.
static Brush linearGradient(float beginX, float beginY, float endX, float endY, const ColorStops &colorStops)
Constructs a relative positioned linear gradient.
const ColorStop & front() const
Returns the first stop.
Definition: Brush.h:119
PositionMode positionMode() const
Returns how brush coordinates are interpreted.
~ColorStops()
Destroys the collection.
Definition: Brush.h:87
void clear()
Removes all stored stops.
Definition: Brush.h:102
Brush(const Color &color)
Constructs a solid brush.
static Brush horizontalGradient(const Color &from, const Color &to)
Constructs a horizontal gradient brush from two colors.
float gradientBeginRadius() const
Radius of a radial gradient begin circle.
Point with floating-point X and Y coordinates.
Definition: Point.h:45
static Brush horizontalGradient(const ColorStops &colorStops)
Constructs a horizontal gradient brush from color stops.
void setTexture(const Image &texture, Pt::int32_t offX=0, Pt::int32_t offY=0)
Sets the texture image and offset.
void setColor(const Color &color)
Sets the solid fill color.
ColorStop(float position, const Color &color)
Constructs a color stop.
Definition: Brush.h:53
Brush()
Contructs a null brush.
static Brush radialGradient(const PointF &begin, float beginRadius, const PointF &end, float endRadius, const ColorStops &colorStops)
Constructs an absolute positioned radial gradient.
const Image & texture() const
Returns the texture image.
std::size_t size() const
Returns the number of stored stops.
Definition: Brush.h:97
const PointF & gradientBegin() const
Begin of a linear or radial gradient.
GradientStyle gradient() const
Returns the gradient style.
bool isGradient() const
Returns true if the brush uses a gradient.
Pt::int32_t offsetY() const
Returns the vertical texture offset.
GradientStyle
Identifies the gradient variant used by the brush.
Definition: Brush.h:167
void calculateInterpolatedColor(Color &res, const float position) const
Calculates the interpolated color for a position.
Basic image.
Definition: Image.h:52
const PointF & gradientEnd() const
End of a linear or radial gradient.
static Brush verticalGradient(const ColorStops &colorStops)
Constructs a vertical gradient brush from color stops.
Standard color type.
Definition: Color.h:47
FillStyle fillStyle() const
Returns the brushes fill style.
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
Fill description for shapes and text.
Definition: Brush.h:145
ColorStops()
Constructs an empty collection.
Definition: Brush.h:82
FillStyle
Identifies the fill source used by the brush.
Definition: Brush.h:150
void add(float position, const Color &color)
Appends a color stop.
Definition: Brush.h:109
static Brush linearGradient(const PointF &begin, const PointF &end, const ColorStops &colorStops)
Constructs an absolute positioned linear gradient.
Pt::int32_t offsetX() const
Returns the horizontal texture offset.
float gradientEndRadius() const
Radius of a radial gradient end circle.
const ColorStop & back() const
Returns the last stop.
Definition: Brush.h:124
Ordered collection of gradient color stops.
Definition: Brush.h:78
Color stop for a gradient brush.
Definition: Brush.h:49