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, not09f).- The import-time
CSS_MAPconstant was removed. Useget_css_map()instead.See the changelog for details.
Installation
uv (recommended)
# 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",
)
How it works
Color.parse(text)calls Rich's original parser.- If Rich raises
ColorParseError,#abcis expanded to#aabbcc, or the text is looked up (case-insensitively) in the CSS color map. - 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_exthas no side effects: nothing is patched until you callinstall(), and the display-only Rich modules (Panel,Table,Columns) are loaded lazily. install()/uninstall()are serialised with a lock, andis_installed()inspectsColor.parseitself.- The patch is installed as a
classmethod, like Rich's own, soColor.parse(...)works on aColorinstance andMyColor.parse(...)on aColorsubclass returns aMyColor.
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 |
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)
| File | Size | Uploaded | |
|---|---|---|---|
| rich_color_ext-3.0.0.tar.gz | 1.0 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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}
|