HSVを指定して色を取得する
HSVをDelphi(Pascal)でHSV形式を利用するための自作関数。
unit HSVtoColor2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TForm1 = class(TForm) Image1: TImage; procedure FormCreate(Sender: TObject); private function getTColor(const h:Integer; const s, v:Double):TColor; end; var Form1: TForm1; implementation {$R *.dfm} uses Math; procedure TForm1.FormCreate(Sender: TObject); var x:integer; begin with Image1.Canvas do begin Pen.Width := 1; for x := 0 to Image1.Width - 1 do begin Pen.Color := getTColor(Round(360*x/Image1.Width), 1.0, 1.0); MoveTo(x, 0); LineTo(x, Image1.Height); end; end; end; // HSVを指定してTColorを得る関数 // 変換式はWikipediaより引用 function TForm1.getTColor(const h:Integer; const s, v:Double):TColor; // GRBを指定してTColorを得る関数 function RGBtoTColor(const r,g,b:Double):TColor; begin result := Round($FF * Min(1.0, Max(0.0, r))) OR (Round($FF * Min(1.0, Max(0.0, g))) shl 8) OR (Round($FF * Min(1.0, Max(0.0, b))) shl 16); end; var _h, hi :integer; _s, _v, f, p, q, t :Double; begin // 0<=h<360、0.0<=s<=1.0、0.0<=v<=1.0に収める _h := h mod 360; _s := Min(1.0, Max(0.0, s)); _v := Min(1.0, Max(0.0, v)); while _h < 0 do _h := _h + 360; if _s = 0.0 then begin result := RGBtoTColor(_v, _v, _v); end else begin hi := Floor(_h/60) mod 6; f := _h/60 - hi; p := _v * (1.0 - _s); q := _v * (1.0 - f * _s); t := _v * (1.0 - (1.0 - f) * _s); case hi of 0: result := RGBtoTColor(_v,t,p); 1: result := RGBtoTColor(q,_v,p); 2: result := RGBtoTColor(p,_v,t); 3: result := RGBtoTColor(p,q,_v); 4: result := RGBtoTColor(t,p,_v); else result := RGBtoTColor(_v,p,q); end; end; end; end.