Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# remove darker colors from KnownColor

Tags:

c#

colors

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);
            }
like image 479
Yang Avatar asked Dec 28 '22 01:12

Yang


1 Answers

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);
}
like image 138
BrokenGlass Avatar answered Jan 08 '23 04:01

BrokenGlass