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