C# 如何创建代表颜色的随机十六进制字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/730625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a random hex string that represents a color?
提问by rahkim
I'm generating some charts that need a hex string for the colors.
我正在生成一些需要十六进制字符串作为颜色的图表。
Example:
例子:
<dataseries name="ford" color="FF00FF" />
I'm creating these dynamically, so I would like to generate the hex code for each dataseries randomly.
我正在动态创建这些,所以我想为每个数据序列随机生成十六进制代码。
What is the best way to do this?
做这个的最好方式是什么?
采纳答案by Samuel
Easiest way is to use String.Format
and use the hexadecimal format for the argument.
最简单的方法是使用String.Format
和使用参数的十六进制格式。
var random = new Random();
var color = String.Format("#{0:X6}", random.Next(0x1000000)); // = "#A197B9"
回答by Chris Doggett
Random rand = new Random(); // specify a seed
int r = rand.Next(0x1000000);
Console.WriteLine("#{0:X6}", r);
回答by Canton
IMO, purely random colours may not be preferable as you need colours that are distinguishable in human eyes.
IMO,纯随机颜色可能并不可取,因为您需要在人眼中可区分的颜色。
What about presetting a number of colours and picking them randomly?
预先设置一些颜色并随机选择它们怎么样?
Perhaps you could find better answers in some open source charting libraries.
也许您可以在一些开源图表库中找到更好的答案。
回答by John Rasch
Samuel's answer is the best way to do this, just make sure that if you're generating the colors inside a loop that you don't instantiate a new Random
object each time because new Random()
seeds the generator using the system clock. Your loop is going to run faster than the clock can tick, so you'll end up generating several of the same colors over and over because random
is being seeded with the same value.
Samuel 的答案是最好的方法,只要确保在循环内生成颜色时,不要Random
每次都实例化一个新对象,因为new Random()
使用系统时钟为生成器设置种子。您的循环将运行得比时钟滴答作响的速度更快,因此您最终会一遍又一遍地生成几种相同的颜色,因为random
它们使用相同的值进行播种。
It should look something like this:
它应该是这样的:
int numColors = 10;
var colors = new List<string>();
var random = new Random(); // Make sure this is out of the loop!
for (int i = 0; i < numColors; i++)
{
colors.Add(String.Format("#{0:X6}", random.Next(0x1000000)));
}
instead of:
代替:
int numColors = 10;
var colors = new List<string>();
for (int i = 0; i < numColors; i++)
{
var random = new Random(); // Don't put this here!
colors.Add(String.Format("#{0:X6}", random.Next(0x1000000)));
}
回答by Greg
A good way of generating a nice set of colours is to define them using fixed saturation and brightness and vary the hue.
生成一组漂亮颜色的一个好方法是使用固定的饱和度和亮度来定义它们并改变色调。
- Set saturation and brightness to something you like, say 50% saturation and 90% brightness.
- Now divide the 360 degrees of hue by the number of distinct colours you want.
- Pick colours from HSV using that interval for hue, and the fixed S and V.
- 将饱和度和亮度设置为您喜欢的值,例如 50% 饱和度和 90% 亮度。
- 现在将 360 度的色调除以您想要的不同颜色的数量。
- 使用该色调间隔以及固定的 S 和 V 从 HSV 中挑选颜色。
This gives you a nice set of colours, which all look like they came from the same 'set' -- all pastel, or all intense, or all off-white, whatever. And it's pretty easy to code if you've got Color.FromHSV().
这为您提供了一组漂亮的颜色,它们看起来都来自同一个“组”——全是柔和的,或全是强烈的,或全是灰白色,等等。如果您有 Color.FromHSV(),则编码非常容易。
It probably stops working once you get too many colours though, they'll be indistinguishable. But you'll probably get that problem anyway.
但是,一旦您获得太多颜色,它可能会停止工作,它们将无法区分。但无论如何你可能会遇到这个问题。
In pseudo code:
在伪代码中:
Sat = 0.5 * 255 //assuming we want range 0-255...
Brightness = 0.9 * 255
NumberOfColours = 7
HueSeparation = 360 / 7
Colors = []
for (i = 0 ; i< NumberOfColours; i++)
Colors.Add(Color.FromHSV(HueSeparation * i, Sat, Brightness)
or
或者
n = 7
Colors = [Color.FromHSV(x*360/n, 128, 230) for x in range(n)]
(I do like list comprehensions...)
(我确实喜欢列表推导式...)
回答by Beska
I noticed that you (Rahkim) commented on Greg's post that you wish you could put his idea (keeping the saturation and value constant, and just varying the hue...a good idea) into code. You can! Or, rather, someone already has for you!
我注意到你 (Rahkim) 评论了 Greg 的帖子,你希望你能把他的想法(保持饱和度和值不变,只是改变色调......一个好主意)到代码中。你可以!或者,更确切地说,有人已经为您准备好了!
I found this blog post on Converting HSV to RGB colour using C#, and I'm sure there are more out there. You'll probably end up with a nicer suite of colors this way than by picking them totally randomly.
我找到了这篇关于使用 C# 将 HSV 转换为 RGB 颜色的博客文章,我相信还有更多。与完全随机选择它们相比,您可能会以这种方式最终得到一套更好的颜色。
Additionally, of course, this method makes it simple to get a nice set of colors...since the Hue goes from 0-359, you could do something like set your Hue something like this:
此外,当然,此方法使获得一组漂亮的颜色变得简单……由于 Hue 从 0 到 359,您可以执行类似设置 Hue 的操作,如下所示:
Hue = (PreviousHue + 50) % 360;
(I picked 50 since it doesn't go into 360 evenly, so if you go beyond 360 you won't start repeating hues instantly. You'd have to toy around with the value to get an ideal separation depending on how many different colors you're expecting.)
(我选择了 50,因为它不会均匀地进入 360,所以如果你超过 360,你就不会立即开始重复色调。你必须根据不同颜色的数量来调整这个值以获得理想的分离度你在期待。)
This way you don't have to worry about the case where the code is randomly picking two colors that are super close to one another when there is still lots of unused "hue" space.
这样您就不必担心代码会随机选择两种非常接近的颜色,而此时仍有大量未使用的“色调”空间。