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

DelphiX 實戰:TDIB.DoColorize 雙色調色映射

     編輯:Delphi

TDIB.DoColorize 將影像灰階值映射到兩指定顏色之間的漸層,產生雙色調藝術效果。本文提供黃→紅映射的完整範例。

效果預覽

DoColorize 效果

核心程式碼

unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DIB, StdCtrls;
type
  TForm1 = class(TForm)
    DXPaintBox1: TDXPaintBox;
    Button1: TButton; // 載入圖片
    Button2: TButton; // 雙色調映射
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;
var Form1: TForm1;
implementation {$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const ImgPath1 = 'C:\Temp\DX.jpg';
begin
  DXPaintBox1.DIB.LoadFromFile(ImgPath1);
  DXPaintBox1.Width := DXPaintBox1.DIB.Width;
  DXPaintBox1.Height := DXPaintBox1.DIB.Height;
  DXPaintBox1.Repaint;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // DoColorize(Color1, Color2: TColor)  // 灰階 0→Color1, 255→Color2
  DXPaintBox1.DIB.DoColorize(clYellow, clRed);
  DXPaintBox1.Repaint;
end;
end.

映射原理

  1. 計算灰階:Gray = 0.299*R + 0.587*G + 0.114*B
  2. 線性插值:Result = Color1 + (Color2 - Color1) * (Gray / 255)
  3. 各通道獨立插值

經典雙色調組合

Color1 (暗部)Color2 (亮部)風格應用
clBlackclWhite標準灰階黑白照片
clNavyclYellow藍黃對比科技感、夜視
clMaroonclGold復古暖調懷舊、複古海報
clGreenclLime綠色終端機駭客風、矩陣風
clBlueclCyan冷冽藍調科幻、冰凍、醫療
clYellowclRed火焰漸層熱成像、能量、警示

應用場景

  • **偽彩色視覺化**:科學數據(溫度、壓力、高度)渲染
  • **藝術風格化**:安迪·沃荷風、波普藝術、極簡海報
  • **UI 主題色**:單一素材生成多主題版本
  • **熱成像/夜視模擬**:軍事、科幻遊戲視覺模式
  • **無障礙高對比度**:自訂前景/背景色組合

進階:多段漸層色調映射


procedure MultiColorize(DIB: TDIB; Colors: array of TColor; Positions: array of Single);
var x, y, i: Integer; c: TRGBQuad; gray: Byte; t: Single;
begin
  for y := 0 to DIB.Height - 1 do
    for x := 0 to DIB.Width - 1 do begin
      c := DIB.Pixels[x, y];
      gray := Round(0.299*c.rgbRed + 0.587*c.rgbGreen + 0.114*c.rgbBlue);
      t := gray / 255;
      // 找區間
      i := 0; while (i < High(Positions)) and (t > Positions[i+1]) do Inc(i);
      if i = High(Positions) then i := High(Positions) - 1;
      // 區間內插值
      var localT := (t - Positions[i]) / (Positions[i+1] - Positions[i]);
      c.rgbRed := Round(GetRValue(Colors[i]) * (1-localT) + GetRValue(Colors[i+1]) * localT);
      c.rgbGreen := Round(GetGValue(Colors[i]) * (1-localT) + GetGValue(Colors[i+1]) * localT);
      c.rgbBlue := Round(GetBValue(Colors[i]) * (1-localT) + GetBValue(Colors[i+1]) * localT);
      DIB.Pixels[x, y] := c;
    end;
end;
// 用法:MultiColorize(DIB, [clBlack, clBlue, clCyan, clYellow, clRed], [0, 0.25, 0.5, 0.75, 1]);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved