첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;

#region HSL 구하기 - GetHSL(red, green, blue)

/// <summary>
/// HSL 구하기
/// </summary>
/// <param name="red">빨강색</param>
/// <param name="green">녹색</param>
/// <param name="blue">파랑색</param>
/// <returns>HSL</returns>
public HSL GetHSL(double red, double green, double blue)
{
    double minimum = Math.Min(red, Math.Min(green, blue));
    double maximum = Math.Max(red, Math.Max(green, blue));
    double delta   = maximum - minimum;

    HSL hsl = new HSL(0, 0d, 0d);

    hsl.Luminance = (maximum + minimum) / 2d;

    if(delta == 0)
    {
        hsl.Hue        = 0;
        hsl.Saturation = 0d;
    }
    else
    {
        if(hsl.Luminance < 0.5d)
        {
            hsl.Saturation = delta / (maximum + minimum);
        }
        else
        {
            hsl.Saturation = delta / (2d - maximum - minimum);
        }

        double deltaRed   = (((maximum - red  ) / 6d) + (maximum / 2d)) / maximum;
        double deltaGreen = (((maximum - green) / 6d) + (maximum / 2d)) / maximum;
        double deltaBlue  = (((maximum - blue ) / 6d) + (maximum / 2d)) / maximum;

        if(red   == maximum)
        {
            hsl.Hue = (int)((deltaBlue - deltaGreen) * 360d);
        }
        else if(green == maximum)
        {
            hsl.Hue = (int)(((1d / 3d) + deltaRed   - deltaBlue) * 360d);
        }
        else if(blue  == maximum)
        {
            hsl.Hue = (int)(((2d / 3d) + deltaGreen - deltaRed) * 360d);
        }

        if(hsl.Hue < 0)
        {
            hsl.Hue += 360;
        }

        if(hsl.Hue > 360)
        {
            hsl.Hue -= 360;
        }
    }

    return hsl;
}

#endregion

※ HSL 구조체 : 'HSL 색상 구조체' 참조

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요