如何根据背景自动获得正确的颜色?如果其背景图像较暗,则会自动将字体颜色更改为较亮的颜色。这有可能吗?有什么想法吗?
发布于 2010-12-13 17:26:26
David的回答大体上是非常有效的。但是有一些选择,我会提到其中的一些。首先,最天真的方法是
function InvertColor(const Color: TColor): TColor;
begin
result := TColor(Windows.RGB(255 - GetRValue(Color),
255 - GetGValue(Color),
255 - GetBValue(Color)));
end;
但这受到#808080问题的困扰(为什么?)。一个非常好的解决方案是David的,但对于一些不幸的背景颜色来说,它看起来非常糟糕。虽然文本肯定是可见的,但它看起来很可怕。其中一种“不幸”的背景颜色是#008080。
通常,如果背景是“浅”,我更喜欢文本是黑色的,如果背景是“暗”,则文本是白色的。我就是这样做的
function InvertColor(const Color: TColor): TColor;
begin
if (GetRValue(Color) + GetGValue(Color) + GetBValue(Color)) > 384 then
result := clBlack
else
result := clWhite;
end;
此外,如果您使用的是Delphi 2009+并且目标是Windows Vista+,那么您可能会对TLabel
的GlowSize
参数感兴趣。
发布于 2010-12-13 16:10:49
我使用下面的代码为我提供与指定颜色形成对比的颜色:
function xorColor(BackgroundColor: TColor): TColor;
begin
BackgroundColor := ColorToRGB(BackgroundColor);
Result := RGB(
IfThen(GetRValue(BackgroundColor)>$40, $00, $FF),
IfThen(GetGValue(BackgroundColor)>$40, $00, $FF),
IfThen(GetBValue(BackgroundColor)>$40, $00, $FF)
);
end;
发布于 2014-01-04 19:39:27
我尝试基于“线性”配色方案计算对比度,但它在粉色和青色输入值上确实不是很好。更好的方法是基于RGB公式进行计算:
brightness = sqrt( .241 * R^2 + .691 * G^2 + .068 * B^2 )
在Delphi中,我创建了这个子例程:
function GetContrastingColor(Color: TColor): TColor;
var r,g,b:double;i:integer;
begin
Color := ColorToRGB(Color);
r:=GetRValue(Color) ;
g:=GetGValue(Color) ;
b:=GetBValue(Color) ;
i:=round( Sqrt(
r * r * 0.241 +
g * g * 0.691 +
b * b * 0.068));
if (i > 128) then // treshold seems good in wide range
Result := clBlack
else
Result := clWhite;
end;
https://stackoverflow.com/questions/4430598
复制相似问题