protected unsafe override bool ApplyFilter()
        {
            int pixelSize = Image.GetPixelFormatSize(this.Bitmap.PixelFormat) / 8;
            byte* pointer = (byte*)this.Scan.ToPointer();

            for (int y = 0; y < this.Bitmap.Height; ++y)
            {
                for (int x = 0; x < this.Bitmap.Width; ++x)
                {
                    var rgb = new Rgb
                    {
                        Red = pointer[Rgb.RedPixel],
                        Green = pointer[Rgb.GreenPixel],
                        Blue = pointer[Rgb.BluePixel]
                    };

                    var hsl = Hsl.FromRgb(rgb);
                    hsl.Hue = this.hue;
                    rgb = hsl.ToRgb();

                    pointer[Rgb.RedPixel] = rgb.Red;
                    pointer[Rgb.GreenPixel] = rgb.Green;
                    pointer[Rgb.BluePixel] = rgb.Blue;
                    pointer += pixelSize;
                }
                pointer += this.Offset;
            }
            return true;
        }
Exemple #2
0
        public static Hsl FromRgb(Rgb rgb)
        {
            const float MaxDivisor = 255.0f;

            var hsl = new Hsl();

            float red = rgb.Red / MaxDivisor;
            float green = rgb.Green / MaxDivisor;
            float blue = rgb.Blue / MaxDivisor;

            float min = Math.Min(Math.Min(red, green), blue);
            float max = Math.Max(Math.Max(red, green), blue);
            float diff = max - min;

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

            if (!diff.Equals(0))
            {
                hsl.Saturation = (hsl.Luminance <= 0.5) ? (diff / (max + min)) : (diff / (2 - max - min));

                float hue;

                if (red.Equals(max))
                {
                    hue = ((green - blue) / 6) / diff;
                }
                else if (green.Equals(max))
                {
                    hue = (1.0f / 3) + ((blue - red) / 6) / diff;
                }
                else
                {
                    hue = (2.0f / 3) + ((red - green) / 6) / diff;
                }

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

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

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

            return hsl;
        }
Exemple #3
0
        public static Hsl FromRgb(Rgb rgb)
        {
            const float MaxDivisor = 255.0f;

            var hsl = new Hsl();

            float red   = rgb.Red / MaxDivisor;
            float green = rgb.Green / MaxDivisor;
            float blue  = rgb.Blue / MaxDivisor;

            float min  = Math.Min(Math.Min(red, green), blue);
            float max  = Math.Max(Math.Max(red, green), blue);
            float diff = max - min;

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

            if (!diff.Equals(0))
            {
                hsl.Saturation = (hsl.Luminance <= 0.5) ? (diff / (max + min)) : (diff / (2 - max - min));

                float hue;

                if (red.Equals(max))
                {
                    hue = ((green - blue) / 6) / diff;
                }
                else if (green.Equals(max))
                {
                    hue = (1.0f / 3) + ((blue - red) / 6) / diff;
                }
                else
                {
                    hue = (2.0f / 3) + ((red - green) / 6) / diff;
                }

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

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

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

            return(hsl);
        }
Exemple #4
0
        public static Rgb ToRgb(Hsl hsl)
        {
            var rgb = new Rgb();

            if (hsl.Saturation == 0)
            {
                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 * HueToRgb(v1, v2, hue + (1.0f / 3)));
                rgb.Green = (byte)(255 * HueToRgb(v1, v2, hue));
                rgb.Blue = (byte)(255 * HueToRgb(v1, v2, hue - (1.0f / 3)));
                rgb.Alpha = 255;
            }

            return rgb;
        }
 public IFilter Gamma(Rgb rgb)
 {
     return new GammaFilter(rgb.Red, rgb.Green, rgb.Blue);
 }
 public IFilter RemappingColor(Rgb rgb)
 {
     return new RemapingColorFilter(rgb.Red, rgb.Green, rgb.Blue);
 }