Creando Grandientes con TCanvas i TImage

Para poder crear diferentes gradientes horizontales o verticales, he subido los siguientes métodos que a partir de un TImage y su TCanvas, podemos dibujar diferentes gradientes, escogiendo el color de origen y de destino:


Los métodos son los siguientes:



procedure GradienteHorizontal(image: TImage; ColorOrigen, ColorDestino: TColor);

procedure ColorToRGB(iColor: TColor; var R, G, B: Byte);
begin
R := GetRValue(iColor);
G := GetGValue(iColor);
B := GetBValue(iColor);
end;
var
dif, dr, dg, db: Extended;
C1, C2: TColor;
r1, r2, g1, g2, b1, b2: Byte;
R, G, B: Byte;
i, j: integer;
begin
ColorToRGB(ColorOrigen, R1, G1, B1);
ColorToRGB(ColorDestino, R2, G2, B2);

dif := image.ClientRect.Right - image.ClientRect.Left;
dr := (R2 - R1) / dif;
dg := (G2 - G1) / dif;
db := (B2 - B1) / dif;

j := 0;
for i := image.ClientRect.Left to image.ClientRect.Right - 1 do
begin
R := R1 + Ceil(dr * j);
G := G1 + Ceil(dg * j);
B := B1 + Ceil(db * j);
image.Canvas.Pen.Color := RGB(R, G, B);
image.Canvas.MoveTo(i, image.ClientRect.Top);
image.Canvas.LineTo(i, image.ClientRect.Bottom);
j := j + 1;
end;
end;

procedure GradienteVertical(image: TImage; ColorOrigen, ColorDestino: TColor);
procedure ColorToRGB(iColor: TColor; var R, G, B: Byte);
begin
R := GetRValue(iColor);
G := GetGValue(iColor);
B := GetBValue(iColor);
end;
var
dif, dr, dg, db: Extended;
C1, C2: TColor;
r1, r2, g1, g2, b1, b2: Byte;
R, G, B: Byte;
i, j: integer;
begin
ColorToRGB(ColorOrigen, R1, G1, B1);
ColorToRGB(ColorDestino, R2, G2, B2);

dif := image.ClientRect.Right - image.ClientRect.Left;
dr := (R2 - R1) / dif;
dg := (G2 - G1) / dif;
db := (B2 - B1) / dif;

j := 0;
for i := image.ClientRect.Top to image.ClientRect.Bottom - 1 do
begin
R := R1 + Ceil(dr * j);
G := G1 + Ceil(dg * j);
B := B1 + Ceil(db * j);
image.Canvas.Pen.Color := RGB(R, G, B);
image.Canvas.MoveTo(image.ClientRect.Left, i);
image.Canvas.LineTo(image.ClientRect.Right, i);
j := j + 1;
end;
end;

Comments

Popular Posts