colorsys — 在颜色系统之间转换

源代码: 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). 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. In all other spaces, the coordinates are all between 0 and 1.

另请参阅

可找到色彩空间的更多有关信息在 https://poynton.ca/ColorFAQ.html and https://www.cambridgeincolour.com/tutorials/color-spaces.htm .

The colorsys 模块定义了下列函数:

colorsys. rgb_to_yiq ( r , g , b )

将颜色从 RGB 坐标转换成 YIQ 坐标。

colorsys. yiq_to_rgb ( y , i , q )

将颜色从 YIQ 坐标转换成 RGB 坐标。

colorsys. rgb_to_hls ( r , g , b )

将颜色从 RGB 坐标转换成 HLS 坐标。

colorsys. hls_to_rgb ( h , l , s )

将颜色从 HLS 坐标转换成 RGB 坐标。

colorsys. rgb_to_hsv ( r , g , b )

将颜色从 RGB 坐标转换成 HSV 坐标。

colorsys. hsv_to_rgb ( h , s , v )

将颜色从 HSV 坐标转换成 RGB 坐标。

范例:

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