Search code examples
c#system.drawing

How to differentiate between "Black" and "ActiveCaptionText"


void Main()
{
    (Color.FromName("Black").ToArgb() == Color.FromName("ActiveCaptionText").ToArgb()).Dump();
    //                           TRUE ^
}

This is a piece of code runned in LINQPad. Is there a way to differentiate between these two colors ?

Context: I receive the colors in their int representation. For example Black is -16777216. I use this enum from System.Drawing namespace called KnownColor to get the name of it.


Solution

  • If you want to get the name of a given color value, then use the ColorTranslator class which uses an internal known color table to set the Name property of the Color struct.

    Example

    Console.WriteLine(Color.FromArgb(-16777216).Name);
    
    // Prints: ff000000
    // Whereas...
    
    Console.WriteLine(ColorTranslator.FromWin32(-16777216).Name);
    
    // Prints: Black