If you have been programming in Python, chances are high that you have encountered at least one library that offers functionalities dealing with representation and customization of colors.
From image processing to data visualization to web application development, you will find the use of colors everywhere.
In fact, most of the modern deep neural networks used for the state of the art computer vision tasks are trained on the spatial distribution of color information of the pixels in images.
In this tutorial, we will look at a few of those libraries, and understand how colors are described and modified programmatically.
Color representation in Computers
Before we look at a specific Python library, let us first understand how colors are generally represented in computer programs.
There are many popular color models used to describe a color, such as the RGB, CMYK, HSV, HSL, etc.
The RGB color model is most commonly used in programming languages.
The RGB color model
In the RGB color model, any color can be generated by mixing 3 primary colors, namely, Red, Green, and Blue.
In this model, a color can be described by specifying a group of 3 numeric values (typically ranging from 0 to 255),
each specifying the intensity of Red, Green, and Blue colors present in a given color.
These 3 colors are also sometimes referred to as ‘channels’ or ‘bands’.
In most programming languages, including Python, the information about these 3 bands is generally specified in one of the 2 ways – a tuple of RGB values or HEX codes.
In the first notation, we represent a color as a tuple of 3 values, corresponding to the Red, Green, and Blue intensities, in that order (unless specified otherwise). The numbers in this representation, ranging from 0 to 255, are represented in decimal (base 10) encoding.
In some cases, these numbers are also scaled-down and specified in the range of 0 to 1.
HEX code, on the other hand, is a way of representing the same information (RGB values) in hexadecimal (base 16) encoding.
For example, the RGB representation of the red color as a tuple is (255, 0, 0), whereas the HEX code equivalent of the same will be ‘#FF0000′(FF = 16*15 + 15 = 255).
Here are some of the common colors along with their RGB values and the corresponding HEX codes:
Print colored text in the terminal
If you have installed software packages using the command line, you may have encountered special instructions, warnings, or errors being shown in different colored texts on the terminal.
This is just one of the many situations where colored outputs on the terminal are helpful.
Python has libraries that allow us to print outputs in the colors of our choice. We can use these libraries to print colored texts as well as colored blocks by customizing the foreground as well as the background of the text.
Let us look at one such library –
from termcolor import colored print("This is a normal text in terminal") print(colored("This is a colored text in terminal","red")) print(colored("This is a colored text with background","red","on_green"))
The first parameter to the
colored method is the text to be printed, followed by the parameters to specify the foreground and background colors of the text.
Escape sequences to customize text color in Python
In the previous section, we used a library to specify the foreground and the background colors of the text.
We can also customize the appearance of texts in the terminal by directly appending special escape sequence characters in the output string.
These escape sequences are called “ANSI sequence codes.”
They are used to give special commands to the terminal to alter the appearance of the text being printed on them.
In this section, we will look at a few examples of how we can alter the foreground and background colors of the texts, along with other text formatting options such as bold and underline.
All the ANSI escape codes we use will begin with the sequence
' 33[' and end with
Let us take a look at an example.
print(" 33[0;30;47mHello World")
Note that we do not need to call any special library to print colored texts in the terminal.
It is just a bunch of characters preceding the text (to be customized) that defines the styling of the text.
The format of the above ANSI code is as follows: