/// <summary> /// Pulls the color from the known catalog based on the "selection" text. /// First looks for reference name. If not found, tries with RGB and closest with different combinaitions. /// Returns null if no match found. /// </summary> /// <param name="selection"></param> /// <returns></returns> public static PantoneColor FindColor(string selection) { // Tries to pull from catalog using reference name var res = PantoneCatalog.Where(u => u.Name.Replace(" ", "") == selection.Replace(" ", "").ToUpper()).FirstOrDefault(); if (res != null) { return(res.DeepCopy()); } // If not found parse text as RGB input RGB rgb = null; CMYK cmyk = null; var input = selection.Split(','); // with comma separator if (input.Count() >= 4) { try { // Create CYMK cmyk = new CMYK((byte)Int32.Parse(input[0]), (byte)(Int32.Parse(input[1])), (byte)(Int32.Parse(input[2])), (byte)(Int32.Parse(input[3]))); } catch { } } if (input.Count() == 3) { try { // Create RGB rgb = new RGB((byte)(Int32.Parse(input[0])), (byte)(Int32.Parse(input[1])), (byte)(Int32.Parse(input[2]))); } catch { } } if (cmyk == null) { try { input = selection.Split(' '); // with space separator if (input.Count() >= 4) { // Create CYMK cmyk = new CMYK((byte)Int32.Parse(input[0]), (byte)(Int32.Parse(input[1])), (byte)(Int32.Parse(input[2])), (byte)(Int32.Parse(input[3]))); } } catch { } } if (rgb == null) { try { input = selection.Split(' '); // with space separator if (input.Count() == 3) { // Create RGB rgb = new RGB((byte)(Int32.Parse(input[0])), (byte)(Int32.Parse(input[1])), (byte)(Int32.Parse(input[2]))); } } catch { } } // Try to pull the closest if (cmyk != null) { res = FindClosestColorByCMYK(cmyk); if (res != null) { return(res.DeepCopy()); } } else if (rgb != null) { res = FindClosestColorByRGB(rgb); if (res != null) { return(res.DeepCopy()); } } // Nothing worked, return null return(null); }
private static PantoneColor FindClosestColorByCMYK(CMYK cmyk) { var res = PantoneCatalog.OrderBy(u => u.CMYKdistance(cmyk)).ToList().FirstOrDefault(); return(res); }