Python中的colorsys模块
此模块允许在RGB(红色绿色蓝色)表示的颜色与其他颜色空间之间进行颜色值的双向转换。它使用的其他三个色彩空间是YIQ(亮度(Y)同相正交),HLS(色相饱和度)和HSV(色相饱和度)。除了YIQ颜色空间中的I和Q值之外,所有坐标都可以在0和1之间。
下表显示了功能及其用途。
示例
import colorsys as csys # "Electric Blue" r, g, b = 0.47, 0.91, 1.00 print("The RGB Values for Electric Blue: ", (r, g, b)) # y, i, q = csys.rgb_to_yiq(r, g, b) print("YIQ", (y, i, q), "becomes", csys.yiq_to_rgb(y, i, q)) h, s, v = csys.rgb_to_hsv(r, g, b) print("HSV", (h, s, v), "becomes", csys.hsv_to_rgb(h, s, v)) h, l, s = csys.rgb_to_hls(r, g, b) print("HLS", (h, l, s), "becomes", csys.hls_to_rgb(h, l, s))
输出结果
运行上面的代码将为我们提供以下结果:
The RGB Values for Electric Blue: (0.47, 0.91, 1.0) YIQ (0.7879, -0.292513, -0.06563100000000005) becomes (0.47, 0.9100000000000001, 1.0) HSV (0.5283018867924528, 0.53, 1.0) becomes (0.47, 0.9099999999999999, 1.0) HLS (0.5283018867924528, 0.735, 1.0) becomes (0.4700000000000001, 0.9099999999999998, 0.9999999999999999)