Solved

label's appropriate font color depending on its background

Posted on 2001-08-02
20
258 Views
Last Modified: 2013-12-03
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
Comment
Question by:ttanyag
  • 7
  • 6
  • 5
  • +1
20 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6343855
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
 
LVL 22

Expert Comment

by:mnasman
ID: 6343883
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
 

Author Comment

by:ttanyag
ID: 6343912
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
 
LVL 22

Expert Comment

by:mnasman
ID: 6343919
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
 

Author Comment

by:ttanyag
ID: 6343920
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6343926
maybe i missed something,
are your labels transparent(property)?
0
 

Author Comment

by:ttanyag
ID: 6343944
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6344005
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6344022
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6344073
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ttanyag
ID: 6344081
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
 

Author Comment

by:ttanyag
ID: 6344101
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6344107
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6344113
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
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 total points
ID: 6344130
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
 

Author Comment

by:ttanyag
ID: 6344299
thanks a bunch
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6344319
maybe i should shut up, and giving no more hints
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6346708
?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6346751
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6347372
ignore me, scrap,
sometimes i have bad days
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now