Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the opposite color of any background images

how to get the right color automatically that is depending on the background? if its darker background image, that will automatically change the font color into brighter colors. was it possible? any idea?

like image 866
XBasic3000 Avatar asked Dec 13 '10 15:12

XBasic3000


People also ask

What color is red inverted?

The opposite of the color red is green.

How do you invert text color in CSS?

CSS allows you to invert the color of an HTML element by using the invert() CSS function that you can pass to the filter property. Because the default color of the text is black, the color is inverted into white with filter: invert(100%) syntax.


1 Answers

David's answer is generally working very well. But there are a few options, and I will mention some of them. First, the very most naïve approach, is to do

function InvertColor(const Color: TColor): TColor;
begin
    result := TColor(Windows.RGB(255 - GetRValue(Color),
                                 255 - GetGValue(Color),
                                 255 - GetBValue(Color)));
end;

but this suffers from the #808080 problem (why?). A very nice solution is David's, but it looks very bad for some unfortunate background colours. Although the text is certainly visible, it looks horrible. One such "unfortunate" background colour is #008080.

Usually I prefer the text to be black if the background is "light", and white if the background is "dark". I thus do

function InvertColor(const Color: TColor): TColor;
begin
  if (GetRValue(Color) + GetGValue(Color) + GetBValue(Color)) > 384 then
    result := clBlack
  else
    result := clWhite;
end;

Also, if you are using Delphi 2009+ and targeting Windows Vista+, you might be interested in the GlowSize parameter of the TLabel.

like image 129
Andreas Rejbrand Avatar answered Sep 22 '22 23:09

Andreas Rejbrand