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

再學GDI+[68]: 路徑漸層筆刷特殊混合模式 - 三角形與鐘形漸層效果

     編輯:Delphi

前言

除了通用的 SetBlend,GDI+ 還提供兩個預設的特殊混合函數:SetBlendTriangularShape(三角形漸層)與 SetBlendBellShape(鐘形漸層)。這兩個方法能以極簡代碼產生視覺豐富的聚光燈、光暈等效果。

兩大特殊混合方法詳解

方法視覺效果參數說明數學模型
SetBlendTriangularShape(focus, scale)尖銳的三角形漸層,像聚光燈focus: 焦點位置 (0~1)
scale: 縮放比例 (0~1,預設 1.0)
線性遞增到焦點再線性遞減
SetBlendBellShape(focus, scale)平滑的鐘形曲線漸層,像柔光focus: 焦點位置 (0~1)
scale: 縮放比例 (0~1,預設 1.0)
類似高斯分佈的平滑曲線

雙軌條即時調控介面

  • TrackBar1:控制 focus 參數(0~100 對應 0.0~1.0)
  • TrackBar2:控制 scale 參數(0~100 對應 0.0~1.0)
  • RadioGroup:切換三角形/鐘形模式

核心實作程式碼

procedure TForm1.FormPaint(Sender: TObject);
const
  colors: array[0..0] of TGPColor = (aclRed);
var
  g: TGPGraphics;
  path: TGPGraphicsPath;
  pb: TGPPathGradientBrush;
  num: Integer;
begin
  g := TGPGraphics.Create(Canvas.Handle);
  path := TGPGraphicsPath.Create;
  path.AddRectangle(MakeRect(30,10,160,150));
  pb := TGPPathGradientBrush.Create(path);
  num := Length(colors);
  pb.SetSurroundColors(@colors, num);
  pb.SetCenterColor(aclYellow);  {中心色為黃色}
  
  case RadioGroup1.ItemIndex of
    0: pb.SetBlendTriangularShape(TrackBar1.Position/100, TrackBar2.Position/100);
    1: pb.SetBlendBellShape(TrackBar1.Position/100, TrackBar2.Position/100);
  end;
  
  g.FillPath(pb, path);
  pb.Free; path.Free; g.Free;
end;

執行效果預覽

三角形與鐘形漸層效果

註:GIF 限制 256 色,實際執行顏色更平滑

參數調教心得

  • focus = 0.5, scale = 1.0:標準對稱漸層
  • focus 偏移:移動亮點位置,模擬光源偏移
  • scale 減小:漸層範圍收縮,邊緣變暗更快
  • scale = 0:幾乎看不見漸層(全暗或全亮)
  • 搭配中心色:SetCenterColor 決定焦點顏色,環繞色決定邊緣顏色

表單定義檔

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 261
  ClientWidth = 221
  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 ... {兩個 TrackBar + RadioGroup 定義略}
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved