Colors API

Classes for color detection and color scheme management.

ColorSupport

class ColorSupport

Constants for color support levels.

TRUECOLOR = "truecolor"

24-bit color (16.7 million colors).

COLOR_256 = "256color"

256 color palette.

BASIC = "basic"

8/16 basic ANSI colors.

NONE = "none"

No color support.

Check

class Check

Auto-detect terminal color support across all major OS.

Returns detected color mode when instantiated (not an instance).

Parameters:

force (str, optional) – Force a specific color mode.

Returns:

Detected or forced color support level.

Return type:

str

static enable_windows_ansi() bool

Enable ANSI processing on Windows.

Returns:

True if successful.

Return type:

bool

classmethod detect_color_support(force=None) str

Detect terminal color capabilities.

Parameters:

force (str, optional) – Force a specific mode.

Returns:

Color support level.

Return type:

str

Example:

from richcolorlog.logger import Check, ColorSupport

# Auto-detect
mode = Check()
print(f"Detected: {mode}")

# Force specific mode
mode = Check(force=ColorSupport.TRUECOLOR)

Colors

class Colors(color_type='ansi', show_background=False, **color_overrides)

Handler for color schemes with various format output.

Parameters:
  • color_type (str) – Output format (‘ansi’ or ‘rich’).

  • show_background (bool) – Include background colors.

  • emergency_color (str) – Custom emergency color.

  • alert_color (str) – Custom alert color.

  • critical_color (str) – Custom critical color.

  • error_color (str) – Custom error color.

  • warning_color (str) – Custom warning color.

  • fatal_color (str) – Custom fatal color.

  • notice_color (str) – Custom notice color.

  • debug_color (str) – Custom debug color.

  • info_color (str) – Custom info color.

check() dict

Detect and return color scheme according to terminal support.

Returns:

Dictionary of color codes by level.

Return type:

dict

rich_color(show_background=False) dict

Return color scheme in Rich library format.

Parameters:

show_background (bool) – Include background colors.

Returns:

Dictionary of Rich style strings by level.

Return type:

dict

Example:

from richcolorlog.logger import Colors

# ANSI colors with background
colors = Colors(color_type='ansi', show_background=True)
scheme = colors.check()
print(scheme['error'])  # ANSI escape code

# Rich colors without background
colors = Colors(color_type='rich', show_background=False)
scheme = colors.check()
print(scheme['error'])  # "red"

# Custom colors
colors = Colors(
    color_type='rich',
    show_background=True,
    error_color='bold white on dark_red',
    warning_color='black on bright_yellow',
)

SafeDict

class SafeDict(dict)

Dictionary that returns None for missing keys instead of raising KeyError.

__missing__(key)

Return None for missing keys.

Example:

from richcolorlog.logger import SafeDict

d = SafeDict({'a': 1})
print(d['a'])  # 1
print(d['b'])  # None (no KeyError)

Default Color Schemes

TrueColor with Background

{
    'debug': "#000000 on #FFAA00",      # Black on orange
    'info': "#000000 on #00FF00",       # Black on green
    'notice': "#000000 on #00FFFF",     # Black on cyan
    'warning': "black on #FFFF00",      # Black on yellow
    'error': "white on red",            # White on red
    'critical': "bright_white on #0000FF",  # White on blue
    'fatal': "blue on #FF557F",         # Blue on pink
    'alert': "bright_white on #005500", # White on dark green
    'emergency': "bright_white on #AA00FF",  # White on purple
}

TrueColor Foreground Only

{
    'debug': "#FFAA00",     # Orange
    'info': "#00FF00",      # Green
    'notice': "#00FFFF",    # Cyan
    'warning': "#FFFF00",   # Yellow
    'error': "red",         # Red
    'critical': "#0000FF",  # Blue
    'fatal': "#FF557F",     # Pink
    'alert': "#005500",     # Dark green
    'emergency': "#AA00FF", # Purple
}

ANSI Escape Codes

# TrueColor (24-bit)
'\x1b[38;2;R;G;Bm'           # Foreground RGB
'\x1b[48;2;R;G;Bm'           # Background RGB
'\x1b[38;2;R;G;B;48;2;R;G;Bm'  # Both

# 256 Color
'\x1b[38;5;Nm'  # Foreground (N = 0-255)
'\x1b[48;5;Nm'  # Background

# Basic (8/16)
'\x1b[30-37m'   # Foreground (30=black, 31=red, ...)
'\x1b[40-47m'   # Background
'\x1b[90-97m'   # Bright foreground
'\x1b[100-107m' # Bright background

# Reset
'\x1b[0m'