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

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

问题描述

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

导入colorsystest_color = colorsys.hsv_to_rgb(359, 100, 100)打印(测试颜色)

并且此代码返回以下废话

(100, -9900.0, -9900.0)

这显然不是 RGB.我做错了什么?

解决方案

该函数要求 s(饱和度)和 v(值)的十进制数,而不是百分比.除以 100.

<预><代码>>>>导入颜色系统# 使用百分比,不正确>>>test_color = colorsys.hsv_to_rgb(359,100,100)>>>测试颜色(100, -9900.0, -9900.0)# 使用十进制,正确>>>test_color = colorsys.hsv_to_rgb(1,1,1)>>>测试颜色(1, 0.0, 0.0)

如果你想要非规范化的 RGB 元组,这里有一个函数来包装 colorsys 函数.

def hsv2rgb(h,s,v):返回元组(round(i * 255) for i in colorsys.hsv_to_rgb(h,s,v))

示例功能

<预><代码>>>>hsv2rgb(0.5,0.5,0.5)(64, 128, 128)

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)

and this code returns the following nonsense

(100, -9900.0, -9900.0)

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

解决方案

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)

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))

Example functionality

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

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

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