Thanks for the code... Just what I was looking for.

Regards

Colin

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Matt Powell
Sent: Monday, 26 June 2000 3:12 pm
To: Multiple recipients of list delphi
Subject: Re: [DUG]: Gradient Fills


> Hi all, just wondering how everyone else does gradient fills... do you do
it
> manually or is there a fast function...

Here's a routine I built in about 10 minutes. It certainly isn't optimal (it
has a comparison inside a loop when the result of the comparison is constant
for the duration of the loop, for example), but on my P200 it fills the
entire screen in about 45ms on average - with WinAmp running in the
background... :)

This works like the function described in the help snippet you attached,
with the exception of the "steps" variable (used as a rough "quality"
variable). Lower values are faster, but look blockier. Values over 256 will
add execution time with no upgrade in quality :) If you're worried about
flicker, one approach I've used when making components is storing a TBitmap
with the gradient in it, then redrawing only at resize time or when the
colours are changed...

> I found a GradientFill in the Windows unit... couldn't find any
> documentation for it in Delphi, implemented it, found that it needed Win
> 2000 or Win 98 to work (I guess that is why it wasn't documented!)...

Usually Win 98/2K only functions seem to be documented in the SDK help,
tho'... I wonder why this one wasn't...

> Marching onwards I found the below in Delphi's help... Couldn't get that
> working (or find the appropriate function) either.

No... I think it needs a special type of Canvas...

HTH,

- Matt

8<---

procedure MyGradientFill(Canvas: TCanvas; Rect: TRect; StartColor, EndColor:
TColor; Horizontal: Boolean; Steps: Integer);
var
 i, d, u, v: Integer;
 r1, g1, b1, r, g, b: Byte;
 dr, dg, db: Integer;
 c1, c2: LongInt;
 rct: TRect;
begin
 r1 := (StartColor and $FF);
 g1 := (StartColor and $FF00) shr 8;
 b1 := (StartColor and $FF0000) shr 16;

 dr := (EndColor and $FF) - r1;
 dg := ((EndColor and $FF00) shr 8) - g1;
 db := ((EndColor and $FF0000) shr 16) - b1;

 rct := Rect;

 if Horizontal then
  d := Rect.Right - Rect.Left + 1
 else
  d := Rect.Bottom - Rect.Top + 1;
 if d < Steps then
  Steps := d;

 with Canvas do
 begin
  Brush.Style := bsSolid;
  for i := 0 to Steps do
  begin
   if Horizontal then
   begin
    rct.Left := MulDiv(d, i, Steps);
    rct.Right := MulDiv(d, i + 1, Steps);
   end else
   begin
    rct.Top := MulDiv(d, i, Steps);
    rct.Bottom := MulDiv(d, i + 1, Steps);
   end;
   r := r1 + MulDiv(dr, i, Steps);
   g := g1 + MulDiv(dg, i, Steps);
   b := b1 + MulDiv(db, i, Steps);
   Brush.Color := r or (g shl 8) or (b shl 16);
   FillRect(rct);
  end;
 end;
end;

8<---



---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to