按以上的理原,小弟實作的程式碼如下:
public string getGrayColor(int totalCount) { string html = ""; int no = 0; int color = 255; int step = color / totalCount; while (color >= 0) { string strColor = String.Format("{0:X}", color); strColor = strColor.PadLeft(2, '0'); strColor += strColor + strColor; html += ("<DIV style='height:20px;width:300px;background-color:#" + strColor + "'>" + no + " " + strColor + "<DIV>"); no++; color -= step; } return html; }
- 程式的參數totcalCount是表示你希望分幾組灰階。
- 若執行getGrayColor(20)的執行結果如下: