首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

再學 GDI+ [95]:TGPImage 色彩旋轉變換 - ColorMatrix 實戰

     編輯:Delphi

透過 TColorMatrix 實現 RGB 三通道兩兩繞第三通道旋轉,同時驗證 GDI+ 原生支援 PNG/GIF/TIF 透明圖片顯示。

三種旋轉矩陣定義

紅綠繞藍旋轉

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)
);

綠藍繞紅旋轉

ColorMatrix: TColorMatrix = (
  (1.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)
);

紅藍繞綠旋轉

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)
);

完整程式碼 (Unit1.pas)

unit Unit1; 

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

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

var Form1: TForm1; 

implementation 
{$R *.dfm} 
uses GDIPOBJ, GDIPAPI; 

const Matrix: TColorMatrix = ( ... ); { 單位矩陣 } 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  TrackBar1.Height := 23; 
  TrackBar1.ShowSelRange := False; 
  TrackBar1.Min := -180; 
  TrackBar1.Max := 180; 
  TrackBar1.Position := 0; 
  with RadioGroup1.Items do 
  begin 
    Add('紅色與綠色繞藍色旋轉'); 
    Add('紅色與藍色繞綠色旋轉'); 
    Add('綠色與藍色繞紅色旋轉'); 
  end; 
  RadioGroup1.ItemIndex := 0; 
  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; { 重置為單位矩陣 } 
  case RadioGroup1.ItemIndex of 
    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; 

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

效果圖

色彩旋轉效果

核心原理

  • TColorMatrix 為 5x5 矩陣,前 3x3 控制 RGB 線性變換
  • 旋轉矩陣標準形式:[cos -sin; sin cos] 放置於對應通道位置
  • TGPImageAttributes.SetColorMatrix 套用至繪圖管線
  • TrackBar 控制角度 (-180°~180°),即時重繪

此範例展示了 GDI+ 強大的色彩空間線性代數操作能力,可應用於圖像特效、色彩校正、藝術濾鏡等領域。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved