An Enum wrapper around Rich colours for simplified colour formatting in Python terminal applications.
colours provides an elegant Enum-based interface for adding colours to your terminal output using Rich's powerful styling capabilities. It simplifies colour formatting by providing predefined colour schemes and convenient methods for printing coloured text without overloading the builtin print.
pip install git+https://github.com/XanaduAI/colours.git- Python >= 3.10
- rich >= 13.9.4
- Enum-based API: Clean, type-safe colour definitions
- Multiple colour options: red, orange, yellow, green, blue, purple
- Bold variants: Uppercase enum values provide bold styling
- Flexible printing: Multiple ways to apply colours to text
- Utility functions: Error highlighting and ANSI escape sequence removal
- Rich integration: Leverages Rich's powerful terminal formatting
from colours import Colour
# Use as a callable to wrap text in colour tags
Colour.print(Colour.red("This is red text"), Colour.GREEN("This is bold green text"))
# Use the print method, mix coloured arguments
Colour.blue.print("This is blue text", Colour.YELLOW("with bold yellow text"))
# Note: if the base print is a BOLD colour, the mixed colours will also be BOLD, even if it isn't specified.
Colour.PURPLE.print("This is bold purple text", Colour.orange("with bold orange text"))Normal colours:
Colour.redColour.orangeColour.yellowColour.greenColour.blueColour.purpleColour.default
Bold colours (uppercase):
Colour.REDColour.ORANGEColour.YELLOWColour.GREENColour.BLUEColour.PURPLE
from colours import Colour, Color # Both spellings supported
# Reset text to the default terminal colour value.
Colour.RED.print("BoldRedError:", Colour.default("written in the default colour, probably white or black."))
# Highlight errors in text automatically for displaying.
error_msg = "ValueError: invalid input"
highlighted = Colour.red_error(error_msg)
Color.print(highlighted) # "ValueError" will be highlighted in red
# You can also print/display it immediately.
Colour.red_error(error_msg, display = True)
# Remove ANSI escape sequences
ansi_text = "\x1b[31mHello, Red World!\x1b[0m"
clean_text = Colour.remove_ansi(ansi_text)
assert clean_text == "Hello, Red World!" # True
# Rainbow colours
clrs = [c for c in Colour if "bold" not in c.value and c.value != "default"]
message = "Hello! This is a message written in cycling rainbow colours for each word.".split()
n = len(clrs)
Colour.print(*(clrs[i % n](word) for i, word in enumerate(message)))__call__(value: Any) -> str: Wraps the value in colour tagsprint(*args, **kwargs): Prints coloured text using Rich printred_error(string: str) -> str: Static method to highlight error patterns in redremove_ansi(string: str) -> str: Static method to remove ANSI escape sequences
Both Colour and Color are available for your preferred spelling:
from colours import Colour # Canadian spelling
from colours import Color # American spellingTo build the latest version of colours first clone the repository through the Code tab above or
git clone https://github.com/XanaduAI/colours.gitWe use uv to develop colours. If you do not have uv installed, you can follow their OS-specific instructions here.
Once you have uv, you can install colours with the following:
uv syncThis will install colours as well as all dev dependencies to a new virtual environment in .venv.
Built using Rich by Textualize.