public void findColor(int R, int G, int B, int formX, int formY) { string region; string colorName; // Determine the color region among ROYGBIV + White and Black // Evenly divided into 8 smaller cubes. if (R <= 128) { // Lower half of the color cube. if (G <= 128) { if (B <= 128) { region = "Black region"; } else { region = "Blue region"; } } else { if (B <= 128) { region = "Green region"; } else { region = "Cyan region"; } } } else { // Higher half of the color cube. if (G <= 128) { if (B <= 128) { region = "Red region"; } else { region = "Magenta region"; } } else { if (B <= 128) { region = "Yellow region"; } else { region = "White region"; } } } Color original = Color.FromArgb(R, G, B); // Calculate the distance from the given color to every other color in the table. double minDistance = 1; double cDistance; int index = 0; for (int i = 0; i < colorTable.Length; i++) { cDistance = Math.Sqrt(Math.Pow((R - colorTable[i].red), 2) + Math.Pow((G - colorTable[i].green), 2) + Math.Pow((B - colorTable[i].blue), 2)); if (0.0 == cDistance) { // Color found! The two points match! colorName = colorTable[i].colorName; Form colorFormExact = new color(true, original, original, region, colorName); colorFormExact.Load += delegate { colorFormExact.Location = new Point(formX, formY); }; colorFormExact.Show(); return; } if (0 == i) { // Initialize minDistance minDistance = cDistance; } if (cDistance < minDistance) { // New minimum. minDistance = cDistance; index = i; } } // Found closest, most similar color. Color similar = Color.FromArgb(colorTable[index].red, colorTable[index].green, colorTable[index].blue); colorName = colorTable[index].colorName; Form colorFormSimilar = new color(false, original, similar, region, colorName); colorFormSimilar.Load += delegate { colorFormSimilar.Location = new Point(formX, formY); }; colorFormSimilar.Show(); }