colorsysConversions between color systems颜色系统之间的转换

Source code: Lib/colorsys.py


The colorsys module defines bidirectional conversions of color values between colors expressed in the RGB (Red Green Blue) color space used in computer monitors and three other coordinate systems: YIQ, HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value). colorsys模块定义了在计算机显示器中使用的RGB(红-绿-蓝)颜色空间中表示的颜色与其他三个坐标系之间的颜色值的双向转换:YIQ、HLS(色调亮度饱和度)和HSV(色调饱和度值)。Coordinates in all of these color spaces are floating point values. 所有这些颜色空间中的坐标都是浮点值。In the YIQ space, the Y coordinate is between 0 and 1, but the I and Q coordinates can be positive or negative. 在YIQ空间中,Y坐标在0和1之间,但I和Q坐标可以为正或负。In all other spaces, the coordinates are all between 0 and 1.在所有其他空间中,坐标都在0和1之间。

The colorsys module defines the following functions:colorsys模块定义了以下功能:

colorsys.rgb_to_yiq(r, g, b)

Convert the color from RGB coordinates to YIQ coordinates.将颜色从RGB坐标转换为YIQ坐标。

colorsys.yiq_to_rgb(y, i, q)

Convert the color from YIQ coordinates to RGB coordinates.将颜色从YIQ坐标转换为RGB坐标。

colorsys.rgb_to_hls(r, g, b)

Convert the color from RGB coordinates to HLS coordinates.将颜色从RGB坐标转换为HLS坐标。

colorsys.hls_to_rgb(h, l, s)

Convert the color from HLS coordinates to RGB coordinates.将颜色从HLS坐标转换为RGB坐标。

colorsys.rgb_to_hsv(r, g, b)

Convert the color from RGB coordinates to HSV coordinates.将颜色从RGB坐标转换为HSV坐标。

colorsys.hsv_to_rgb(h, s, v)

Convert the color from HSV coordinates to RGB coordinates.将颜色从HSV坐标转换为RGB坐标。

Example:例子:

>>> import colorsys
>>> colorsys.rgb_to_hsv(0.2, 0.4, 0.4)
(0.5, 0.5, 0.4)
>>> colorsys.hsv_to_rgb(0.5, 0.5, 0.4)
(0.2, 0.4, 0.4)