在Delphi中随机生成调色板的算法

时间:2013-11-21 19:06:09

标签: delphi colors

如何在Delphi中精美地生成随机颜色? 我在java中有这个代码,但我没有设法翻译成Delphi。

public Color generateRandomColor(Color mix) {
    Random random = new Random();
    int red = random.nextInt(256);
    int green = random.nextInt(256);
    int blue = random.nextInt(256);

    // mix the color
    if (mix != null) {
        red = (red + mix.getRed()) / 2;
        green = (green + mix.getGreen()) / 2;
        blue = (blue + mix.getBlue()) / 2;
    }

    Color color = new Color(red, green, blue);
    return color;
}

1 个答案:

答案 0 :(得分:2)

只需按照您发布的代码作为示例并将其转换为Delphi,这很容易做到

function GenerateRandomColor(const Mix: TColor = clWhite): TColor;
var
  Red, Green, Blue: Integer;
begin
  Red := Random(256);      
  Green := Random(256);
  Blue := Random(256);

  Red := (Red + GetRValue(ColorToRGB(Mix))) div 2;      
  Green := (Green + GetGValue(ColorToRGB(Mix))) div 2;
  Blue := (Blue + GetBValue(ColorToRGB(Mix))) div 2;
  Result := RGB(Red, Green, Blue);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Self.Color := GenerateRandomColor;
end;

initialization
  Randomize;
相关问题