Get each Color component from RGB string in Python

Today we’ll learn how to get each color component in an RGB string in Python. For those of you who don’t know what an RGB color model is, it is a way of producing a specific color by varying the values/proportions of each of the three color components (Red, Green, Blue) in the RGB string. As you may have guessed already, RGB is the starting initials of Red, Green, and Blue. It is the most widely used Color representation model in computers, televisions, etc. In brief, to extract each color component from an RGB string we will slice, split and then map the resulting list to integer values.

So, Let’s get started!

Defining our RGB string

First, we’ll need an RGB string to work with. An RGB string is of the form ‘rgb(r_cmp,g_cmp,b_cmp)’.
In the above format, the first component is red, the second is green and the third is blue. In addition, each component must have a value between 0 and 255. We then consider a string ‘rgb(128,0,128)’, which, if you were wondering, is the RGB format for ‘Purple’.

rgbstr= "rgb(128,0,128)"

Slicing the string

Due to the syntax of the string, you may have noticed that the first 4 characters and the last character is of no use to us to determine the values of each component. So, we need to eliminate those redundant characters. We can do that by slicing the string using the slicing operator ‘:’.Here, we start our new string from the fourth character and stop at the character before the last.

rgbstr=rgbstr[4:-1]
print(rgbstr)

Output:

128,0,128

Splitting the string

Then, we need to get rid of the commas in between. So we use the split() method. Here, the separator will be ‘,’. This will return a list of strings.

rgblist=rgbstr.split(',')
print(rgblist)

Output:

['128', '0', '128']

We now need to convert the strings in the list to their corresponding integer values. Therefore, we use map() which applies a given function to every element of an iterable. Here we apply the int typecaster to all the elements of the list. To know more about map() visit Python documentation.

Converting strings to integers

rgblist=list(map(int,rgblist))
print(rgblist)

Output:

[128, 0, 128]

At this point, we have got the RGB values in a list. So now we can iterate through the list to get their values and perform whatever computations/alterations we may need to perform. For Eg:

print("Red color component= {}".format(rgblist[0]))
print("Green color component= {}".format(rgblist[1]))
print("Blue color component= {}".format(rgblist[2]))

Output:

Red color component= 128
Green color component= 0
Blue color component= 128

Whole Code:

rgbstr= "rgb(128,0,128)"
rgbstr=rgbstr[4:-1]
print(rgbstr)
rgblist=rgbstr.split(',')
print(rgblist)
rgblist=list(map(int,rgblist))
print(rgblist)
print("Red color component= {}".format(rgblist[0]))
print("Green color component= {}".format(rgblist[1]))
print("Blue color component= {}".format(rgblist[2]))

Also read:

Leave a Reply

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