/// <summary> /// Initialize the color lookup module /// </summary> public static void Initialize() { using (CSVReader reader = new CSVReader(Properties.Resources.colors)) { List <KdColorNode> nodes = new List <KdColorNode>(); string line; while (reader.ReadRow(out line)) { string[] split = line.Split(','); string name = split[0].Trim(new char[] { ' ', '"' }); double red = double.Parse(split[2].Trim()); double green = double.Parse(split[3].Trim()); double blue = double.Parse(split[4].Trim()); KdColorNode node = new KdColorNode(name, new double[] { red, green, blue }); nodes.Add(node); } lookup = new KdTreeUtil(nodes.ToArray()); } initialized = true; }
/// <summary> /// Lookup a color name using the RGB(0-255) values /// </summary> /// <exception cref="LookupException"></exception> /// <param name="red">Red value (0-255)</param> /// <param name="green">Green value (0-255)</param> /// <param name="blue">Blue value (0-255)</param> /// <returns>The nearest matched color information</returns> public static ColorInformation Match(double red, double green, double blue) { if (!initialized) { throw new LookupException("Match called before initialization"); } KdColorNode match = lookup.NearestMatch(new double[] { red, green, blue }); ColorInformation information = new ColorInformation(match.colorName, (int)match.color[0], (int)match.color[1], (int)match.color[2]); return(information); }