多色漸變需要: 一個 "顏色數組" 和一個 "位置數組" (0-1 之間的位置比例);
IGPLinearGradIEntBrush.InterpolationColors 屬性對應一個 IGPColorBlend 對象, 此對象專門用於裝載上面兩個數組.
IGPColorBlend 的 Colors 和 Positions 兩個屬性分別對應這兩個數組.
下面的三個示例的結果都是一樣的.
測試效果圖:
測試一, 使用開放數組參數構建 IGPColorBlend
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
const
C1 = $FF000000 ;
C2 = $FFFF0000 ;
C3 = $FFFFFF00 ;
C4 = $FFFF0000;
C5 = $FF000000;
var
Graphics: IGPGraphics;
Rect: TGPRect;
Brush: IGPLinearGradIEntBrush;
ColorBlend: IGPColorBlend;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
Rect.Initialize(ClIEntRect);
Brush := TGPLinearGradIEntBrush.Create(Rect, 0, 0, 0);
ColorBlend := TGPColorBlend.Create([C1, C2, C3, C4, C5], [0, 0.25, 0.5, 0.75, 1]);
Brush.InterpolationColors := ColorBlend;
Graphics.FillRectangle(Brush, Rect);
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Repaint;
end;
測試二, 使用靜態數組參數構建 IGPColorBlend:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics: IGPGraphics;
Rect: TGPRect;
Brush: IGPLinearGradIEntBrush;
ColorBlend: IGPColorBlend;
cs: array[0..4] of TGPColor;
ps: array[0..4] of Single;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
Rect.Initialize(ClIEntRect);
Brush := TGPLinearGradIEntBrush.Create(Rect, 0, 0, 0);
cs[0] := $FF000000;
cs[1] := $FFFF0000;
cs[2] := $FFFFFF00;
cs[3] := $FFFF0000;
cs[4] := $FF000000;
ps[0] := 0;
ps[1] := 0.25;
ps[2] := 0.5;
ps[3] := 0.75;
ps[4] := 1;
ColorBlend := TGPColorBlend.Create(cs, ps);
Brush.InterpolationColors := ColorBlend;
Graphics.FillRectangle(Brush, Rect);
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Repaint;
end;
測試三, 使用數組常量或動態數組參數構建 IGPColorBlend:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
const
ps: array[0..4] of Single = (0, 0.25, 0.5, 0.75, 1);
var
Graphics: IGPGraphics;
Rect: TGPRect;
Brush: IGPLinearGradIEntBrush;
ColorBlend: IGPColorBlend;
cs: array of TGPColor;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
Rect.Initialize(ClIEntRect);
Brush := TGPLinearGradIEntBrush.Create(Rect, 0, 0, 0);
SetLength(cs, 5);
cs[0] := $FF000000;
cs[1] := $FFFF0000;
cs[2] := $FFFFFF00;
cs[3] := $FFFF0000;
cs[4] := $FF000000;
ColorBlend := TGPColorBlend.Create(cs, ps);
Brush.InterpolationColors := ColorBlend;
Graphics.FillRectangle(Brush, Rect);
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Repaint;
end;