• C# RGB与16进制颜色转换方法


    #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);

            }

    }

     

     

     

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

  • 相关阅读:
    一个或多个listeners启动失败,更多详细信息查看对应的容器日志文件
    java: Compilation failed: internal java compiler error
    新建IEDA项目,运行跳出页面404
    windows版 nvm 1.1.7 安装(填坑)
    向量法求三角形垂点
    error C4430:missing type specifier 解决错误
    QML 从无到有 3 (自动更新)
    QML 从无到有 2 (移动适配)
    QML 从无到有 (基础)
    c++ builder 2010 错误 F1004 Internal compiler error at 0x9740d99 with base 0x9
  • 原文地址:https://www.cnblogs.com/daming1233/p/6702634.html
Copyright © 2020-2023  润新知