CSharp examples for System.Drawing:Color RGB
Get the closest ConsoleColor to the RGB values
using System.Threading.Tasks; using System.Text; using System.Linq; using System.Collections.Generic; using System;/* w w w. j ava 2 s . c o m*/ public class Main{ /// <summary> /// Get the closest ConsoleColor to the RGB values /// </summary> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public static ConsoleColor ClosestConsoleColor(byte r, byte g, byte b) { ConsoleColor ret = 0; double rr = r, gg = g, bb = b, delta = double.MaxValue; foreach (ConsoleColor cc in Enum.GetValues(typeof(ConsoleColor))) { var n = Enum.GetName(typeof(ConsoleColor), cc); var c = System.Drawing.Color.FromName(n == "DarkYellow" ? "Orange" : n); // bug fix var t = Math.Pow(c.R - rr, 2.0) + Math.Pow(c.G - gg, 2.0) + Math.Pow(c.B - bb, 2.0); if (t == 0.0) return cc; if (t < delta) { delta = t; ret = cc; } } return ret; } }