hexrgba
Parse, validate and normalize CSS hex colors and named colors, with exact alpha handling,
CSS rgb() / rgba() output, WCAG contrast and alpha blending. Typed, zero dependencies.
Install
pip install hexrgba
Usage
from hexrgba import Color, contrast_ratio, is_hex, normalize, parse, parse_color, to_hex
is_hex("#f00") # True
parse("#ff000080") # (255, 0, 0, 128)
normalize("#F00") # "#ff0000"
normalize("#AABBCC", short=True) # "#abc"
to_hex(255, 0, 128, 64) # "#ff008040"
color = parse_color("RebeccaPurple") # Color(r=102, g=51, b=153, a=255)
color.hex # "#663399"
color.to_css() # "rgb(102, 51, 153)"
parse_color("#ff00ffed").to_css() # "rgba(255, 0, 255, 0.93)"
parse_color("#00ffff").name # "aqua"
Color.from_rgba(255, 0, 0, 0.5).hex # "#ff000080"
contrast_ratio("#767676", "white") # 4.54...
What it covers:
- All four hex forms (
#rgb,#rgba,#rrggbb,#rrggbbaa) and all 148 CSS named colors plustransparent, matched ASCII case-insensitively as CSS requires. - Strict or lenient.
strict=Truefollows the CSS grammar (leading#, no surrounding whitespace). The default also accepts a missing#and strips surrounding Python whitespace (str.strip()). - A frozen
Colorwithhex,to_hex(short=True),to_css(),name,alpha,with_alpha()andfrom_rgba(). - Exact alpha.
alpha_to_byteandcss_alphafollow CSS Color 4; every alpha byte round-trips through its 0..1 float and through its CSS string. - WCAG 2.x
relative_luminance,contrast_ratioandpasses_contrast(AA / AAA). - Blending of a translucent color over a background with
blend. - Typed errors, all
ValueError:ColorError,HexColorError,ColorNameError,ChannelError.
The full guide, with the exact rules and the sources they come from, is in GUIDE.md.
Command line
hexrgba normalize "#FFAA00" # #ffaa00
hexrgba normalize "#aabbcc" --short # #abc
hexrgba convert "#ff00ffed" --to css # rgba(255, 0, 255, 0.93)
hexrgba convert "#00ffff" --to name # aqua
hexrgba contrast "#767676" white # 4.54:1
hexrgba contrast "#777" white --level AA # prints 4.47:1, exits 1
hexrgba blend "#0000ff80" red # #7f0080
Quote a leading #: most shells read it as the start of a comment. Errors go to stderr.
Exit codes: 0 success, 1 the pair fails --level, 2 bad usage or an invalid color. python -m hexrgba works too.
Why
webcolors is built around the CSS named-color tables, and most other color packages pull
in a wider model of color spaces. hexrgba stays small and exact: the hex and keyword
syntax of CSS, checked against the spec tables and the Web Platform Tests vectors, an
alpha conversion that never drifts, and the WCAG contrast math, with no dependencies.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for hexrgba 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hexrgba-0.2.1.tar.gz | 30.7 kB | Without provenance | Details for hexrgba-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hexrgba-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for hexrgba-0.2.1-py3-none-any.whl |
Total release size: 47.3 kB
Release files / hexrgba-0.2.1.tar.gz
| Download URL | hexrgba-0.2.1.tar.gz |
|---|---|
| Size | 30.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
90ce7b085b037ffb9b30eeffc95a62c922eaed3dbcffa2627384fa2d27b838b3
|
|
BLAKE2b-256 checksum How to use checksums |
fff2527e32c186315a3e570af93d81bddb792dbd635f3d807885d8bccfd3c146
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / hexrgba-0.2.1-py3-none-any.whl
| Download URL | hexrgba-0.2.1-py3-none-any.whl |
|---|---|
| Size | 16.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d7382abde497f0ca09913d2608800a64fe49ff57bc33553b8ebd57e41d8621d0
|
|
BLAKE2b-256 checksum How to use checksums |
3cc651390b64536edcf4661bf7f11a786be5536ec2c5d7cb6b48258918aa546c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for hexrgba 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.