Skip to main content

rich-color-ext logo

Python versions PyPI version uv badge

Docs build status Docs

rich-color-ext extends the great rich library to be able to parse 3-digit hex colors (ie. #09F, the # is required) and CSS color names (ie. rebeccapurple).

Rich's own parser always runs first. The extensions only apply to colors Rich rejects, so everything Rich already understands (including ANSI names like red) behaves exactly as before.

Warning: Breaking changes in v3.0.0

  • Item
  • Rich's parser now runs first; CSS names and 3-digit hex are only a fallback for colors Rich rejects. Names Rich already knows (red, green, white, orchid, ...) keep Rich's own color instead of the CSS truecolor value.
  • 3-digit hex requires the leading # (#09f, not 09f).
  • The import-time CSS_MAP constant was removed. Use get_css_map() instead.

See the changelog for details.

Installation

# via uv directly
uv add rich-color-ext

or

# or via pip through uv
uv pip install rich-color-ext

pip

pip install rich-color-ext

Usage

To make use of rich-color-ext all you need to do is import and install it at the start of your program:

from rich_color_ext import install
from rich.console import Console
from rich.panel import Panel

install()  # Patch Rich's Color.parse method

console = Console(width=64)
console.print(
    Panel(
        "This is the [b #00ff99]rich_color_ext[/b #00ff99] example for printing CSS named colors ([bold rebeccapurple]rebeccapurple[/bold rebeccapurple]), 3-digit hex colors ([bold #f0f]#f0f[/bold #f0f]), and [b #99ff00]rich.color_triplet.ColorTriplet[/b #99ff00] & [b #00ff00]rich.color.Color[/b #00ff00] instances.",
        padding=(1, 2),
    ),
    justify="center",
)

example

How it works

  1. Color.parse(text) calls Rich's original parser.
  2. If Rich raises ColorParseError, #abc is expanded to #aabbcc, or the text is looked up (case-insensitively) in the CSS color map.
  3. Anything else re-raises Rich's original ColorParseError.

Results are cached with functools.lru_cache, like Rich's own parser.

Install, uninstall and inspect

from rich_color_ext import get_css_map, install, is_installed, uninstall

install()  # safe to call more than once, and from multiple threads
assert is_installed()

get_css_map()["rebeccapurple"]  # '#663399'

uninstall()  # restores Rich's original Color.parse exactly
  • Importing rich_color_ext has no side effects: nothing is patched until you call install(), and the display-only Rich modules (Panel, Table, Columns) are loaded lazily.
  • install() / uninstall() are serialised with a lock, and is_installed() inspects Color.parse itself.
  • The patch is installed as a classmethod, like Rich's own, so Color.parse(...) works on a Color instance and MyColor.parse(...) on a Color subclass returns a MyColor.

Migrating from v2

v2 v3
Color.parse("red") → CSS truecolor #ff0000 Rich's ANSI red (Rich wins)
Color.parse("09f") → #0099ff ColorParseError; use #09f
from rich_color_ext import CSS_MAP from rich_color_ext import get_css_map

rich-color-ext by Max Ludden

Metadata

Release files for rich-color-ext 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rich-color-ext 3.0.0
File Size Uploaded
rich_color_ext-3.0.0.tar.gz 1.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for rich-color-ext 3.0.0
File Interpreter ABI Platform
rich_color_ext-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.0 MB

Release files / rich_color_ext-3.0.0.tar.gz

Download URL rich_color_ext-3.0.0.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
1bcdf8ac4aa3e9a13dd58b19597d527ec11a36c705b5d212f8ad8c2e8fdf529c
BLAKE2b-256 checksum
How to use checksums
f36b413e81401b11ce2a3b6dbaf0c1c78e7aee814fe4af4717e3dc79a0efb906
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / rich_color_ext-3.0.0-py3-none-any.whl

Download URL rich_color_ext-3.0.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c81f54fb3a979184dcbc897fe29568d2a0139519fed6bcda17a4eb190865e49
BLAKE2b-256 checksum
How to use checksums
b89e674b30d907543c8ae8cc159f942d408f7131c5075b8c484b37f7b51119c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.0.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page