GetContrastingColor

        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();

Description

Gets a contrasting color based on the current color

Details

  • Author: Zack Wagner
  • Submitted on: 4/3/2015 4:28:27 PM
  • Language: C#
  • Type: Color
  • Views: 982

Double click on the code to select all.

 

;