⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 color-list.aspx

📁 ASP.NET Web Forms Techniques
💻 ASPX
字号:
<%@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>
<style type="text/css">
body, td, tr, th {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input, select {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style>
<title>Browser-safe and Standard Colors</title>
</head>
<body>
<span class="heading">Browser-safe and Standard Colors</span><p />

<!-- placeholder to hold table of results -->
<asp:PlaceHolder id="ctlPlaceholder" runat="server" />

<hr /><span class="cite">&copy;2002 <a class="cite" href="http://www.daveandal.net/">Dave And Al</a></span>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -