Skip to content

Commit 808a127

Browse files
committed
add changing text color tutorial
1 parent 6613d43 commit 808a127

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

general/printing-in-colors/colors.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from colorama import init, Fore, Back, Style
2+
3+
# essential for Windows environment
4+
init()
5+
# all available foreground colors
6+
FORES = [ Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE ]
7+
# all available background colors
8+
BACKS = [ Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE ]
9+
# brightness values
10+
BRIGHTNESS = [ Style.DIM, Style.NORMAL, Style.BRIGHT ]
11+
12+
13+
def print_with_color(s, color=Fore.WHITE, brightness=Style.NORMAL, **kwargs):
14+
"""Utility function wrapping the regular `print()` function
15+
but with colors and brightness"""
16+
print(f"{brightness}{color}{s}{Style.RESET_ALL}", **kwargs)
17+
18+
# printing all available foreground colors with different brightness
19+
for fore in FORES:
20+
for brightness in BRIGHTNESS:
21+
print_with_color("Hello world!", color=fore, brightness=brightness)
22+
23+
# printing all available foreground and background colors with different brightness
24+
for fore in FORES:
25+
for back in BACKS:
26+
for brightness in BRIGHTNESS:
27+
print_with_color("A", color=back+fore, brightness=brightness, end=' ')
28+
print()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)