[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

label's appropriate font color depending on its background

Posted on 2001-08-02
20
Medium Priority
?
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Mohammed Nasman
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
Independent Software Vendors: 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!

 
LVL 22

Expert Comment

by:Mohammed Nasman
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
 

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 800 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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

649 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