我从系统中得到了一个 knowncolor 列表,但我想删除一些太暗并使前景字符不可见的列表。我尝试了以下代码,但KnownColor.Black 仍然出现。有没有办法通过他们的黑暗来命令他们?
if (knownColor > KnownColor.Transparent && knownColor < KnownColor.MidnightBlue && knownColor < KnownColor.Navy)
{
//add it to our list
colors.Add(knownColor);
}
最佳答案
您可以将已知颜色转换为 Color 实例,然后使用 GetBrightness()
方法比较亮度:
float brightness = Color.FromKnownColor(KnownColor.Transparent).GetBrightness();
应用于您的示例,如下所示应该可以工作(测试黑色和黄色):
KnownColor knownColor = KnownColor.Yellow;
float transparentBrightness = Color.FromKnownColor(KnownColor.Transparent).GetBrightness();
float midnightBlueBrightness = Color.FromKnownColor(KnownColor.MidnightBlue).GetBrightness();
float navyBrightness = Color.FromKnownColor(KnownColor.Navy).GetBrightness();
float knownColorBrightness = Color.FromKnownColor(knownColor).GetBrightness();
if (knownColorBrightness < transparentBrightness
&& knownColorBrightness > midnightBlueBrightness
&& knownColorBrightness > navyBrightness)
{
//add it to our list
colors.Add(knownColor);
}
关于C# 从KnownColor 中移除较深的颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4881235/