11 Vote

Delphi/Lazarus: Readable Font Color for arbitrary Background Color

Tip by Stefan Trost | Last update on 2023-01-27 | Created on 2015-11-02

How to select a suitable color for a font, if the background can have an arbitrary color? Recently, I asked myself this question when I was programming my own color dialog.

In this dialog, I wanted to show the value of the color directly on the corresponding color and of course, the color value should always remain readable, no matter which background color is selected. I have coded the following function for this purpose.

function GetContrastColorBlackOrWhite(const AColor: TColor): TColor;
var
  R, G, B: single;
begin
  R := GetRValue(AColor) * 0.25;
  G := GetGValue(AColor) * 0.625;
  B := GetBValue(AColor) * 0.125;

  if (R + G + B) > 128 then begin
    result := clBlack;
  end else begin
    result := clWhite;
  end; 
 
end;   

You can pass an arbitrary color to this function and the function will give you either black or white as return value, depending on which color is better fitting to the passed color as background or foreground color.

As an example, if we pass the color white to this function, we get black as a suitable font color in front of a white background. On the other hand, if we hand over the color black to this function, we receive white as a suitable font color. Of course, the function not only works for these extreme cases, but also for gray values or tones of all other colors. For yellow or light red, green or blue as well as lighter shades of gray, we get the font color black, while darker gray tones or a dark red, green or blue return the font color white.

Application Example

Of course we would also like to see a small application example. In the following example code, we are setting the background color of an edit field to black and we are calling the function in order to find a suitable color for the text, which can be read despite the black background. The function will return white white as color of choice.

Edit1.Color      := clBlack;
Edit1.Font.Color := GetContrastColorBlackOrWhite(clBlack);

The trick of the function is, that the function is building the sum of all color values with a specific factor in order to compare the result with 128. Of course, you can also change this threshold to a higher or lower value, depending on where you would like to have the jump between black and white.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.