supervision
supervision copied to clipboard
feat: add support for hexadecimal RGBA color format
Summary
Adds support for 4-digit and 8-digit hexadecimal color codes with alpha channel to the Color class, addressing issue #1849.
Users can now use colors like:
#FF00FF80(50% transparent magenta)#f0f8(shorthand 4-digit RGBA)
Changes Made
Core Functionality
- Extended
Colordataclass with optionalafield (alpha channel, default 255) - Updated
_validate_color_hex()to accept lengths 3, 4, 6, and 8 - Modified
from_hex()to parse 4-digit (#RGBA) and 8-digit (#RRGGBBAA) hex codes - Updated
as_hex()to return #RRGGBBAA format when alpha != 255, otherwise #RRGGBB
New Methods
as_rgba()- returns RGBA tupleas_bgra()- returns BGRA tuplefrom_rgba_tuple()- creates Color from RGBA tuplefrom_bgra_tuple()- creates Color from BGRA tuple
Internal Updates
- Updated
__eq__()and__hash__()to include alpha channel - Comprehensive docstrings with examples for all new functionality
Testing
- Added 27 new unit tests covering:
- Parsing 4-digit and 8-digit hex codes with alpha
- Converting to/from RGBA and BGRA tuples
- Round-trip conversions
- Edge cases (alpha=0, alpha=255)
- Invalid inputs
- All 795 tests pass successfully
- Pre-commit hooks pass (ruff, bandit, codespell, etc.)
Backward Compatibility
✅ Fully backward compatible - all existing code continues to work:
- Default alpha value is 255 (fully opaque)
- Existing 3-digit and 6-digit hex codes work as before
as_hex()returns 6-digit format when alpha=255- Public API remains unchanged
Resolves
Closes #1849