Colors API
Classes for color detection and color scheme management.
ColorSupport
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:
- static enable_windows_ansi() bool
Enable ANSI processing on Windows.
- Returns:
True if successful.
- Return type:
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:
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
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'