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

#region RGB를 HSL로 변환하기 - RGB2HSL(rgb, hsl)

/// <summary>
/// RGB를 HSL로 변환하기
/// </summary>
/// <param name="rgb">RGB</param>
/// <param name="hsl">HSL</param>
public void RGB2HSL(RGB rgb, HSL hsl)
{
    double red   = (rgb.Red   / 255d);
    double green = (rgb.Green / 255d);
    double blue  = (rgb.Blue  / 255d);

    double minimum = Math.Min(Math.Min(red, green), blue);
    double maximum = Math.Max(Math.Max(red, green), blue);
    double delta   = maximum - minimum;

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

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

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

        if(red == maximum)
        {
            hue = deltaBlue - deltaGreen;
        }
        else if(green == maximum)
        {
            hue = (1d / 3d) + deltaRed - deltaBlue;
        }
        else
        {
            hue = (2d / 3d) + deltaGreen - deltaRed;
        }

        if(hue < 0d)
        {
            hue += 1d;
        }

        if(hue > 1d)
        {
            hue -= 1d;
        }

        hsl.Hue = (int)(hue * 360d);
    }
}

#endregion

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

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

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

댓글을 달아 주세요