C/C++: How to print colourful chars in the terminal

Vitor Matias
4 min readAug 15, 2020

--

Hello, everyone😁! I’m writing to share the solution of a problem that I had when I was working on a personal project, a Snake game in C++ running in console. After finishing the game logic, I tried to make it look nicer, so I decided to print coloured characters on the screen, but as far as I know, this language does not have native functions to do that 😥. For this reason, I searched for some external library, however, I didn’t wish to import a lot of stuff to make such a simple task, then I ended up finding a better solution: ANSI escape code.

Why do I think it’s a good idea to use them?

Because it’s a built-in console function, You don’t need to import any external library to your code, which is a good thing, in my opinion, because every computer that is compatible with ANSI will run your program. Another good point of using escape codes is that you can use them in other programming languages that run in the terminal if they don’t have a better way to work with the Scape features, what makes this a great addition to your devs tool kit.

So, what is the ANSI Escape Code?

ANSI escape sequences are a standard, developed in the 70s, used to format outputs in computer terminal. They are made of tags, making the console interpret what comes next as a command instead of a simple string. Those codes are very useful and can make many things like: move the cursor, apply bold or italic in text, clean the screen and change the background colour. However, I will just talk about the colour codes in this article.

Where does it work?

This feature is compatible with OS based on Unix systems, if you use Linux distributions like Ubuntu or macOS, this tutorial will, probably, work fine🙃. On the other hand, Windows became compatible with ANSI escape code after version 1511 of Windows 10. If you have installed Windows Terminal on your computer, you are lucky! Because it’s compatible with the escape code by default👏. Previous versions of Windows like 8.1 or 7 are not compatible with it natively, that being so, you may need to install some extra programs. If you only want to use default stuff this tutorial does not work to you😢.

Let’s get started!

The code looks like this:

\ESC[code Your output \ESC[m

The first two digits of the tag are the ASCII value of ESC, ( 27, 1Bh, 33o) and “[“ (91, 5Bh, 133o), the other part is an alphanumeric code representing some operation that will be applied in the output. We have a stop tag too, which is the last part of the command, and its function, as the name suggests, is to stop applying the modifications in the console.

Eight colours

The escape code has some colour specifications, the simplest one only has eight tones for text and background, with an additional lighter variation, I printed them in the table below :

Values of the basics colour tones of ANSI escape code.

The table’s values between 30 to 37 and 40 to 47 represent the standard text and background colours, respectively, the 30 is unreadable because it’s in black. The values in range 90–97 and 100–107 are the lighter versions of the foreground and screen.

The code used to generate this table is here:

This code prints a table with eight shades of console background and text colours.

Also, if you want to modify both text and background, you may write your code like that:

ESC[background_colour;Text_colourm output ESC[m”

An application of what I said above is here:

You have a purple text with a light blue background.

The code is here, I only wrote the part that matters the print instead of the whole subroutine.

Another thing that I like to do, to Improve the readability is to use macros once we have a small number of colours. I will show an example by rewriting the piece of code that I used to change both text and console colour.

256 colours

The other specification that I want to talk about is way more complete, it counts with 256 texts and background colour options, with all these alternative you can let your imagination run free while you paint your computer terminal😎!

Text

This code prints a table with 256 shades of text colours generated using ANSI escape code.

Codes of the 256 options of ANSI text colours

Background

This one generates all the options available with the 256 encodings.

Codes of the background colours available

If you look at the last pieces of code that I displayed you will notice that they have a number at the beginning of the code. The first one has a 38, this means that we are working with the text colour. The second one has a 48 and shows to the system that the code is referent of console’s background.

Awesome!😍 don’t you think? We passed from a black-white terminal to a very colourful one!

--

--

Vitor Matias

A very curious student of Electrical Engineering and an enthusiast in technology in general!💻⚡😁