Search code examples
c#winformsdevexpressdevexpress-windows-ui

How to get the ForeColor associated with the BackColor of the skin?


When you set a SimpleButton.Appearance.BackColor property to a DX Skin color (@Danger, @Question, @Success, @Primary or @Info as shown in the image):

Color selector

The SimpleButton.Appearance.ForeColor property is evaluated at run-time with the appropriate color (if the skin color is too dark, Color.White would be used; if the skin color is too bright, Color.Black would be used):

Dark @Success:

Dark

Light @Success:

Light

How do I get this ForeColor that is evaluated in run-time?

I have tried getting SimpleButton.ForeColor, SimpleButton.Appearance.ForeColor and SimpleButton.Appearance.GetForeColor(e.GraphicsCache) (I'm trying to get this color inside a CustomDraw event) but it's always Color.Black


Solution

  • I ended up with this extension method according to this implementation of the W3C standard, making some modifications per Miral's tests:

    public static Color GetContrastColor(this Color color)
    {
        return (color.R * 0.299M) + (color.G * 0.587M) + (color.B * 0.114M) > 149 ? 
            Color.Black : 
            Color.White;
    }