Python 生成随机颜色 (RGB)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28999287/
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
Generate random colors (RGB)
提问by Travis A.
I just picked up image processing in python this past week at the suggestion of a friend to generate patterns of random colors. I found this piece of script online that generates a wide array of different colors across the RGB spectrum.
上周我刚刚在朋友的建议下使用 python 进行图像处理,以生成随机颜色的图案。我在网上找到了这段脚本,它在 RGB 光谱中生成了大量不同的颜色。
def random_color():
levels = range(32,256,32)
return tuple(random.choice(levels) for _ in range(3))
I am simply interesting in appending this script to only generate one of three random colors. Preferably red, green, and blue.
我只是有趣地附加此脚本以仅生成三种随机颜色中的一种。最好是红色、绿色和蓝色。
采纳答案by Fran Borcic
Here:
这里:
def random_color():
rgbl=[255,0,0]
random.shuffle(rgbl)
return tuple(rgbl)
The result is either red, green or blue. The method is not applicable to other sets of colors though, where you'd have to build a list of all the colors you want to choose from and then use random.choice to pick one at random.
结果是红色、绿色或蓝色。但是,该方法不适用于其他颜色集,您必须在其中构建要从中选择的所有颜色的列表,然后使用 random.choice 随机选择一个。
回答by Hugo
With custom colours (for example, dark red, dark green and dark blue):
使用自定义颜色(例如,深红色、深绿色和深蓝色):
import random
COLORS = [(139, 0, 0),
(0, 100, 0),
(0, 0, 139)]
def random_color():
return random.choice(COLORS)
回答by varagrawal
A neat way to generate RGB triplets within the 256 (aka 8-byte) range is
在 256(又名 8 字节)范围内生成 RGB 三元组的一种巧妙方法是
color = list(np.random.choice(range(256), size=3))
color = list(np.random.choice(range(256), size=3))
color
is now a list of size 3 with values in the range 0-255. You can save it in a list to record if the color has been generated before or no.
color
现在是大小为 3 的列表,值在 0-255 范围内。您可以将其保存在列表中以记录该颜色之前是否已生成。
回答by Khalil Al Hooti
You could also use Hex Color Code,
您也可以使用十六进制颜色代码,
Name Hex Color Code RGB Color Code
Red #FF0000 rgb(255, 0, 0)
Maroon #800000 rgb(128, 0, 0)
Yellow #FFFF00 rgb(255, 255, 0)
Olive #808000 rgb(128, 128, 0)
For example
例如
import matplotlib.pyplot as plt
import random
number_of_colors = 8
color = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])
for i in range(number_of_colors)]
print(color)
['#C7980A', '#F4651F', '#82D8A7', '#CC3A05', '#575E76', '#156943', '#0BD055', '#ACD338']
['#C7980A'、'#F4651F'、'#82D8A7'、'#CC3A05'、'#575E76'、'#156943'、'#0BD055'、'#ACD338']
Lets try plotting them in a scatter plot
让我们尝试在散点图中绘制它们
for i in range(number_of_colors):
plt.scatter(random.randint(0, 10), random.randint(0,10), c=color[i], s=200)
plt.show()
回答by Rahul Kumar
color = lambda : [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
回答by Shital Shah
Inspired by other answers this is more correct code that produces integer 0-255 values and appends alpha=255 if you need RGBA:
受其他答案的启发,这是更正确的代码,它生成整数 0-255 值并在需要 RGBA 时附加 alpha=255:
tuple(np.random.randint(256, size=3)) + (255,)
If you just need RGB:
如果您只需要 RGB:
tuple(np.random.randint(256, size=3))