I got a list of Knowncolor from the system but I want to remove some which are too dark and make the foreground character unseen. I tried the following code but KnownColor.Black still shows up. Is there anyway to order them by their darkness?
if (knownColor > KnownColor.Transparent && knownColor < KnownColor.MidnightBlue && knownColor < KnownColor.Navy)
{
//add it to our list
colors.Add(knownColor);
}
You can convert the known colors to a Color instance and then compare brightness using the GetBrightness()
method:
Gets the hue-saturation-brightness (HSB) brightness value for this Color structure. The brightness ranges from 0.0 through Blockquote 1.0, where 0.0 represents black and 1.0 represents white.
float brightness = Color.FromKnownColor(KnownColor.Transparent).GetBrightness();
Applied to your example, something like the following should work (tested for black and yellow):
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With