GdiPlus[13]: IGPLinearGradientBrush 之多色渐变


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

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

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

下面的三个示例的结果都是一样的.

测试效果图:

o_09121403.png

测试一, 使用开放数组参数构建 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;

转载于:https://www.cnblogs.com/del/archive/2009/12/14/1624020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值