生成指定数量的随机但独特的颜色
我想做的是给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 C# 3.5
如果您只有 C# 2.0,您可以用 Dictionary 替换 HashSet,其中 bool 只是您不会使用的占位符,但 while 表达式会变得更复杂一点
With C# 3.5
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
我将填充“使用的颜色”列表,因此每次在调用 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.