コード例 #1
0
ファイル: ColorConverter.cs プロジェクト: tchivs/AForgeCore
        /// <summary>
        /// Convert from HSL to RGB color space.
        /// </summary>
        ///
        /// <param name="hsl">Source color in <b>HSL</b> color space.</param>
        /// <param name="rgb">Destination color in <b>RGB</b> color space.</param>
        ///
        public static void ToRGB(HSL hsl, RGB rgb)
        {
            if (hsl.Saturation == 0)
            {
                // gray values
                rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255);
            }
            else
            {
                float v1, v2;
                float hue = (float)hsl.Hue / 360;

                v2 = (hsl.Luminance < 0.5) ?
                     (hsl.Luminance * (1 + hsl.Saturation)) :
                     ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation));
                v1 = 2 * hsl.Luminance - v2;

                rgb.Red   = (byte)(255 * Hue_2_RGB(v1, v2, hue + (1.0f / 3)));
                rgb.Green = (byte)(255 * Hue_2_RGB(v1, v2, hue));
                rgb.Blue  = (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0f / 3)));
            }
            rgb.Alpha = 255;
        }
コード例 #2
0
        // Gather statistics for the specified image
        private unsafe void ProcessImage(UnmanagedImage image, byte *mask, int maskLineSize)
        {
            // get image dimension
            int width  = image.Width;
            int height = image.Height;

            pixels = pixelsWithoutBlack = 0;

            int[] s   = new int[256];
            int[] l   = new int[256];
            int[] swb = new int[256];
            int[] lwb = new int[256];
            RGB   rgb = new RGB( );
            HSL   hsl = new HSL( );

            int pixelSize  = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int offset     = image.Stride - width * pixelSize;
            int maskOffset = maskLineSize - width;

            // do the job
            byte *p = (byte *)image.ImageData.ToPointer( );

            if (mask == null)
            {
                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += pixelSize)
                    {
                        rgb.Red   = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue  = p[RGB.B];

                        // convert to HSL color space
                        AForgeCore.Imaging.HSL.FromRGB(rgb, hsl);

                        s[(int)(hsl.Saturation * 255)]++;
                        l[(int)(hsl.Luminance * 255)]++;
                        pixels++;

                        if (hsl.Luminance != 0.0)
                        {
                            swb[(int)(hsl.Saturation * 255)]++;
                            lwb[(int)(hsl.Luminance * 255)]++;
                            pixelsWithoutBlack++;
                        }
                    }
                    p += offset;
                }
            }
            else
            {
                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += pixelSize, mask++)
                    {
                        if (*mask == 0)
                        {
                            continue;
                        }

                        rgb.Red   = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue  = p[RGB.B];

                        // convert to HSL color space
                        AForgeCore.Imaging.HSL.FromRGB(rgb, hsl);

                        s[(int)(hsl.Saturation * 255)]++;
                        l[(int)(hsl.Luminance * 255)]++;
                        pixels++;

                        if (hsl.Luminance != 0.0)
                        {
                            swb[(int)(hsl.Saturation * 255)]++;
                            lwb[(int)(hsl.Luminance * 255)]++;
                            pixelsWithoutBlack++;
                        }
                    }
                    p    += offset;
                    mask += maskOffset;
                }
            }

            // create histograms
            saturation = new ContinuousHistogram(s, new Range(0, 1));
            luminance  = new ContinuousHistogram(l, new Range(0, 1));

            saturationWithoutBlack = new ContinuousHistogram(swb, new Range(0, 1));
            luminanceWithoutBlack  = new ContinuousHistogram(lwb, new Range(0, 1));
        }