Mirror

Find the contrasting colour (2) (Views: 704)


Problem/Question/Abstract:

I have x items, and I want to assign a color for each, but I want the colors to be as different as possible. I think the best would be to distribute x points with the greatest distance possible in an RGB cube. I have no idea how to implement that, or is there some other solution?

Answer:

You can use the invers color:

InversColor := ColorToRGB(AColor) xor $FFFFFF;

I you need a maximal contrast, the result is black or white, dependent of the brightness:

function MaximumContrastColor(Value: TColor): TColor;
var
  R, G, B: Integer;
  Min, Max: Integer;
begin
  Value := ColorToRGB(Value);
  R := GetRValue(Value);
  G := GetGValue(Value);
  B := GetBValue(Value);
  Min := R;
  if Min > G then
    Min := G;
  if Min > B then
    Min := B;
  Max := R;
  if Max < G then
    Max := G;
  if Max < B then
    Max := B;
  if Min + Max > 255 then
    Result := clBlack
  else
    Result := clWhite;
end;

<< Back to main page