Hex utils
rich-color-ext.hex_utils.py
Helpers for handling hex color codes.
expand_3digit_hex(hex3)
¶
Expand a 3-digit hex string with a leading '#' (e.g. '#ABC') into 6 digits, e.g. '#AABBCC'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex3
|
str
|
The 3-digit hex string, including the leading '#'. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string of format '#RRGGBB'. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input is not a valid '#'-prefixed 3-digit hex representation. |
Source code in src/rich_color_ext/hex_utils.py
is_3digit_hex(string)
¶
Test whether a string is a '#'-prefixed 3-digit hex colour code (e.g. '#ABC'), case-insensitive. Bare words such as 'bad' or 'add' are not hex colours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
input string. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if matches 3-digit hex format. |
Source code in src/rich_color_ext/hex_utils.py
is_dark(hex_str)
¶
Determine if a hex colour is 'dark' based on its luminance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_str
|
str
|
A hex colour string of format '#RRGGBB'. |
required |
Returns: True if the colour is dark, False otherwise.