产生随机色彩(RGB) [英] Generate random colors (RGB)

查看:525
本文介绍了产生随机色彩(RGB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周,在朋友的建议下,我刚开始使用python进行图像处理,以生成随机颜色的图案.我在网上找到了这段脚本,可以在RGB光谱中生成各种各样的不同颜色.

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.

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.

推荐答案

此处:

def random_color():
    rgbl=[255,0,0]
    random.shuffle(rgbl)
    return tuple(rgbl)

结果是红色,绿色或蓝色.但是,该方法不适用于其他颜色集,在这种情况下,您必须构建要从中选择的所有颜色的列表,然后使用random.choice随机选择一种.

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.

这篇关于产生随机色彩(RGB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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