Pure Python implementation of FIGlet for creating ASCII art text from regular text using various fonts
—
ANSI color support for terminal output with foreground/background color specifications and RGB color values. PyFiglet provides comprehensive color management for terminal-based ASCII art display.
Parses color specifications in various formats and converts them to ANSI escape sequences.
def parse_color(color: str) -> str:
"""
Parse foreground:background color specification into ANSI codes.
Parameters:
- color (str): Color specification in "foreground:background" format
Returns:
str: ANSI escape sequence for the specified colors
Format examples:
- "red:blue" - red foreground, blue background
- "red:" - red foreground only
- ":blue" - blue background only
- "255;100;50:0;0;255" - RGB foreground:background
Raises:
InvalidColor: If color specification is invalid
"""Converts individual color specifications to ANSI escape codes.
def color_to_ansi(color: str, isBackground: bool) -> str:
"""
Convert color specification to ANSI escape sequence.
Parameters:
- color (str): Color name or RGB specification
- isBackground (bool): True for background color, False for foreground
Returns:
str: ANSI escape sequence for the color
Supported formats:
- Named colors: "RED", "GREEN", "BLUE", etc.
- RGB format: "255;128;0" (values 0-255)
Raises:
InvalidColor: If color name not found or RGB format invalid
"""import pyfiglet
# Print with named colors
pyfiglet.print_figlet("Hello", colors="red:blue")
# Foreground only
pyfiglet.print_figlet("Red Text", colors="red:")
# Background only
pyfiglet.print_figlet("Blue Background", colors=":blue")
# No colors (default)
pyfiglet.print_figlet("Plain Text", colors=":")import pyfiglet
# RGB foreground and background
pyfiglet.print_figlet("RGB Colors", colors="255;100;50:0;128;255")
# RGB foreground only
pyfiglet.print_figlet("RGB Foreground", colors="200;50;100:")
# RGB background only
pyfiglet.print_figlet("RGB Background", colors=":50;200;100")from pyfiglet import parse_color, color_to_ansi, print_figlet
# Generate ANSI codes manually
fg_code = color_to_ansi("RED", False)
bg_code = color_to_ansi("BLUE", True)
reset_code = "\033[0m"
print(f"{fg_code}{bg_code}Colored Text{reset_code}")
# Parse complex color specifications
ansi_codes = parse_color("LIGHT_GREEN:DARK_GRAY")
print(f"{ansi_codes}Terminal Text\033[0m")
# Programmatic color application
colors = ["red:black", "green:white", "blue:yellow"]
for i, color in enumerate(colors):
print_figlet(f"Line {i+1}", colors=color, font="small")PyFiglet supports the following named colors via the COLOR_CODES constant:
COLOR_CODESColor functions raise InvalidColor exceptions for:
from pyfiglet import InvalidColor, print_figlet
try:
print_figlet("Test", colors="invalid_color:bad_bg")
except InvalidColor as e:
print(f"Color error: {e}")
try:
print_figlet("Test", colors="300;400;500:") # Invalid RGB values
except InvalidColor as e:
print(f"RGB error: {e}")PyFiglet generates standard ANSI escape sequences:
\033[{code}m\033[{code+10}m\033[38;2;R;G;Bm (foreground) or \033[48;2;R;G;Bm (background)\033[0mColor support depends on terminal capabilities:
The print_figlet() function automatically handles:
Install with Tessl CLI
npx tessl i tessl/pypi-pyfiglet