1 2 //If the user can change the background color of a component, 3 //make sure that the caption/text is displayed in a contrasting color. 4 5 unit Unit1; 6 7 interface 8 9 uses 10 Windows, SysUtils, Graphics, Controls, Forms, StdCtrls, Buttons, ComCtrls, 11 Classes; 12 13 type 14 TForm1 = class(TForm) 15 Edit1: TEdit; 16 BitBtn1: TBitBtn; 17 procedure BitBtn1Click(Sender: TObject); 18 end; 19 20 var 21 Form1: TForm1; 22 23 implementation 24 25 {$R *.dfm} 26 27 function ContrastColor(clBackground: TColor): TColor; 28 begin 29 with TRGBQuad(clBackground) do 30 if (rgbRed * 0.3) + (rgbGreen * 0.59) + (rgbBlue * 0.11) > 127.5 then 31 Result:= clBlack 32 else Result:= clWhite; 33 end; 34 35 //To test: 36 procedure TForm1.BitBtn1Click(Sender: TObject); 37 begin 38 Edit1.Color:= Random(255) shl 16 + Random(255) shl 8 + Random(255); 39 Edit1.Font.Color:= ContrastColor(Edit1.Color); 40 end; 41 42 end.