【问题标题】:Delphi 10.2 themes - "light" vsd "dark"Delphi 10.2 主题 - “light” vsd “dark”
【发布时间】:2024-01-06 11:01:01
【问题描述】:

使用 Delphi 10.2 和主题:是否有可靠的方法来确定当前主题是“浅色”(白色或浅色 clWindow 颜色)还是“深色”(黑色、接近黑色或深色 clWindow 颜色)主题?我必须用特定颜色绘制某些元素(例如,无论主题颜色如何,警报指示器都是红色的),但需要在深色背景下使用“亮”红色而不是在浅色背景下使用更“柔和”的红色。

我已经尝试查看主题 clWindow 颜色的各种 RGB 组件集(即,如果 3 个组件颜色中的 2 个 > $7F,它是一个“浅色”主题),但这对于某些主题并不总是可靠的。

是否有更好/更可靠的方法来确定这一点?

【问题讨论】:

  • 你说的是IDE插件吗?
  • 您需要考虑背景中每个颜色分量的亮度。公式可以在in here 找到。然后就可以用$7F的简单比较了。
  • Uwe - 不。不幸的是,主题名称不能满足我的需求。我认为汤姆的答案是正确的。
  • Tom - 这似乎是正确的,至少对于我测试过的主题而言。谢谢!

标签: delphi themes


【解决方案1】:

如果您正在编写 IDE 插件,您可以向 BorlandIDEServices 询问 IOTAIDEThemingServices。后者包含一个属性ActiveTheme,它为您提供当前主题名称。

【讨论】:

    【解决方案2】:

    根据 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;```
    

    【讨论】: