Colorful

0.5.8 · active · verified Sun Apr 05

Colorful is a Python library that provides expressive and consistent terminal string styling. It supports various color modes including 8 ANSI, 256 ANSI, and true colors, along with predefined styles (like Solarized) and custom color palettes. The current version is 0.5.8, and it is actively maintained with several releases per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic text coloring, combining styles, using f-strings for dynamic content, extending the color palette, and employing context managers for specific color modes. The recommended import is `import colorful as cf`.

import colorful as cf
import os

# Basic text styling
print(cf.red('Hello, red World!'))
print(cf.bold_green('This is bold and green.'))

# Combining styles using bitwise operators
print(cf.italic & cf.coral_on_beige | 'Styled with italic, coral text on beige background.')

# Using f-strings (Python >= 3.6)
user_name = os.environ.get('USER', 'Guest')
print(f'{cf.blue_on_white}Hello, {user_name}! Enjoy Colorful!{cf.reset}')

# Extending the default color palette
cf.update_palette({'companyOrange': '#f4b942', 'companyBlue': '#4287f5'})
print(cf.companyOrange_on_companyBlue('Custom company colors!'))

# Using a context manager for temporary color mode
with cf.with_8_ansi_colors() as c:
    print(c.bold_magenta('This text uses 8 ANSI colors only.'))

# Printing with format-string like syntax
cf.print('{c.cyan}Formatted {c.underline}output{c.reset} with {c.yellow}placeholders{c.reset}',
         name=cf.bold_white('Colorful'))

view raw JSON →