Sign in Join
Wiki copytemporary

TFPCustomBrush: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TFPCustomBrush

OldNewLine
1+**`TFPCustomBrush`** is a fundamental property of `TFPCustomCanvas` and defines how a geometric shape is filled in *fcl-image*.
2+
3+## Properties
4+
5+- **`Canvas`**: points to the `TFPCustomCanvas` on which the brush operates. The property is inherited from the ancestor `TFPCanvasHelper`.
6+- **`FPColor`**: specifies the basic color (type `TFPColor`) used to fill the shape (inherited from ancestor `TFPCanvasHelper`).
7+- **`Style`**: an enumeration of type `TFPBrushStyle` which defines the pattern used for filling:
8+ - **`bsClear`**: no filling at all, `FPColor` is ignored.
9+ - **`bsSolid`**: uniform fill of the shape with the `FPColor` specified
10+ - The following styles define **hatch patterns** which are drawn in the `FPColor` specified. The background between the hatch lines is left empty, it can only be filled by drawing the shape with `bsSolid` before.
11+ - **`bsHorizontal`**, **`bsVertical`**, **`bsFDiagonal`**, **`bsBDiagonal`** define a single-line hatch pattern.
12+ - **`bsCross`** and **`bsDiagCross`** define a crossed pattern.
13+ - **`bsImage`**: fills the shape by tiling the picture given in property `Image` on the canvas.
14+ - **`bsPattern`**: uses a custom pattern to fill the shape. The pattern is given by property `Pattern`. See below.
15+- **`Image`**: specifies the image (type `TFPCustomImage`) used when `Style` is `bsImage`. The alpha channel is respected.
16+- **`Pattern`**: specifies the custom pattern for filling the shape when `Style` is `bsPattern`. The pattern is defined as a 32-element array of `LongInt` values. This sets up a matrix of 32x32 bits. Each one-bit corresponds to a colored pixel. Pixels corresponding to zero-bits are not drawn.
17+
18+These properties are applied in all subsequent drawing commands.
19+
20+> **Note:** The advanced styles `bsImage` and `bsPattern` are availabel for non-rectangular shapes only for FPC v3.3.1+
21+
22+## Methods
23+
24+- **`function CopyBrush: TFPCustomBrush`**: creates a new brush and copies the current properties to it.
25+
26+## Sample code
27+
28+### Filling a circle with an image pattern (`bsImage`)
29+
30+<figure>
31+<img src="fpcustombrush_bsimage.png" title="fpcustombrush_bsimage.png" />
32+<figcaption>fpcustombrush_bsimage.png</figcaption>
33+</figure>
34+
35+``` Pascal
36+program project1;
37+
38+uses
39+ classes, FPImage, FPCanvas, FPImgCanv, FPReadPNG, FPWritePNG;
40+
41+var
42+ image: TFPCustomImage;
43+ pattern: TFPCustomImage;
44+ canvas: TFPCustomCanvas;
45+ writer: TFPCustomImageWriter;
46+
47+begin
48+ // "pattern.png" is some png image (with alpha channel).
49+ // Select one from the Lazarus image collection in (lazarus)/images
50+ pattern := TFPMemoryImage.Create(0, 0);
51+ pattern.LoadFromFile('pattern.png');
52+
53+ image := TFPMemoryImage.Create(200,200);
54+ try
55+ canvas := TFPImageCanvas.Create(image);
56+ try
57+ // Set the pen style
58+ canvas.Pen.Mode := pmCopy;
59+ canvas.Pen.Style := psSolid;
60+ canvas.Pen.Width := 1;
61+ canvas.Pen.FPColor := colWhite;
62+
63+ // Set the brush for the circle: the image stored in "pattern"
64+ canvas.Brush.Style := bsImage;
65+ canvas.Brush.Image := pattern;
66+ // Draw a circle filled by "pattern"
67+ canvas.Ellipse(10, 10, 90, 90);
68+
69+ // Save to file
70+ writer := TFPWriterPNG.Create;
71+ try
72+ // Activate alpha channel in saved image
73+ TFPWriterPNG(writer).UseAlpha := true;
74+ // Write image to file
75+ image.SaveToFile('DrawTest.png', writer);
76+ finally
77+ writer.Free;
78+ end;
79+
80+ finally
81+ canvas.Free;
82+ end;
83+
84+ finally
85+ pattern.Free;
86+ image.Free;
87+ end;
88+end.
89+```
90+
91+### Filling a rectangle with a custom pattern (`bsPattern`)
92+
93+<figure>
94+<img src="fpcustombrush_bspattern.png" title="fpcustombrush_bspattern.png" />
95+<figcaption>fpcustombrush_bspattern.png</figcaption>
96+</figure>
97+
98+``` pascal
99+program project1;
100+
101+uses
102+ classes, sysutils,
103+ FPImage, FPCanvas, FPImgCanv, FPWritePNG;
104+
105+const
106+ BIT_PATTERN: TBrushPattern = (
107+ %11111111111111111111111111111111, // 1
108+ %11111111111000000000001111111111, // 2
109+ %11111111110000000000000111111111, // 3
110+ %11111111100000000000000011111111, // 4
111+ %11111111000000000000000001111111, // 5
112+ %11111110000000000000000000111111, // 6
113+ %11111100000000000000000000011111, // 7
114+ %11111000000000000000000000001111, // 8
115+ %11110000000000000000000000000111, // 9
116+ %11100000000000000000000000000011, // 10
117+ %11000000000000001000000000000001, // 11
118+ %10000000000000011100000000000000, // 12
119+ %10000000000000111110000000000000, // 13
120+ %10000000000001111111000000000000, // 14
121+ %10000000000011110111100000000000, // 15
122+ %10000000000111100011110000000000, // 16
123+ %10000000000011110111100000000000, // 17
124+ %10000000000001111111000000000000, // 18
125+ %10000000000000111110000000000000, // 19
126+ %10000000000000011100000000000000, // 20
127+ %10000000000000001000000000000000, // 21
128+ %10000000000000000000000000000000, // 22
129+ %11000000000000000000000000000001, // 23
130+ %11100000000000000000000000000011, // 24
131+ %11110000000000000000000000000111, // 25
132+ %11111000000000000000000000001111, // 26
133+ %11111100000000000000000000011111, // 27
134+ %11111110000000000000000000111111, // 28
135+ %11111111000000000000000001111111, // 29
136+ %11111111100000000000000011111111, // 30
137+ %11111111110000000000000111111111, // 31
138+ %11111111111000000000001111111111 // 32
139+ );
140+
141+var
142+ image: TFPCustomImage;
143+ canvas: TFPCustomCanvas;
144+ writer: TFPCustomImageWriter;
145+
146+begin
147+ image := TFPMemoryImage.Create(200, 200);
148+ try
149+ canvas := TFPImageCanvas.Create(image);
150+ try
151+ // Set the pen style
152+ canvas.Pen.Mode := pmCopy;
153+ canvas.Pen.Style := psSolid;
154+ canvas.Pen.Width := 3;
155+ canvas.Pen.FPColor := colNavy;
156+
157+ // Set the brush for the rectangle: a bit pattern
158+ // Unless the shape should be transparent draw the background first
159+ canvas.Brush.Style := bsSolid;
160+ canvas.Brush.FPColor := colYellow;
161+ canvas.Rectangle(10, 110, 190, 190);
162+ // Now draw the pattern
163+ canvas.Brush.Style := bsPattern;
164+ canvas.Brush.FPColor := colBlue; // Color for every set bit (1) in the pattern
165+ canvas.Brush.Pattern := BIT_PATTERN;
166+ canvas.Rectangle(10, 110, 190, 190);
167+
168+ // Save to file
169+ writer := TFPWriterPNG.Create;
170+ try
171+ // Activate alpha channel in saved image
172+ TFPWriterPNG(writer).UseAlpha := true;
173+ // Write image to file
174+ image.SaveToFile('DrawTest.png', writer);
175+ finally
176+ writer.Free;
177+ end;
178+
179+ finally
180+ canvas.Free;
181+ end;
182+ finally
183+ image.Free;
184+ end;
185+end.
186+```
187+
188+---
189+*From the Free Pascal wiki, page "TFPCustomBrush", as it stood on 2023-11-24 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history