简体   繁体   中英

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. ie 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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM