Sign in Join
Wiki copytemporary

TFPCustomBrush: what changed

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

OldNewLine
11**`TFPCustomBrush`** is a fundamental property of `TFPCustomCanvas` and defines how a geometric shape is filled in *fcl-image*.
22
33## Properties
44
55- **`Canvas`**: points to the `TFPCustomCanvas` on which the brush operates. The property is inherited from the ancestor `TFPCanvasHelper`.
66- **`FPColor`**: specifies the basic color (type `TFPColor`) used to fill the shape (inherited from ancestor `TFPCanvasHelper`).
77- **`Style`**: an enumeration of type `TFPBrushStyle` which defines the pattern used for filling:
88 - **`bsClear`**: no filling at all, `FPColor` is ignored.
99 - **`bsSolid`**: uniform fill of the shape with the `FPColor` specified
1010 - 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.
1111 - **`bsHorizontal`**, **`bsVertical`**, **`bsFDiagonal`**, **`bsBDiagonal`** define a single-line hatch pattern.
1212 - **`bsCross`** and **`bsDiagCross`** define a crossed pattern.
1313 - **`bsImage`**: fills the shape by tiling the picture given in property `Image` on the canvas.
1414 - **`bsPattern`**: uses a custom pattern to fill the shape. The pattern is given by property `Pattern`. See below.
1515- **`Image`**: specifies the image (type `TFPCustomImage`) used when `Style` is `bsImage`. The alpha channel is respected.
1616- **`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.
1717
1818These properties are applied in all subsequent drawing commands.
1919
2020> **Note:** The advanced styles `bsImage` and `bsPattern` are availabel for non-rectangular shapes only for FPC v3.3.1+
2121
2222## Methods
2323
2424- **`function CopyBrush: TFPCustomBrush`**: creates a new brush and copies the current properties to it.
2525
2626## Sample code
2727
2828### Filling a circle with an image pattern (`bsImage`)
2929
30-<figure>
31-<img src="fpcustombrush_bsimage.png" title="fpcustombrush_bsimage.png" />
32-<figcaption>fpcustombrush_bsimage.png</figcaption>
33-</figure>
30+![](/wiki-images/b/b6/Fpcustombrush_bsimage.png)
3431
3532``` Pascal
3633program project1;
3734
3835uses
3936 classes, FPImage, FPCanvas, FPImgCanv, FPReadPNG, FPWritePNG;
4037
4138var
4239 image: TFPCustomImage;
4340 pattern: TFPCustomImage;
4441 canvas: TFPCustomCanvas;
4542 writer: TFPCustomImageWriter;
4643
4744begin
4845 // "pattern.png" is some png image (with alpha channel).
4946 // Select one from the Lazarus image collection in (lazarus)/images
5047 pattern := TFPMemoryImage.Create(0, 0);
5148 pattern.LoadFromFile('pattern.png');
5249
5350 image := TFPMemoryImage.Create(200,200);
5451 try
5552 canvas := TFPImageCanvas.Create(image);
5653 try
5754 // Set the pen style
5855 canvas.Pen.Mode := pmCopy;
5956 canvas.Pen.Style := psSolid;
6057 canvas.Pen.Width := 1;
6158 canvas.Pen.FPColor := colWhite;
6259
6360 // Set the brush for the circle: the image stored in "pattern"
6461 canvas.Brush.Style := bsImage;
6562 canvas.Brush.Image := pattern;
6663 // Draw a circle filled by "pattern"
6764 canvas.Ellipse(10, 10, 90, 90);
6865
6966 // Save to file
7067 writer := TFPWriterPNG.Create;
7168 try
7269 // Activate alpha channel in saved image
7370 TFPWriterPNG(writer).UseAlpha := true;
7471 // Write image to file
7572 image.SaveToFile('DrawTest.png', writer);
7673 finally
7774 writer.Free;
7875 end;
7976
8077 finally
8178 canvas.Free;
8279 end;
8380
8481 finally
8582 pattern.Free;
8683 image.Free;
8784 end;
8885end.
8986```
9087
9188### Filling a rectangle with a custom pattern (`bsPattern`)
9289
93-<figure>
94-<img src="fpcustombrush_bspattern.png" title="fpcustombrush_bspattern.png" />
95-<figcaption>fpcustombrush_bspattern.png</figcaption>
96-</figure>
90+![](/wiki-images/e/e0/Fpcustombrush_bspattern.png)
9791
9892``` pascal
9993program project1;
10094
10195uses
10296 classes, sysutils,
10397 FPImage, FPCanvas, FPImgCanv, FPWritePNG;
10498
10599const
106100 BIT_PATTERN: TBrushPattern = (
107101 %11111111111111111111111111111111, // 1
108102 %11111111111000000000001111111111, // 2
109103 %11111111110000000000000111111111, // 3
110104 %11111111100000000000000011111111, // 4
111105 %11111111000000000000000001111111, // 5
112106 %11111110000000000000000000111111, // 6
113107 %11111100000000000000000000011111, // 7
114108 %11111000000000000000000000001111, // 8
115109 %11110000000000000000000000000111, // 9
116110 %11100000000000000000000000000011, // 10
117111 %11000000000000001000000000000001, // 11
118112 %10000000000000011100000000000000, // 12
119113 %10000000000000111110000000000000, // 13
120114 %10000000000001111111000000000000, // 14
121115 %10000000000011110111100000000000, // 15
122116 %10000000000111100011110000000000, // 16
123117 %10000000000011110111100000000000, // 17
124118 %10000000000001111111000000000000, // 18
125119 %10000000000000111110000000000000, // 19
126120 %10000000000000011100000000000000, // 20
127121 %10000000000000001000000000000000, // 21
128122 %10000000000000000000000000000000, // 22
129123 %11000000000000000000000000000001, // 23
130124 %11100000000000000000000000000011, // 24
131125 %11110000000000000000000000000111, // 25
132126 %11111000000000000000000000001111, // 26
133127 %11111100000000000000000000011111, // 27
134128 %11111110000000000000000000111111, // 28
135129 %11111111000000000000000001111111, // 29
136130 %11111111100000000000000011111111, // 30
137131 %11111111110000000000000111111111, // 31
138132 %11111111111000000000001111111111 // 32
139133 );
140134
141135var
142136 image: TFPCustomImage;
143137 canvas: TFPCustomCanvas;
144138 writer: TFPCustomImageWriter;
145139
146140begin
147141 image := TFPMemoryImage.Create(200, 200);
148142 try
149143 canvas := TFPImageCanvas.Create(image);
150144 try
151145 // Set the pen style
152146 canvas.Pen.Mode := pmCopy;
153147 canvas.Pen.Style := psSolid;
154148 canvas.Pen.Width := 3;
155149 canvas.Pen.FPColor := colNavy;
156150
157151 // Set the brush for the rectangle: a bit pattern
158152 // Unless the shape should be transparent draw the background first
159153 canvas.Brush.Style := bsSolid;
160154 canvas.Brush.FPColor := colYellow;
161155 canvas.Rectangle(10, 110, 190, 190);
162156 // Now draw the pattern
163157 canvas.Brush.Style := bsPattern;
164158 canvas.Brush.FPColor := colBlue; // Color for every set bit (1) in the pattern
165159 canvas.Brush.Pattern := BIT_PATTERN;
166160 canvas.Rectangle(10, 110, 190, 190);
167161
168162 // Save to file
169163 writer := TFPWriterPNG.Create;
170164 try
171165 // Activate alpha channel in saved image
172166 TFPWriterPNG(writer).UseAlpha := true;
173167 // Write image to file
174168 image.SaveToFile('DrawTest.png', writer);
175169 finally
176170 writer.Free;
177171 end;
178172
179173 finally
180174 canvas.Free;
181175 end;
182176 finally
183177 image.Free;
184178 end;
185179end.
186180```
187181
188182---
189183*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