• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

label's appropriate font color depending on its background

My application uses labels that is either black or white as its font color. My question is ... how can you determine wether to use black or white font color  depending on the color of the label's background color?

I would like to use a function to get this result,
something like the one below

function Get_FontColor(background: TColor): TColor;
var
  FontColor: TColor;
begin
  given the background
  if background is ..... then FontColor:=clBlack
  else ... FontColor:=clWhite;
  Result:=FontColor;
end;

thanks so much :)
0
ttanyag
Asked:
ttanyag
  • 7
  • 6
  • 5
  • +1
1 Solution
 
kretzschmarCommented:
maybe something like this

function getcolor(ParentControl : TWinControl) : TColor;
begin
  result := clBlack;
  If assigned(ParentControl) then
  case ParentControl.Color of
    clBlack : result := clWhite;
    //.... more specifications
  end;
end;

to call
label1.font.color := getcolor(label1.parent);

meikl ;-)
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

 you mean some thing like that?

function Get_FontColor(background: TColor): TColor;
var
 FontColor: TColor;
begin

 if background = clblack then FontColor:=clBlack
 else FontColor:=clWhite;
 Result:=FontColor;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Color := Get_fontcolor(Lable1.caption);
end;
0
 
ttanyagAuthor Commented:
my background color can vary from light yellow to navy blue to light gray

... i want to be able to identify wether to use black or white font depending on the RGB of the background color.

is there any formula or color range availabe ...
ex.
condition:
if background color>{$00000000 - certain hex color} then  
  fontcolor:=clwhite
else
  fontcolor:=clblack;

i am asking for that certain condition
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohammed NasmanSoftware DeveloperCommented:
sorry wrong code

you mean you want change the color for label, if it black to white and vice versa?

function Get_FontColor(background: TColor): TColor;
var
 FontColor: TColor;
begin

 if background = clWhite then FontColor:=clBlack
 else FontColor:=clwhite;
 Result:=FontColor;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Color := Get_fontcolor(Label1.color);
end;
0
 
ttanyagAuthor Commented:
as much as possible i don't want to use a case statement identifying each of my background colors because there a lot. i want the procedure to be more generic.
0
 
kretzschmarCommented:
maybe i missed something,
are your labels transparent(property)?
0
 
ttanyagAuthor Commented:
yup they are transaprent ...

but like a white font label does not look nice on a cream (light colored) background or a black font label does not look nice on a maroon background (dark colored).

i'm wondering if ther is any color range avaialbe so that i would be able to distinguish light colors from dark colors?

so that my condition woud go something like
 
  if backgroundcolor is within light color range then i would use the black font

  if backgroundcolor is within dark color range then i would use the white font
0
 
kretzschmarCommented:
well, maybe you should split up the detected backgroundcolor into its rgb values,
and is someone greater than 128 or 180,
then use clblack otherwise clWhite
0
 
scrapdogCommented:
You could use the "negative" of the background color, this way it is guaranteed to contrast.

To get the negative of a color, you exclusive-or it with white.  For example:

FontColor := BackgroundColor xor clWhite;
0
 
scrapdogCommented:
You can also test a color's brightness by adding the three components (r, g, b) together.  If the result is greater than 382, then the color is closer to white.  If less than or equal to 382, then the color is closer to black.
0
 
ttanyagAuthor Commented:
getting the negative is really a good idea (thanks) however, i might need to use just black and white due to specifications for our color scheme :)

how do you convert TColor to RGB values?
0
 
ttanyagAuthor Commented:
getting the negative is really a good idea (thanks) however, i might need to use just black and white due to specifications for our color scheme :)

how do you convert TColor to RGB values?
0
 
scrapdogCommented:
here is one way:

type TRGB = packed record b,g,r,a :byte; end;

with TRGB(BackgroundColor) do
  if r+g+b > 382 then BackgroundColor := clBlack else BackgroundColor := clWhite;


This is the way I usually do it (the "type" declaration is usually local to the procedure I use it in).

I know there is a structure somewhere in the VCL for casting to RGB, but this works just as well.
0
 
scrapdogCommented:
Oops, this should read

with TRGB(BackgroundColor) do
 if r+g+b > 382 then FontColor := clBlack else FontColor := clWhite;


I've been making a lot of typos tonight. =)

0
 
scrapdogCommented:
To complete your function:

function Get_FontColor(background: TColor): TColor;
type
  TRGB = packed record b,g,r,a :byte; end;
var
 FontColor: TColor;
begin
  with TRGB(Background) do
    if r+g+b > 382 then Result := clBlack else Result := clWhite;
end;

=)
0
 
ttanyagAuthor Commented:
thanks a bunch
0
 
kretzschmarCommented:
maybe i should shut up, and giving no more hints
0
 
scrapdogCommented:
?
0
 
scrapdogCommented:
kretzschmar:  sorry if it looks like I stole your answer, but if you look closely, it is quite different than yours

ttanyaq:  you have probably already caught this, but the "var FontColor :TColor" is not necessary in the procedure (I forgot to take it out).  Thanks for the raise in points, btw.
0
 
kretzschmarCommented:
ignore me, scrap,
sometimes i have bad days
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now