生成指定数量的随机但独特的颜色

发布于 2024-10-25 01:53:45 字数 578 浏览 2 评论 0原文

我想做的是给 listView 中的每个项目赋予唯一的颜色。所以我所拥有的是列表视图中的项目数为“计数”

我的方法是调用下面的方法并给出项目的方法编号,然后它应该有一个数组来保存第一种颜色,然后保存下一种颜色将要生成它,应该将其与数组中之前的颜色进行比较。

问题是我无法弄清楚我需要哪种数组以及如何将数组中每个插入的颜色与最后插入的颜色进行比较。这是我的代码:

public Color GetUniqueRandomColor(int count){

for(int i = 0; i < count; i++)
{
Color.FromArgb(randomColor.Next(70, 200), randomColor.Next(100, 225), randomColor.Next(100, 230));
}

return Color.Red;}

这是元素在数组中的外观:

颜色[A=255,R=132,G=148,B=181]

如您所见,我的方法仍然缺乏所需的数组以及将新插入的颜色与其进行比较的算法。我们将提供一些帮助和提示!

What I am trying to do is give each item in my listView to have a unique color. So what I have is number of items in my list view as 'count'

My approach is to call the method below and give the method number of my items, and then it should have an array which saves the first color, then when next color is going to be generated it should be compared with colors before it in the array.

The problem is I cant figure out a way for what kind of array I am going to need and how do I compare each inserted color in the array with last inserted colors. here is my code:

public Color GetUniqueRandomColor(int count){

for(int i = 0; i < count; i++)
{
Color.FromArgb(randomColor.Next(70, 200), randomColor.Next(100, 225), randomColor.Next(100, 230));
}

return Color.Red;}

this is how an element will look in the array:

Color [A=255, R=132, G=148, B=181]

As you can see my method still lacks the needed array and the algorithm to compare new inserted colors to it. Some help and tips will be appriciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-11-01 01:53:45

使用 C# 3.5

public static Color[] GetUniqueRandomColor(int count)
{
    Color[] colors = new Color[count];
    HashSet<Color> hs = new HashSet<Color>();

    Random randomColor = new Random();

    for(int i = 0; i < count; i++)
    {
        Color color;
        while (!hs.Add(color = Color.FromArgb(randomColor.Next(70, 200), randomColor.Next(100, 225), randomColor.Next(100, 230))));
        colors[i] = color;
    }    

    return colors;
}

如果您只有 C# 2.0,您可以用 Dictionary 替换 HashSet,其中 bool 只是您不会使用的占位符,但 while 表达式会变得更复杂一点

public static Color[] GetUniqueRandomColor(int count)
{
    Color[] colors = new Color[count];
    Dictionary<Color, bool> hs = new Dictionary<Color, bool>();

    Random randomColor = new Random();

    for (int i = 0; i < count; i++)
    {
        Color color;
        while (hs.ContainsKey(color = Color.FromArgb(randomColor.Next(70, 200), randomColor.Next(100, 225), randomColor.Next(100, 230)))) ;
        hs.Add(color, true);
        colors[i] = color;
    }

    return colors;
}

With C# 3.5

public static Color[] GetUniqueRandomColor(int count)
{
    Color[] colors = new Color[count];
    HashSet<Color> hs = new HashSet<Color>();

    Random randomColor = new Random();

    for(int i = 0; i < count; i++)
    {
        Color color;
        while (!hs.Add(color = Color.FromArgb(randomColor.Next(70, 200), randomColor.Next(100, 225), randomColor.Next(100, 230))));
        colors[i] = color;
    }    

    return colors;
}

If you only have C# 2.0, you can substitute HashSet with a Dictionary, where the bool is only a placeholder that you won't use, but the while expression will get a little more complex

public static Color[] GetUniqueRandomColor(int count)
{
    Color[] colors = new Color[count];
    Dictionary<Color, bool> hs = new Dictionary<Color, bool>();

    Random randomColor = new Random();

    for (int i = 0; i < count; i++)
    {
        Color color;
        while (hs.ContainsKey(color = Color.FromArgb(randomColor.Next(70, 200), randomColor.Next(100, 225), randomColor.Next(100, 230)))) ;
        hs.Add(color, true);
        colors[i] = color;
    }

    return colors;
}
秋意浓 2024-11-01 01:53:45

我将填充“使用的颜色”列表,因此每次在调用 Color.FromArgb 之前生成新颜色时,您都可以在列表中检查它。如果颜色存在,您将再次调用随机函数,否则您将生成颜色并将值添加到列表中。

I will populate a list of "used color", so everytime you generate a new color before calling Color.FromArgb you can check it in your list. If the color exists you will call again the random function else you generate the color and add the value to the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文