CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Computer Concepts : Display Colorful text on the Screen

When we write console-based (text-based) Applications, most of the time, the display text will be on a White Black background. Graphics-based applications display colorful graphics & provide the user interface for the user to interact with the Applications. We can write Text-based Applications to display colorful text on the screen, and a user interface to allow the user to interact with the Applications.

Some languages provide libraries, to display the text-based output in colors; but I would like to explain here, the different approaches to displaying the text with different colors.

The approach that I am going to explain here is non-compiler specific; non-related to a particular language (but for example code to mention here, I prefer “C”); and which is a standard ANSI has introduced to gives the opportunity to console window to render/display the text with different attributes. By setting these text attributes, we can display the text in different colors.

We call these Control Sequences; can use through print statements. For example printf function in “C”.

Control Sequences are not the functions that we use in “C” or any other language; these are like ANSI Escape Sequences. We use ANSI Control Sequences to instruct the console (where the text is displayed) to display the text in different colors.

This is not straightforward, like some ANSI Escape Sequences we use in “C”; “\n” – newline, “\t” – tab, etc, we use.

I would like to give a little bit more information about the Controls Sequences before we start using them below.

Escape Sequences are the ANSI’s standards to display special characters like newline, tab, etc., on the console window; and almost all the console windows support these standards. Other than these frequently used Escape Sequences; there is another type of sequence most people are not aware of; are the Control Sequences used to change the behavior of the console or terminal; for example: display text with different attributes & in turn, the text will display in different colors.

ANSI Control Sequences start with ANSI Escape Sequence “ESC[“ and are followed by Control Sequence Codes. For each Control Sequence Code, we need to pass the Control Sequence Parameters. Each Control Sequence Code is meant for a particular behavior.

For display attributes of the text, we use the Control Code name “SGR (Select Graphic Rendition)”; representing “<<param>> m”. Where <<param>> is the Control Sequence Code parameter & each parameter can be separated by a semi-colon (“;”). And “m” indicates the Control Sequence Code “SGR”.

That means, to use Control Sequence Code to modify text attributes we have to follow the below representation:

ESC[<<param>>m

Confused? I know it is a little bit tricky to understand these things; as I stated earlier, this is not straightforward. But don’t worry; I will put things in a bit more detail.

Let’s put everything together step-by-step.

Step 1. ANSI Control Sequences are the standards used to change the behavior of the console or terminal window; one such behavior is changing the text attributes to display the text in different colors.

ANSI Control Sequences start with ANSI Escape Sequence “ESC[“.

Step 2. How do we represent “ESC[” in “C”.? As I mentioned above, we use ANSI Control Sequences in printf statement.

  • Escape Sequence in “C”, start with “\”.
  • ESC (Escape Key) hex value is “x1b” (Decimal value 27)

So we can write, “ESC[” as printf(“\x1b[“);

Step 3. Now we need to select the Control Sequence Code to use. As I mentioned above, SGR is the Control Sequence Code, we use to change the attributes of the text. And we represent this as “m”. So our above printf statement extends to add Control Sequence Code, and looks like below:

printf("\x1b[m");

Step 4. Where to add the parameters now.? Control Sequence Code parameters should be added before the Control Code, “m”. When you add parameters, the behavior of the console will change. To reset the behavior to a normal state, we need to pass “0” or no parameters in Control Sequence Code.

If we, do not specify any parameters to the Control Sequence Code; means, resetting the display attributes to normal. Above printf statement, mentioned in Step 3. sets the console behavior to a normal state.

Step 5.  Each Control Sequence Code parameter sets different display attributes. We can combine them with a semi-colon (“;”) to change the display behavior.

Below are the parameters we use for changing the text color:

  • 30 – 37 are for setting foreground color.
  • 40 – 47 are for setting the background color.

There are so many other parameters, for different purposes. I am not going to list all of them here. But mainly, we use the above parameters for setting the text color.

Step 6. Now we know what parameters we can use along with Control Sequence Code. Each color parameter produces different colors on the screen when we apply them with the Control Sequence Code. After adding the parameter, our printf statement looks like the below:

printf("\x1b[30m");

To combine both foreground & background color; re-write the above printf like below:

printf("\x1b[30;45m");

That means Control Sequence Code parameter value 30 is used for the foreground & 45 is used for the background color.

Step 7. Let’s put everything in a simple “C” program and see the display text as it looks like. Before we do, here is the sequence that needs to follow.

  1. Before displaying the text, set the attribute of the text using Control Sequences.
  2. Display the text.
  3. Once done, reset the attributes to normal.

Here is the complete code:

#include <stdio.h>

void main()
{
        printf("\x1b[37;41m");
        printf("Colored Text.\n");
        printf("\x1b[m");
        printf("Normal Text.\n");
}

Step 7. Compile the above “C” program & Run it; and observe the result that, White color Text is displayed on Red background.

Colored Text
Colored Text

If you like this article, please give your feedback in the below Comments section.

I would like to write another Article on Control Sequences using “C” Programming; will try to give more generic functions to use to apply different attributes.

// Malin

Computer Concepts : Display Colorful text on the Screen

2 thoughts on “Computer Concepts : Display Colorful text on the Screen

Leave a Reply

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

Scroll to top
Exit mobile version