http://demo.tc/Post/237

頁面上我們給它三個文字方塊,用以輸入RGB三色碼

  1. <div>
  2. <asp:TextBox ID="txtR" runat="server">asp:TextBox>
  3. <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtR"
  4. Display="Dynamic" ErrorMessage="超過255" MaximumValue="255" MinimumValue="0" Type="Integer">asp:RangeValidator>
  5. <asp:TextBox ID="txtG" runat="server">asp:TextBox>
  6. <asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="txtG"
  7. Display="Dynamic" ErrorMessage="超過255" MaximumValue="255" MinimumValue="0" Type="Integer">asp:RangeValidator>
  8. <asp:TextBox ID="txtB" runat="server">asp:TextBox>
  9. <asp:RangeValidator ID="RangeValidator3" runat="server" ControlToValidate="txtB"
  10. Display="Dynamic" ErrorMessage="超過255" MaximumValue="255" MinimumValue="0" Type="Integer">asp:RangeValidator>
  11. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="給我轉" /><br />
  12. div>
  13. <table>
  14. <tr>
  15. <td style="width: 100px">
  16. 原始顏色td>
  17. <td style="width: 30px">
  18. td>
  19. <td rowspan="2" style="width: 100px">
  20. <table>
  21. <tr>
  22. <td align="center" style="font-weight: bold; width: 204px">
  23. HEX
  24. td>
  25. tr>
  26. <tr>
  27. <td align="center" style="width: 100px">
  28. <asp:Label ID="labHEX" runat="server">asp:Label>
  29. td>
  30. tr>
  31. <tr>
  32. <td align="center" style="font-weight: bold; width: 204px">
  33. HSB
  34. td>
  35. tr>
  36. <tr>
  37. <td align="center" style="width: 100px">
  38. <asp:Label ID="labHSB" runat="server">asp:Label>
  39. td>
  40. tr>
  41. table>
  42. <br />
  43. <br />
  44. td>
  45. tr>
  46. <tr>
  47. <td style="width: 100px">
  48. <asp:Panel ID="show" runat="server" Height="100px" Width="100px">
  49. asp:Panel>
  50. td>
  51. <td style="width: 30px">
  52. td>
  53. tr>
  54. table>

程式頁就直接給它以下code

#region 變數宣告到位

int R = Convert.ToInt32(this.txtR.Text);

int G = Convert.ToInt32(this.txtG.Text);

int B = Convert.ToInt32(this.txtB.Text);

this.show.BackColor = System.Drawing.Color.FromArgb(255, R, G, B);

float sH, sB, sS, aH, aS, aB, aF, aP, aQ, aT;

int lH;

#endregion

#region 利用陣列排序取出最大值和最小值

ArrayList colorArr = new ArrayList();

colorArr.Add(R);

colorArr.Add(G);

colorArr.Add(B);

colorArr.Sort();

float max = Convert.ToInt16(colorArr[colorArr.Count - 1]);

float mix = Convert.ToInt16(colorArr[0]);

#endregion

#region RGB TO HEX

this.labHEX.Text = string.Format("#{0:X2}{1:X2}{2:X2}", R, G, B);

#endregion

#region RGB TO HSB

sH = System.Drawing.Color.FromArgb(255, R, G, B).GetHue();

sS = (max - mix) / max;

sS = Convert.ToSingle(sS.ToString("N2").Substring(sS.ToString().IndexOf('.') + 1, 2));

sB = max;

sB = max / 255;

sB = Convert.ToSingle(sB.ToString("N2").Substring(sB.ToString().IndexOf('.') + 1, 2));

this.labHSB.Text = string.Format("{0:F0},{1},{2}", sH, sS, sB);

colorArr = null;

#endregion

#region HSB TO RGB

sH = (sH % 360);

if (sS > 100)

{

sS = 100;

}

else if (sS <>

{

sS = 0;

}

if (sB > 100)

{

sB = 100;

}

else if (sB <>

{

sB = 0;

}

if (sS > 0)

{

aH = sH / 60;

aS = sS / 100;

aB = sB / 100;

lH = Convert.ToInt32(aH);

aF = aH - lH;

aP = aB * (1 - aS);

aQ = aB * (1 - aS * aF);

aT = aB * (1 - aS * (1 - aF));

switch (lH)

{

case 0:

R = Convert.ToInt32(aB * 255);

G = Convert.ToInt32(aT * 255);

B = Convert.ToInt32(aP * 255);

break;

case 1:

R = Convert.ToInt32(aQ * 255);

G = Convert.ToInt32(aB * 255);

B = Convert.ToInt32(aP * 255);

break;

case 2:

R = Convert.ToInt32(aP * 255);

G = Convert.ToInt32(aB * 255);

B = Convert.ToInt32(aT * 255);

break;

case 3:

R = Convert.ToInt32(aP * 255);

G = Convert.ToInt32(aQ * 255);

B = Convert.ToInt32(aB * 255);

break;

case 4:

R = Convert.ToInt32(aT * 255);

G = Convert.ToInt32(aP * 255);

B = Convert.ToInt32(aB * 255);

break;

case 5:

R = Convert.ToInt32(aB * 255);

G = Convert.ToInt32(aP * 255);

B = Convert.ToInt32(aQ * 255);

break;

}

}

else

{

R = Convert.ToInt32((sB * 255) / 100);

G = Convert.ToInt32(R);

B = Convert.ToInt32(R);

}

R = R > 255 ? 255 : R;

G = G > 255 ? 255 : G;

B = B > 255 ? 255 : B;

#endregion


arrow
arrow
    全站熱搜

    雀悦 發表在 痞客邦 留言(0) 人氣()