1.5. color

This module provides functions for converting and using colors for arbitrary purposes including terminal output.

1.5.1. Functions

convert_hex_to_tuple(hex_color, raw=False)[source]

Converts an RGB hex triplet such as #ff0000 into an RGB tuple. If raw is True then each value is on a scale from 0 to 255 instead of 0.0 to 1.0.

Parameters:
  • hex_color (str) – The hex code for the desired color.
  • raw (bool) – Whether the values are raw or percentages.
Returns:

The color as a red, green, blue tuple.

Return type:

tuple

convert_tuple_to_hex(rgb, raw=False)[source]

Converts an RGB color tuple info a hex string such as #ff0000. If raw is True then each value is treated as if it were on a scale from 0 to 255 instead of 0.0 to 1.0.

Parameters:
  • rgb (tuple) – The RGB tuple to convert into a string.
  • raw (bool) – Whether the values are raw or percentages.
Returns:

The RGB color as a string.

Return type:

str

get_scale(color_low, color_high, count, ascending=True)[source]

Create a scale of colors gradually moving from the low color to the high color.

Parameters:
  • color_low (tuple) – The darker color to start the scale with.
  • color_high (tuple) – The lighter color to end the scale with.
  • count – The total number of resulting colors.
  • ascending (bool) – Whether the colors should be ascending from lighter to darker or the reverse.
Returns:

An array of colors starting with the low and gradually transitioning to the high.

Return type:

tuple

print_error(message)[source]

Print an error message to the console.

Parameters:message (str) – The message to print
print_good(message)[source]

Print a good message to the console.

Parameters:message (str) – The message to print
print_status(message)[source]

Print a status message to the console.

Parameters:message (str) – The message to print

1.5.2. Classes

class ColoredLogFormatter(fmt=None, datefmt=None, style='%')[source]

A formatting class suitable for use with the logging module which colorizes the names of log levels.

format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

static formatException(exc_info)[source]

Format and return the specified exception information as a string.

This default implementation just uses traceback.print_exception()