当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Lecture 4 SVN
SCA兼容性分析工具(ORACLE/MySQL/DB2---&gt;MogDB/openGauss/PostgreSQL)
Analysis and application of portrait segmentation technology
idea永久激活教程(新版)
【毕设选题推荐】机器人工程专业毕设选题推荐
Interviewer: Tell me the difference between NIO and BIO
用过Apifox这个API接口工具后,确实感觉postman有点鸡肋......
博途1200/1500PLC斜坡指令RAMP(带暂停功能)
AVR学习笔记之熔丝位
Niuke.com Brush Question Record || Linked List
Fuse bit of AVR study notes
企业应当实施的5个云安全管理策略
零基础可以转行软件测试吗 ?这篇文章告诉你
SLAM 05.视觉里程计-2-特征法
How to stress the MySQL performance indicators TPS\QPS\IOPS?
橄榄枝大课堂APP正式启动上线
阿里老鸟终于把测试用例怎么写说的明明白白了,小鸟必看
Various problems with npm install
SLAM 04.视觉里程计-1-相机模型
【LeetCode】38、外观数列








