Colour depth overview
The module supports three colour depths:- 4-bit — 16 colours (black, red, green, yellow, blue, magenta, cyan, white + bright variants)
- 8-bit — 256 colours (16 named + 216 RGB cube + 24 greyscale)
- 24-bit — 16 million colours (true colour, R,G,B 0-255 each)
Capability detection
Functions for detecting terminal colour capabilities.colour::supports()
Check if the terminal supports any colour (at least 8 colours).colour::depth()
Return the number of colours the terminal supports.colour::supports_256()
Check if terminal supports 256 colours.colour::supports_truecolor()
Check if terminal supports true colour (24-bit). Checks$COLORTERM environment variable.
$COLORTERM is “truecolor” or “24bit”, 1 otherwise.
Escape code generation
Core functions for generating ANSI escape sequences.colour::esc()
Generate a raw ANSI escape sequence for any colour depth.$1- Bit depth:4,8, or24$2- Foreground or background:fgorbg$3+- Colour value (format depends on bit depth)
\033[30-37m— foreground colours\033[40-47m— background colours\033[90-97m— bright foreground\033[100-107m— bright background
\033[38;5;Nm— foreground colour N (0-255)\033[48;5;Nm— background colour N (0-255)
\033[38;2;R;G;Bm— foreground RGB\033[48;2;R;G;Bm— background RGB
colour::safe_esc()
Generate escape code only if terminal supports the requested depth. Gracefully degrades by returning empty string if unsupported.colour::esc()
Returns: Escape sequence if supported, empty string otherwise.
Index lookup (internal helpers)
These functions return numeric colour indices for escape codes.colour::index::4bit()
Get 4-bit ANSI colour code index (30-37, 40-47, 90-97, 100-107).$1- Colour name (e.g.,red,bright green)$2-fgorbg(default:fg)
colour::index::8bit()
Get 8-bit colour index (0-255).$1- Colour specification (named, RGB, or greyscale)
- 0-15: Named colours (0-7 normal, 8-15 bright)
- 16-231: 6×6×6 RGB cube (216 colours)
- 232-255: Greyscale ramp (24 shades)
Text attributes
Functions for text styling (not colour-depth dependent).Attribute enable functions
Attribute enable functions
colour::reset()
Reset all attributes and colours to default.\033[0mcolour::bold()
Enable bold text.\033[1mcolour::dim()
Enable dim/faint text.\033[2mcolour::italic()
Enable italic text (not supported in all terminals).\033[3mcolour::underline()
Enable underlined text.\033[4mcolour::blink()
Enable blinking text (rarely supported).\033[5mcolour::reverse()
Enable reverse video (swap foreground and background).\033[7mcolour::hidden()
Enable hidden/invisible text.\033[8mcolour::strike()
Enable strikethrough text.\033[9mAttribute reset functions
Attribute reset functions
Reset individual attributes without affecting others.
colour::reset::bold()
Reset bold/dim to normal intensity.Escape sequence:\033[22mcolour::reset::dim()
Reset dim (same as reset::bold).Escape sequence:\033[22mcolour::reset::italic()
Reset italic.Escape sequence:\033[23mcolour::reset::underline()
Reset underline.Escape sequence:\033[24mcolour::reset::blink()
Reset blink.Escape sequence:\033[25mcolour::reset::reverse()
Reset reverse video.Escape sequence:\033[27mcolour::reset::hidden()
Reset hidden.Escape sequence:\033[28mcolour::reset::strike()
Reset strikethrough.Escape sequence:\033[29mcolour::reset::fg()
Reset foreground colour to default.Escape sequence:\033[39mcolour::reset::bg()
Reset background colour to default.Escape sequence:\033[49m4-bit named colour shortcuts
Convenience functions for direct colour application.Higher-level helpers
Convenience functions for common colour operations.colour::print()
Print text wrapped in colour with automatic reset.$1- Bit depth:4,8, or24$2-fgorbg$3- Colour specification$4- Text to print
colour::println()
Same ascolour::print() but adds a newline after text.
colour::print()
Output: Coloured text with trailing newline, followed by reset.
colour::wrap()
Wrap text in escape codes and return as string (no direct print).$1- Bit depth:4,8, or24$2-fgorbg$3- Colour specification$4- Text to wrap
colour::strip()
Strip all ANSI escape codes from a string.$1- Text containing escape codes
colour::visible_length()
Return the visible length of a string (excluding escape codes). Useful for padding and alignment with coloured strings.$1- Text (may contain escape codes)
colour::has_colour()
Check if a string contains any ANSI escape codes.$1- Text to check
Usage examples
4-bit colour example
4-bit colour example
8-bit colour example
8-bit colour example
24-bit true colour example
24-bit true colour example
Styled output example
Styled output example