colorise package

Python module for easy, cross-platform colored output to the terminal.

colorise.can_redefine_colors(file)[source]

Return True if the terminal supports redefinition of colors.

Only returns True for Windows 7/Vista and beyond as of now.

colorise.redefine_colors(color_map, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Redefine colors using a color map of indices and RGB tuples.

Note

It is not currently possible to redefine colors on Mac and Linux systems via colorise.

colorise.color_names()[source]

Return a list of supported color names.

colorise.num_colors()[source]

Return the number of colors supported by the terminal.

colorise.set_color(fg=None, bg=None, attributes=None, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Set the current colors.

If no arguments are given, sets default colors.

colorise.reset_color(file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Reset all colors and attributes.

colorise.cprint(string, fg=None, bg=None, attributes=None, end='\n', file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, enabled=True)[source]

Print a string to a target stream with colors and attributes.

The fg and bg keywords specify foreground- and background colors while attributes is a list of desired attributes. The remaining two keyword arguments are the same as Python’s built-in print function.

Colors and attributes are reset before the function returns.

colorise.fprint(fmt, autoreset=True, end='\n', file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, enabled=True)[source]

Print a string with color formatting.

The autoreset keyword controls if colors and attributes are reset before each new color format. For example:

>>> colorise.fprint('{fg=blue}Hi {bg=red}world', autoreset=False)

would print ‘Hi’ in blue foreground colors and ‘world’ in blue foreground colors AND a red background color, whereas:

>>> colorise.fprint('{fg=blue}Hi {bg=red}world', autoreset=True)

would print ‘Hi’ in blue foreground colors but ‘world’ only with a red background color since colors are reset when ‘{bg=red}’ is encountered.

The remaining two keyword arguments are the same as Python’s built-in print function.

Colors and attribtues are reset before the function returns.

colorise.highlight(string, indices, fg=None, bg=None, attributes=None, end='\n', file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, enabled=True)[source]

Highlight characters using indices and print to a target stream.

The indices argument is a list of indices (not necessarily sorted) for which to apply the colors and attributes. Indices that are out of bounds are ignored.

fg and bg specify foreground- and background colors while attributes is a list of desired attributes. The remaining two keyword arguments are the same as Python’s built-in print function.

Colors and attribtues are reset before the function returns.

Submodules

colorise.attributes module

Attributes supported by colorise.

Depending on your terminal’s capabilities, some of these attributes may have no effect.

class colorise.attributes.Attr[source]

Bases: enum.Enum

Console attributes.

Each attribute’s value is defined via its ANSI escape code.

Bold = 1
Faint = 2
Intense = 1
Italic = 3
Reset = 0
Reverse = 7
Underline = 4
from_name = <bound method Attr.from_name of <enum 'Attr'>>
names = <bound method Attr.names of <enum 'Attr'>>
names_with_aliases = <bound method Attr.names_with_aliases of <enum 'Attr'>>

colorise.cluts module

Main color look-up table (CLUT) functions.

All look-up tables presented here are entirely based on research and the author’s own knowledge as it is impossible to ensure complete cross-platform support across all kinds of terminals. Therefore, there is no guarantee that any of these tables will be accurate.

If you have corrections or suggestions, please submit an issue.

colorise.cluts.get_color(value, color_count, cluts, bg=False, attributes=None, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Return the color given by a color format.

colorise.cluts.match_color_formats(value)[source]

Return the color format of the first format to match the given value.

colorise.color_tools module

Functions for converting and comparing colors.

colorise.color_tools.closest_color(rgb, clut)[source]

Return the CLUT index of the closest RGB color to a given RGB tuple.

colorise.color_tools.color_difference(rgb1, rgb2)[source]

Return the sums of component differences between two colors.

colorise.color_tools.hls_to_rgb(hue, lightness, saturation)[source]

Convert HLS (hue, lightness, saturation) values to RGB.

colorise.color_tools.hsv_to_rgb(hue, saturation, value)[source]

Convert HSV (hue, saturation, value) values to RGB.

colorise.error module

Custom colorise exceptions.

exception colorise.error.NotSupportedError[source]

Bases: Exception

Raised when functionality is not supported.

colorise.formatter module

ColorFormatter class for colorise.fprint.

This class extends the string.Formatter class.

class colorise.formatter.ColorFormatter(set_color_func, reset_func)[source]

Bases: string.Formatter

Class for formatting strings containing color syntax.

As opposed to an ordinary derived string.Formatter, this one does not construct and return a formatted string but immediately outputs the formatted string. This is necessary to support Windows consoles that cannot interpret ANSI escape sequences since they have no way of embedding colors into strings but instead set colors through an API call.

autoreset

If True, automatically reset before each color format field.

enabled

Whether colors are enabled or not.

file

Target output handle of the formatter.

parse(format_string)

Parse a format string and generate tokens.

vformat(format_string, args, kwargs)

Hijack the internals of string.Formatter.vformat.

Copied (almost) verbatim from the Python 3.7 source code but does not return a formatted string.

colorise.terminal module

Terminal functions.

colorise.terminal.terminal_name()[source]

Return the name of the terminal.