GdiPlus[13]: IGPLinearGradientBrush 之多色渐变


多色渐变需要: 一个 "颜色数组" 和一个 "位置数组" (0-1 之间的位置比例);

IGPLinearGradientBrush.InterpolationColors 属性对应一个 IGPColorBlend 对象, 此对象专门用于装载上面两个数组.

IGPColorBlend 的 Colors 和 Positions 两个属性分别对应这两个数组.

下面的三个示例的结果都是一样的.
测试效果图:

GdiPlus[13]: IGPLinearGradientBrush 之多色渐变
测试一, 使用开放数组参数构建 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;

 
 
 
 
 

 

 
  

你可能感兴趣的:(GdiPlus[13]: IGPLinearGradientBrush 之多色渐变)