Skip to content

Add support for the hexadecimal RGBA format. #1849

@Oilgrim

Description

@Oilgrim

Description

Many users provide 4- or 8-digit hexadecimal color codes that include an alpha channel (e.g., #FF00FF80 for 50% transparent magenta). The library should be extended to support these formats natively, without introducing any changes to the existing public API.

  • Parse 4- and 8-digit hex anywhere a color hex is allowed (Color.from_hex, ColorPalette, annotator color= arguments, etc.)

  • Extend the Color dataclass with an optional a field (default 255):

    from supervision import sv
    
    sv.Color(r=255, g=0, b=255, a=128)
  • If a != 255, as_hex() returns #RRGGBBAA; otherwise it keeps #RRGGBB.
    Add as_rgba() helper; keep as_rgb() unchanged.

  • Update _validate_color_hex to allow lengths 3, 4, 6, 8; raise ValueError otherwise.

  • Annotators keep their signatures. When a Color carries alpha make them draw with opacity.

Additional

  • Please share a Google Colab with minimal code to test the new feature. We know it's additional work, but it will speed up the review process. The reviewer must test each change. Setting up a local environment to do this is time-consuming. Please ensure that Google Colab can be accessed without any issues (make it public). Thank you! 🙏🏻
  • Provide full unit-test coverage matching existing test style.
  • Update the library docs (docstrings, mkdocs) to reflect the new API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions