Python中的 colorsys 模块和示例
Python中的colorsys模块定义了RGB(红绿蓝)颜色与其他三坐标YIQ(亮度(Y)同相正交)、HLS(色相亮度饱和度)和HSV(色相饱和度值)之间的双向转换。
colorsys 模块定义了以下函数:
- colorsys.rgb_to_yiq(r, g, b): It convert the color from RGB coordinates to YIQ coordinates.
- colorsys.yiq_to_rgb(y, i, q): It convert the color from YIQ coordinates to RGB coordinates.
- colorsys.rgb_to_hls(r, g, b): It convert the color from RGB coordinates to HLS coordinates.
- colorsys.hls_to_rgb(h, l, s): It convert the color from HLS coordinates to RGB coordinates.
- colorsys.rgb_to_hsv(r, g, b): It convert the color from RGB coordinates to HSV coordinates.
- colorsys.hsv_to_rgb(h, s, v): It convert the color from HSV coordinates to RGB coordinates.
除colorsys.yiq_to_rgb()
之外的所有上述函数都接受范围在 0 和 1 之间的浮点值作为其参数。在函数colorsys.yiq_to_rgb(y, i, q)
中,参数y是介于 0 和 1 之间的浮点值,参数i和q也接受介于 0 和 1 之间的浮点值,但它可以是正数或负数。
上述所有函数都返回一个表示结果坐标的元组。
代码 #1:将颜色从 RGB 坐标转换为 YIQ 坐标。
# Python program to explain colorsys.rgb_to_yiq() method
# importing colorsys module
import colorsys
# Define RGB coordinates
r = 0.2
g = 0.4
b = 0.4
# Convert the color from RGB
# coordinates to YIQ coordinates
yiq = colorsys.rgb_to_yiq(r, g, b)
# Print the yiq coordinates
print(yiq)
输出:
(0.33999999999999997, -0.11979999999999999, -0.04259999999999996)
代码 #2:将颜色从 YIQ 坐标转换为 RGB 坐标。
# Python program to explain colorsys.yiq_to_rgb() method
# importing colorsys module
import colorsys
# Define YIQ coordinates
y = 0.34
i = -0.12
q = -0.04
# Convert the color from RGB
# coordinates to YIQ coordinates
rgb = colorsys.yiq_to_rgb(y, i, q)
# Print the RGB coordinates
print(rgb)
输出:
(0.20143187066974597, 0.3984021607233726, 0.40466512702078516)
代码 #3:将颜色从 RGB 坐标转换为 HLS 坐标。
# Python program to explain colorsys.rgb_to_hls() method
# importing colorsys module
import colorsys
# Define RGB coordinates
r = 0.2
g = 0.4
b = 0.4
# Convert the color from RGB
# coordinates to HLS coordinates
hls = colorsys.rgb_to_hls(r, g, b)
# Print the HLS coordinates
print(hls)
输出:
(0.5, 0.30000000000000004, 0.3333333333333333)
代码 #4:将颜色从 HLS 坐标转换为 RGB 坐标。
# Python program to explain colorsys.hls_to_rgb() method
# importing colorsys module
import colorsys
# Define HLS coordinates
h = 0.2
l = 0.7
s = 0.5
# Convert the color from HLS
# coordinates to RGB coordinates
rgb = colorsys.hls_to_rgb(h, l, s)
# Print the RGB coordinates
print(rgb)
输出:
(0.7899999999999999, 0.85, 0.5499999999999999)
代码 #5:将颜色从 RGB 坐标转换为 HSV 坐标。
# Python program to explain colorsys.rgb_to_hsv() method
# importing colorsys module
import colorsys
# Define RGB coordinates
r = 0.2
g = 0.4
b = 0.4
# Convert the color from RGB
# coordinates to HSV coordinates
hsv = colorsys.rgb_to_hsv(r, g, b)
# Print the HSV coordinates
print(hsv)
输出:
(0.5, 0.5, 0.4)
代码 #6:将颜色从 HSV 坐标转换为 RGB 坐标。
# Python program to explain colorsys.hsv_to_rgb() method
# importing colorsys module
import colorsys
# Define HSV coordinates
h = 0.5
s = 0.5
v = 0.4
# Convert the color from HSV
# coordinates to RGB coordinates
rgb = colorsys.hsv_to_rgb(h, s, v)
# Print the RGB coordinates
print(rgb)
输出:
(0.2, 0.4, 0.4)