Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print RGB colour to the terminal

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 = '\033[94m'

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

I tried

GREEN = '\038[2;0;153;0m'
ENDC = '\033[0m'
    
print(f"{GREEN} some text {ENDC}")

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

like image 250
Lupascu Gabriel Cristian Avatar asked Oct 22 '25 11:10

Lupascu Gabriel Cristian


2 Answers

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

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

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

Explanation:

\033[38;2;146;255;12mHello!\033[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, \033 (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.

like image 85
S3DEV Avatar answered Oct 24 '25 23:10

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")

Examples of RGB color in terminal

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")

Examples of HSL color in terminal

Or similarly, you can also define colors in Hex:

from colorist import ColorHex, BgColorHex

watermelon_red = ColorHex("#ff5733")
bg_mint_green = BgColorHex("#99ff99")

print(f"I want to use {watermelon_red}watermelon pink{watermelon_red.OFF} and {bg_mint_green}mint green{bg_mint_green.OFF} colors inside this paragraph")

Examples of Hex color in terminal

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.

Color cube

like image 45
Jakob Bagterp Avatar answered Oct 25 '25 01:10

Jakob Bagterp