Reputation: 125
I have a rectangle filled with a color(it's a different color each time). I want to be able to get the name of the color-for example "Green", through the color of the filling on that rectangle. Does anyone know how can I do that? I have tried the following :
Brush a = rect.Fill;
Color color = ((SolidColorBrush)a).Color;
string thecolor = color.ToString();
switch (thecolor)
{
case "Red": furText.SelectedIndex = 0; break;
case "Blue": furText.SelectedIndex = 1; break;
case "Yellow": furText.SelectedIndex = 2; break;
case "Pink": furText.SelectedIndex = 3; break;
case "Green" : furText.SelectedIndex = 4;break;
}
I am basically trying to change my combobox's selected default item to be that of the color of the filling of the rectangle. by debugging I could see my code can infact get the brush, and even convert it to solid colorbrushs, however the solid color brush is in hex format, and I need the name itself. Does anyone know what to do?
Upvotes: 0
Views: 444
Reputation: 1882
You can try this it works fine. your color name will be in selectedcolorname
Brush a = rect.Fill;
Color color = ((SolidColorBrush)a).Color;
string selectedcolorname;
foreach (var colorvalue in typeof(Colors).GetRuntimeProperties())
{
if ((Color)colorvalue.GetValue(null) == color)
{
selectedcolorname = colorvalue.Name;
}
}
Upvotes: 1