简体   繁体   中英

get a color name from hex in .Net core (without using System.Drawing.Color) C#

i am trying to fetch the color name from the hex for an application but system.Drawing.color is not available in .netcore can someone suggest an alternative solution any API?

I tried MagickImage lib , that rturns hex code from string but not vice versa

I want something like this : string ColorName = GetColorName("#8090A6");

The only solution I know of is to make a lookup table of known HTML colors browsers support and perform the lookup. Dictionaries are thread safe for read only operations so you do not need to lock on access.

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;
    }

}

You could define your own Colors - struct or class aswell as a method that returns the corresponding name of a given hex-color. This could look like:

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;
    }
}

If you are using C# 7.2, you could also make the struct readonly, which would look like:

public readonly struct Colors {

    public string AliceBlue = "#FFF0F8FF";
    // and so on ...
}

Instead of using a struct with constant fields, you could also use a class with static fields:

public class Colors {

    public static string AliceBlue = "#FFF0F8FF";
    // and so on ...
}


Usage

To get the value of a color or vice versa, you would do this:

// get hex value of color
string hex = Colors.AliceBlue;

// get name by hex color (using reflection)
string colorName = Colors.GetColorName("#FF00008B"); // returns "DarkBlue"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM