logalpha.color




class Color(name: str, foreground: str, background: str)[source]

Associates a name (str) with its corresponding foreground and background (str) ANSI codes. Construct one or more instances using the factory methods.


classmethod from_name(name: str) → logalpha.color.Color[source]

Lookup ANSI codes by name.

Example:
>>> red = Color.from_name('red')
>>> red
Color(name='red', foreground='[31m', background='[41m')

classmethod from_names(names: List[str]) → List[logalpha.color.Color][source]

Returns a tuple of Color instances using the singular from_name() factory.

Example:
>>> colors = Color.from_names(['blue', 'green'])
>>> colors
[Color(name='blue', foreground='[34m', background='[44m'),
 Color(name='green', foreground='[32m', background='[42m')]



NAMES = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']

Names of supported colors to map to ANSI codes.

ANSI_RESET = '\x1b[0m'

ANSI reset code.

ANSI_COLORS = {'background': {'black': '\x1b[40m', 'blue': '\x1b[44m', 'cyan': '\x1b[46m', 'green': '\x1b[42m', 'magenta': '\x1b[45m', 'red': '\x1b[41m', 'white': '\x1b[47m', 'yellow': '\x1b[43m'}, 'foreground': {'black': '\x1b[30m', 'blue': '\x1b[34m', 'cyan': '\x1b[36m', 'green': '\x1b[32m', 'magenta': '\x1b[35m', 'red': '\x1b[31m', 'white': '\x1b[37m', 'yellow': '\x1b[33m'}}

Dictionary of all ANSI code sequences.


For convenience and readability, a set of global named instances are included for all of the colors.

BLACK = Color(name='black', foreground='\x1b[30m', background='\x1b[40m')
RED = Color(name='red', foreground='\x1b[31m', background='\x1b[41m')
GREEN = Color(name='green', foreground='\x1b[32m', background='\x1b[42m')
YELLOW = Color(name='yellow', foreground='\x1b[33m', background='\x1b[43m')
BLUE = Color(name='blue', foreground='\x1b[34m', background='\x1b[44m')
MAGENTA = Color(name='magenta', foreground='\x1b[35m', background='\x1b[45m')
CYAN = Color(name='cyan', foreground='\x1b[36m', background='\x1b[46m')
WHITE = Color(name='white', foreground='\x1b[37m', background='\x1b[47m')