Skip to content

Conversation

@aza-ali
Copy link

@aza-ali aza-ali commented Nov 4, 2025

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

This commit adds support for 4-digit and 8-digit hexadecimal color codes
with alpha channel (e.g., #FF00FF80 for 50% transparent magenta).

Changes:
- Extended Color dataclass with optional alpha field (default 255)
- Updated _validate_color_hex to accept lengths 3, 4, 6, 8
- Updated from_hex to parse 4-digit (#RGBA) and 8-digit (#RRGGBBAA) hex codes
- Modified as_hex to return #RRGGBBAA when alpha != 255
- Added as_rgba() and as_bgra() methods
- Added from_rgba_tuple() and from_bgra_tuple() class methods
- Updated __eq__ and __hash__ to include alpha channel
- Added comprehensive unit tests for all RGBA functionality
- Updated docstrings with examples of new alpha channel support

Resolves roboflow#1849
@aza-ali aza-ali requested a review from SkalskiP as a code owner November 4, 2025 00:45
@CLAassistant
Copy link

CLAassistant commented Nov 4, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for the hexadecimal RGBA format.

2 participants