- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试从应用程序的十六进制中获取颜色名称,但 system.Drawing.color 在 .netcore 中不可用有人可以建议任何 API 的替代解决方案吗?
我试过 MagickImage lib ,它从字符串中转出十六进制代码,但反之则不行
我想要这样的东西:string ColorName = GetColorName("#8090A6");
最佳答案
您可以定义自己的 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"
关于c# - 从 .Net 核心中的十六进制获取颜色名称(不使用 System.Drawing.Color)C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45748049/
我有一个带有帮助页面的 Web API 2 项目,该项目在本地运行良好,但当我将其推送到 Azure 时抛出此错误: Method not found: 'System.String System.S
我有两台服务器,但通常运行相同的设置 - IIS、SQL Server 等。一台给我这个错误,另一台没有。我从 Visual Studio 向两者发布相同的代码。 它们都在运行 .NET CLR Ve
System.out声明为 public static final PrintStream out。 但是你可以调用System.setOut()重新分配它。 嗯?如果它是 final,这怎么可能?
System.out被声明为 public static final PrintStream out。 但是您可以调用System.setOut()重新分配它。 嗯?如果是 final,这怎么可能?
我有这个 linq 查询: private void GetReceivedInvoiceTasks(User user, List tasks) { var areaIds = user.A
我有一个 MonoTouch 应用程序,当我为设备编译它时,出现以下错误: Error MT2002: Can not resolve reference: System.Boolean System
您好,我有一个名为 DailyVisitReport 的 View 。在该 View 中,我有两个名为 FromDate 和 toDate 的字段。如果我选择 FromDate 和 ToDate 取决
是否可以从 ObjectContext 对象中读取元组列表? 我在存储过程中有类似这样的数据库查询 SELECT T.Id as Item1, -- this is guid T.Wo
我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。 我在下面的非常简单示例中重新创建了它。 数据库表 EDMX(片段)
我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。 我在下面的非常简单示例中重新创建了它。 数据库表 EDMX(片段)
我有一个方法可以从数据读取器的数据中生成类类型列表。 if (datareader != null && datareader .HasRows) { Dictionary pDict= GetP
我有一些旧的 C++ 代码,它们使用 stdio 进行输入和输出。该代码还通过 fork 生成新进程。它将 stdio 重新映射到每个新进程,以便每个 session 获取其各自的数据。 我正在考虑使
我的应用程序可以很好地构建/链接/部署到模拟器,但我只是第一次尝试将应用程序构建/部署到真实设备,并且链接器失败。 我不使用 System.Console或 ConsoleColor在我的应用程序的任
主要是我很好奇。 我们有一个名为 Unit 的对象在我们的代码库中 - 代表桥梁或道路的组件。在我们的例子中,看到带有 Unit 的 ReactiveUI 命令可能会模棱两可。作为声明中的泛型之一。
我试图将Object变量转换为StreamWriter。但是,它不起作用。有什么错? StreamWriter file = (StreamWriter) myObject; 最佳答案 myObjec
为什么以下不编译? using System; using System.Linq; using System.Linq.Expressions; public static class Extens
我正在使用 Visual Studio Community 2015 开发面向 .NET 4.5 的 Visual Basic 应用程序.我没有编写应用程序,所以我使用 NuGet 添加了所有缺失的依
我刚刚开始使用 powershell,我正在制作一个非常简单的加密功能。我想获取字符串中的每个字符,将其转换为 int 并添加一个选定的数字,然后将其转换回一个字符。 这工作正常: function
一些使用我的应用程序的人似乎变得越来越 System.MissingMethodException: Method not found: 'System.Object System.Windows.T
我是 C# 和实体的新手 我想知道是否有人在这里帮助我。我选择了哪个返回我的 customerid,所以我想将它作为参数传递给我的构造函数,我的构造函数参数类型是 guid 但我的选择类型不同,我不知
我是一名优秀的程序员,十分优秀!