Here are three different ways to get the contrasting color of a given color:
function ContrastingColor(const AColor: TColor): TColor;
{ Return contracting color of the passed color. }
begin
if ((AColor and $FF) * 77 + ((AColor shr 8) and $FF) * 150 +
((AColor shr 16) and $FF) * 29) > 127 * 256 then
Result := clBlack
else
Result := clWhite;
end;
function GetContrastingColor(AColor: TColor): TColor;
{ Get a contrasting color. }
begin
AColor := ColorToRGB(AColor); { convert system-defined colors }
Result := (AColor + $000080) and $0000FF + { red component }
(AColor + $008000) and $00FF00 + { green component }
(AColor + $800000) and $FF0000; { blue component }
end;
function GetContrastColor(AColor: TColor): TColor;
{ Get contrast color. }
begin
AColor := ColorToRGB(AColor); { convert system-defined colors }
if (AColor + AColor shr 8 + AColor shr 16) and $0000FF >= $180 then
Result := clBlack
else
Result := clWhite;
end;
Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development