gpt4 book ai didi

c# - 从 .Net 核心中的十六进制获取颜色名称(不使用 System.Drawing.Color)C#

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:05 25 4
gpt4 key购买 nike

我正在尝试从应用程序的十六进制中获取颜色名称,但 system.Drawing.color 在 .netcore 中不可用有人可以建议任何 API 的替代解决方案吗?

我试过 MagickImage lib ,它从字符串中转出十六进制代码,但反之则不行

我想要这样的东西:string ColorName = GetColorName("#8090A6");

最佳答案

您可以定义自己的 Colors-structclass 以及返回给定十六进制颜色的相应名称的方法。这可能看起来像:

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"

关于c# - 从 .Net 核心中的十六进制获取颜色名称(不使用 System.Drawing.Color)C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45748049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com