Web safe color list : Web safe color « XML « ASP.Net






Web safe color list

<%--
Pro ASP.NET Web Forms Techniques, Second Edition

By Alex Homer
ISBN: 1-59059-317-0
Published: Dec 2003
Publisher: Apress.com

--%>       
       
<%@Page Language="C#" %>
<%@Import Namespace="System.Drawing" %>
<script runat="server">
void Page_Load() {
  // array to hold list of 168 "known" colors
  Color[] aKnownCols = new Color[168];
  KnownColor eValue;
  for (eValue = 0; eValue <= KnownColor.YellowGreen; eValue++) {
    aKnownCols[(int) eValue] = Color.FromKnownColor(eValue);
  }

  // array to hold list of 216 browser-safe colors
  Color[] aSafeCols = new Color[216];
  int rValue, gValue, bValue;
  int iPointer = 0;
  for (rValue = 0; rValue <= 255; rValue += 51) {
    for (gValue = 0; gValue <= 255; gValue += 51) {
      for (bValue = 0; bValue <= 255; bValue += 51) {
        aSafeCols[iPointer] = Color.FromArgb(rValue, gValue, bValue);
        iPointer += 1;
      }
    }
  }

  // create a table containing the browser-safe colors
  // and indicate any matching .NET standard color

  // declare variables, we'll create 5 rows simultaneously
  TableRow oRow1, oRow2, oRow3, oRow4, oRow5;
  TableCell oCell;
  Color oKnown;
  String sDecimalVals, sHexVals, sKnown;

  // to create empty cell with colored background
  String sColorCell = "<font size=5>&nbsp; &nbsp; &nbsp; &nbsp;</font>";

  // create empty Table object and five Row objects
  Table oTable = new Table();
  oRow1 = new TableRow();
  oRow2 = new TableRow();
  oRow3 = new TableRow();
  oRow4 = new TableRow();
  oRow5 = new TableRow();

  // set horizontal alignment for three rows
  oRow2.HorizontalAlign = HorizontalAlign.Center;
  oRow3.HorizontalAlign = HorizontalAlign.Center;
  oRow4.HorizontalAlign = HorizontalAlign.Center;

  // iterate through array of safe colors
  foreach (Color oColor in aSafeCols) {

    // create the strings showing decimal and hex RGB values
    sDecimalVals = oColor.R.ToString() + "," + oColor.G.ToString()
                 + "," + oColor.B.ToString();
    sHexVals = "#" + oColor.R.ToString("x2") + oColor.G.ToString("x2")
             + oColor.B.ToString("x2");

    // see if this color is same as any standard color by iterating
    // through "known" colors array and comparing ARGB values.
    // not breaking at first match avoids returning "system" colors
    // such as "DialogText" and "WindowTitleBarText" which come first
    sKnown = "";
    foreach (Color oEKnown in aKnownCols) {
      if (oEKnown.ToArgb() == oColor.ToArgb()) {
        sKnown = oEKnown.Name;
      }
    }

    // create a new cell, and add LiteralControl containing value
    oCell = new TableCell();
    oCell.Controls.Add(new LiteralControl(sColorCell));

    // set properties for this cell, and add to row 1
    oCell.BorderColor = Color.Black;
    oCell.BorderStyle = BorderStyle.Solid;
    oCell.BorderWidth = Unit.Pixel(1);
    oCell.BackColor = oColor;
    oRow1.Cells.Add(oCell);

    // repeat for cells containing decimal and hex RGB values
    oCell = new TableCell();
    oCell.Controls.Add(new LiteralControl(sDecimalVals));
    oRow2.Cells.Add(oCell);
    oCell = new TableCell();
    oCell.Controls.Add(new LiteralControl(sHexVals));
    oRow3.Cells.Add(oCell);

    // create cell for matching "known" color if there is one
    oCell = new TableCell();
    if (sKnown != "") {
      oCell.Controls.Add(new LiteralControl(sKnown));

      // create color object using "known" color name
      Color oEKnown = Color.FromName(sKnown);
      oCell.BackColor = oEKnown;
      oCell.BorderColor = Color.Black;
      oCell.BorderStyle = BorderStyle.Solid;
      oCell.BorderWidth = Unit.Pixel(1);

      // see if we need to use white text on this background
      // color when displaying color name in cell
      if (oEKnown.R < 124 && oEKnown.G < 124) {
        oCell.ForeColor = Color.White;
      }

    }

    // add "known color" cell to row 4
    oRow4.Cells.Add(oCell);

    // create cell in row 5 to provide space between color rows
    oCell = new TableCell();
    oCell.Controls.Add(new LiteralControl("&nbsp;"));
    oRow5.Cells.Add(oCell);

    // if Blue element of color has reached 255 start a new row
    // add five existing rows to table, create new ones and set
    // horizontal alignment for the three of them holding text

    if (oColor.B == 255) {
      oTable.Rows.Add(oRow1);
      oTable.Rows.Add(oRow2);
      oTable.Rows.Add(oRow3);
      oTable.Rows.Add(oRow4);
      oTable.Rows.Add(oRow5);
      oRow1 = new TableRow();
      oRow2 = new TableRow();
      oRow3 = new TableRow();
      oRow4 = new TableRow();
      oRow5 = new TableRow();
      oRow2.HorizontalAlign = HorizontalAlign.Center;
      oRow3.HorizontalAlign = HorizontalAlign.Center;
      oRow4.HorizontalAlign = HorizontalAlign.Center;
    }

  }

  // table complete, so insert into page within PlaceHolder control
  ctlPlaceholder.Controls.Add(oTable);

}

</script>

<!doctype html public "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Browser-safe and Standard Colors</title>
</head>
<body>
Browser-safe and Standard Colors
<asp:PlaceHolder id="ctlPlaceholder" runat="server" />
</body>
</html>

           
       








Related examples in the same category