Draw a gradient line

Submitted by:Jhon Jhon

Date added:12 March, 2013

Category:Delphi

Delphi code example to draw a gradient line

Tags: gradient line

Code Snippet:

    procedure DrawGradientLine(bCanvas: TCanvas; X, Y, Width: integer;
StartColor, EndColor: TColor);
var
fX:integer;
dr,dg,db:Extended;
C1,C2:TColor;
r1,r2,g1,g2,b1,b2:Byte;
R,G,B:Byte;
cnt:integer;
begin
C1 := StartColor;
R1 := GetRValue(C1) ;
G1 := GetGValue(C1) ;
B1 := GetBValue(C1) ;

C2 := EndColor;
R2 := GetRValue(C2) ;
G2 := GetGValue(C2) ;
B2 := GetBValue(C2) ;

dr := (R2-R1) / Width;
dg := (G2-G1) / Width;
db := (B2-B1) / Width;

cnt := 0;
for fX := X to X + Width - 1 do
begin
R := R1+Ceil(dr*cnt) ;
G := G1+Ceil(dg*cnt) ;
B := B1+Ceil(db*cnt) ;

bCanvas.Pixels[fX, Y] := RGB(R,G,B);
inc(cnt) ;
end;
end;
 
 

Comments