Criando um efeito degradê de cores

Criando um efeito degradê de cores

Esta dica mostra como criar um efeito degradê em um Canvas qualquer.
Neste caso, estamos utilizando um componente TPaintBox e o evento OnPaint. Dependendo da utilização deste recurso, esta rotina pode ser adaptada para funcionar em um outro componente.

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
clStart: TColor = clRed;
clEnd: TColor = clBlack;
var
ACanvas: TCanvas;
ARect: TRect;
i : Integer;
rc, gc, bc, h: Integer;
begin

ACanvas := PaintBox1.Canvas;
ARect := PaintBox1.ClientRect;

h := ARect.Bottom – ARect.Top;

{ desenha o degradê }
for i := 0 to (ARect.Bottom – ARect.Top) do
begin
rc := GetRValue(clStart);
gc := GetGValue(clStart);
bc := GetBValue(clStart);
rc := rc + (((GetRValue(clEnd) – rc) * (ARect.Top + i)) div h);
gc := gc + (((GetGValue(clEnd) – gc) * (ARect.Top + i)) div h);
bc := bc + (((GetBValue(clEnd) – bc) * (ARect.Top + i)) div h);
ACanvas.Brush.Style := bsSolid;
ACanvas.Brush.Color := RGB(rc, gc, bc);
ACanvas.FillRect(Rect(ARect.Left, ARect.Top + i, ARect.Right, ARect.Top + i + 1));
end;

end;

Posts Similares