C # remove dark colors from KnownColor

I have a Knowncolor list from the system, but I want to remove some that are too dark and make an invisible foreground character. I tried the following code, but KnownColor.Black is still displayed. Is there any way to order them in my own darkness?

if (knownColor > KnownColor.Transparent && knownColor < KnownColor.MidnightBlue && knownColor < KnownColor.Navy) { //add it to our list colors.Add(knownColor); } 
+4
source share
2 answers

You can convert known colors to an instance of Color and then compare the brightness using the GetBrightness() method:

Gets the hue saturation brightness (HSB) for this color composition. Brightness ranges from 0.0 to Blockquote 1.0, where 0.0 is black and 1.0 is white.

 float brightness = Color.FromKnownColor(KnownColor.Transparent).GetBrightness(); 

For your example, something like the following should work (checked 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); } 
+7
source

Take a look at my answer for determining foreground color - it involves calculating the perceived brightness of the background color to decide whether to display white or black as the foreground. You can use the same method and just choose to make the colors too dark:

Make foreground color black or white depending on the background

+3
source

Source: https://habr.com/ru/post/1338177/


All Articles