AForge.Imaging.HSL.FromRGB C# (CSharp) Method

FromRGB() public static method

Convert from RGB to HSL color space.

See HSL and HSV Wiki for information about the algorithm to convert from RGB to HSL.

public static FromRGB ( RGB rgb, HSL hsl ) : void
rgb RGB Source color in RGB color space.
hsl HSL Destination color in HSL color space.
return void
        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;

            // get luminance value
            hsl.Luminance = ( max + min ) / 2;

            if ( delta == 0 )
            {
                // gray color
                hsl.Hue = 0;
                hsl.Saturation = 0.0f;
            }
            else
            {
                // get saturation value
                hsl.Saturation = ( hsl.Luminance <= 0.5 ) ? ( delta / ( max + min ) ) : ( delta / ( 2 - max - min ) );

                // get hue value
                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;
                }

                // correct hue if needed
                if ( hue < 0 )
                    hue += 1;
                if ( hue > 1 )
                    hue -= 1;

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

Same methods

HSL::FromRGB ( RGB rgb ) : HSL

Usage Example

Beispiel #1
0
        private unsafe void ProcessImage(UnmanagedImage image)
        {
            this.CheckSourceFormat(image.PixelFormat);
            int width  = image.Width;
            int height = image.Height;

            this.pixels = this.pixelsWithoutBlack = 0;
            int[] numArray  = new int[0x100];
            int[] numArray2 = new int[0x100];
            int[] numArray3 = new int[0x100];
            int[] numArray4 = new int[0x100];
            RGB   rgb       = new RGB();
            HSL   hsl       = new HSL();
            int   num3      = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   num4      = image.Stride - (width * num3);
            byte *numPtr    = (byte *)image.ImageData.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num6 = 0;
                while (num6 < width)
                {
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    HSL.FromRGB(rgb, hsl);
                    numArray[(int)(hsl.Saturation * 255.0)]++;
                    numArray2[(int)(hsl.Luminance * 255.0)]++;
                    this.pixels++;
                    if (hsl.Luminance != 0.0)
                    {
                        numArray3[(int)(hsl.Saturation * 255.0)]++;
                        numArray4[(int)(hsl.Luminance * 255.0)]++;
                        this.pixelsWithoutBlack++;
                    }
                    num6++;
                    numPtr += num3;
                }
                numPtr += num4;
            }
            this.saturation             = new ContinuousHistogram(numArray, new DoubleRange(0.0, 1.0));
            this.luminance              = new ContinuousHistogram(numArray2, new DoubleRange(0.0, 1.0));
            this.saturationWithoutBlack = new ContinuousHistogram(numArray3, new DoubleRange(0.0, 1.0));
            this.luminanceWithoutBlack  = new ContinuousHistogram(numArray4, new DoubleRange(0.0, 1.0));
        }
All Usage Examples Of AForge.Imaging.HSL::FromRGB