Skip to content

How to Print 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.

Cheat Sheets#

Normal Colors#

Color Color Code Example
Green BgColor.GREEN This is GREEN background!
Yellow BgColor.YELLOW This is YELLOW background!
Red BgColor.RED This is RED background!
Magenta BgColor.MAGENTA This is MAGENTA background!
Blue BgColor.BLUE This is BLUE background!
Cyan BgColor.CYAN This is CYAN background!
White BgColor.WHITE This is WHITE background!
Black BgColor.BLACK This is BLACK background!
- BgColor.DEFAULT -
- BgColor.OFF -

Bright Colors#

Color Color Code Example
Bright green BgBrightColor.GREEN This is BRIGHT GREEN background!
Bright yellow BgBrightColor.YELLOW This is BRIGHT YELLOW background!
Bright red BgBrightColor.RED This is BRIGHT RED background!
Bright magenta BgBrightColor.MAGENTA This is BRIGHT MAGENTA background!
Bright blue BgBrightColor.BLUE This is BRIGHT BLUE background!
Bright cyan BgBrightColor.CYAN This is BRIGHT CYAN background!
Bright white BgBrightColor.WHITE This is BRIGHT WHITE background!
Bright black BgBrightColor.BLACK This is BRIGHT BLACK background!
- BgBrightColor.DEFAULT -
- BgBrightColor.OFF -