# 再学 GDI+[95]: TGPImage(15) - 颜色的旋转变换

//红色与绿色绕蓝色旋转(r 是弧度)
ColorMatrix: TColorMatrix = (
(Cos(r),   Sin(r), 0.0,  0.0,  0.0),
(-Sin(r),  Cos(r), 0.0,  0.0,  0.0),
(0.0,      0.0,    1.0,  0.0,  0.0),
(0.0,      0.0,    0.0,  1.0,  0.0),
(0.0,      0.0,    0.0,  0.0,  1.0));

//绿色与蓝色绕红色旋转(r 是弧度)
ColorMatrix: TColorMatrix = (
(0.0,  0.0,     0.0,     0.0,  0.0),
(0.0,  Cos(r),  Sin(r),  0.0,  0.0),
(0.0,  -Sin(r), Cos(r),  0.0,  0.0),
(0.0,  0.0,     0.0,     1.0,  0.0),
(0.0,  0.0,     0.0,     0.0,  1.0));

//红色与蓝色绕绿色旋转(r 是弧度)
ColorMatrix: TColorMatrix = (
(Cos(r),   0.0,  Sin(r),  0.0,  0.0),
(-Sin(r),  0.0,  Cos(r),  0.0,  0.0),
(0.0,      0.0,  1.0,     0.0,  0.0),
(0.0,      0.0,  0.0,     1.0,  0.0),
(0.0,      0.0,  0.0,     0.0,  1.0));



unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
TForm1 = class(TForm)
TrackBar1: TTrackBar;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

uses GDIPOBJ, GDIPAPI;

const
Matrix: TColorMatrix = (
(1.0, 0.0, 0.0, 0.0, 0.0),
(0.0, 1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 0.0, 1.0));

procedure TForm1.FormCreate(Sender: TObject);
begin
TrackBar1.Height := 23;
TrackBar1.ShowSelRange := False;
TrackBar1.Min := -180;
TrackBar1.Max := 180;
TrackBar1.Position := 0;

begin
end;

DoubleBuffered := True;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
img: TGPImage;
ImageAttributes: TGPImageAttributes;
r: Single;
ColorMatrix: TColorMatrix;
begin
g := TGPGraphics.Create(Canvas.Handle);
img := TGPImage.Create('c:\temp\test.png');

ImageAttributes := TGPImageAttributes.Create;

r := TrackBar1.Position * pi / 180; {根据角度算出弧度}
ColorMatrix := Matrix;              {回复默认值}
0: begin {红色与绿色绕蓝色旋转}
ColorMatrix[0,0] := Cos(r);
ColorMatrix[0,1] := Sin(r);
ColorMatrix[1,0] := -Sin(r);
ColorMatrix[1,1] := Cos(r);
end;
1: begin {红色与蓝色绕绿色旋转}
ColorMatrix[0,0] := Cos(r);
ColorMatrix[0,2] := Sin(r);
ColorMatrix[2,0] := -Sin(r);
ColorMatrix[2,2] := Cos(r);
end;
2: begin {绿色与蓝色绕红色旋转}
ColorMatrix[1,1] := Cos(r);
ColorMatrix[1,2] := Sin(r);
ColorMatrix[2,1] := -Sin(r);
ColorMatrix[2,2] := Cos(r);
end;
end;

ImageAttributes.SetColorMatrix(ColorMatrix);

g.DrawImage(img,
MakeRect(4, 4, img.GetWidth, img.GetHeight),
0,
0,
img.GetWidth,
img.GetHeight,
UnitPixel,
ImageAttributes
);

ImageAttributes.Free;
img.Free;
g.Free;
end;

begin
Repaint;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
Repaint;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
Repaint;
end;

end.



object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 290
ClientWidth = 208
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object TrackBar1: TTrackBar
Left = 3
Top = 170
Width = 202
Height = 22
TabOrder = 0
OnChange = TrackBar1Change
end