I'm working on a Delphi FMX project where I need to generate a set of 10 random colors for use in a UI. The problem is that when I use TAlphaColorRec with random RGB values, many of the colors end up looking too similar.
For example, multiple shades of blue or green that are hard to tell apart.
This is my current code that generates the numbers:
function GenerateRandomColors(Count: UInt64): TArray<TAlphaColor>;
begin
SetLength(Result, Count);
for var i := 1 to Count do
begin
var C: TAlphaColorRec;
C.R := Random(256);
C.G := Random(256);
C.B := Random(256);
C.A := 255;
Result[i-1] := C.Color;
end;
end;
And I'm using it within this procedure:
procedure TForm1.GenerateColors;
begin
Randomize;
Memo1.Lines.Clear;
GridLayout1.DeleteChildren;
var Colors := GenerateRandomColors(10);
for var Col in Colors do
begin
Memo1.Lines.Add(AlphaColorToString(Col));
var Rect := TRectangle.Create(GridLayout1);
Rect.Parent := GridLayout1;
Rect.Fill.Color := Col;
Rect.Stroke.Kind := TBrushKind.None;
end;
end;
And this is how it looks:
I need to modify GenerateRandomColors so that it generates colors that are more visually distinct from each other.
I have looked at the following questions already:
- How to automatically generate N "distinct" colors?
- Algorithm to randomly generate an aesthetically-pleasing color palette
- Algorithm For Generating Unique Colors
- Generate Random Color distinguishable to Humans
But the answers are not super useful because I need it to work with the Delphi Programming Language and most of those answers are using other programming languages.
Can someone please help me to modify GenerateRandomColors so that it generates distinct colors using Delphi within the FireMonkey (FMX) framework.


