1

So i want to make a function where you put in a color (like White, Red, Aquamarine, etc) and it sets the color of the rectangle to the color you have putten in. What is the best way to do this?

I got this:

public void setVisible(GraphicsDevice gd ,SpriteBatch sb, object c) {

        rec = new Rectangle(ButtonXPosition,ButtonYPosition,ButtonWidthSize,ButtonHeightSize);
        Texture2D pixel = new Texture2D(gd, ButtonWidthSize,ButtonHeightSize);
        sb.Draw(pixel,rec,Color.c);
    }
Dallox
  • 487
  • 5
  • 8
  • 19
  • 1
    http://stackoverflow.com/questions/2290262/search-for-a-string-in-enum-and-return-the-enum Here you go. – Ryan Bennett Dec 07 '12 at 17:12
  • 1
    @RyanBennett [XNA Color](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.color_members%28v=xnagamestudio.31%29.aspx) is not an enum, it is a struct with static properties representing preset values. – neeKo Dec 07 '12 at 20:44

1 Answers1

5

You can retrieve the preset colors by name with a bit of reflection:

var prop = typeof(Color).GetProperty(nameOfColor);
if (prop != null)
    return (Color)prop.GetValue(null, null);
return default(Color);
Cole Campbell
  • 4,846
  • 1
  • 17
  • 20