如何从System.Drawing.Color中选择一种随机颜色? [英] How to choose a random color from System.Drawing.Color?

查看:173
本文介绍了如何从System.Drawing.Color中选择一种随机颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要一种方法从

 System.Drawing.Color 

中选择随机颜色,拜托,有人帮我...

解决方案

假设您想从System.Drawing中的已知颜色中随机选择:

  private 列表与LT; knowncolor> colorList; 
private 随机rand;
private int maxColorIndex;

private KnownColor getRandomColor()
{
return colorList [rand.Next( 0 ,maxColorIndex)];
}

private TestRandomKnownColor()
{
// 从使用索引编制所需的IEnumerable转换

// 此列表将包括所有颜色,包括OS系统颜色
colorList = Enum.GetValues( typeof (KnownColor))
.Cast< knowncolor>()
.ToList();

// 如果您只想要Microsoft命名的颜色,例如'AliceBlue
// 而不是OS主题定义的颜色,例如'ActiveBorder
// 使用它来构建列表
// colorList = Enum.GetValues(typeof(KnownColor))
// 。Cast< KnownColor>()
// 。其中(clr =>!(Color.FromKnownColor(clr).IsSystemColor))
// 。ToList();

rand = new Random(DateTime.Now.Ticks.GetHashCode( ));

maxColorIndex = colorList.Count();

for int i = 0 ; i < 10 ; i ++)
{
KnownColor randColor = getRandomColor();
Console.WriteLine(randColor);
}
}


颜色有RGB数据,所有值(红色,绿色和蓝色)都是0到255之间的整数(包括0和255),你可以使用 Random 类生成0到255之间的三个整数。

随机rand =  new  Random(); 
int max = byte .MaxValue + 1 ; // 256
int r = rand.Next(max);
int g = rand.Next(max);
int b = rand.Next(max);
颜色c = Color.FromArgb(r,g,b);



rand.Next(max); 返回0到255之间的整数,但我们将 max (= 256)作为参数传递,因为的参数为rand.Next 是独占的上限,所以如果我们在这里使用256,它将包括255,但不包括256.



关于 Random.Next : http://msdn.microsoft。 com / en-us / library / System.Random.Next.aspx [ ^ ]


您的回答如何在C#中生成随机颜色名称 [ ^ ]

Hello everyone, I need a way to choose a random color from

System.Drawing.Color 

, please, anyone help me...

解决方案

Assuming you want a random selection from the Known Colors in System.Drawing:

private List<knowncolor> colorList;
private Random rand;
private int maxColorIndex;

private KnownColor getRandomColor()
{
    return colorList[rand.Next(0, maxColorIndex)];
}

private TestRandomKnownColor()
{
    // conversion from IEnumerable necessary to use indexing

    // this list will include all Colors including OS system colors
    colorList = Enum.GetValues(typeof(KnownColor))
       .Cast<knowncolor>()
       .ToList();

    // if you want only the Microsoft named colors, like 'AliceBlue
    // and not the colors defined by the OS theme, like 'ActiveBorder
    // use this to build the list
    //colorList = Enum.GetValues(typeof(KnownColor))
        //.Cast<KnownColor>()
        //.Where(clr => ! (Color.FromKnownColor(clr).IsSystemColor))
        //.ToList();

    rand = new Random(DateTime.Now.Ticks.GetHashCode());

    maxColorIndex = colorList.Count();

    for (int i = 0; i < 10; i++)
    {
        KnownColor randColor = getRandomColor();
        Console.WriteLine(randColor);
    }
}


As a color has RGB data and all values (red, green and blue) are integers from 0 to 255 (0 and 255 included), you can use the Random class to generate three integers between 0 and 255.

Random rand = new Random();
int max = byte.MaxValue + 1; // 256
int r = rand.Next(max);
int g = rand.Next(max);
int b = rand.Next(max);
Color c = Color.FromArgb(r, g, b);


rand.Next(max); returns an integer from 0 to 255, but we pass max (= 256) as argument because the argument of rand.Next is the exclusive upper bound, so if we use 256 here, it will include 255, but not 256.

More information about Random.Next: http://msdn.microsoft.com/en-us/library/System.Random.Next.aspx[^]


Your answer at How to generate random color names in C#[^].


这篇关于如何从System.Drawing.Color中选择一种随机颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆