Skip to content

ColorTerm

Module: terminaltexteffects.utils.colorterm

Convert XTerm 256 color codes and RGB hex colors into ANSI escape sequences.

Functions:

Name Description
fg

str | int) -> str: Set the foreground color using an XTerm code or RGB hex string.

bg

str | int) -> str: Set the background color using an XTerm code or RGB hex string.

bg(color_code)

Set the background color of the terminal text.

Parameters:

Name Type Description Default
color_code str | int

The background color as an XTerm 256 color code or an RGB hex string, with or without a leading #.

required

Returns:

Name Type Description
str str

The ANSI escape sequence to set the background color.

Source code in terminaltexteffects/utils/colorterm.py
def bg(color_code: str | int) -> str:
    """Set the background color of the terminal text.

    Args:
        color_code (str | int): The background color as an XTerm 256 color code
            or an RGB hex string, with or without a leading `#`.

    Returns:
        str: The ANSI escape sequence to set the background color.

    """
    return _color(color_code, 48)

fg(color_code)

Set the foreground color of the terminal text.

Parameters:

Name Type Description Default
color_code str | int

The foreground color as an XTerm 256 color code or an RGB hex string, with or without a leading #.

required

Returns:

Name Type Description
str str

The ANSI escape sequence to set the foreground color.

Source code in terminaltexteffects/utils/colorterm.py
def fg(color_code: str | int) -> str:
    """Set the foreground color of the terminal text.

    Args:
        color_code (str | int): The foreground color as an XTerm 256 color code
            or an RGB hex string, with or without a leading `#`.

    Returns:
        str: The ANSI escape sequence to set the foreground color.

    """
    return _color(color_code, 38)