CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyfiglet

Pure Python implementation of FIGlet for creating ASCII art text from regular text using various fonts

Pending
Overview
Eval results
Files

color-support.mddocs/

Color Support

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.

Capabilities

Color Parsing

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
    """

ANSI Code Generation

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
    """

Usage Examples

Basic Color Usage

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=":")

RGB Color Usage

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")

Advanced Color Usage

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")

Named Colors

PyFiglet supports the following named colors via the COLOR_CODES constant:

Standard Colors

  • BLACK (30)
  • RED (31)
  • GREEN (32)
  • YELLOW (33)
  • BLUE (34)
  • MAGENTA (35)
  • CYAN (36)
  • LIGHT_GRAY (37)
  • DEFAULT (39)

Bright Colors

  • DARK_GRAY (90)
  • LIGHT_RED (91)
  • LIGHT_GREEN (92)
  • LIGHT_YELLOW (93)
  • LIGHT_BLUE (94)
  • LIGHT_MAGENTA (95)
  • LIGHT_CYAN (96)
  • WHITE (97)

Special

  • RESET (0) - Resets all formatting

Color Format Specifications

Named Color Format

  • Case-insensitive color names from COLOR_CODES
  • Examples: "red", "GREEN", "Light_Blue"

RGB Format

  • Three semicolon-separated values: "R;G;B"
  • Each value must be 0-255
  • Examples: "255;0;0" (red), "128;128;128" (gray)

Combined Format

  • Foreground and background separated by colon
  • Either component can be empty
  • Examples:
    • "red:blue" - red text on blue background
    • "red:" - red text, default background
    • ":blue" - default text, blue background
    • "255;0;0:0;0;255" - red text on blue background (RGB)

Error Handling

Color functions raise InvalidColor exceptions for:

  • Invalid RGB format (wrong number of components, values outside 0-255 range)
  • Unknown named colors
  • Malformed color specifications
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}")

ANSI Escape Sequences

PyFiglet generates standard ANSI escape sequences:

  • Foreground colors: \033[{code}m
  • Background colors: \033[{code+10}m
  • RGB colors: \033[38;2;R;G;Bm (foreground) or \033[48;2;R;G;Bm (background)
  • Reset: \033[0m

Platform Support

Color support depends on terminal capabilities:

  • Full support: Unix/Linux terminals, Windows Terminal, modern terminal emulators
  • Limited support: Traditional Windows Command Prompt (basic colors only)
  • No support: Non-terminal output (files, pipes) - ANSI codes appear as text

Integration with Print Functions

The print_figlet() function automatically handles:

  • Color code generation and application
  • Reset sequence after output
  • Stdout writing for proper color display
  • Cross-platform terminal detection

Install with Tessl CLI

npx tessl i tessl/pypi-pyfiglet

docs

advanced-rendering.md

cli.md

color-support.md

font-management.md

index.md

string-manipulation.md

text-rendering.md

tile.json