using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace ImageCreator
{
    public partial class Form1 : Form
    {
        ImageList list = new ImageList();

        public Form1()
        {
            InitializeComponent();

        }

        private void generateButtonsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //original Buttons
            for (int i = 0; i <= 20; i++)
            {
                createButton(100f / 20f * (float)i, "img"+100f / 20f * (float)i +".png" , Color.Blue, Color.Green);

                list.Images.Add(Image.FromFile("img"+100f / 20f * (float)i +".png" ));
            }

            //Pressed Buttons
            for (int i = 0; i <= 20; i++)
            {
                createButton(100f / 20f * (float)i, "img" + 100f / 20f * (float)i + "_p.png", Color.DarkBlue, Color.DarkGreen);

                list.Images.Add(Image.FromFile("img" + 100f / 20f * (float)i + "_p.png"));
            }


            //Red
            createButton(0.5f, "imgRed.png", Color.FromArgb(150, 0, 0), Color.FromArgb(120, 0,0));
            list.Images.Add( Image.FromFile("imgRed.png"));
            
            //this.listView1.LargeImageList = this.list;
            //this.listView1.RedrawItems(1, this.list.Images.Count-1, false);
            //this.listView1.Invalidate();
        }

        /// <summary>
        /// Value between 0 and 1
        /// </summary>
        /// <param name="percentValue"></param>
        public void createButton(float percentValue, string fileName, Color left, Color right)
        {
            int width = 273;
            int height = 86;
            float border = 2f;
            float gradientWidth = 0.6f;
            
            Color outerTransparent = Color.Gray;
            Color innerTransparent = Color.Black;
            Color borderColor = Color.White;

            Bitmap b = new Bitmap(width+4, height+4, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(b);
            g.SmoothingMode = SmoothingMode.None;

            Pen pen = new Pen(borderColor, border);

            this.DrawGradient(g, height + (int)(2 * border), width + (int)(2 * border), left, right, percentValue / 100f, gradientWidth);

            Image img = this.CreateTransparencyMask(pen, 2, 2, width + (int)(2 * border), height + (int)(2 * border), border, 15, outerTransparent, innerTransparent);

            g.DrawImage(img, new Rectangle(0, 0, width + (int)(2 * border), height + (int)(2 * border)));

            b.MakeTransparent(outerTransparent);

            //DrawRoundRect(g, myBrush, pen, (int)border, (int)border, 273, 86, 15);

            b.Save(fileName, ImageFormat.Png);

            b.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// Creates a gradient with specified colors, and the transient region where the 
        /// gradient will be drawn in percent of the whole 
        /// length.
        /// </summary>
        /// <param name="g">Graphics object</param>
        /// <param name="height">width of rectangle</param>
        /// <param name="hidth">heigth of rectangle</param>
        /// <param name="left">color on the left side</param>
        /// <param name="right">color on the right side</param>
        /// <param name="ValueInPercent">value in percent, where transition between color left und right happens</param>
        /// <param name="transientWidth">width in percent of total width</param>
        public void DrawGradient(Graphics g, int height, int width, Color left, Color right, float ValueInPercent, float transientWidth)
        {
            Brush brush1 = new SolidBrush(left);
            Brush brush2 = new SolidBrush(right);

            g.FillRectangle( brush1, new Rectangle(0,0, (int)(width * ValueInPercent), height));
            g.FillRectangle( brush2, new Rectangle((int)(width * ValueInPercent), 0, width, height));

            Point leftPoint = new Point((int)(width * (ValueInPercent - transientWidth / 2f)) - 1, 0);
            Point rightPoint = new Point((int)(width * (ValueInPercent + transientWidth / 2f)) + 1, 0);

            LinearGradientBrush myBrush = new LinearGradientBrush(leftPoint,rightPoint , left, right);

            Rectangle rect = new Rectangle((int)(width * ( (ValueInPercent - transientWidth/2f))), 0, (int)(width * transientWidth), height);
            
            g.FillRectangle(myBrush,rect );
        }

        public Image CreateTransparencyMask(Pen p, int x, int y, int width, int height, float borderThikness, float radius, Color transparentColor, Color innerTransColor)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics g2 = Graphics.FromImage(bmp);
            g2.SmoothingMode = SmoothingMode.None;

            g2.FillRectangle(new SolidBrush(transparentColor), new Rectangle(0, 0, width, height));

            this.DrawRoundRect(g2, new SolidBrush(innerTransColor), p, x, y, width - (int)(2 * borderThikness), height - (int)(2 * borderThikness), radius);

            bmp.MakeTransparent(innerTransColor);
            
            g2.Dispose();
            
            return bmp;
        }

        public void DrawRoundRect(Graphics g, Brush brush, Pen p, float x, float y, float width, float height, float radius)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
            gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
            gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
            gp.CloseFigure();

            if (brush == null)
                g.DrawPath(p, gp);
            else
                g.FillPath(brush, gp);

            g.DrawPath(p, gp);

            gp.Dispose();
        }

    }
}
