我正在尝试从应用程序的十六进制中获取颜色名称但是.netcore中没有system.Drawing.color可以有人建议任何API的替代解决方案吗?
我尝试了MagickImage lib,它从字符串中rturns十六进制代码,但反之亦然
我想要这样的东西:string ColorName = GetColorName("#8090A6");
答案 0 :(得分:0)
我知道的唯一解决方案是制作known HTML colors browsers support的查找表并执行查找。字典对于只读操作是线程安全的,因此您无需锁定访问权限。
using System.Collections.Generic;
public static class ColorUtils
{
private static IReadOnlyDictionary<string, string> ColorMapping = new Dictionary<string, string>
{
["#F0F8FF"] = "AliceBlue",
["#FAEBD7"] = "AntiqueWhite",
//... the other 136 colors
["#FFFF00"] = "Yellow",
["#9ACD32"] = "YellowGreen"
};
/// <summary>
/// Returns the color name or null if the name is not found.
/// </summary>
public static string ToColorName(string htmlColor)
{
ColorMapping.TryGetValue(htmlColor, out var name);
return name;
}
}
答案 1 :(得分:0)
您可以定义自己的Colors
-struct
或class
以及返回给定十六进制颜色的相应名称的方法。可能看起来像:
public struct Colors
{
public const string AliceBlue = "#FFF0F8FF";
public const string AntiqueWhite = "#FFFAEBD7";
public const string Aqua = "#FF00FFFF";
public const string Aquamarine = "#FF7FFFD4";
public const string Azure = "#FFF0FFFF";
public const string Beige = "#FFF5F5DC";
public const string Bisque = "#FFFFE4C4";
public const string Black = "#FF000000";
public const string BlanchedAlmond = "#FFFFEBCD";
public const string Blue = "#FF0000FF";
public const string BlueViolet = "#FF8A2BE2";
public const string Brown = "#FFA52A2A";
public const string BurlyWood = "#FFDEB887";
public const string CadetBlue = "#FF5F9EA0";
public const string Chartreuse = "#FF7FFF00";
public const string Chocolate = "#FFD2691E";
public const string Coral = "#FFFF7F50";
public const string CornflowerBlue = "#FF6495ED";
public const string Cornsilk = "#FFFFF8DC";
public const string Crimson = "#FFDC143C";
public const string Cyan = "#FF00FFFF";
public const string DarkBlue = "#FF00008B";
public const string DarkCyan = "#FF008B8B";
public const string DarkGoldenrod = "#FFB8860B";
public const string DarkGray = "#FFA9A9A9";
public const string DarkGreen = "#FF006400";
public const string DarkKhaki = "#FFBDB76B";
public const string DarkMagenta = "#FF8B008B";
public const string DarkOliveGreen = "#FF556B2F";
public const string DarkOrange = "#FFFF8C00";
public const string DarkOrchid = "#FF9932CC";
public const string DarkRed = "#FF8B0000";
public const string DarkSalmon = "#FFE9967A";
public const string DarkSeaGreen = "#FF8FBC8F";
public const string DarkSlateBlue = "#FF483D8B";
public const string DarkSlateGray = "#FF2F4F4F";
public const string DarkTurquoise = "#FF00CED1";
public const string DarkViolet = "#FF9400D3";
public const string DeepPink = "#FFFF1493";
public const string DeepSkyBlue = "#FF00BFFF";
public const string DimGray = "#FF696969";
public const string DodgerBlue = "#FF1E90FF";
public const string Firebrick = "#FFB22222";
public const string FloralWhite = "#FFFFFAF0";
public const string ForestGreen = "#FF228B22";
public const string Fuchsia = "#FFFF00FF";
public const string Gainsboro = "#FFDCDCDC";
public const string GhostWhite = "#FFF8F8FF";
public const string Gold = "#FFFFD700";
public const string Goldenrod = "#FFDAA520";
public const string Gray = "#FF808080";
public const string Green = "#FF008000";
public const string GreenYellow = "#FFADFF2F";
public const string Honeydew = "#FFF0FFF0";
public const string HotPink = "#FFFF69B4";
public const string IndianRed = "#FFCD5C5C";
public const string Indigo = "#FF4B0082";
public const string Ivory = "#FFFFFFF0";
public const string Khaki = "#FFF0E68C";
public const string Lavender = "#FFE6E6FA";
public const string LavenderBlush = "#FFFFF0F5";
public const string LawnGreen = "#FF7CFC00";
public const string LemonChiffon = "#FFFFFACD";
public const string LightBlue = "#FFADD8E6";
public const string LightCoral = "#FFF08080";
public const string LightCyan = "#FFE0FFFF";
public const string LightGoldenrodYellow = "#FFFAFAD2";
public const string LightGray = "#FFD3D3D3";
public const string LightGreen = "#FF90EE90";
public const string LightPink = "#FFFFB6C1";
public const string LightSalmon = "#FFFFA07A";
public const string LightSeaGreen = "#FF20B2AA";
public const string LightSkyBlue = "#FF87CEFA";
public const string LightSlateGray = "#FF778899";
public const string LightSteelBlue = "#FFB0C4DE";
public const string LightYellow = "#FFFFFFE0";
public const string Lime = "#FF00FF00";
public const string LimeGreen = "#FF32CD32";
public const string Linen = "#FFFAF0E6";
public const string Magenta = "#FFFF00FF";
public const string Maroon = "#FF800000";
public const string MediumAquamarine = "#FF66CDAA";
public const string MediumBlue = "#FF0000CD";
public const string MediumOrchid = "#FFBA55D3";
public const string MediumPurple = "#FF9370DB";
public const string MediumSeaGreen = "#FF3CB371";
public const string MediumSlateBlue = "#FF7B68EE";
public const string MediumSpringGreen = "#FF00FA9A";
public const string MediumTurquoise = "#FF48D1CC";
public const string MediumVioletRed = "#FFC71585";
public const string MidnightBlue = "#FF191970";
public const string MintCream = "#FFF5FFFA";
public const string MistyRose = "#FFFFE4E1";
public const string Moccasin = "#FFFFE4B5";
public const string NavajoWhite = "#FFFFDEAD";
public const string Navy = "#FF000080";
public const string OldLace = "#FFFDF5E6";
public const string Olive = "#FF808000";
public const string OliveDrab = "#FF6B8E23";
public const string Orange = "#FFFFA500";
public const string OrangeRed = "#FFFF4500";
public const string Orchid = "#FFDA70D6";
public const string PaleGoldenrod = "#FFEEE8AA";
public const string PaleGreen = "#FF98FB98";
public const string PaleTurquoise = "#FFAFEEEE";
public const string PaleVioletRed = "#FFDB7093";
public const string PapayaWhip = "#FFFFEFD5";
public const string PeachPuff = "#FFFFDAB9";
public const string Peru = "#FFCD853F";
public const string Pink = "#FFFFC0CB";
public const string Plum = "#FFDDA0DD";
public const string PowderBlue = "#FFB0E0E6";
public const string Purple = "#FF800080";
public const string Red = "#FFFF0000";
public const string RosyBrown = "#FFBC8F8F";
public const string RoyalBlue = "#FF4169E1";
public const string SaddleBrown = "#FF8B4513";
public const string Salmon = "#FFFA8072";
public const string SandyBrown = "#FFF4A460";
public const string SeaGreen = "#FF2E8B57";
public const string SeaShell = "#FFFFF5EE";
public const string Sienna = "#FFA0522D";
public const string Silver = "#FFC0C0C0";
public const string SkyBlue = "#FF87CEEB";
public const string SlateBlue = "#FF6A5ACD";
public const string SlateGray = "#FF708090";
public const string Snow = "#FFFFFAFA";
public const string SpringGreen = "#FF00FF7F";
public const string SteelBlue = "#FF4682B4";
public const string Tan = "#FFD2B48C";
public const string Teal = "#FF008080";
public const string Thistle = "#FFD8BFD8";
public const string Tomato = "#FFFF6347";
public const string Transparent = "#00FFFFFF";
public const string Turquoise = "#FF40E0D0";
public const string Violet = "#FFEE82EE";
public const string Wheat = "#FFF5DEB3";
public const string White = "#FFFFFFFF";
public const string WhiteSmoke = "#FFF5F5F5";
public const string Yellow = "#FFFFFF00";
public const string YellowGreen = "#FF9ACD32";
public static string GetColorName(string hex)
{
FieldInfo[] fields = typeof(Colors).GetFields();
foreach (var field in fields)
{
if (field.GetValue(typeof(Colors)).ToString() == hex)
{
// corresponding color found -> return its name
return field.Name;
}
}
// no corresponding color found -> return null
return null;
}
}
如果您使用的是C#7.2,还可以将结构设为只读,如下所示:
public readonly struct Colors {
public string AliceBlue = "#FFF0F8FF";
// and so on ...
}
除了将struct
与常量字段一起使用之外,还可以将class
与静态字段一起使用:
public class Colors {
public static string AliceBlue = "#FFF0F8FF";
// and so on ...
}
用法
要获取颜色的值,反之亦然,您可以这样做:
// get hex value of color
string hex = Colors.AliceBlue;
// get name by hex color (using reflection)
string colorName = Colors.GetColorName("#FF00008B"); // returns "DarkBlue"