ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

GetContrastingColor

Gets a contrasting color based on the current color

Source

public static Color GetContrastingColor(this Color value)
{
    var d = 0;

    // Counting the perceptive luminance - human eye favors green color... 
    double a = 1 - (0.299 * value.R + 0.587 * value.G + 0.114 * value.B) / 255;

    if (a < 0.5)
        d = 0; // bright colors - black font
    else
        d = 255; // dark colors - white font

    return Color.FromArgb(d, d, d);
}

Example

var blue = System.Drawing.Color.Orange.GetContrastingColor();

Author: Zack Wagner

Submitted on: 3 apr 2015

Language: C#

Type: Color

Views: 3441