src/Foundation/Media/Gimela.Media.Imaging/Colors/HSL.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>
/// 
/// <remarks><para>See <a href="http://en.wikipedia.org/wiki/HSI_color_space#Conversion_from_RGB_to_HSL_or_HSV">HSL and HSV Wiki</a>
/// for information about the algorithm to convert from RGB to HSL.</para></remarks>
/// 
Convert from RGB to HSL color space.
Convert from RGB to HSL color space .
public static void FromRGB(RGB rgb, HSL hsl)
    {
      float r = (rgb.Red / 255.0f);
      float g = (rgb.Green / 255.0f);
      float b = (rgb.Blue / 255.0f);
      float min = Math.Min(Math.Min(r, g), b);
      float max = Math.Max(Math.Max(r, g), b);
      float delta = max - min;
      hsl.Luminance = (max + min) / 2;
      if (delta == 0)
      {
        hsl.Hue = 0;
        hsl.Saturation = 0.0f;
      }
      else
      {
        hsl.Saturation = (hsl.Luminance <= 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));
        float hue;
        if (r == max)
        {
          hue = ((g - b) / 6) / delta;
        }
        else if (g == max)
        {
          hue = (1.0f / 3) + ((b - r) / 6) / delta;
        }
        else
        {
          hue = (2.0f / 3) + ((r - g) / 6) / delta;
        }
        if (hue < 0)
          hue += 1;
        if (hue > 1)
          hue -= 1;
        hsl.Hue = (int)(hue * 360);
      }
    }
public static void FromRGB ( RGB rgb , HSL hsl ) { float r = ( rgb . Red / 255.0f ) ; float g = ( rgb . Green / 255.0f ) ; float b = ( rgb . Blue / 255.0f ) ; float min = Math . Min ( Math . Min ( r , g ) , b ) ; float max = Math . Max ( Math . Max ( r , g ) , b ) ; float delta = max - min ; hsl . Luminance = ( max + min ) / 2 ; if ( delta == 0 ) { hsl . Hue = 0 ; hsl . Saturation = 0.0f ; } else { hsl . Saturation = ( hsl . Luminance <= 0.5 ) ? ( delta / ( max + min ) ) : ( delta / ( 2 - max - min ) ) ; float hue ; if ( r == max ) { hue = ( ( g - b ) / 6 ) / delta ; } else if ( g == max ) { hue = ( 1.0f / 3 ) + ( ( b - r ) / 6 ) / delta ; } else { hue = ( 2.0f / 3 ) + ( ( r - g ) / 6 ) / delta ; } if ( hue < 0 ) hue += 1 ; if ( hue > 1 ) hue -= 1 ; hsl . Hue = ( int ) ( hue * 360 ) ; } }
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
remarks
See HSL and HSV Wiki
See HSL and HSV Wiki