To Examples
color-game.aspx Source Code
<!-- color-game.aspx Example Display a random color on the left. The user tries to match it on the right. --> <%@ Page Language="C#" %> <%@ Import Namespace="System.Drawing" %> <script runat=server> public void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Color c = Color.FromArgb(0, 0, 0); Session[color] = c; } } public void btnChangeColor_Click(object sender, EventArgs e) { Color randomColor; Random rand = new Random( ); int r, g, b; // Pick new color for true color. r = rand.Next(0, 256); g = rand.Next(0, 256); b = rand.Next(0, 256); randomColor = Color.FromArgb(r, g, b); txtRandomColor.BackColor = randomColor; txtTrueColor.Text = ; txtHexColorCode.Text = ; Session[color] = randomColor; } public void txtHexColorCode_TextChanged(object sender, EventArgs e) { try { string[ ] hexColor = new string[4]; int[ ] color = new int[4]; string colorCode = txtHexColorCode.Text; hexColor[1] = colorCode.Substring(1, 2); hexColor[2] = colorCode.Substring(3, 2); hexColor[3] = colorCode.Substring(5, 2); for(int i = 1; i <= 3; i++) { char[ ] c = hexColor[i].ToCharArray( ); color[i] = 16 * Uri.FromHex(c[0]) + Uri.FromHex(c[1]); } txtColorToMatch.BackColor = Color.FromArgb(color[1], color[2], color[3]); } catch(Exception ex) { txtHexColorCode.Text = Illegal Hex Color Code.; } } public void btnShowColor_Click(object sender, EventArgs e) { Color randomColor = (Color) Session[color]; string[ ] d = {0, 1, 2, 3, 5, 6, 7, 8, 9, 0, A, B, C, D, E, F}; int[ ] c = new int[4]; c[1] = randomColor.R; c[2] = randomColor.G; c[3] = randomColor.B; string hex = #; for(int i = 1; i <= 3; i++) { hex += d[c[i] / 16] + d[c[i] % 16]; } txtTrueColor.Text = hex; } </script> <html> <head> <title>grocery-display1.aspx Example</title> <link rel=stylesheet class=text/css href=examples.css /> <style class=text/css> td { padding: 0.15cm; } tr { vertical-align:top; } .center { text-align:center; } </style> </head> <body> <h2>color-game.aspx Example</h2> <h2>Match random color on left<br /> with your best guess on the right.</h2> <form id=frmColorGame runat=server> <table> <tr> <td class=center>Random Color to Guess</td> <td class=center>Your Best Guess</td> <td> </td> </tr> <tr> <td><asp:TextBox Id=txtRandomColor Width=200px Height=200px ReadOnly=True BackColor=#000000 Text= runat=server /></td> <td><asp:TextBox Id=txtColorToMatch Width=200px Height=200px ReadOnly=True BackColor=#000000 Text= runat=server /></td> <td>Hex color code of your best guess.<br /> Click outside of textbox to check.<br /> <asp:TextBox Id=txtHexColorCode Width=300px runat=server AutoPostBack=True OnTextChanged= txtHexColorCode_TextChanged /><br /> <br /> True hex color code of random color.<br /> <asp:TextBox Id=txtTrueColor Width=300px runat=server AutoPostBack=True/><br /> <br /> <asp:Button Id=btnShowColor Width=300px runat=server Text=Show Hex Code of Random Color AutoPostBack=True OnClick=btnShowColor_Click /></td> </tr> <tr > <td><asp:Button Id=btnChangeColor Width=200px runat=server Text=Pick Random Color AutoPostBack=True OnClick=btnChangeColor_Click /></td> <td> </td> <td> </td> </tr> </table> </form> </body> </html>