Mirror

Invert the color of a TEdit (Views: 705)


Problem/Question/Abstract:

I have a TEdit control and there are two color properties I wish to set: TEdit.Color and TEdit.Font.Color. I did something like

TEdit.Color := clBlack
TEdit.Font.Color := TEdit.Color xor $7FFFFFFF;

but TEdit.Font.Color doesn't show as contrast of TEdit.Color. Can anybody advise on that. I wish to make the TEdit.Color and TEdit.Font.Color always contrast to each other. My application allows the user to change the TEdit.color at runtime.

Answer:

Try this one:

function InverseColor(color: TColor): TColor;
var
  rgb_: TColorRef;

  function Inv(b: Byte): Byte;
  begin
    if b > 128 then
      result := 0
    else
      result := 255;
  end;

begin
  rgb_ := ColorToRgb(color);
  rgb_ := RGB(Inv(GetRValue(rgb_)), Inv(GetGValue(rgb_)), Inv(GetBValue(rgb_)));
  Result := rgb_;
end;

<< Back to main page