Conversions between color systems using Python (colorsys)

PythonServer Side ProgrammingProgramming

The RGB color model, named so because of the initials of the three additive primary colors, is an additive color model in which red, green and blue light are added to reproduce various colors.

The RGB color model is used in representation and display of images in electronic systems, such as televisions and computers. It is based on human perception of colors. Other alternative representations of color model are:

YIQ: Luminance, Chrominance (used by composite video signals)

HLS: Hue, Luminance, Saturation

HSV: Hue, Saturation, Value

The colorsys module defines functions for conversion of color values between RGB color model and three other coordinate systems. In the YIQ model, the Y value is between 0 and 1, but the I and Q values may be positive or negative. In RGB, HLS and HSV model, the values are all between 0 and 1.

This module provides two functions for each color system to RGB and vice versa.

rgb_to_yiq()Convert RGB to YIQ
yiq_to_rgb()Convert YIQ to RGB
rgb_to_hls()Convert RGB to HLS
hls_to_rgb()Convert HLS to RGB
rgb_to_hsv()Convert RGB to HSV
hsv_to_rgb()Convert HSV to RGB

Example

>>> import colorsys
>>> r,g,b=1, 0.753, 0.80
>>> colorsys.rgb_to_hls(r,g,b)
(0.9682860998650472, 0.8765000000000001, 1.0)
>>> colorsys.rgb_to_hsv(r,g,b)
(0.9682860998650472, 0.247, 1)
>>> colorsys.rgb_to_yiq(r,g,b)
(0.83227, 0.1328331, 0.06727970000000007)
raja
Published on 20-Feb-2019 15:46:52
Advertisements