1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| procedure DrawGradient(aCanvas: TCanvas; Rect: TRect;
Horizontal: Boolean; Colors: array of TColor);
{ http://www.swissdelphicenter.ch/en/showcode.php?id=1162
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.
Example Calls:
DrawGradient(Image1.Canvas, Rect(0, 0, 100, 200), False, [clRed, $00FFA9B4]);
DrawGradient(Canvas, GetClientRect, True, [121351, clBtnFace, clBlack, clWhite]);
}
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 Horizontal 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 3 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 Horizontal 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; |
Partager