I'm looking for a method to pick a random Brush
in Brushes
collection (Aqua,Azure, ...Black,...). Any clue?
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.
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.
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)];
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)));
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;
}
This should work:
Brush RandomBrush()
{
PropertyInfo[] brushInfo = typeof(Brushes).GetProperties();
Brush[] brushList = new Brush[brushInfo.Length];
for(int i = 0;i < brushInfo.Length;i++)
{
brushList[i] = (Brush)brushInfo[i].GetValue(null, null);
}
Random randomNumber = new Random(DateTime.Now.Second);
return brushList[randomNumber.Next(1, brushList.Length)];
}
To get WPF default brushes:
var brushes = typeof(Brushes).GetProperties(BindingFlags.Public | BindingFlags.Static).Select(pi => (Brush)pi.GetValue(null, null));
© 2022 - 2024 — McMap. All rights reserved.
Color myColor = Color.FromRgb(randomByte1, randomByte2, randomByte3); SolidColorBrush myBrush = new SolidColorBrush(myColor);
(though admittedly the second suggestion in this answer is much neater) Thanks! – Phlebosclerosis