How to print RGB colour to the terminal

Question:

Can ANSI escape code SGR 38 – Set foreground color with argument 2;r;g;b be used with print function?
Example of use with code 33 is of course

OKBLUE = '33[94m'

I would like to use 038 instead to be able to use any RGB color. Is that posible?

I tried

GREEN = '38[2;0;153;0m'
ENDC = '33[0m'
    
print(f"{GREEN} some text {ENDC}")

Expected to change the color of "some text" in green

Answers:

Below code will give you an idea.

print('33[90m' + 'hello' + '33[96m' + ' there?' )
Answered By: su yong kim

To use an RGB color space within the terminal* the following escape sequence can be used:

# Print Hello! in lime green text.
print('33[38;2;146;255;12mHello!33[0m')
#           ^
#           |
#            The 38 goes here, to indicate a foreground colour.

# Print Hello! in white text on a fuschia background.
print('33[48;2;246;45;112mHello!33[0m') 

Explanation:

33[38;2;146;255;12mHello!33[0m
^     ^ ^  ^   ^   ^   ^     ^   ^ 
|     | |  R   G   B   |     |   |
|     | |  |           |     |    Reset the colour to default
|     | |  |           |     | 
|     | |  |           |      Escape character
|     | |  |           |
|     | |   R;G;B      Text to print
|     | |
|     |  Indicate the following sequence is RGB
|     |
|      Code to instruct the setting of an 8 or 24-bit foreground (text) colour
|
 Escape character

The use of 38;2 indicates an RGB (foreground) sequence is to follow. However, the use of 38;5 indicates the following (foreground) value comes from the 256-colour table.

To clarify what appears to be a misconception, 33 (octal) or x1b (hexidecimal) corresponds to the ASCII table’s ESC character, which is used here to introduce an escape sequence of terminal text colouring. Whereas the 38 is used to instruct the following 8 or 24-bit colour to be set as foreground, (after the escape sequence has been introduced). Additionally, 48 can be used to set the background colour, as demonstrated in the code example above.

*Providing the terminal emulator supports 24-bit colour sequences. (e.g. Xterm, GNOME terminal, etc.)

Link to the Wikipedia article which explains this topic of 24-colour (RGB) in greater depth.

Answered By: S3DEV

In full disclosure, I’m the author of the Colorist package. Colorist is lightweight, less verbose and supports RGB colors out of the box. Simply install the package with pip install colorist and type:

from colorist import ColorRGB, BgColorRGB

dusty_pink = ColorRGB(194, 145, 164)
bg_steel_blue = BgColorRGB(70, 130, 180)

print(f"I want to use {dusty_pink}dusty pink{dusty_pink.OFF} and {bg_steel_blue}steel blue{bg_steel_blue.OFF} colors inside this paragraph")

enter image description here

If you prefer to define colors in HSL, that’s also possible as Colorist will translate this to RGB:

from colorist import ColorHSL, BgColorHSL

mustard_green = ColorHSL(60, 56, 43)
bg_steel_gray = BgColorHSL(190, 2, 49)

print(f"I want to use {mustard_green}mustard green{mustard_green.OFF} and {bg_steel_gray}steel blue{bg_steel_gray.OFF} colors inside this paragraph")

enter image description here

Note that not all terminals support advanced RGB colors as they weren’t part of the initial ANSI specification. You may want to try various terminals to find the right one for you.

Answered By: Jakob Bagterp
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.