colorsys module in Python with examples - GeeksforGeeks

22 May, 2019
The colorsys module in Python defines bidirectional conversions of color values between RGB (Red Green Blue) color and other three coordinate YIQ (Luminance (Y) In-phase Quadrature), HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value).
The colorsys module defines the following functions:
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.
All above function except colorsys.yiq_to_rgb() accepts floating point values having range between 0 and 1 as their parameter. In function colorsys.yiq_to_rgb(y, i, q), parameter y is a floating value in range between 0 and 1 and parameter i and q also accepts floating value in range between 0 and 1 but it can be positive or negative.
All the above function returns a tuple which represents the resultant coordinate.
# Python program to explain colorsys.rgb_to_yiq() method      # importing colorsys module import colorsys  # Define RGB coordinatesr = 0.2g = 0.4b = 0.4  # Convert the color from RGB # coordinates to YIQ coordinatesyiq = colorsys.rgb_to_yiq(r, g, b)  # Print the yiq coordinatesprint(yiq)
(0.33999999999999997, -0.11979999999999999, -0.04259999999999996)

 
Code #2: Convert the color from YIQ coordinates to RGB coordinates.
# Python program to explain colorsys.yiq_to_rgb() method      # importing colorsys module import colorsys  # Define YIQ coordinatesy = 0.34i = -0.12q = -0.04  # Convert the color from RGB # coordinates to YIQ coordinatesrgb = colorsys.yiq_to_rgb(y, i, q)  # Print the RGB coordinatesprint(rgb)
(0.20143187066974597, 0.3984021607233726, 0.40466512702078516)

 
Code #3: Convert the color from RGB coordinates to HLS coordinates.
# Python program to explain colorsys.rgb_to_hls() method      # importing colorsys module import colorsys  # Define RGB coordinatesr = 0.2g = 0.4b = 0.4  # Convert the color from RGB # coordinates to HLS coordinateshls = colorsys.rgb_to_hls(r, g, b)  # Print the HLS coordinatesprint(hls)
(0.5, 0.30000000000000004, 0.3333333333333333)

 
Code #4: Convert the color from HLS coordinates to RGB coordinates.
# Python program to explain colorsys.hls_to_rgb() method      # importing colorsys module import colorsys  # Define HLS coordinatesh = 0.2l = 0.7s = 0.5  # Convert the color from HLS # coordinates to RGB coordinatesrgb = colorsys.hls_to_rgb(h, l, s)  # Print the RGB coordinatesprint(rgb)
(0.7899999999999999, 0.85, 0.5499999999999999)

 
Code #5: Convert the color from RGB coordinates to HSV coordinates.
# Python program to explain colorsys.rgb_to_hsv() method      # importing colorsys module import colorsys  # Define RGB coordinatesr = 0.2g = 0.4b = 0.4  # Convert the color from RGB # coordinates to HSV coordinateshsv = colorsys.rgb_to_hsv(r, g, b)  # Print the HSV coordinatesprint(hsv)
(0.5, 0.5, 0.4)

 
Code #6: Convert the color from HSV coordinates to RGB coordinates.
# Python program to explain colorsys.hsv_to_rgb() method      # importing colorsys module import colorsys  # Define HSV coordinatesh = 0.5s = 0.5v = 0.4  # Convert the color from HSV # coordinates to RGB coordinatesrgb = colorsys.hsv_to_rgb(h, s, v)  # Print the RGB coordinatesprint(rgb)
(0.2, 0.4, 0.4)

python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python OOPs Concepts
How to Install PIP on Windows ?
Bar Plot in Matplotlib
Defaultdict in Python
Python Classes and Objects
Deque in Python
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python - Ways to remove duplicates from list
Class method vs Static method in Python