Pick a Random Brush
Solution 1
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.
Solution 2
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.
Solution 3
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)];
Solution 4
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)));
Solution 5
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;
}
Related videos on Youtube
ojsim
Updated on May 14, 2020Comments
-
ojsim almost 4 years
I'm looking for a method to pick a random
Brush
inBrushes
collection (Aqua,Azure, ...Black,...). Any clue? -
ojsim almost 13 yearsThanks :) Thats what i was looking for.
-
Ed S. almost 13 yearsI don't think the one time cost of using reflection would justify this approach. You also get the benefit of grabbing all properties if the class is ever modified in a future version of the framework. Is there another reason you suggest doing it this way?
-
Guffa almost 13 years@Ed S.: A lot of people seem to be using reflection as the first resort, so I suggested an alternative to show that you don't have to use reflection for everything.
-
Ed S. almost 13 yearsYeah, that's why I mentioned doing it this way as as well. That said, this is a case in which reflection seems to be the best route.
-
Guffa over 9 yearsWhy the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer.
-
ruffin about 9 yearsTo expand quickly on the first sentence & save future new-to-wpf-me's some googling,
Color myColor = Color.FromRgb(randomByte1, randomByte2, randomByte3); SolidColorBrush myBrush = new SolidColorBrush(myColor);
(though admittedly the second suggestion in this answer is much neater) Thanks!