Author: Jonas Bilinkevicius
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:
1 2 function InverseColor(color: TColor): TColor;
3 var4 rgb_: TColorref;
5 function Inv(b: Byte): Byte;
6 begin7 if b > 128 then8 result := 0
9 else10 result := 255;
11 end;
12 begin13 rgb_ := ColorToRgb(color);
14 rgb_ := RGB(Inv(GetRValue(rgb_)), Inv(GetGValue(rgb_)), Inv(GetBValue(rgb_)));
15 Result := rgb_;
16 end;
17 18 procedure TForm1.Button1Click(Sender: TObject);
19 begin20 with colordialog do21 begin22 color := label1.color;
23 if execute then24 begin25 label1.color := color;
26 label1.font.color := InverseColor(color);
27 end;
28 end;
29 end;