Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Get Python to Output In RGB Color on a Windows Terminal?

I'm using a simple python (3.9.2) interpreter via command prompt on a windows 10 machine.

I am trying to get colored output texts from the python interpreter. I know how to get colored outputs however I'm looking for something that would allow RGB specified inputs so that I may output varying color intensities.

I found one thread HERE with such an example (you may have to scroll down to see the code line specifying the RGB input).

so.. I tried this part: (I also had to import colorama for ANY of the escape sequences to display actual color)

from sty import fg, bg, ef, rs

foo = fg.red + 'This is red text!' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs

# Add custom colors:

from sty import Style, RgbFg

fg.orange = Style(RgbFg(255, 150, 50))

buf = fg.orange + 'Yay, Im orange.' + fg.rs

print(foo, bar, baz, qux, qui, buf, sep='\n')

The fg.red worked okay, but the RGB specified lines did not produce the correct colors. nor the intensities. Is there anything else for suggestions where I can get this to work on a windows machine??

like image 268
Anonymous Avatar asked Dec 21 '25 07:12

Anonymous


1 Answers

You can find several good packages that solves your issue. In full disclosure, I'm the author of the Colorist package for Python. 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 104
Jakob Bagterp Avatar answered Dec 23 '25 01:12

Jakob Bagterp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!