Sign in Join
Wiki copytemporary

Double Gradient: what changed

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

OldNewLine
11Now 'DoubleGradientAlphaFill' & 'nGradientAlphaFill' are included in [[BGRABitmap]] in the unit 'BGRAGradients'.
22
33## Double Gradient
44
5-<figure>
6-<img src="sampledoublegd.png" title="sampledoublegd.png" />
7-<figcaption>sampledoublegd.png</figcaption>
8-</figure>
5+![](/wiki-images/b/b2/Sampledoublegd.png)
96
107### Unit
118
129#### DoubleGradient
1310
1411``` pascal
1512unit doublegradient;
1613
1714{$mode objfpc}{$H+}
1815
1916interface
2017
2118uses
2219 Classes, Graphics;
2320
2421function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;
2522 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;
2623
2724implementation
2825
2926function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;
3027 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;
3128var
3229 ABitmap: TBitmap; ARect1,ARect2: TRect;
3330begin
3431 ABitmap := TBitmap.Create;
3532 ABitmap.Width:=ARect.Right;
3633 ABitmap.Height:=ARect.Bottom;
3734 if AValue <> 0 then ARect1:=ARect;
3835 if AValue <> 1 then ARect2:=ARect;
3936 if APos = gdVertical then begin
4037 ARect1.Bottom:=Round(ARect1.Bottom * AValue);
4138 ARect2.Top:=ARect1.Bottom;
4239 end
4340 else if APos = gdHorizontal then begin
4441 ARect1.Right:=Round(ARect1.Right * AValue);
4542 ARect2.Left:=ARect1.Right;
4643 end;
4744 if AValue <> 0 then ABitmap.Canvas.GradientFill(ARect1,AStart1,AStop1,ADirection1);
4845 if AValue <> 1 then ABitmap.Canvas.GradientFill(ARect2,AStart2,AStop2,ADirection2);
4946 Result:=ABitmap;
5047end;
5148
5249end.
5350```
5451
5552#### DoubleGradientAlpha
5653
5754This requires [[BGRABitmap]].
5855
5956``` pascal
6057unit doublegradientalpha;
6158
6259{$mode objfpc}{$H+}
6360
6461interface
6562
6663uses
6764 Classes, Graphics,
6865 BGRABitmap, BGRABitmapTypes;
6966
7067function DoubleGradientAlphaFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TBGRAPixel;
7168 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBGRABitmap;
7269
7370implementation
7471
7572function DoubleGradientAlphaFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TBGRAPixel;
7673 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBGRABitmap;
7774var
7875 ABitmap: TBGRABitmap; ARect1,ARect2: TRect; APoint1,APoint2,APoint3,APoint4: TPointF;
7976begin
8077 ABitmap := TBGRABitmap.Create(ARect.Right,ARect.Bottom);
8178 if AValue <> 0 then ARect1:=ARect;
8279 if AValue <> 1 then ARect2:=ARect;
8380 if APos = gdVertical then begin
8481 ARect1.Bottom:=Round(ARect1.Bottom * AValue);
8582 ARect2.Top:=ARect1.Bottom;
8683 end
8784 else if APos = gdHorizontal then begin
8885 ARect1.Right:=Round(ARect1.Right * AValue);
8986 ARect2.Left:=ARect1.Right;
9087 end;
9188 if ADirection1 = gdVertical then begin
9289 APoint1:=PointF(ARect1.Left,ARect1.Top);
9390 APoint2:=PointF(ARect1.Left,ARect1.Bottom);
9491 end
9592 else if ADirection1 = gdHorizontal then begin
9693 APoint1:=PointF(ARect1.Left,ARect1.Top);
9794 APoint2:=PointF(ARect1.Right,ARect1.Top);
9895 end;
9996 if ADirection2 = gdVertical then begin
10097 APoint3:=PointF(ARect2.Left,ARect2.Top);
10198 APoint4:=PointF(ARect2.Left,ARect2.Bottom);
10299 end
103100 else if ADirection2 = gdHorizontal then begin
104101 APoint3:=PointF(ARect2.Left,ARect2.Top);
105102 APoint4:=PointF(ARect2.Right,ARect2.Top);
106103 end;
107104 if AValue <> 0 then
108105 ABitmap.GradientFill(ARect1.Left,ARect1.Top,ARect1.Right,ARect1.Bottom,
109106 AStart1,AStop1,gtLinear,APoint1,APoint2,dmDrawWithTransparency,True,False);
110107 if AValue <> 1 then
111108 ABitmap.GradientFill( ARect2.Left,ARect2.Top,ARect2.Right,ARect2.Bottom,
112109 AStart2,AStop2,gtLinear,APoint3,APoint4,dmDrawWithTransparency,True,False);
113110 Result:=ABitmap;
114111end;
115112
116113end.
117114```
118115
119116### Usage
120117
121118First you must have a 'TBitmap' to store the gradient, then you can draw the image, for example, in all the 'Form1' visible area:
122119
123-<figure>
124-<img src="defaultdbgd.png" title="defaultdbgd.png" />
125-<figcaption>defaultdbgd.png</figcaption>
126-</figure>
120+![](/wiki-images/7/72/Defaultdbgd.png)
127121
128122*This is the result (using DoubleGradientFill).*
129123
130124``` pascal
131125procedure TForm1.FormPaint(Sender: TObject);
132126var
133127 ABitmap: TBitmap;
134128begin
135129 ABitmap:=DoubleGradientFill(Self.ClientRect,clMedGray,clWhite,clSilver,clGray,gdVertical,gdVertical,gdVertical,0.50);
136130 Self.Canvas.Draw(0,0,ABitmap);
137131 ABitmap.Free
138132end;
139133```
140134
141135If you use DoubleGradientAlphaFill first add BGRABitmap and BGRABitmapTypes in the Uses section, then use the function:
142136
143137``` pascal
144138var
145139 myBitmap: TBGRABitmap;
146140begin
147141 myBitmap:= DoubleGradientAlphaFill(
148142 Self.ClientRect,
149143 BGRA(0,0,0,10),BGRA(255,255,255,100),
150144 BGRA(100,100,100,255),BGRA(150,150,150,10),
151145 gdVertical,gdVertical,gdVertical,0.5);
152146 Self.Canvas.Draw(0,0,myBitmap.Bitmap);
153147 myBitmap.Free;
154148end;
155149```
156150
157151### Editor
158152
159153You can use the 'Double Gradient Editor' that is a GUI to instantly see the result (only for double gradients), save to a bitmap file, save to '*.doublegradient' session file or just copy code to clipboard to use in lazarus.
160154
161-<figure>
162-<img src="dbgedit.png" title="dbgedit.png" />
163-<figcaption>dbgedit.png</figcaption>
164-</figure>
155+![](/wiki-images/8/88/Dbgedit.png)
165156
166157- To access the menu right click the ScrollBox (gradient area):
167158 - Save bitmap.. Show a dialog to save as a *.bmp file.
168159 - Load gradient.. Show a dialog to load a *.doublegradient session file.
169160 - Save gradient.. Show a dialog to save the current session settings to a file.
170161 - Copy code to clipboard.. Just copy the settings as text in the clipboard, then you can use it to paste in lazarus source editor to call the doublegradientfill procedure.
171162
172163- default.doublegradient: this file has the last settings in the editor, is loaded at startup, saved when you close the application.
173164
174165[Double Gradient Editor 1.0 Source Code](http://www.multiupload.com/5WJ6N8E0N0) dbgdedit1.0.zip (5.64 KB)
175166
176167You are free to edit and improve this editor, this is free of charge.
177168
178169## nGradient
179170
180171With this unit 'ngradient' you can easily make gradients for toolbars, buttons, etc. Save the below code in a text file 'ngradient.pas' and add 'ngradient' in the 'uses' section of your project.
181172
182173### Unit
183174
184175#### nGradientFill
185176
186177Copy the code and save to a text file 'ngradient.pas':
187178
188179``` pascal
189180unit ngradient;
190181
191182{$mode objfpc}{$H+}
192183
193184interface
194185
195186uses
196187 Classes, Graphics;
197188
198189type
199190 TnGradientInfo = record
200191 StartColor,StopColor:TColor;
201192 Direction: TGradientDirection;
202193 endPercent:single; // This is not the percent of the width, this is the percent of the end of the rect- which means, if this value is 1 - the rect could be from 0.99 to 1 and needs not be from 0 to 1
203194 end;
204195
205196function nGradientFill(ARect: TRect;APos: TGradientDirection; AGradient: array of TnGradientInfo): TBitmap;
206197
207198implementation
208199
209200function nGradientFill(ARect: TRect;APos: TGradientDirection; AGradient: array of TnGradientInfo): TBitmap;
210201var
211202 i:integer;
212203 AnRect,OldRect: TRect;
213204begin
214205 Result := TBitmap.Create;
215206 Result.Width:=ARect.Right-ARect.Left;
216207 Result.Height:=ARect.Bottom-ARect.Top;
217208 OldRect := ARect;
218209 if APos = gdVertical then OldRect.Bottom := ARect.Top
219210 else OldRect.Right := ARect.Left ; // upside down... in case of i = 0...
220211
221212 for i := 0 to high(AGradient) do
222213 begin
223214 AnRect:=OldRect;
224215 if APos = gdVertical then
225216 begin
226217 AnRect.Bottom:=Round((ARect.Bottom-ARect.Top) * AGradient[i].endPercent + ARect.Top);
227218 AnRect.Top:=OldRect.Bottom;
228219 end
229220 else
230221 begin
231222 AnRect.Right:=Round((ARect.Right-ARect.Left) * AGradient[i].endPercent + ARect.Left);
232223 AnRect.Left:=OldRect.Right;
233224 end;
234225
235226 Result.Canvas.GradientFill(AnRect,AGradient[i].StartColor,AGradient[i].StopColor,AGradient[i].Direction);
236227 OldRect := AnRect;
237228 end;
238229end;
239230```
240231
241232#### nGradientAlphaFill
242233
243234This requires [[BGRABitmap]].
244235
245236Copy the code and save to a text file 'ngradientalpha.pas':
246237
247238``` pascal
248239unit ngradientalpha;
249240
250241{$mode objfpc}{$H+}
251242
252243interface
253244
254245uses
255246 Classes, Graphics, BGRABitmap, BGRABitmapTypes;
256247
257248type
258249 TnGradientInfo = record
259250 StartColor, StopColor : TBGRAPixel;
260251 Direction : TGradientDirection;
261252 endPercent : single;
262253 end;
263254
264255function nGradientAlphaFill(ARect: TRect;APos: TGradientDirection;
265256 AGradient: array of TnGradientInfo): TBGRABitmap;
266257
267258implementation
268259
269260function nGradientAlphaFill(ARect: TRect;APos: TGradientDirection;
270261 AGradient: array of TnGradientInfo): TBGRABitmap;
271262var
272263 i:integer; AnRect, OldRect: TRect; Point1, Point2: TPointF;
273264begin
274265 Result := TBGRABitmap.Create(ARect.Right-ARect.Left,ARect.Bottom-ARect.Top);
275266 OldRect := ARect;
276267 if APos = gdVertical then OldRect.Bottom := ARect.Top else OldRect.Right := ARect.Left;
277268 for i := 0 to high(AGradient) do
278269 begin
279270 AnRect:=OldRect;
280271 if APos = gdVertical then begin
281272 AnRect.Bottom:=Round((ARect.Bottom-ARect.Top) * AGradient[i].endPercent + ARect.Top);
282273 AnRect.Top:=OldRect.Bottom;
283274 Point1:=PointF(AnRect.Left,AnRect.Top);
284275 Point2:=PointF(AnRect.Left,AnRect.Bottom);
285276 end
286277 else begin
287278 AnRect.Right:=Round((ARect.Right-ARect.Left) * AGradient[i].endPercent + ARect.Left);
288279 AnRect.Left:=OldRect.Right;
289280 Point1:=PointF(AnRect.Left,AnRect.Top);
290281 Point2:=PointF(AnRect.Right,AnRect.Top);
291282 end;
292283 Result.GradientFill(AnRect.Left,AnRect.Top,AnRect.Right,AnRect.Bottom,
293284 AGradient[i].StartColor,AGradient[i].StopColor,gtLinear,Point1,Point2,dmDrawWithTransparency,True,False);
294285 OldRect := AnRect;
295286 end;
296287end;
297288
298289end.
299290```
300291
301292### Usage
302293
303294If you use nGradientAlphaFill first add BGRABitmap and BGRABitmapTypes in the Uses section, then use the function:
304295
305296``` pascal
306297var
307298 myBitmap: TBGRABitmap;
308299 AGradInfo: array [0..1] of TnGradientInfo;
309300begin
310301 AGradInfo[0].Direction:=gdVertical;
311302 AGradInfo[0].endPercent:=0.50;
312303 AGradInfo[0].StartColor:=BGRA(0,0,0,200);
313304 AGradInfo[0].StopColor:=BGRA(255,255,255,100);
314305
315306 AGradInfo[1].Direction:=gdVertical;
316307 AGradInfo[1].endPercent:=1.00;
317308 AGradInfo[1].StartColor:=BGRA(0,0,0,100);
318309 AGradInfo[1].StopColor:=BGRA(255,255,255,50);
319310
320311 myBitmap:= nGradientAlphaFill(Self.ClientRect,gdVertical,Agradinfo);
321312 Self.Canvas.Draw(0,0,myBitmap.Bitmap);
322313 myBitmap.Free;
323314end;
324315```
325316
326317### Editor
327318
328319nGradient Editor is an updated version of Double Gradient Editor with a lot of changes and improvements, like the ability to create gradients with 'n' number of gradients.
329320
330321With nGradient Editor you can use 'n' (number) of gradients.
331322
332-<figure>
333-<img src="ngedit.png" title="ngedit.png" />
334-<figcaption>ngedit.png</figcaption>
335-</figure>
323+![](/wiki-images/8/8f/Ngedit.png)
336324
337325[nGradient Editor 1.1 (With alpha support) Source Code](http://www.multiupload.com/Z5CEFYE5CO) ngdedit1.1alpha.zip (22.58 KB) / [forum attach](http://lazarus.freepascal.org/index.php?action=dlattach;topic=12145.0;attach=1641)
338326
339327**Note:** This editor doesn't use BGRABitmap, this uses their own Alpha procedures.
340328
341329[nGradient Editor 1.1 Source Code](http://www.multiupload.com/SJDCG5P21Z) ngdedit1.1.zip (26.14 KB) / [forum attach](http://lazarus.freepascal.org/index.php?action=dlattach;topic=12145.0;attach=1636)
342330
343331---
344332*From the Free Pascal wiki, page "Double Gradient", as it stood on 2020-02-14 after a change by Trev, 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