http://demo.tc/Post/237 超讚的程式碼,試過沒問題~^^


原始顏色td>
HEX
HSB



#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;"> 100) {     sB = 100; } else if (sB < sb =" 0;"> 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) 人氣()