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 
177  {
178  SourcePixels = 0
179  };
180 
181  public:
184  Brush();
185 
188  Brush(const Color& color);
189 
197  static Brush imageTexture(const Image& image,
198  const Gfx::PointF& origin = Gfx::PointF());
199 
202  static Brush verticalGradient(const Color& from, const Color& to);
203 
206  static Brush horizontalGradient(const Color& from, const Color& to);
207 
210  static Brush verticalGradient(const ColorStops& colorStops);
211 
214  static Brush horizontalGradient(const ColorStops& colorStops);
215 
218  static Brush linearGradient(const PointF& begin, const PointF& end,
219  const ColorStops& colorStops);
220 
223  static Brush linearGradient(float beginX, float beginY,
224  float endX, float endY,
225  const ColorStops& colorStops);
226 
229  static Brush radialGradient(const PointF& begin, float beginRadius,
230  const PointF& end, float endRadius,
231  const ColorStops& colorStops);
232 
235  static Brush radialGradient(float beginX, float beginY, float beginRadius,
236  float endX, float endY, float endRadius,
237  const ColorStops& colorStops);
238 
242 
246 
249  void setColor(const Color& color);
250 
253  const Color& color() const;
254 
258 
259  // remove when linear gradients use color stops
262  const Color& gradientColor() const;
263 
266  const ColorStops& gradientStops() const;
267 
270  const PointF& gradientBegin() const;
271 
274  float gradientBeginRadius() const;
275 
278  const PointF& gradientEnd() const;
279 
282  float gradientEndRadius() const;
283 
287 
290  const Image& texture() const;
291 
297  const PointF& textureOrigin() const;
298 
301  bool isGradient() const;
302 
305  bool isTexture() const;
306 
309  bool isNull() const;
310 
311  private:
312  Brush(BrushData* data);
313 
314  private:
315  SmartPtr<BrushData> _brushData;
316 };
317 
318 
319 class BrushData
320 {
321  public:
322  BrushData()
323  : _isNull(true)
324  , _fillStyle(Brush::Solid)
325  , _positionMode(Brush::Relative)
326  , _color(0, 0, 0)
327  , _gradient(Brush::Horizontal)
328  , _gradientBeginRadius(0.0f)
329  , _gradientEndRadius(0.0f)
330  , _textureMode(Brush::SourcePixels)
331  , _textureOrigin()
332  , _texture(0)
333  {}
334 
335  BrushData(const Color& color)
336  : _isNull(false)
337  , _fillStyle(Brush::Solid)
338  , _positionMode(Brush::Relative)
339  , _color(color)
340  , _gradient(Brush::Horizontal)
341  , _gradientBeginRadius(0.0f)
342  , _gradientEndRadius(0.0f)
343  , _textureMode(Brush::SourcePixels)
344  , _textureOrigin()
345  , _texture(0)
346  {}
347 
348  BrushData(const Image& texture, const PointF& textureOrigin);
349 
350  BrushData(const BrushData& other);
351 
352  BrushData& operator=(const BrushData& other);
353 
354  // only for old Painter
355  BrushData(const Color& from, const Color& to,
357 
358  ~BrushData();
359 
360  Brush::FillStyle fillStyle() const
361  { return _fillStyle; }
362 
363  Brush::PositionMode positionMode() const
364  { return _positionMode; }
365 
366  void setSolid(const Color& color);
367 
368  const Color& color() const
369  { return _gradientStops.empty() ? _color : _gradientStops.front().color(); }
370 
371  // 1D gradient
372  void set1DGradient(Brush::GradientStyle g, const ColorStops& colorStops);
373 
374  // absolute positioned linear gradient
375  void setLinearGradient(const PointF& begin, const PointF& end,
376  const ColorStops& colorStops);
377 
378  // relative positioned linear gradient
379  void setLinearGradient(float beginX, float beginY,
380  float endX, float endY,
381  const ColorStops& colorStops);
382 
383 
384  // absolute positioned radial gradient
385  void setRadialGradient(const PointF& begin, float beginRadius,
386  const PointF& end, float endRadius,
387  const ColorStops& colorStops);
388 
389  // relative positioned radial gradient
390  void setRadialGradient(float beginX, float beginY, float beginRadius,
391  float endX, float endY, float endRadius,
392  const ColorStops& colorStops);
393 
394  Brush::GradientStyle gradient() const
395  { return _gradient; }
396 
397  // remove when linear gradients use color stops
398  const Color& gradientColor() const
399  { return _gradientStops.empty() ? _color : _gradientStops.back().color(); }
400 
401  const ColorStops& gradientStops() const
402  { return _gradientStops; }
403 
404  const PointF& gradientBegin() const
405  { return _gradientBegin; }
406 
407  float gradientBeginRadius() const
408  { return _gradientBeginRadius; }
409 
410  const PointF& gradientEnd() const
411  { return _gradientEnd; }
412 
413  float gradientEndRadius() const
414  { return _gradientEndRadius; }
415 
416  void setImageTexture(const Image& texture, const PointF& textureOrigin);
417 
418  const Image& texture() const;
419 
420  Brush::TextureMode textureMode() const
421  { return _textureMode; }
422 
423  const PointF& textureOrigin() const
424  { return _textureOrigin; }
425 
426  bool isGradient() const
427  { return _fillStyle == Brush::Gradient; }
428 
429  bool isTexture() const
430  { return _fillStyle == Brush::Texture; }
431 
432  bool isNull() const
433  { return _isNull; }
434 
435  private:
436  bool _isNull;
437  Brush::FillStyle _fillStyle;
438  Brush::PositionMode _positionMode;
439  Color _color;
440 
441  Brush::GradientStyle _gradient;
442  ColorStops _gradientStops;
443  PointF _gradientBegin;
444  float _gradientBeginRadius;
445  PointF _gradientEnd;
446  float _gradientEndRadius;
447 
448  Brush::TextureMode _textureMode;
449  PointF _textureOrigin;
450  Bitmap* _texture;
451 };
452 
453 } // namespace
454 
455 } // namespace
456 
457 #endif
TextureMode
Identifies how image texture coordinates are interpreted.
Definition: Brush.h:177
Core module.
Definition: Allocator.h:33
bool empty() const
Returns true if no stops are stored.
Definition: Brush.h:92
const ColorStops & gradientStops() const
Color stops of a gradient.
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.
static Brush imageTexture(const Image &image, const Gfx::PointF &origin=Gfx::PointF())
Constructs an image texture brush.
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:47
const PointF & textureOrigin() const
Returns the texture pattern origin.
static Brush horizontalGradient(const ColorStops &colorStops)
Constructs a horizontal gradient brush from color stops.
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.
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.
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.
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
TextureMode textureMode() const
Returns how the texture origin is interpreted.