using System.Drawing;

namespace ZionGraphics.Filtering
{
    class ColorIntensity
    {
        public int max = 255;
        public int min = 0;
    }
    class Warmth : IFilter
    {
        public override Bitmap Apply(Bitmap bitmap)
        {
            AForge.Imaging.ImageStatistics histogram = new AForge.Imaging.ImageStatistics(bitmap);

            ColorIntensity red = new ColorIntensity();
            ColorIntensity blue = new ColorIntensity();
            ColorIntensity green = new ColorIntensity();

            red.min = histogram.Red.Min;
            red.max = histogram.Red.Max;

            blue.min = histogram.Blue.Min;
            blue.max = histogram.Blue.Max;

            green.min = histogram.Green.Min;
            green.max = histogram.Green.Max;


            if (Intensity < 0)
            {
                blue.min += Intensity;
                blue.max += Intensity;

                if (blue.min < 0)
                    blue.min = 0;
                if (blue.max > 255)
                    blue.max = 255;

                red.min -= Intensity;
                red.max -= Intensity;

                if (red.min < 0)
                    red.min = 0;
                if (red.max < 0)
                    red.max = 0;
            }
            else if (Intensity > 0)
            {
                red.min -= Intensity;
                red.max -= Intensity;

                if (red.min < 0)
                    red.min = 0;
                if (red.max > 255)
                    red.max = 255;

                blue.min += Intensity;
                blue.max += Intensity;

                if (blue.min < 0)
                    blue.min = 0;
                if (blue.max < 0)
                    blue.max = 0;
            }

            AForge.Imaging.Filters.LevelsLinear filter = new AForge.Imaging.Filters.LevelsLinear();
            filter.InBlue = new AForge.IntRange(blue.min, blue.max);
            filter.InGreen = new AForge.IntRange(green.min, green.max);
            filter.InRed = new AForge.IntRange(red.min, red.max);

            filter.ApplyInPlace(bitmap);
            return bitmap;
        }
    }
}
