Skip to content

feat: add support for hexadecimal RGBA color format#2004

Merged
Borda merged 9 commits intoroboflow:developfrom
aza-ali:feat/rgba-hex-color-support
Mar 30, 2026
Merged

feat: add support for hexadecimal RGBA color format#2004
Borda merged 9 commits intoroboflow:developfrom
aza-ali:feat/rgba-hex-color-support

Conversation

@aza-ali
Copy link
Copy Markdown
Contributor

@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
Copy Markdown

CLAassistant commented Nov 4, 2025

CLA assistant check
All committers have signed the CLA.

Borda
Borda previously approved these changes Jan 26, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76%. Comparing base (a6ee3e9) to head (5050b34).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2004   +/-   ##
=======================================
  Coverage       76%     76%           
=======================================
  Files           62      62           
  Lines         7547    7575   +28     
=======================================
+ Hits          5714    5743   +29     
+ Misses        1833    1832    -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Borda
Borda previously approved these changes Jan 26, 2026
Borda and others added 3 commits March 30, 2026 14:46
---
Co-authored-by: Claude Code <noreply@anthropic.com>
- Raise ValueError for out-of-range RGBA/BGRA values, consistent with from_rgb_tuple/from_bgr_tuple
- Add error-path tests for both methods

---
Co-authored-by: Claude Code <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds alpha-channel support to supervision.draw.color.Color, enabling parsing/serialization of 4- and 8-digit hex color codes (#RGBA, #RRGGBBAA) and providing RGBA/BGRA tuple helpers.

Changes:

  • Extend Color with an a (alpha) channel and update hex validation/parsing to accept 3/4/6/8-digit formats.
  • Update as_hex() to emit #RRGGBBAA when a != 255, and add RGBA/BGRA conversion helpers.
  • Expand unit tests to cover new hex formats and tuple conversions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/supervision/draw/color.py Implements RGBA support (alpha field, hex parsing/formatting, RGBA/BGRA helpers, equality/hash updates).
tests/draw/test_color.py Adds/updates tests for RGBA hex parsing, as_hex() alpha behavior, and RGBA/BGRA tuple helpers.

Comment thread src/supervision/draw/color.py Outdated
Comment thread src/supervision/draw/color.py
Comment thread src/supervision/draw/color.py
Comment thread src/supervision/draw/color.py
Comment thread src/supervision/draw/color.py
Borda and others added 3 commits March 30, 2026 15:20
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Fix from_rgba_tuple/from_bgra_tuple docstrings: Google style, pycon doctest format, add Raises section
- Fix as_rgba/as_bgra Returns: remove redundant type restatement, switch to pycon format
- Add test_color_repr and test_color_hash covering alpha-conditional repr and hash/eq contract

---
Co-authored-by: Claude Code <noreply@anthropic.com>
@Borda Borda merged commit fdbbfef into roboflow:develop Mar 30, 2026
24 checks passed
Borda added a commit that referenced this pull request Mar 30, 2026
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

---------

Co-authored-by: jirka <6035284+Borda@users.noreply.github.com>
Co-authored-by: Claude Code <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Borda Borda mentioned this pull request Apr 17, 2026
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.

4 participants