using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class ColorPicker : System.Web.UI.UserControl, IPostBackEventHandler
{
    protected override void Render(HtmlTextWriter writer)
    {
        drawTable();
        base.Render(writer);
    }

    private void drawTable()
    {
        tblShow.Width = (int)csBoardSize;
        tblPicker.Width = (int)csBoardSize;
        int liIncrement = ((int)csBoardSize / (int)crFineness);

        cellPal1.Attributes.Add("onClick", Page.ClientScript.GetPostBackEventReference(this, "pal1"));
        cellPal2.Attributes.Add("onClick", Page.ClientScript.GetPostBackEventReference(this, "pal2"));
        cellPal3.Attributes.Add("onClick", Page.ClientScript.GetPostBackEventReference(this, "pal3"));
        cellPal4.Attributes.Add("onClick", Page.ClientScript.GetPostBackEventReference(this, "pal4"));
        cellPal5.Attributes.Add("onClick", Page.ClientScript.GetPostBackEventReference(this, "pal5"));

        string tmpCol;

        for (int x = 0; x <= (int)csBoardSize; x += liIncrement)
        {
            TableRow tr = new TableRow();
            for (int y = 0; y <= (int)csBoardSize; y += liIncrement)
            {
                tmpCol = getColor(x, y);
                TableCell tc = new TableCell();
                tc.Attributes.Add("bgcolor", tmpCol);
                tc.Attributes.Add("width", liIncrement.ToString());
                tc.Attributes.Add("height", liIncrement.ToString());
                tc.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, tmpCol));
                tr.Cells.Add(tc);
            }
            tblPicker.Rows.Add(tr);
        }
    }

    public enum Size
    {
        Large = 256,
        Medium = 128,
        Small = 64
    }

    public enum Resolution
    {
        Fine = 32,
        Medium = 16,
        Coarse = 4
    }

    private Resolution crFineness = Resolution.Medium;

    public Resolution Fineness
    {
        get { return crFineness; }
        set { crFineness = value; }
    }

    private Size csBoardSize = Size.Medium;

    public Size BoardSize
    {
        get { return csBoardSize; }
        set { csBoardSize = value; }
    }

    private string Palette
    {
        get { return (string)ViewState["Palette"]; }
        set { ViewState["Palette"] = value; }
    }

    public Color SelectedColor
    {
        get
        {
            try
            {
                return (Color)ViewState["selectedColor"];
            }
            catch (NullReferenceException)
            {
                return Color.White;
            }
        }
        set { ViewState["selectedColor"] = value; }
    }

    private string getColor(int liX, int liY)
    {
        int liXmod = (int)((255F / (float)csBoardSize) * (float)liX);
        int liYmod = (int)((255F / (float)csBoardSize) * (float)liY);

        int liR = 0, liG = 0, liB = 0;

        switch (Palette)
        {
            case "pal1":
                liR = liXmod;
                liG = liYmod;
                break;
            case "pal2":
                liG = liXmod;
                liB = liYmod;
                break;
            case "pal3":
                liR = liXmod;
                liB = liYmod;
                break;
            case "pal4":
                liR = liYmod;
                liG = liXmod;
                liB = liYmod;
                break;
            case "pal5":
                liR = liYmod;
                liG = liYmod;
                liB = liYmod;
                break;
            default:
                liR = liXmod;
                liG = liYmod;
                break;
        }
        return ColorTranslator.ToHtml(Color.FromArgb(liR, liG, liB));
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        switch (eventArgument)
        {
            case "pal1":
            case "pal2":
            case "pal3":
            case "pal4":
            case "pal5":
                Palette = eventArgument;
                break;
            default:
                try
                {
                    cellPreview.BackColor = ColorTranslator.FromHtml(eventArgument);
                    cellPreview.ForeColor = Color.FromArgb(~cellPreview.BackColor.ToArgb());
                    this.SelectedColor = cellPreview.BackColor;
                    cellPreview.Text = eventArgument;
                }
                catch (Exception)
                {

                }
                break;
        }
    }
}
