Mechanik
Mechanik

Reputation: 679

Random Color Generation in particular range

I need to generate a random color for each item in my gridview. My requirement is that I must generate these colors between, for example, one shade of blue and another shade of blue. i.e. all colors must be within that range. I have a start color and an end color and I have a random object to generate bytes. How do I ensure that the colors generated stay within the given range? I need to write this in C# for Windows 8. The only color generation option it shows me in Windows.UI.Color is the FromArgb method. Any ideas on how I could achieve that?

Upvotes: 2

Views: 3050

Answers (2)

Jodrell
Jodrell

Reputation: 35706

Ok, given your limited example,

static Color RandomBlue(byte minBlue, byte maxBlue, byte red, byte green)
{
    var blue = Random.Next(minBlue, maxBlue);
    return Color.FromArgb(red, green, blue);
}

will generate a Color or random blueness within a range.

Upvotes: 2

Marco
Marco

Reputation: 366

Random random = new Random();
byte randomNumber = (byte) random.Next(20, 256);
Color blueish = Color.FromRgb(20, 20, randomNumber);

Should give you a blueish color between 20 and 255 with fixed red and green value's

Upvotes: 9

Related Questions