HSV到RGB颜色转换 [英] HSV to RGB Color Conversion

查看:322
本文介绍了HSV到RGB颜色转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用python中的pygame模块将HSV颜色参数转换为RGB类型颜色参数?我尝试了以下代码,但它返回了荒谬的值。

Is there a way to convert HSV color arguments to RGB type color arguments using pygame modules in python? I tried the following code, but it returns ridiculous values.

import colorsys
test_color = colorsys.hsv_to_rgb(359, 100, 100)
print(test_color)

,此代码返回以下废话

(100, -9900.0, -9900.0)

这显然不是RGB。我在做什么错?

This obviously isn't RGB. What am I doing wrong?

推荐答案

该函数期望 s 为十进制(饱和度)和 v (值),而不是百分比。除以100。

That function expects decimal for s (saturation) and v (value), not percent. Divide by 100.

>>> import colorsys

# Using percent, incorrect
>>> test_color = colorsys.hsv_to_rgb(359,100,100)
>>> test_color
(100, -9900.0, -9900.0)

# Using decimal, correct
>>> test_color = colorsys.hsv_to_rgb(1,1,1)
>>> test_color
(1, 0.0, 0.0)

如果您想要非归一化RGB元组,这是一个包装 colorsys 函数的函数。

If you would like the non-normalized RGB tuple, here is a function to wrap the colorsys function.

def hsv2rgb(h,s,v):
    return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h,s,v))

示例功能

>>> hsv2rgb(0.5,0.5,0.5)
(64, 128, 128)

这篇关于HSV到RGB颜色转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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