whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

37 Visitors Online


 
...draw a gradient on a canvas with an arbitrary number of colors?
Autor: David Johannes Rieger
Homepage: http://www.djr-delphi.gmxhome.de
[ Print tip ]  

Tip Rating (17):  
     


{
  The following code allows you to draw a gradient on a canvas with
  an arbitrary number of colors (minimum 2).
  To draw a gradient on a form's canvas,
  call the DrawGradient() in the OnPaint and OnResize-event handlers.
}

{
  Mit dieser Prozedur kann man auf einen Canvas einen Farbverlauf mit
  beliebig vielen Farben (min. 2) zeichnen.
  Z.B. wenn auf eine Form ein Farbverlauf gezeichnet werden soll,
  rufe die DrawGradient() Funktion im OnPaint-Ereignis und
  im OnResize-Ereignis der Form auf.
}

procedure DrawGradient(ACanvas: TCanvas; Rect: TRect;
  Horicontal: Boolean; Colors: array of TColor);
type
  
RGBArray = array[0..2] of Byte;
var
  
x, y, z, stelle, mx, bis, faColorsh, mass: Integer;
  Faktor: double;
  A: RGBArray;
  B: array of RGBArray;
  merkw: integer;
  merks: TPenStyle;
  merkp: TColor;
begin
  
mx := High(Colors);
  if mx > 0 then
  begin
    if 
Horicontal then
      
mass := Rect.Right - Rect.Left
    else
      
mass := Rect.Bottom - Rect.Top;
    SetLength(b, mx + 1);
    for x := 0 to mx do
    begin
      
Colors[x] := ColorToRGB(Colors[x]);
      b[x][0] := GetRValue(Colors[x]);
      b[x][1] := GetGValue(Colors[x]);
      b[x][2] := GetBValue(Colors[x]);
    end;
    merkw := ACanvas.Pen.Width;
    merks := ACanvas.Pen.Style;
    merkp := ACanvas.Pen.Color;
    ACanvas.Pen.Width := 1;
    ACanvas.Pen.Style := psSolid;
    faColorsh := Round(mass / mx);
    for y := 0 to mx - 1 do
    begin
      if 
y = mx - 1 then
        
bis := mass - y * faColorsh - 1
      else
        
bis := faColorsh;
      for x := 0 to bis do
      begin
        
Stelle := x + y * faColorsh;
        faktor := x / bis;
        for z := 0 to do
          
a[z] := Trunc(b[y][z] + ((b[y + 1][z] - b[y][z]) * Faktor));
        ACanvas.Pen.Color := RGB(a[0], a[1], a[2]);
        if Horicontal then
        begin
          
ACanvas.MoveTo(Rect.Left + Stelle, Rect.Top);
          ACanvas.LineTo(Rect.Left + Stelle, Rect.Bottom);
        end
        else
        begin
          
ACanvas.MoveTo(Rect.Left, Rect.Top + Stelle);
          ACanvas.LineTo(Rect.Right, Rect.Top + Stelle);
        end;
      end;
    end;
    b := nil;
    ACanvas.Pen.Width := merkw;
    ACanvas.Pen.Style := merks;
    ACanvas.Pen.Color := merkp;
  end
  else
    
// Please specify at least two colors
    
raise EMathError.Create('Es müssen mindestens zwei Farben angegeben werden.');
end;

// Example Calls:
// Aufrufbeispiele:

DrawGradient(Image1.Canvas, Rect(0, 0, 100, 200), False, [clRed, $00FFA9B4]);

DrawGradient(Canvas, GetClientRect, True, [121351, clBtnFace, clBlack, clWhite]);

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners