Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick a Random Brush

Tags:

c#

.net

wpf

brushes

I'm looking for a method to pick a random Brush in Brushes collection (Aqua,Azure, ...Black,...). Any clue?

like image 267
ojsim Avatar asked May 21 '11 20:05

ojsim


5 Answers

You can use a bit of reflection, like so:

private Brush PickBrush()
{
    Brush result = Brushes.Transparent;

    Random rnd = new Random();

    Type brushesType = typeof(Brushes);

    PropertyInfo[] properties = brushesType.GetProperties();

    int random = rnd.Next(properties.Length);
    result = (Brush)properties[random].GetValue(null, null);

    return result;
}

That will do the trick. You may want to change the randomisation to use an external Random instance, instead of re-creating a new seed each time the method is called, as in my example.

like image 112
Edwin Groenendaal Avatar answered Nov 10 '22 18:11

Edwin Groenendaal


If you simply want random colors, any random colors, just use a Random object to generate (a)rgb values between 0 and 255.

If you actually want a named color (Brush) you could store all of the predefined values in a lookup table and randomly generate an index into it. As Brushes is a class (as opposed to an `enum') it gets a bit trickier to randomly fetch a static property, but you could use reflection to do it. Store all of the property names via reflection in a lookup table and then use reflection once again to get the value of the property that corresponds to the stored name.

private List<Brush> _brushes;
private void InitBrushes()
{
    _brushes = new List<Brush>();
    var props = typeof(Brushes).GetProperties( BindingFlags.Public | BindingFlags.Static );
    foreach( var propInfo in props )
    {
        _brushes.Add( (Brush)propInfo.GetValue( null, null ) );
    }
}

And to get a random Brush...

private Random _rand = new Random();
private Brush GetRandomBrush()
{
   return _brushes[_rand.Next(_brushes.Count)];
}

I hope I didn't make any errors here, I'm on my phone and can't test it out, but you get the general idea.

like image 23
Ed S. Avatar answered Nov 10 '22 19:11

Ed S.


The Brushes is not a collection, it's a class with a lot of static properties. You could pick out the static properties with reflection, but I would suggest that you just create an array with the brushes that you want:

Brush[] brushes = new Brush[] {
  Brushes.AliceBlue,
  Brushes.AntiqueWhite,
  Brushes.Aqua,
  ...
  Brushes.YellowGreen
};

Then you can easily pick one by random:

Random rnd = new Random();
Brush brush = brushes[rnd.Next(brushes.Length)];
like image 7
Guffa Avatar answered Nov 10 '22 19:11

Guffa


Random rand = new Random();
Brush brush = new SolidColorBrush(Color.FromRgb((byte) rand.Next(0, 256), (byte) rand.Next(0, 256), (byte) rand.Next(0, 256)));
like image 3
Kyriacos Avatar answered Nov 10 '22 18:11

Kyriacos


I have added a utility method as bellow, which will return a random color.

    public static Brush getRandomBrush()
    {
        string[] brushArray = typeof(Brushes).GetProperties().
                                    Select(c => c.Name).ToArray();

        Random randomGen = new Random();
        string randomColorName = brushArray[randomGen.Next(brushArray.Length)];
        SolidColorBrush color = (SolidColorBrush)new BrushConverter().ConvertFromString(randomColorName);

        return color;
    }
like image 1
Malu MN Avatar answered Nov 10 '22 19:11

Malu MN