Create graphical text with gradient colors

Create graphical text with gradient colors

Create graphical text with gradient colors

This tutorial shows how to create graphical text with gradient colors on form.

DownloadDownload This Tutorials

Bookmark:

Create graphical text with gradient colors

This example shows how to apply gradient color to selected color in form. Over here we first create a text on form with black color. Then we will replace black color with gradient color. It is very important that we do not have any other contents on the form with the same color, if it does everything with black color will be fill with this new gradient color.

Gradient text

Following procedure create gradient at startup.

procedure TForm1.FormCreate(Sender: TObject);
Var
    Y, C, R, H, W, L : Integer;
    Step : Real;
begin
    { Get form size }
    H:=Image1.Height;
    W:=Image1.Width;

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

    { Insert text }
    Image1.Canvas.Font.Name:='Arial Black';
    Image1.Canvas.Font.Size:=35;
    Image1.Canvas.Font.Style:=[fsBold];
    Image1.Canvas.TextOut(5,20,'digitalcoding.com');

    { Set fill size }
    L:=W Div 3;

    { Set fill step size }
    Step:=255/L;

    For R:=0 To W Do
    Begin
        { Select color for gradient starting from RED
        to YELLOW }

        { 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;

        For Y:=0 to H Do
        { Fill only text area using text color}
        If Image1.Canvas.Pixels[R,Y]=clBlack Then
            Image1.Canvas.Pixels[R,Y]:=Image1.Canvas.Pen.Color;
    End;
end;

Download This Delphi Tutorials.

Download materials for this article (Delphi - Tutorials)

Download - gradient-text.zipgradient-text.zip
       File size: 4 KB, File type: zip
       Total downloads: 240, Upload date: February 10 - 2009

Leave a comment