十六进制和RGB颜色互转

上传:yhygf 浏览: 84 推荐: 0 文件:CS 大小:2.1KB 上传时间:2018-12-09 09:20:36 版权申诉
颜色的十六进制和RGB格式进行相互转化 public class ColorConvert { #region [颜色:16进制转成RGB] /// /// [颜色:16进制转成RGB] /// /// 设置16进制颜色 [返回RGB] /// 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); } } public static System.Drawing.Color GetColor(string val) { val = val.Trim(); if (val.Substring(0, 1) == "#") return colorHx16toRGB(val); else return Color.FromName(val); } #endregion #region [颜色:RGB转成16进制] /// /// [颜色:RGB转成16进制] /// /// 红 int /// 绿 int /// 蓝 int /// public static string colorRGBtoHx16(int R, int G, int B) { return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B)); } #endregion }
上传资源
用户评论

chaotic4746 2018-12-09 09:20:36

不错,很好用!不错的代码

qqmug87350 2018-12-09 09:20:36

不错,很有用!

bao99956 2018-12-09 09:20:36

非常好的代码,谢谢分享