2006. január 17., kedd

Gradient fill procedure


Problem/Question/Abstract:

Gradient fill

Answer:

Paste this on your form + 1 button and 2 panels and a paintbox and link the panels onclick event and you will see it work it is fairly fast. When using this code in your application it is suggested to first draw to a tbitmap this will when the screen area neads to be redrawn speed it up considerably.

function getnewcolor(M1, M2: TColor; Location: Integer): TColor;
var
  V: array[0..2] of Byte; //BeginRGBValue
  D: array[0..2] of integer; //RGBDifference
  R, G, B: Byte;
  K1, K2: Longint;
begin
  K1 := ColorToRGB(M1);
  K2 := ColorToRGB(M2);
  V[0] := GetRValue(K1);
  V[1] := GetGValue(K1);
  V[2] := GetBValue(K1);

  D[0] := GetRValue(K2) - V[0];
  D[1] := GetGValue(K2) - V[1];
  D[2] := GetBValue(K2) - V[2];

  R := V[0] + MulDiv(Location, D[0], Form1.PaintBox1.Width - 1);
  G := V[1] + MulDiv(Location, D[1], Form1.PaintBox1.Width - 1);
  B := V[2] + MulDiv(Location, D[2], Form1.PaintBox1.Width - 1);
  Result := RGB(R, G, B);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  for i := 1 to PaintBox1.Width do
  begin
    PaintBox1.Canvas.Pen.Color := GetNewColor(panel1.color, panel2.color, i);
    PaintBox1.Canvas.MoveTo(i, 1);
    PaintBox1.Canvas.LineTo(i, Panel2.Height);
  end;
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then
    if Sender = Panel1 then
      Panel1.Color := ColorDialog1.Color
    else
      Panel2.Color := ColorDialog1.Color;
end;

Nincsenek megjegyzések:

Megjegyzés küldése