Python colorsys module – All you need to know!

Filed Under: Python Modules
Python Colorsys Module (1)

Hey, readers! In this article, we will be focusing on Python colorsys module, in detail.

So, let us begin!! 馃檪

What is Python colorsys module?

As we have seen before, with Python comes various list of modules that enables us to have standard as well as custom functionalities for data and also lets us manipulate the same according to requirement.

One such module is Python colorsys module.

Yes, as the name suggests, with colorsys module, we can have values in the form of different bi-directional color schemes. With this there would a equivalent color value for all the standard type of color schemes-

  1. (Hue Lightness Saturation)
  2. YIQ (Luminance (Y) In-phase Quadrature)
  3. HSV (Hue Saturation Value)
  4. RGB (Red, Green, Blue)

Color conversion in Python colorsys module

As discussed above, colorsys modules offers us with various functions to have bi-directional conversions between the above mentioned standard schemes.

In the context of this topic, we will be have a look at the below inter-conversions through colorsys module-

  • RGB to YIQ inter-conversion
  • HSV to RGB inter-conversion
  • RGB to HLS inter-conversion

Let us look at the functioning of each of them in the upcoming section!

1. HSV to RGB inter-conversion

In order to have inter-conversion between HSV and RGB schema, Python colorsys module offers us with the below functions–

  • hsv_to_rgb(H,S,V) function: Converts the color endpoints from HSV schema to RGB form in Python.
  • rgb_to_hsv() function: This function converts the RGB type values to equivalent HSV values easily.



rgb_to_hsv(R, G, B)

Example 01–

In this example, we have performed conversion from HSV type value to RGB points of color schemas using hsv_to_rgb() function.

import colorsys 
H = 0.2
S = 0.4
V = 0.6
RGB = colorsys.hsv_to_rgb(H, S, V) 


(0.5519999999999999, 0.6, 0.36)

Example 02–

Here, we have performed conversion from RGB type value to HSV points of color schemas using rgb_to_hsv() function.

import colorsys 
R = 0.2
G = 0.4
B = 0.6
HSV = colorsys.rgb_to_hsv(R, G, B) 


(0.5833333333333334, 0.6666666666666666, 0.6)

2. RGB to HLS inter-conversion

In order to have interconversions between RGB and HLS color schemes, we make use of the below two functions offered by Python colorsys module–

  • rgb_to_hls(R, G, B) function: This function enables us to convert the RGB scheme values into equivalent HLS form.
  • hls_to_rgb(H, L, S) function: With this function, we can convert the standard HLS color scheme to RGB format.


rgb_to_hls(R, G, B)

hls_to_rgb(H, L, S)

Example 01–

Here, we have performed conversion from RGB to HLS using the below values–

import colorsys 
R = 0.2
G = 0.4
B = 0.6
HLS = colorsys.rgb_to_hls(R, G, B) 


(0.5833333333333334, 0.4, 0.49999999999999994)

Example 02–

To perform the conversion from HLS to RGB, we have made use of hls_to_rgb() function as shown–

import colorsys 
H = 0.2
L = 0.4
S = 0.6
RGB = colorsys.hls_to_rgb(H, L, S) 


(0.544, 0.6400000000000001, 0.15999999999999992)

3. RGB to YIQ inter-conversion

The below methods can be used to have inter-conversion between RGB and YIQ color scheme in Python colorsys module.


rgb_to_yiq(Red, Green, Blue)
yiq_to_rgb(Y, I, Q)

Example 01– RGB to YIQ conversion

import colorsys 
R = 0.2
G = 0.4
B = 0.6
YIQ = colorsys.rgb_to_yiq(R, G, B) 


(0.362, -0.18413999999999997, 0.019820000000000004)

Example 02– YIQ to RGB conversion

import colorsys 
Y = 0.2
I = 0.4
Q = 0.6
RGB = colorsys.yiq_to_rgb(Y, I, Q) 


(0.9528868360277136, 0.0, 0.7819861431870669)


By this, we have come to the end the of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python & it’s modules, Stay tuned with us!

Till then, Happy Learning!! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors