첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
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
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요