COLOUR GRADIENT FILL IN DELPHI
See
http://www.efg2.com/lab/Library/Delphi/Graphics/Color.htm
for lots of really good Delphi COLOUR TipNTriks
along with
************************************************************
The easiest way to make a gradient fill is to paint several rectangles, changing their colours:
[BUT THE NEXT EXAMPLE IS MUCH BETTER]
procedure TForm1.FormPaint(Sender: TObject);
const N=100;
var Y:Integer;
Cl:TColor;
begin
for Y:=0 to N-1 do
with Canvas do
begin
Cl:=RGB(0,0,Round(50+205*(Y/N)));
Pen.Color:=Cl;
Brush.Color:=cl;
Rectangle(0,Round(ClientHeight*(Y/N)),ClientWidth,Round(ClientHeight*((Y+1)/N)));
end;
end;
This will add a gradient- filled background, as often seen in installation programs. To avoid problems with resizing the form, you should add the following code as well:
procedure TForm1.FormResize(Sender: TObject);
begin
Invalidate;
end;
***********************************************************
Working With Colors V - SimpleGradient
05/17/1999
The hexadecimal value of the color was explained in the tip a few days ago, review it if you have not seen it yet. Now that we have a good idea how colors are broken down, let's do something a little fancy. The manipulation we will do to create a gradient look starts with dark end of the scalle, black, and moves toward our selected color. To do this tip is quite simple, we will be using a TColorDialog to select our color and we will also be using the WIN32 API calls GetRValue(), GetBValue(), GetGValue(), and RGB() to manipulate our intensities along the way. We will also be using TCanvas.MoveTo() and TCanvas.LineTo() to draw our line across the screen.
For this tip we will need a TButton and a TButton.OnClick event. You can easily create the OnClick event through the Events tab of the Object Inspector. This event will allow us to call TColorDialog to get our EndColor. Don't forget to call Repaint within this event or our form will not draw our gradient.
Drop a TColorDialog onto the TForm so we can our ending color.
We will need a TForm.OnPaint event to do our painting of our gradient and a TForm.OnCreate event to initialize our ending color.
We also will need to add our EndColor variable to the private section of our unit becasue it is used between several different procedures. By initializing our EndColor variable to clBlack we can test it in our form's OnPaint event so we do not do the painting of the gradient until it is set.
In or OnPaint event we will be looping from a starting point to an ending point. I broke this into seperate variables of the TForm.Height and TForm.Width so you can see that this can be used anywhere. As we go through the loop from start to finish you will notice that our variable x will eventually equal our variable GradientDistance causing the final line to be drawn as our EndColor variable.
By using TCanvas.MoveTo() and TCanvas.LineTo() within our loop we can draw our line according to how far we are inot our gradient.
GetRValue() returns the intensity of red. The only parameter is an RGB value.
GetGValue() returns the intensity of green. The only parameter is an RGB value.
GetBValue() returns the intensity of blue. The only parameter is an RGB value.
RGB() sets a red, green, blue (RGB). The first parameter is our red, the second is our blue, and the third is our green.
TCanvas.MoveTo() changes the starting position of our TCanvas.Pen. The first parameter is our x-coordinate and the second parameter is our y-coordinate.
TCanvas.LineTo() draws a line from our starting pen position to an ending position according top the coordinates sent in. The first parameter is our x-coordinate and the second parameter is our y-coordinate.
Example 1
{...}
type
TForm1 = class(TForm)
ColorDialog1: TColorDialog;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
EndColor:TColor;
public
{ Public declarations }
end;
{...}
procedure TForm1.Button1Click(Sender: TObject);
begin
{ get color to gradient to }
ColorDialog1.Execute;
EndColor := ColorDialog1.Color;
{ call our form's paint event }
Repaint;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{ initialize our end color }
EndColor := clBlack;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
x,GradientDistance,GradientWidth : Integer;
tmpColor : TColor;
NewRed,NewGreen,NewBlue : Byte;
EndRed,EndGreen,EndBlue : Byte;
begin
{ exit if our ending color is not set }
if EndColor = clBlack then
Exit;
{ initialize our tmpcolor }
tmpColor := EndColor;
{ set our gradient distance }
GradientDistance := Height;
{ set our gradient width }
GradientWidth := Width;
{ get initial red, green, and blue }
EndRed := GetRValue(EndColor);
EndBlue := GetBValue(EndColor);
EndGreen := GetGValue(EndColor);
{ go form start to finish of our gradient }
for x := 1 to GradientDistance do
begin
{ offset our red, green, and blue according
to the distance into our gradient against
the total distance to go }
NewRed := (x*EndRed) div GradientDistance;
NewBlue := (x*EndBlue) div GradientDistance;
NewGreen := (x*EndGreen) div GradientDistance;
{ set our new color acording to our changed
red, green, and blue values }
tmpColor := RGB(NewRed,NewGreen,NewBlue);
{ set the new pen color }
Canvas.Pen.Color := tmpColor;
{ draw our line according to the new color }
Canvas.MoveTo(0,x);
Canvas.LineTo(GradientWidth,x);
end;
end;
{...}