delphi 色彩搭配,HSV与RGB转换

把之前练手写的小玩意翻出来了,看看能不能整合下用起来。2年后再次感谢@moon_sky_98

主要用到:
1. HSV颜色模型

HSV(HSV颜色模型)_百度百科 (baidu.com)

2. delphi自带的GDI,Winapi.GDIPAPI, Winapi.GDIPOBJ, Winapi.GDIPUTIL

3. HSV与RGB互换

procedure RGB2HSV(R, G, B: Byte; var H, S, V: Single);
var
  iMin,iMax: Byte;
  dR, dG, dB: Double;
begin
  iMin := Min(Min(R, G), B);
  iMax := Max(Max(R, G), B);
  if iMin = iMax then
    H := 0
  else if (iMax = R) and Equal_F64G(G, B) then
    H := 60.0 * ((G - B) / (iMax - iMin))
  else if (iMax = R) and not Equal_F64G(G, B) then
    H := 60.0 * ((G - B) / (iMax - iMin)) + 360
  else if iMax = G then
    H := 60.0 * ((B - R) / (iMax - iMin)) + 120
  else if iMax = B then
    H := 60.0 * ((R - G) / (iMax - iMin)) + 240;

  if iMax = 0 then
    S := 0
  else
    S := 1 - (iMin * 1.0 / iMax);

  dR := R / 255.0;
  dG := G / 255.0;
  dB := B / 255.0;

  V := max(max(dR, dG), dB);
  if H >= 360 then
    H := 0;
  S := S * 100.0;
  V := V * 100.0;
end;

procedure HSV2RGB(H, S, V: Single; var R, G, B: Byte);
var
  i: Integer;
  f,p,q,t,dR,dG,dB: Double;
begin
  V := V / 100.0;
  S := S / 100.0;
  if (H = 360.0) or (H > 360.0) then
    H := 0;
  if S = 0 then
  begin
    R := Byte(Round(V * 255.0));
    G := Byte(Round(V * 255.0));
    B := Byte(Round(V * 255.0));
  end
  else
  begin
    i := Floor(H / 60.0) mod 6;
    f := H / 60.0 - i;
    p := V * (1 - S);
    q := V * (1 - S * f);
    t := V * (1 - S * (1 - f));
    case i of
      0: begin dR := V; dG := t; dB := p; end;
      1: begin dR := q; dG := V; dB := p; end;
      2: begin dR := p; dG := V; dB := t; end;
      3: begin dR := p; dG := q; dB := V; end;
      4: begin dR := t; dG := p; dB := V; end;
      5: begin dR := V; dG := p; dB := q; end;
    end;
    R := Byte(Round(dR * 255.0));
    G := Byte(Round(dG * 255.0));
    B := Byte(Round(dB * 255.0));
  end;
end;

4. 基于HSV颜色模型,计算区域、计算并填充颜色

posted on 2022-03-24 19:59  Mozzie2020  阅读(133)  评论(0编辑  收藏  举报

导航