using System.Text.RegularExpressions;
using System.Collections.Generic;
public interface IColorData
public class ColorDataFirstTable : IColorData {
public string name {get; set;}
public string simpleName {get; set;}
public string hex {get; set;}
public ColorDataFirstTable(string name, string hex) {
public class ColorDataSecondTable : IColorData {
public string name {get; set;}
public string hex {get; set;}
public ColorDataSecondTable(string hex) {
List<ColorDataFirstTable> etalonColors = new List<ColorDataFirstTable>() {new ColorDataFirstTable("first color", "#345665"), new ColorDataFirstTable("second color", "#752233")};
List<ColorDataFirstTable> closestColors = matchAllColors<ColorDataFirstTable>("#345666", etalonColors, 0);
foreach (ColorDataFirstTable c in closestColors) {
Console.WriteLine("name");
Console.WriteLine(c.name);
Console.WriteLine("hex");
Console.WriteLine(c.hex);
public List<T> matchAllColors<T> (
if (etalonColors.ToArray().Length < 1)
throw new ArgumentException("More than one etalon colors required");
double[] colorRgb = this.HEXToRGB(colorHex);
double[] firstEtalonColorRgb = HEXToRGB(etalonColors[0].hex);
bool hasDistanceLimit = Convert.ToBoolean(distanceLimit);
double minDist = hasDistanceLimit
: this.getDistance(colorRgb, firstEtalonColorRgb);
List<T> closestColors = hasDistanceLimit ? new List<T>() : new List<T>{etalonColors[0]};
foreach (T c in etalonColors) {
double[] etalonColorRgb = HEXToRGB(c.hex);
double currentDistance = getDistance(colorRgb, etalonColorRgb);
if (currentDistance < minDist) {
minDist = currentDistance;
closestColors = new List<T> {c};
} else if (currentDistance == minDist) {
bool isUniq = closestColors.IndexOf(c) == -1;
public double getDistance(
double dist = Math.Sqrt(Math.Pow(Math.Abs(c1[0] - c2[0]), 2) + Math.Pow(Math.Abs(c1[1] - c2[1]), 2) + Math.Pow(Math.Abs(c1[2] - c2[2]), 2));
public double[] HEXToRGB(string hex) {
Regex reg = new Regex(@"^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$", RegexOptions.IgnoreCase);
Match m = reg.Match(hex);
double[] rgb = new double[3];
rgb[0] = Convert.ToInt32(Convert.ToString(m.Groups[1]), 16) / 255.0;
rgb[1] = Convert.ToInt32(Convert.ToString(m.Groups[2]), 16) / 255.0;
rgb[2] = Convert.ToInt32(Convert.ToString(m.Groups[3]), 16) / 255.0;