Skip to content

Background Colors#

Introduction#

When you want to print colored backgrounds in the terminal, Colorist offers two ways of doing so:

  • Full text functions: Print a full line of colored text with the bg_green(), bg_yellow(), bg_red(), etc. functions.
  • Custom string styling: Mix colors to any part of a string with the BgColor and BgBrightColor classes.

How to print a full line of colored text in the terminal:

Python
1
2
3
4
5
from colorist import bg_green, bg_yellow, bg_red

bg_green("This is GREEN background!")
bg_yellow("This is YELLOW background!")
bg_red("This is RED background!")

How it appears in the terminal:

% This is GREEN background!
% This is YELLOW background!
% This is RED background!

Background colors can also be mixed inside a paragraph:

Python
1
2
3
4
5
from colorist import BgColor

print(f"Put {BgColor.RED}red{BgColor.OFF} background color inside this paragraph")
print(f"Both {BgColor.GREEN}green{BgColor.OFF}...")
print(f"... and {BgColor.YELLOW}yellow{BgColor.OFF} are nice background colors")

How it appears in the terminal:

% Put red background color inside this paragraph
% Both green...
% ... and yellow are nice background colors

Tip

Remember to turn off a color with BgColor.OFF or BgBrightColor.OFF every time you want to revert back to the default terminal text style. Otherwise, the effect may spill over and into other terminal messages.

Bright Colors#

Most terminals support bright colors that stand more out:

Python
1
2
3
from colorist import BgBrightColor

print(f"Add {BgBrightColor.CYAN}cyan{BgBrightColor.OFF} background color")

How it appears in the terminal:

% Add cyan background color

Refer to the documentation for a complete list of full line text color functions and custom string styling.