使用Delphi 10.2和主题:是否有可靠的方法来确定当前主题是否是一个"轻";(白色或浅色clWindow颜色(或";深色";(黑色、近黑色或深色clWindow颜色(主题?我必须用特定的颜色绘制某些元素(例如,不管主题颜色如何,都用红色绘制报警指示器(,但需要使用"红色";明亮的";暗背景下的红色与更"明亮"的背景相比;静音";红色背景。
我尝试过查看主题的clWindow颜色的各种RGB组件(即,如果3种组件颜色中有2种>$7F,则它是一个"浅色"主题(,但对于某些主题来说,这并不总是可靠的。
有没有更好/更可靠的方法来确定这一点?
如果您正在编写IDE插件,可以向BorlandIDEServices
请求IOTAIDEThemingServices
。后者包含一个属性ActiveTheme
,它为您提供当前主题名称。
基于Tom的信息,我实现了以下功能,它对我尝试过的每个主题都很有效:
function IsLightTheme: Boolean;
var
WC: TColor;
PL: Integer;
R,G,B: Byte;
begin
//if styles are disabled, use the default Windows window color,
//otherwise use the theme's window color
if (not StyleServices.Enabled) then
WC := clWindow
else
WC := StyleServices.GetStyleColor(scWindow);
//break out it's component RGB parts
ColorToRGBParts(WC, R, G, B);
//calc the "perceived luminance" of the color. The human eye perceives green the most,
//then red, and blue the least; to determine the luminance, you can multiply each color
//by a specific constant and total the results, or use the simple formula below for a
//faster close approximation that meets our needs. PL will always be between 0 and 255.
//Thanks to Glenn Slayden for the formula and Tom Brunberg for pointing me to it!
PL := (R+R+R+B+G+G+G+G) shr 3;
//if the PL is greater than the color midpoint, it's "light", otherwise it's "dark".
Result := (PL > $7F);
end;```