colorsys module in Python

The ‘colorsys’ module in Python  used for the inter-conversion of color values between RGB(Red, Green and Blue) into three other types namely:

  • YIQ ( Luminance In-phase( Quadrature)
  • HLS (Hue Lightness Saturation)
  • HSV (Hue Saturation Value)

There are six predefined functions in this module namely:

  1. rgb_to_yiq(r,g,b) : It takes 3 parameters i.e (r,g,b) and converts into YIQ format.
  2. rgb_to_hls(r,g,b): It takes 3 parameters i.e (r,g,b) and converts into HLS format.
  3. rgb_to_hsv(r,g,b): Takes 3 parameters i.e (r,g,b) and converts into HSV format.
  4. yiq_to_rgb(y,i,q): It takes 3 parameters i.e (y,i,q) and converts into RGB format.
  5. hls_to_rgb(h,l,s): It takes 3 parameters i.e (h,l,s) and converts into RGB format.
  6. hsv_to_rgb(h,s,v): It takes 3 parameters i.e (h,s,v) and converts into RGB format.

 Constraints:

All the parameters have floating point values between 0 to 1 except in yiq_to_rgb function. Here y is positive while parameters i and q can also have negative floating point values.

Code to demonstrate the working of each function

Now let us take a look at a sample code. The code is a menu-driven program to let user use each of the options available.

import colorsys as c 
def func1():
  r=float(input("Enter r: "))
  g=float(input("Enter g: "))
  b=float(input("Enter b: "))
  yiq=c.rgb_to_yiq(r,g,b)
  print(yiq)

def func2():
  r=float(input("Enter r: "))
  g=float(input("Enter g: "))
  b=float(input("Enter b: "))
  hls=c.rgb_to_hls(r,g,b)
  print(hls)

def func3():
  r=float(input("Enter r: "))
  g=float(input("Enter g: "))
  b=float(input("Enter b: "))
  hsv=c.rgb_to_hsv(r,g,b)
  print(hsv)

def func4():
  y=float(input("Enter y: "))
  i=float(input("Enter i: "))
  q=float(input("Enter q: "))
  rgb=c.yiq_to_rgb(y,i,q)
  print(rgb)

def func5():
  h=float(input("Enter h: "))
  l=float(input("Enter l: "))
  s=float(input("Enter s: "))
  rgb=c.hls_to_rgb(h,l,s)
  print(rgb)

def func6():
  h=float(input("Enter h: "))
  s=float(input("Enter s: "))
  v=float(input("Enter v: "))
  rgb=c.hsv_to_rgb(h,s,v)
  print(rgb)

def driver():

    choice ='y'
    if(choice=='y'):
        print("1. RGB TO YIQ")
        print("2. RGB TO HLS")
        print("3. RGB TO HSV")
        print("4. YIQ TO RGB")
        print("5. HLS TO RGB")
        print("6. HSV TO RGB")
        ch=int(input("Enter your choice: "))
        if(ch==1):
          func1()
        elif(ch==2):
          func2()
        elif(ch==3):
          func3()
        elif(ch==4):
          func4()
        elif(ch==5):
          func5()
        elif(ch==6):
          func6()
        else:
          print("invalid choice!")
              
    option=input("Do you want to continue?(y/n): ")
    choice=option
    if(choice=='y'):
      driver()
    
driver()


Output:

1. RGB TO YIQ
2. RGB TO HLS
3. RGB TO HSV
4. YIQ TO RGB
5. HLS TO RGB
6. HSV TO RGB
Enter your choice: 1
Enter r: 0.2
Enter g: 0.4
Enter b: 0.2
(0.318, -0.055459999999999995, -0.10501999999999999)
Do you want to continue?(y/n): y
1. RGB TO YIQ
2. RGB TO HLS
3. RGB TO HSV
4. YIQ TO RGB
5. HLS TO RGB
6. HSV TO RGB
Enter your choice: 2
Enter r: 0.3
Enter g: 0.1
Enter b: 0.2
(0.9166666666666666, 0.2, 0.49999999999999994)
Do you want to continue?(y/n): y
1. RGB TO YIQ
2. RGB TO HLS
3. RGB TO HSV
4. YIQ TO RGB
5. HLS TO RGB
6. HSV TO RGB
Enter your choice: 3
Enter r: 0.4
Enter g: 0.5
Enter b: 0.1
(0.20833333333333334, 0.8, 0.5)
Do you want to continue?(y/n): y
1. RGB TO YIQ
2. RGB TO HLS
3. RGB TO HSV
4. YIQ TO RGB
5. HLS TO RGB
6. HSV TO RGB
Enter your choice: 4
Enter y: 0.4
Enter i: -0.2
Enter q: -0.3
(0.023556581986143188, 0.6456648530160097, 0.10900692840646653)
Do you want to continue?(y/n): y
1. RGB TO YIQ
2. RGB TO HLS
3. RGB TO HSV
4. YIQ TO RGB
5. HLS TO RGB
6. HSV TO RGB
Enter your choice: 5
Enter h: 0.6
Enter l: 0.2
Enter s: 0.4
(0.12000000000000005, 0.184, 0.27999999999999997)
Do you want to continue?(y/n): y
1. RGB TO YIQ
2. RGB TO HLS
3. RGB TO HSV
4. YIQ TO RGB
5. HLS TO RGB
6. HSV TO RGB
Enter your choice: 6
Enter h: 0.4
Enter s: 0.7
Enter v: 0.8
(0.24000000000000005, 0.8, 0.46400000000000025)
Do you want to continue?(y/n): n
>>> 

Leave a Reply

Your email address will not be published. Required fields are marked *