//PickColor Control 2004 Source Code....
using System;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
namespace WMB.WebControls
{
[ToolboxBitmap(typeof(DropDownList)),
DefaultProperty("Text"),
ToolboxData("<{0}:ColorPick runat='server' />")]
public class ColorPick : Control, INamingContainer
{
// we need a textbox, a span and a table of which we can set or get properties
TextBox _TextBox = new TextBox();
Label _Label = new Label();
Table MainTable = new Table();
[Category("Data"),
Description("Set to true if you want to create the colortables serverside and send them to the browser."),
DefaultValue(false)]
public bool ShowColors
{
get
{
if (ViewState["ShowColors"] == null)
{
return false;//default value
}
else
{
return (bool)ViewState["ShowColors"];
}
}
set
{
if (value)
{
_Label.Style["visibility"] = "visible";
}
else
{
_Label.Style["visibility"] = "hidden";
}
MainTable.Visible = value;
ViewState["ShowColors"] = value;
}
}
[Category("Data"),
Description("Textbox text."),
DefaultValue("")]
public string Text
{
get
{
return _TextBox.Text;
}
set
{
Color c;
if (value.Trim() != "")
{
c = Color.FromName(value);
if (!c.IsKnownColor)
{
string hexvalue = Regex.Replace(value, "[^0-9a-fA-F]", "");
if (hexvalue.Length == 6)
{
c = ColorTranslator.FromHtml("#" + hexvalue);
value = ColorTranslator.ToHtml(c);
}
else
{
c = Color.White;
value = "#Error";
}
}
else
{
value = ColorTranslator.ToHtml(c);
}
}
else
{
c = Color.White;
}
if (c.GetBrightness() < 0.5)
{
_TextBox.Style["color"] = "white";
}
else
{
_TextBox.Style["color"] = "black";
}
_TextBox.Text = value;
_TextBox.Style["background-color"] = ColorTranslator.ToHtml(c);
}
}
protected override void OnInit(EventArgs e)
{
// set default visibility of the color tables
ShowColors = ShowColors;
//the textbox is added here in order to maintain viewstate
_TextBox.TextChanged += new EventHandler(TextBox_TextChanged);
_TextBox.AutoPostBack = true;
_TextBox.ID = "ColorField";
Controls.Add(_TextBox);
base.OnInit(e);
}
protected override void CreateChildControls()
{
//First we edit the main span
_Label.Style["position"] = "absolute";
_Label.ID = "ColorSpan";
//We need a table for layout and header
MainTable.Attributes.Add("style", "color:captiontext;background-color:threedface;border:2px threedhighlight outset;font-weight:bold;border-collapse:collapse;");
//this is our header row
TableRow MainTr = new TableRow();
MainTr.Style["background-color"] = "activecaption";
// this is the tablecell with the header text
TableCell MainTd = new TableCell();
MainTd.Style["padding-left"] = "5px";
MainTd.ColumnSpan = 2;
MainTd.HorizontalAlign = HorizontalAlign.Left;
MainTd.Text = "Colorpick 2004";
MainTr.Controls.Add(MainTd);
// this is tablecell with the closebutton
MainTd = new TableCell();
MainTd.HorizontalAlign = HorizontalAlign.Right;
// this is our closebutton
Button CloseButton = new Button();
CloseButton.Text = "x";
CloseButton.Attributes.Add("style", "font-weight:bold;width:20px;");
CloseButton.CausesValidation = false;
CloseButton.ToolTip = "Close";
CloseButton.Click += new EventHandler(ShowButton_Click);
// all add it to our layout table
MainTd.Controls.Add(CloseButton);
MainTr.Controls.Add(MainTd);
MainTable.Controls.Add(MainTr);
MainTr = new TableRow();
// lets create our colortables and add them to our maintable
Table ColorTable;
TableRow ColorTr = new TableRow();
TableCell ColorTd = new TableCell();
for (int i1 = 0; i1 <= 255; i1 += 51)
{
MainTd = new TableCell();
ColorTable = new Table();
for (int i2 = 0; i2 <= 255; i2 += 51)
{
ColorTr = new TableRow();
for (int i3 = 0; i3 <= 255; i3 += 51)
{
ColorTd = new TableCell();
Button ColorButton = new Button();
ColorButton.CssClass = "colorbutton";
Color c = Color.FromArgb(i1, i2, i3);
ColorButton.Style["background-color"] = ColorTranslator.ToHtml(c);
ColorButton.ToolTip = ColorButton.Style["background-color"];
ColorButton.CausesValidation = false;
ColorButton.Click += new EventHandler(ColorButton_Click);
ColorTd.Controls.Add(ColorButton);
ColorTr.Controls.Add(ColorTd);
}
ColorTable.Controls.Add(ColorTr);
}
MainTd.Controls.Add(ColorTable);
MainTr.Controls.Add(MainTd);
if (i1 == 102 || i1 == 255)
{
MainTable.Controls.Add(MainTr);
MainTr = new TableRow();
}
}
// add the table to _Label and _Label to control
_Label.Controls.Add(MainTable);
Controls.Add(_Label);
//let us create and add a button to show the colortables
Button ShowButton = new Button();
ShowButton.ID = "ShowButton";
ShowButton.Text = "...";
ShowButton.Style["width"] = "20px";
ShowButton.ToolTip = "Show colortables";
ShowButton.CausesValidation = false;
ShowButton.Click += new EventHandler(ShowButton_Click);
Controls.Add(ShowButton);
}
//here's three functions for the controls
private void ShowButton_Click(object sender, EventArgs e)
{
ShowColors = !ShowColors;
}
private void ColorButton_Click(object sender, EventArgs e)
{
this.Text = ((Button)sender).Style["background-color"];
ShowColors = false;
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
this.Text = _TextBox.Text;
}
//we need lots of javascript which we create on prerender
protected override void OnPreRender(EventArgs e)
{
//if we don't have the javascript in our page yet we'll get it out of cache... and if it's not there... we'll create it
if (!Page.IsClientScriptBlockRegistered("WMBColorPick"))
{
string ClientScript = (string)HttpContext.Current.Cache.Get("WMBColorPick");
if (ClientScript == null)
{
StringBuilder sb = new StringBuilder();
sb.Append("
1<style media='\"screen\"' type='\"text/css\"'></style>