1

I want to store a color hex value (such as "#FFFF1493") as its "human-friendly" name (such as "DeepPink").

With help from SLaks and nvoigt here, I've now got this code:

if (sender is Canvas)
{
    var canvas = sender as Canvas;
    var brush = canvas.Background as SolidColorBrush;
    var color = brush.Color;
    String brushColorAsStr = color.ToString();

    IAppSettings appSettings;
    if (App.SaveSettingsLocally)
    {
        appSettings = new AppSettingLocal();
    }
    else
    {
        appSettings = new AppSettingsRemote();
    }
    appSettings.SaveSetting(VisitsConsts.APP_BAR_COLOR_SETTING, brushColorAsStr);
}

...but the value in brushColorAsStr is "#FFFF1493" (when I click the DeepPink canvas control), and that doesn't work with my code to change the app bar color based on the color selected:

String brushColor = appSettings.GetSetting(VisitsConsts.APP_BAR_COLOR_SETTING);
if (null == brushColor) return;

if (brushColor.Equals("Blue"))
{
    CmdBar.Background = new SolidColorBrush(Colors.Blue);
}
else if (brushColor.Equals("Aqua"))
. . .

From here I got these suggestions:

Color colour = (Color)ColorConverter.ConvertFromString(brushColorAsStr);
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(brushColorAsStr);

...but "ColorConverter" and "Drawing" are unresolvable in my app. How can I get a human-readable name from a hex color val?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • The first thing you probably should do is strip out the alpha channel, ie the first two hex digits.. – TaW Nov 30 '14 at 18:49

2 Answers2

3

There isn't a mapping from numbers to names, but the Colors class maps from names to numbers. You can use reflection to build the reverse mapping.

See this previous answer for sample code: How to convert a Windows.UI.Color into a string color name in a Windows Universal app

Community
  • 1
  • 1
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
1

These links might be helpful for you:

convert hex code to color name

http://www.xiirus.net/articles/article-c-convert-drawing_color-to-html-hex-color-value-and-back-cc8mp.aspx

https://social.msdn.microsoft.com/Forums/en-US/f92a8d0a-8116-4fa6-8072-0b0f09536847/how-to-convert-the-hex-value-into-its-equivalent-color-name?forum=wpf

Community
  • 1
  • 1
Ishpreet
  • 659
  • 5
  • 19