System.Drawing.Color.GetBrightness C# (CSharp) Method

GetBrightness() public method

public GetBrightness ( ) : float
return float
        public float GetBrightness()
        {
            float r = R / 255.0f;
            float g = G / 255.0f;
            float b = B / 255.0f;

            float max, min;

            max = r; min = r;

            if (g > max) max = g;
            if (b > max) max = b;

            if (g < min) min = g;
            if (b < min) min = b;

            return (max + min) / 2;
        }

Usage Example

		private static String FormatColor(String format, Color arg)
		{
			if (String.IsNullOrWhiteSpace(format))
			{
				return arg.ToString();
			}

			var numberFormatInfo =
				new NumberFormatInfo
				{
					NumberDecimalDigits = 1,
					PercentDecimalDigits = 0,
					PercentNegativePattern = 1,
					PercentPositivePattern = 1
				};
			switch (format)
			{
				case "hex":
					{
						return String.Format(numberFormatInfo, "#{0:x2}{1:x2}{2:x2}", arg.R, arg.G, arg.B);
					}
				case "HEX":
					{
						return String.Format(numberFormatInfo, "#{0:X2}{1:X2}{2:X2}", arg.R, arg.G, arg.B);
					}
				case "rgb":
					{
						return String.Format(numberFormatInfo, "rgb({0}, {1}, {2})", arg.R, arg.G, arg.B);
					}
				case "rgb%":
					{
						return String.Format(numberFormatInfo, "rgb({0:P}, {1:P}, {2:P})", arg.R / 255d, arg.G / 255d, arg.B / 255d);
					}
				case "rgba":
					{
						return String.Format(numberFormatInfo, "rgba({0}, {1}, {2}, {3:0.#})", arg.R, arg.G, arg.B, arg.A / 255d);
					}
				case "rgba%":
					{
						return String.Format(numberFormatInfo, "rgba({0:P}, {1:P}, {2:P}, {3:0.#})", arg.R / 255d, arg.G / 255d, arg.B / 255d, arg.A / 255d);
					}
				case "hsl":
					{
						return String.Format(numberFormatInfo, "hsl({0:F0}, {1:P}, {2:P})", arg.GetHue(), arg.GetSaturation(), arg.GetBrightness());
					}
				case "hsla":
					{
						return String.Format(numberFormatInfo, "hsla({0:F0}, {1:P}, {2:P}, {3:0.#})", arg.GetHue(), arg.GetSaturation(), arg.GetBrightness(), arg.A / 255d);
					}
				default:
					{
						throw new FormatException(String.Format("Invalid format specified: \"{0}\".", format));
					}
			}
		}
All Usage Examples Of System.Drawing.Color::GetBrightness