Delphi Tutorial


 

Rainbow color gradient

 

      

Download this example

      Following procedure draw rainbow gradient at startup. Within that you will see another procedure call DrawLine. It will draw single 1 pixel width line look like a color gradient. What the main procedure does is call DrawLine procedure and change it starting position.

procedure TForm1.FormCreate(Sender: TObject);

   { Draw 1 pixcel width line }
   procedure DrawLine(X,Y,H,L:Integer; Step:Real);
   Var
      R, C : Integer;
   Begin
      For R:=0 To H Do
      Begin
         { Change fill color }
         If R<=L Then
         Begin
            C:=Round(R*Step);
            { Red -> Green }
            Image1.Canvas.Pen.Color:=RGB(255-C,C,0)
         End
         Else If R<=2*L Then
         Begin
            C:=Round((R-L)*Step);
            { Green -> Blue }
            Image1.Canvas.Pen.Color:=RGB(0,255-C,C)
         End
         Else If R<=3*L Then
         Begin
            C:=Round((R-2*L)*Step);
            { Blue -> Yellow }
            Image1.Canvas.Pen.Color:=RGB(C,C,255-C);
         End;
         { Fill area by drawing lines }
         Image1.Canvas.MoveTo(X,Y+R);
         Image1.Canvas.LineTo(X+1,Y+R);
      End;

   End;

Var
   Angle, X, Y, H, H2, W, L : Integer;
   Step : Real;
begin
   { Get form size }
   H:=Image1.Height;
   W:=Image1.Width;

   { Fill image area by form color }
   Image1.Canvas.Brush.Color:=Form1.Color;
   Image1.Canvas.Pen.Color:=Form1.Color;
   Image1.Canvas.Rectangle(0,0,W,H);


   { Set fill size }
   H2:=150;
   L:=H2 Div 3;

   { Set fill step size }
   Step:=255/L;
   Angle:=0;
   For X:=1 to W Do
   Begin
      Y:=Round(100*Sin(Angle*(Pi/180)));
      DrawLine(X,(H Div 3)-Y,H2,L,Step);
      Inc(Angle);
      If Angle>360 Then Angle:=0;
   End;
end;


 

DOWNLOAD this example

Click here to download this complete example for Delphi 6. (File size 4KB)

 

 

Go top

 


Previous Page First Page Next Page
 
Copyright © 2002 digitalcoding.com. All rights reserved.