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
38namespace Pt {
39
40namespace Gfx {
41
42class BrushData;
43class Bitmap;
44
49{
50 public:
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
150class PT_GFX_API Brush
151{
152 public:
156 {
157 Solid = 0,
158 Texture = 1,
159 Gradient = 2,
160 };
161
165 {
166 Absolute = 0,
167 Relative = 1
168 };
169
173 {
174 Horizontal = 0, // only for old painters
175 Vertical = 1, // only for old painters
176 Linear = 2,
177 Radial = 3
178 };
179
183 {
184 SourcePixels = 0
185 };
186
187 public:
191
195
203 static Brush imageTexture(const Image& image,
204 const Gfx::PointF& origin = Gfx::PointF());
205
208 static Brush verticalGradient(const Color& from, const Color& to);
209
212 static Brush horizontalGradient(const Color& from, const Color& to);
213
216 static Brush verticalGradient(const ColorStops& colorStops);
217
220 static Brush horizontalGradient(const ColorStops& colorStops);
221
224 static Brush linearGradient(const PointF& begin, const PointF& end,
225 const ColorStops& colorStops);
226
229 static Brush linearGradient(float beginX, float beginY,
230 float endX, float endY,
231 const ColorStops& colorStops);
232
235 static Brush radialGradient(const PointF& begin, float beginRadius,
236 const PointF& end, float endRadius,
237 const ColorStops& colorStops);
238
241 static Brush radialGradient(float beginX, float beginY, float beginRadius,
242 float endX, float endY, float endRadius,
243 const ColorStops& colorStops);
244
248
252
255 void setColor(const Color& color);
256
259 const Color& color() const;
260
264
265 // remove when linear gradients use color stops
268 const Color& gradientColor() const;
269
272 const ColorStops& gradientStops() const;
273
276 const PointF& gradientBegin() const;
277
280 float gradientBeginRadius() const;
281
284 const PointF& gradientEnd() const;
285
288 float gradientEndRadius() const;
289
293
296 const Image& texture() const;
297
303 const PointF& textureOrigin() const;
304
307 bool isGradient() const;
308
311 bool isTexture() const;
312
315 bool isNull() const;
316
317 private:
318 Brush(BrushData* data);
319
320 private:
321 SmartPtr<BrushData> _brushData;
322};
323
324
325class BrushData
326{
327 public:
328 BrushData()
329 : _isNull(true)
330 , _fillStyle(Brush::Solid)
331 , _positionMode(Brush::Relative)
332 , _color(0, 0, 0)
333 , _gradient(Brush::Horizontal)
334 , _gradientBeginRadius(0.0f)
335 , _gradientEndRadius(0.0f)
336 , _textureMode(Brush::SourcePixels)
337 , _textureOrigin()
338 , _texture(0)
339 {}
340
341 BrushData(const Color& color)
342 : _isNull(false)
343 , _fillStyle(Brush::Solid)
344 , _positionMode(Brush::Relative)
345 , _color(color)
346 , _gradient(Brush::Horizontal)
347 , _gradientBeginRadius(0.0f)
348 , _gradientEndRadius(0.0f)
349 , _textureMode(Brush::SourcePixels)
350 , _textureOrigin()
351 , _texture(0)
352 {}
353
354 BrushData(const Image& texture, const PointF& textureOrigin);
355
356 BrushData(const BrushData& other);
357
358 BrushData& operator=(const BrushData& other);
359
360 // only for old Painter
361 BrushData(const Color& from, const Color& to,
363
364 ~BrushData();
365
366 Brush::FillStyle fillStyle() const
367 { return _fillStyle; }
368
369 Brush::PositionMode positionMode() const
370 { return _positionMode; }
371
372 void setSolid(const Color& color);
373
374 const Color& color() const
375 { return _gradientStops.empty() ? _color : _gradientStops.front().color(); }
376
377 // 1D gradient
378 void set1DGradient(Brush::GradientStyle g, const ColorStops& colorStops);
379
380 // absolute positioned linear gradient
381 void setLinearGradient(const PointF& begin, const PointF& end,
382 const ColorStops& colorStops);
383
384 // relative positioned linear gradient
385 void setLinearGradient(float beginX, float beginY,
386 float endX, float endY,
387 const ColorStops& colorStops);
388
389
390 // absolute positioned radial gradient
391 void setRadialGradient(const PointF& begin, float beginRadius,
392 const PointF& end, float endRadius,
393 const ColorStops& colorStops);
394
395 // relative positioned radial gradient
396 void setRadialGradient(float beginX, float beginY, float beginRadius,
397 float endX, float endY, float endRadius,
398 const ColorStops& colorStops);
399
400 Brush::GradientStyle gradient() const
401 { return _gradient; }
402
403 // remove when linear gradients use color stops
404 const Color& gradientColor() const
405 { return _gradientStops.empty() ? _color : _gradientStops.back().color(); }
406
407 const ColorStops& gradientStops() const
408 { return _gradientStops; }
409
410 const PointF& gradientBegin() const
411 { return _gradientBegin; }
412
413 float gradientBeginRadius() const
414 { return _gradientBeginRadius; }
415
416 const PointF& gradientEnd() const
417 { return _gradientEnd; }
418
419 float gradientEndRadius() const
420 { return _gradientEndRadius; }
421
422 void setImageTexture(const Image& texture, const PointF& textureOrigin);
423
424 const Image& texture() const;
425
426 Brush::TextureMode textureMode() const
427 { return _textureMode; }
428
429 const PointF& textureOrigin() const
430 { return _textureOrigin; }
431
432 bool isGradient() const
433 { return _fillStyle == Brush::Gradient; }
434
435 bool isTexture() const
436 { return _fillStyle == Brush::Texture; }
437
438 bool isNull() const
439 { return _isNull; }
440
441 private:
442 bool _isNull;
443 Brush::FillStyle _fillStyle;
444 Brush::PositionMode _positionMode;
445 Color _color;
446
447 Brush::GradientStyle _gradient;
448 ColorStops _gradientStops;
449 PointF _gradientBegin;
450 float _gradientBeginRadius;
451 PointF _gradientEnd;
452 float _gradientEndRadius;
453
454 Brush::TextureMode _textureMode;
455 PointF _textureOrigin;
456 Bitmap* _texture;
457};
458
459} // namespace
460
461} // namespace
462
463#endif
Off-screen image that can be painted.
Definition Bitmap.h:71
Fill color, gradient or texture.
Definition Brush.h:151
Brush(const Color &color)
Constructs a solid brush.
PositionMode positionMode() const
Returns how brush coordinates are interpreted.
bool isTexture() const
Returns true if the brush uses a texture.
bool isGradient() const
Returns true if the brush uses a gradient.
const Color & gradientColor() const
Returns the legacy secondary gradient color.
FillStyle fillStyle() const
Returns the brushes fill style.
void setColor(const Color &color)
Sets the solid fill color.
static Brush verticalGradient(const Color &from, const Color &to)
Constructs a vertical gradient brush from two colors.
TextureMode
Identifies how image texture coordinates are interpreted.
Definition Brush.h:183
const ColorStops & gradientStops() const
Color stops of a gradient.
PositionMode
Identifies how brush coordinates are interpreted.
Definition Brush.h:165
TextureMode textureMode() const
Returns how the texture origin is interpreted.
const PointF & gradientBegin() const
Begin of a linear or radial gradient.
static Brush linearGradient(float beginX, float beginY, float endX, float endY, const ColorStops &colorStops)
Constructs a relative positioned linear gradient.
static Brush radialGradient(float beginX, float beginY, float beginRadius, float endX, float endY, float endRadius, const ColorStops &colorStops)
Constructs a relative positioned radial gradient.
GradientStyle gradient() const
Returns the gradient style.
FillStyle
Identifies the fill source used by the brush.
Definition Brush.h:156
float gradientEndRadius() const
Radius of a radial gradient end circle.
const Color & color() const
Returns the solid or first gradient color.
static Brush horizontalGradient(const Color &from, const Color &to)
Constructs a horizontal gradient brush from two colors.
GradientStyle
Identifies the gradient variant used by the brush.
Definition Brush.h:173
static Brush linearGradient(const PointF &begin, const PointF &end, const ColorStops &colorStops)
Constructs an absolute positioned linear gradient.
static Brush horizontalGradient(const ColorStops &colorStops)
Constructs a horizontal gradient brush from color stops.
bool isNull() const
Returns true if the brush is null.
static Brush imageTexture(const Image &image, const Gfx::PointF &origin=Gfx::PointF())
Constructs an image texture brush.
const PointF & gradientEnd() const
End of a linear or radial gradient.
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.
float gradientBeginRadius() const
Radius of a radial gradient begin circle.
const Image & texture() const
Returns the texture image.
const PointF & textureOrigin() const
Returns the texture pattern origin.
static Brush verticalGradient(const ColorStops &colorStops)
Constructs a vertical gradient brush from color stops.
Color stop for a gradient brush.
Definition Brush.h:49
float position() const
Returns the normalized stop position.
Definition Brush.h:60
const Color & color() const
Returns the stop color.
Definition Brush.h:65
ColorStop(float position, const Color &color)
Constructs a color stop.
Definition Brush.h:53
Ordered collection of gradient color stops.
Definition Brush.h:78
const ColorStop & operator[](std::size_t n) const
Returns the stop at the given index.
Definition Brush.h:114
void calculateInterpolatedColor(Color &res, const float position) const
Calculates the interpolated color for a position.
~ColorStops()
Destroys the collection.
Definition Brush.h:87
bool empty() const
Returns true if no stops are stored.
Definition Brush.h:92
ColorStops()
Constructs an empty collection.
Definition Brush.h:82
void add(float position, const Color &color)
Appends a color stop.
Definition Brush.h:109
void clear()
Removes all stored stops.
Definition Brush.h:102
std::size_t size() const
Returns the number of stored stops.
Definition Brush.h:97
const ColorStop & front() const
Returns the first stop.
Definition Brush.h:119
const ColorStop & back() const
Returns the last stop.
Definition Brush.h:124
8-bit ARGB color.
Definition Color.h:65
Policy based smart pointer.
Definition SmartPtr.h:462
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33