Solved

label's appropriate font color depending on its background

Posted on 2001-08-02
20
261 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: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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Font: I need Lato on my Mac 10 361
how to update exe applicatio from internet ? 6 87
Multi-layered image in FireMonkey 9 49
GET error with Font Awesome 3 18
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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