MapSuiteGisEditor/GisEditorPluginCore/Shares/UIs/Controls/Utils/ColorConverter.cs
/// <summary>
/// Convert from RGB to HSL color space.
/// </summary>
/// 
/// <param name="rgb">Source color in <b>RGB</b> color space.</param>
/// <param name="hsl">Destination color in <b>HSL</b> color space.</param>
/// 
Convert from RGB to HSL color space.
Convert from RGB to HSL color space .
public static void RGB2HSL(RGB rgb, HSL hsl)
        {
            double r = (rgb.Red / 255.0);
            double g = (rgb.Green / 255.0);
            double b = (rgb.Blue / 255.0);
            double min = Math.Min(Math.Min(r, g), b);
            double max = Math.Max(Math.Max(r, g), b);
            double delta = max - min;
            hsl.Luminance = (max + min) / 2;
            if (delta == 0)
            {
                hsl.Hue = 0;
                hsl.Saturation = 0.0;
            }
            else
            {
                hsl.Saturation = (hsl.Luminance < 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));
                double del_r = (((max - r) / 6) + (delta / 2)) / delta;
                double del_g = (((max - g) / 6) + (delta / 2)) / delta;
                double del_b = (((max - b) / 6) + (delta / 2)) / delta;
                double hue;
                if (r == max)
                    hue = del_b - del_g;
                else if (g == max)
                    hue = (1.0 / 3) + del_r - del_b;
                else
                    hue = (2.0 / 3) + del_g - del_r;
                if (hue < 0)
                    hue += 1;
                if (hue > 1)
                    hue -= 1;
                hsl.Hue = (int)(Math.Ceiling(hue * 100) * 3.6);
            }
        }
public static void RGB2HSL ( RGB rgb , HSL hsl ) { double r = ( rgb . Red / 255.0 ) ; double g = ( rgb . Green / 255.0 ) ; double b = ( rgb . Blue / 255.0 ) ; double min = Math . Min ( Math . Min ( r , g ) , b ) ; double max = Math . Max ( Math . Max ( r , g ) , b ) ; double delta = max - min ; hsl . Luminance = ( max + min ) / 2 ; if ( delta == 0 ) { hsl . Hue = 0 ; hsl . Saturation = 0.0 ; } else { hsl . Saturation = ( hsl . Luminance < 0.5 ) ? ( delta / ( max + min ) ) : ( delta / ( 2 - max - min ) ) ; double del_r = ( ( ( max - r ) / 6 ) + ( delta / 2 ) ) / delta ; double del_g = ( ( ( max - g ) / 6 ) + ( delta / 2 ) ) / delta ; double del_b = ( ( ( max - b ) / 6 ) + ( delta / 2 ) ) / delta ; double hue ; if ( r == max ) hue = del_b - del_g ; else if ( g == max ) hue = ( 1.0 / 3 ) + del_r - del_b ; else hue = ( 2.0 / 3 ) + del_g - del_r ; if ( hue < 0 ) hue += 1 ; if ( hue > 1 ) hue -= 1 ; hsl . Hue = ( int ) ( Math . Ceiling ( hue * 100 ) * 3.6 ) ; } }
Convert from RGB to HSL color space.
Convert from RGB to HSL color space .
// get luminance value // gray color // get saturation value // get hue value // correct hue if needed
rgb
RGB
Source color in RGB
Source color in RGB
hsl
HSL
Destination color in HSL
Destination color in HSL