Difference between revisions of "Double Gradient"

From Lazarus wiki
Jump to navigationJump to search
m (typos,etc...)
m (template added)
Line 1: Line 1:
 +
{{Double Gradient}}
 +
 
== Unit ==
 
== Unit ==
  

Revision as of 00:16, 18 February 2011

Deutsch (de) English (en)

Unit

sampledoublegd.png

With this unit 'doublegradient' you can easily make gradients for toolbars, buttons, etc. Save the below code in a text file 'doublegradient.pas' and add 'doublegradient' in the 'uses' section of your project.

<delphi>unit doublegradient;

{$mode objfpc}{$H+}

interface

uses

 Classes, Graphics;

function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;

 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;

implementation

function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;

 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;

var

 ABitmap: TBitmap; ARect1,ARect2: TRect;

begin

 ABitmap := TBitmap.Create;
 ABitmap.Width:=ARect.Right;
 ABitmap.Height:=ARect.Bottom;
 if AValue <> 0 then ARect1:=ARect;
 if AValue <> 1 then ARect2:=ARect;
 if APos = gdVertical then begin
   ARect1.Bottom:=Round(ARect1.Bottom * AValue);
   ARect2.Top:=ARect1.Bottom;
 end
 else if APos = gdHorizontal then begin
   ARect1.Right:=Round(ARect1.Right * AValue);
   ARect2.Left:=ARect1.Right;
 end;
 if AValue <> 0 then ABitmap.Canvas.GradientFill(ARect1,AStart1,AStop1,ADirection1);
 if AValue <> 1 then ABitmap.Canvas.GradientFill(ARect2,AStart2,AStop2,ADirection2);
 Result:=ABitmap;

end;

end.</delphi>

Usage

First you must have a 'TBitmap' to store the gradient, then you can draw the image, for example, in all the 'Form1' visible area:

defaultdbgd.png

<delphi>procedure TForm1.FormPaint(Sender: TObject); var

 ABitmap: TBitmap;

begin

 ABitmap:=DoubleGradientFill(Self.ClientRect,clMedGray,clWhite,clSilver,clGray,gdVertical,gdVertical,gdVertical,0.50);
 Self.Canvas.Draw(0,0,ABitmap);
 ABitmap.Free

end;</delphi>

Editor

Also you can use the 'Double Gradient Editor' that is a GUI to instantly see the result, save to a bitmap file, save to '*.doublegradient' session file or just copy code to clipboard to use in lazarus.

dbgedit.png

  • To access the menu right click the ScrollBox (gradient area):
    • Save bitmap.. Show a dialog to save as a *.bmp file.
    • Load gradient.. Show a dialog to load a *.doublegradient session file.
    • Save gradient.. Show a dialog to save the current session settings to a file.
    • 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.
  • default.doublegradient: this file has the last settings in the editor, is loaded at startup, saved when you close the application.

Downloads

Double Gradient Editor 1.0 Source Code dbgdedit1.0.zip (5.64 KB)

You are free to edit and improve this editor, this is free of charge.