C# RGB與16進位色彩轉換方法

來源:互聯網
上載者:User

標籤:

http://www.cnblogs.com/goldarch/archive/2010/08/16/1801053.html

#region [顏色:16進位轉成RGB]
        /// <summary>
        /// [顏色:16進位轉成RGB]
        /// </summary>
        /// <param name="strColor">設定16進位顏色 [返回RGB]</param>
        /// <returns></returns>
        public static System.Drawing.Color colorHx16toRGB(string strHxColor)
        {
            try
            {
                if (strHxColor.Length == 0)
                {//如果為空白
                    return System.Drawing.Color.FromArgb(0, 0, 0);//設為黑色
                }
                else
                {//轉換顏色
                    return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2),           System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
                }
            }
            catch
            {//設為黑色
                return System.Drawing.Color.FromArgb(0, 0, 0);
            }
        }
        #endregion

 


#region [顏色:RGB轉成16進位]
        /// <summary>
        /// [顏色:RGB轉成16進位]
        /// </summary>
        /// <param name="R">紅 int</param>
        /// <param name="G">綠 int</param>
        /// <param name="B">藍 int</param>
        /// <returns></returns>
        public static string colorRGBtoHx16(int R, int G, int B)
        {
            return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));
        }
#endregion

 

又:

 

   private string ToHexColor(Color color)
        {
            string R = Convert.ToString(color.R, 16);
            if (R == "0")
                R = "00";
            string G = Convert.ToString(color.G, 16);
            if (G == "0")
                G = "00";
            string B = Convert.ToString(color.B, 16);
            if (B == "0")
                B = "00";
            string HexColor = "#" + R + G + B;
            return HexColor;
        }

 

    public string ForeColor

    {

        set

        {

             //value = #ab364f

            int r = Convert.ToInt32("0x" + value.Substring(1, 2),16);

            int g = Convert.ToInt32("0x" + value.Substring(3, 2),16);

            int b = Convert.ToInt32("0x" + value.Substring(5, 2),16);

            txtUrl.ForeColor = System.Drawing.Color.FromArgb(r,g,b);

        }

 }

C# RGB與16進位色彩轉換方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.