■ 가장 가까운 색상을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System.Drawing; #region 가장 가까운 색상 구하기 - GetNearestColor(colorArray, red, green, blue) /// <summary> /// 가장 가까운 색상 구하기 /// </summary> /// <param name="colorArray">색상 배열</param> /// <param name="red">빨강색</param> /// <param name="green">녹색</param> /// <param name="blue">파랑색</param> /// <returns>인덱스</returns> public int GetNearestColor(Color[] colorArray, byte red, byte green, byte blue) { int distanceSquared; int minimumDistanceSquared = 195076; // 255 * 255 + 255 * 255 + 255 * 255 + 1 int bestIndex = 0; int redDifference; int greenDifference; int blueDifference; for(int i = 0; i < colorArray.Length; i++) { redDifference = red - colorArray[i].R; greenDifference = green - colorArray[i].G; blueDifference = blue - colorArray[i].B; distanceSquared = redDifference * redDifference + greenDifference * greenDifference + blueDifference * blueDifference; if(distanceSquared < minimumDistanceSquared) { minimumDistanceSquared = distanceSquared; bestIndex = i; } } return bestIndex; } #endregion |