supervision icon indicating copy to clipboard operation
supervision copied to clipboard

feat: add support for hexadecimal RGBA color format

Open aza-ali opened this issue 1 month ago • 1 comments

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 Color dataclass with optional a field (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 tuple
  • as_bgra() - returns BGRA tuple
  • from_rgba_tuple() - creates Color from RGBA tuple
  • from_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

aza-ali avatar Nov 04 '25 00:11 aza-ali

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 04 '25 00:11 CLAassistant