下是一個基于歐幾里得距離的C#顏色相似度查找算法實現,包含詳細注釋和優化策略:
using System;
using System.Collections.Generic;public class ColorMatcher
{// 顏色容器 - 使用字典存儲顏色ID到RGB的映射private readonly Dictionary<int, byte[]> _colorDictionary = new Dictionary<int, byte[]>{{1, new byte[] {255, 0, 0}},{2, new byte[] {255, 255, 0}},// ...此處省略其他顏色數據,實際使用時需完整填充};// 權重配置(可根據需要調整)private readonly double[] _channelWeights = new double[] {1.0, 2.0, 1.0}; // R:1, G:2, B:1(亮度優先)/// <summary>/// 查找與目標顏色最相似的顏色ID/// </summary>/// <param name="targetColor">目標顏色RGB數組</param>/// <returns>最相似顏色ID</returns>public int FindClosestColor(byte[] targetColor){if (targetColor == null || targetColor.Length != 3)throw new ArgumentException("Invalid color array");double minDistance = double.MaxValue;int closestColorId = -1;foreach (var kvp