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.
Description
Many users provide
4-or8-digithexadecimal color codes that include an alpha channel (e.g.,#FF00FF80for50%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, annotatorcolor= arguments, etc.)Extend the Color dataclass with an optional a field (default 255):
If
a != 255,as_hex()returns#RRGGBBAA; otherwise it keeps#RRGGBB.Add
as_rgba()helper; keepas_rgb()unchanged.Update
_validate_color_hexto allow lengths3,4,6,8; raiseValueErrorotherwise.Annotators keep their signatures. When a
Colorcarriesalphamake them draw withopacity.Additional