2006. június 11., vasárnap

How to automatically set the font color according to the background color


Problem/Question/Abstract:

How can I make the font color dependent from the background color? I want to make the color selection between white and black dependent from the background color.

Answer:

This seems to work pretty well, at least on a non-palette video mode:

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;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with colordialog do
  begin
    color := label1.color;
    if execute then
    begin
      label1.color := color;
      label1.font.color := InverseColor(color);
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése