排序列表的颜色(调色板)的色调和亮度,使用delphi



我有几个颜色列表(TColor)存储在数组中,我喜欢使用HUE或Luminosity存在任何delphi库或组件具有这种函数或算法?

Delphi包含一个称为GraphUtil的单元,该单元具有称为SortColorArray的函数

procedure SortColorArray(ColorArray: TColorArray; L, R: Integer;
  SortType: TColorArraySortType; Reverse: Boolean = False);

这个函数可以按色相,饱和度,亮度,红色,绿色,蓝色对颜色列表进行排序。

,你可以这样使用

var
 ColorList : TColorArray; 
 i         : Integer;
begin
  SetLength(ColorList,WebNamedColorsCount);
  //fill the list the colors in this case using webcolors
  for i := 0 to WebNamedColorsCount - 1 do
  begin
   ColorList[i].Value:=WebNamedColors[i].Value;
   ColorList[i].Name :='';
  end;
  //sort the colors by HUE
  SortColorArray(ColorList,0,WebNamedColorsCount-1,stHue,False);
  //do your stuff here
end;

我愿意

type
  TRGB = record
    rgbRed,              // Red intensity in [0, 1]
    rgbGreen,            // Green intensity in [0, 1]
    rgbBlue: double;     // Blue intensity in [0, 1]
  end;
  THSV = record
    hsvHue,             // Hue angle in [0, 360]
    hsvSaturation,      // Saturation in [0, 1]
    hsvValue: double;   // Value in [0, 1]
  end;
function MaxComponent(const Color: TRGB): real;
begin
  with Color do
    result := max(max(rgbRed, rgbGreen), rgbBlue);
end;
function MinComponent(const Color: TRGB): real;
begin
  with Color do
    result := min(min(rgbRed, rgbGreen), rgbBlue);
end;
function Fix360(const a: Real): real;
begin
  result := a;
  if result > 360 then
    while result > 360 do
      result := result - 360
  else if result < 0 then
    while result < 0 do
      result := result + 360;
end;

function RGBToHSV(const Color: TRGB): THSV;
var
  cmax, cmin, cdiff: real;
begin
  cmax := MaxComponent(Color);
  cmin := MinComponent(Color);
  cdiff := cmax - cmin;
  with Color, result do
  begin
    // Hue
    if cmax = cmin then
      hsvHue := 0
    else if cmax = rgbRed then
      hsvHue := (60 * (rgbGreen - rgbBlue) / cdiff)
    else if cmax = rgbGreen then
      hsvHue := (60 * (rgbBlue - rgbRed) / cdiff) + 120
    else
      hsvHue := (60 * (rgbRed - rgbGreen) / cdiff) + 240;
    hsvHue := Fix360(hsvHue);
    // Saturation
    if cmax = 0 then
      hsvSaturation := 0
    else
      hsvSaturation := 1 - cmin / cmax;
    // Value
    hsvValue := cmax;
  end;
end;
function HSVToRGB(const Color: THSV): TRGB;
var
  hi: integer;
  f, q, p, t: real;
begin
  with Color do
  begin
    hi := floor(hsvHue / 60) mod 6;
    f := hsvHue / 60 - floor(hsvHue / 60);
    p := hsvValue * (1 - hsvSaturation);
    q := hsvValue * (1 - f * hsvSaturation);
    t := hsvValue * (1 - (1 - f) * hsvSaturation);
    case hi of
      0: result := RGB(hsvValue, t, p);
      1: result := RGB(q, hsvValue, p);
      2: result := RGB(p, hsvValue, t);
      3: result := RGB(p, q, hsvValue);
      4: result := RGB(t, p, hsvValue);
      5: result := RGB(hsvValue, p, q);
    end;
  end;
end;

在RGB和HSV之间进行转换。现在您可以轻松地获得任何颜色的色调,排序应该是微不足道的。

如何从TColor中得到TRGB ?只做

function PascalColorToRGB(Color: TColor): TRGB;
begin
  Color := GetSysColor(Color);
  with result do
  begin
    rgbRed := GetRValue(Color) / 255;
    rgbGreen := GetGValue(Color) / 255;
    rgbBlue := GetBValue(Color) / 255;
  end;
end;

如何从TRGB中获得TColor ?只做

function GetPascalColor(const Color: TRGB): TColor;
begin
  with Color do
    result := Windows.RGB(round(255*rgbRed),
      round(255*rgbGreen),
      round(255*rgbBlue));
end;

最新更新