-1

I have a problem with getting the name of a color from a colordialog box in C#. I now have the following code to accomplish this:

  private void bPickColor_Click(object sender, EventArgs e)
    {
        ColorDialog colorDlg = new ColorDialog();
        colorDlg.AllowFullOpen = false;
        colorDlg.AnyColor = true;
        colorDlg.SolidColorOnly = false;
        colorDlg.Color = Color.Red;

        if (colorDlg.ShowDialog() == DialogResult.OK)
        {
            //ColorConverter conv = new ColorConverter();
            Color c = colorDlg.Color;
            //string s = conv.ConvertToString(c);
            //string h = Conversion.Hex(s.ToArgb);
            _ColorName = c.Name.ToString();
            MessageBox.Show(_ColorName);
            bPickColor.BackColor = colorDlg.Color;
        }
    }

As you can see I use 'C.Name.ToString' to get the color name, but somehow this gives back the color code like for example: f0018000. What I need is the name of the color like for example: RED, or BLUE.

The reason I need this is because I store the colors in a datatable and read them using this code:

Color.FromName((string)dtrow["color"]);

So what I've tried so far is to search for code that can get the color name based on the color code, but the pieces of code I found didn't work.

Now I also tried to make a dictionary of all color codes with the corresponding color names, but I couldn't make this to work either.

Does anybody has a solution for this problem? How can I get the name (Blue, Red) of the color?

too_cool
  • 1,164
  • 8
  • 24
user3176938
  • 31
  • 1
  • 6
  • possible duplicate of [How to get Color from Hexadecimal color code using .NET?](http://stackoverflow.com/questions/2109756/how-to-get-color-from-hexadecimal-color-code-using-net) – Alex Neves Jul 29 '15 at 09:25
  • 1
    How could you possibly get a Name string for every color someone might select in a colorpicker? They are too many to have a name each... thats why there is the hexadecimal values... (i guess) :) – cnom Jul 29 '15 at 09:30
  • 1
    possible duplicate of [convert hex code to color name](http://stackoverflow.com/questions/7791710/convert-hex-code-to-color-name) –  Jul 29 '15 at 09:31
  • Yeah but I don't need a color based on the color code but the color name. The answers mentioned in the question above doesn't solve my problem. – user3176938 Jul 29 '15 at 09:32
  • See comment below re. number of colors. – PaulF Jul 29 '15 at 09:47
  • There are only a few named colors. Obviously. – TaW Jul 29 '15 at 09:47

4 Answers4

2

Try this

   _ColorName = c.Name.ToString();
    Color myColor = ColorTranslator.FromHtml(_ColorName);
    MessageBox.Show(myColor.Name);
too_cool
  • 1,164
  • 8
  • 24
1

I have found the solution. I have used this piece of code (system.Linq is needed to make this piece of code work) that I found here: convert hex code to color name :

string GetColorName(Color color)
    {
        var colorProperties = typeof(Color)
            .GetProperties(BindingFlags.Public | BindingFlags.Static)
            .Where(p => p.PropertyType == typeof(Color));
        foreach (var colorProperty in colorProperties)
        {
            var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
            if (colorPropertyValue.R == color.R
                   && colorPropertyValue.G == color.G
                   && colorPropertyValue.B == color.B)
            {
                return colorPropertyValue.Name;
            }
        }

        //If unknown color, fallback to the hex value
        //(or you could return null, "Unkown" or whatever you want)
        return ColorTranslator.ToHtml(color);
    }

In the colordialog picker I now use this code:

 private void bPickColor_Click(object sender, EventArgs e)
    {
        ColorDialog colorDlg = new ColorDialog();

        colorDlg.AllowFullOpen = false;
        colorDlg.AnyColor = true;

        colorDlg.SolidColorOnly = false;

        colorDlg.Color = Color.Red;



        if (colorDlg.ShowDialog() == DialogResult.OK)
        {


            Color c = colorDlg.Color;

            _ColorName = GetColorName(c);
            MessageBox.Show(_ColorName);

            bPickColor.BackColor = colorDlg.Color;

        }
    }

This way it works perfectly. Although I indeed don't know if all colors have a corresponding name but well I can handle that in the code anyway. Thanks for all the answers!

Community
  • 1
  • 1
user3176938
  • 31
  • 1
  • 6
  • 1
    The hex code ('f0018000' in your example) represents a 32 bit number, giving a possibility of 4294967296 different colors - so obviously there are not names for all of them. If the precise color is required, you will need to store the hex code. – PaulF Jul 29 '15 at 09:46
  • One comment: somehow this doesn't work very well because the colors are not all translated to a name. Does anybody know how I can make a dictionary with all the color codes as key and the color names as value? Such a dictionary could solve this problem. – user3176938 Jul 29 '15 at 09:48
  • Not all colors have a name - as I stated above there are 4294967296 possible hex codes, there are not that many variations on color names. – PaulF Jul 29 '15 at 12:09
0

Check this out: https://msdn.microsoft.com/en-us/library/aa358802.aspx

These are the possible color names. So make a selection among these if you MUST have a colorname stored! ;-)

Here is an example of how to make the dictionary you need:

Dictionary<string, string> colors =
    new Dictionary<string, string>();

colors.Add("0x000001", "red");
colors.Add("0x000002", "green");
colors.Add("0x000003", "blue");
colors.Add("0x000004", "black");
cnom
  • 3,071
  • 4
  • 30
  • 60
  • Thanks. I need this in a dictionary (with the color code as key and the color name as value). Anybody has an idea on how to accomplish this? – user3176938 Jul 29 '15 at 09:53
  • I updated the answer with a dictionary example for colors – cnom Jul 29 '15 at 10:21
0

I now solve the problems mentioned above (that not all the colors have a name) like this:

string GetColorName(Color color)
    {
        var colorProperties = typeof(Color)
            .GetProperties(BindingFlags.Public | BindingFlags.Static)
            .Where(p => p.PropertyType == typeof(Color));
        foreach (var colorProperty in colorProperties)
        {
            var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
            if (colorPropertyValue.R == color.R
                   && colorPropertyValue.G == color.G
                   && colorPropertyValue.B == color.B)
            {
                return colorPropertyValue.Name;
                //colorPropertyValue.ToArgb();

            }
        }

        //If unknown color, fallback to the hex value
        //(or you could return null, "Unkown" or whatever you want)

        MessageBox.Show(" This color is not supported yet");
        //return ColorTranslator.ToHtml(color);
        return "Red";

    }

So if a color doesn't have a name I return 'red' and give a message that the color is not supported yet.

user3176938
  • 31
  • 1
  • 6