当前位置:网站首页>C# winforms 输入颜色转换颜色名
C# winforms 输入颜色转换颜色名
2022-08-04 13:47:00 【林德熙】
本文告诉大家如何输入颜色,如0xFFFF8000
转换为 Orange 在 winforms 程序
可以使用下面代码转换
public static class HexColorTranslator
{
private static Dictionary<string, string> _hex2Name;
private static Dictionary<string, string> Hex2Name
{
get
{
if (_hex2Name == null)
{
_hex2Name = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (KnownColor ce in typeof(KnownColor).GetEnumValues())
{
var name = ce.ToString();
var color = Color.FromKnownColor(ce);
var hex = HexConverter(color);
_hex2Name[hex] = name;
}
}
return _hex2Name;
}
}
private static string HexConverter(Color c)
{
return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
public static string GetKnownColorFromHex(string hex)
{
hex = hex.TrimStart('#');
if (Hex2Name.TryGetValue(hex, out var result))
{
return result;
}
return "???";
}
}
调用的方式是传入颜色
HexColorTranslator.GetKnownColorFromHex("#FFFF8000");
参见: https://stackoverflow.com/a/51238017/6116637
边栏推荐
猜你喜欢
随机推荐
FreeConfig.h文件
大势所趋之下的nft拍卖,未来艺术品的新赋能
Week 7 Latent Variable Models and Expectation Maximization
一文梳理NLP主要模型发展脉络
如何才能有效、高效阅读?猿辅导建议“因材因时施教”
Script to get local IP address
php中的ceil和floo以及round函数「建议收藏」
Utility function---string processing
如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?
How to stress the MySQL performance indicators TPS\QPS\IOPS?
yum 查看已经安装过的包并卸载
面试官:说一下NIO和BIO的区别
odoo13笔记点
PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)
智能电视可以打开小程序应用,再也不用头痛内存了
Is there a replacement for the LM2596?LM2576 can
荧光磷脂PEG衍生物之一磷脂-聚乙二醇-荧光素,Fluorescein-PEG-DSPE
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
MySQL性能指标TPS\QPS\IOPS如何压测?
router---模式