diff --git a/docs/changelog.md b/docs/changelog.md index adedcb3f..f4515da9 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,6 +18,7 @@ date_modified: 2026-07-08 - `sv.mask_non_max_merge` now computes exact mask overlap at the original mask resolution and ignores the deprecated `mask_dimension` parameter. Code that relied on downscaled mask overlap should recalibrate thresholds; passing `mask_dimension` positionally now emits a deprecation warning, and the parameter is scheduled for removal in `0.33.0` ([#2400](https://github.com/roboflow/supervision/pull/2400)). ### Fixed +- `sv.hex_to_rgba` now rejects multiple leading `#` characters instead of silently normalizing them, matching `sv.is_valid_hex` and the documented single optional prefix. - `sv.box_iou_batch` now upcasts box corners to `float64` before computing areas and intersections, returning `float32`. This fixes integer-dtype overflow (e.g. `int32` coordinates around `50_000` could previously wrap to a negative area and produce an incorrect `0.0` IoU) and gives full `float64` precision to callers that pass `float64`/`int64` coordinates directly. It does not recover precision already lost when coordinates are stored as `float32` before this function is called (e.g. `Detections.xyxy`, which is `float32` throughout the library) — such callers must upcast their own arrays to `float64`/`int64` before calling `box_iou_batch` to benefit from this fix. Results for small-coordinate inputs are unchanged. - Legacy COCO prediction loading in `sv.EvaluationDataset.load_predictions` now raises `ValueError` for image ids absent from the ground-truth COCO set instead of relying on a bare `assert`, so the check is no longer silently skipped under `python -O`. - `sv.HeatMapAnnotator` now exposes a `reset()` method to clear accumulated heat, so a single annotator instance can be reused across independent streams without carrying over heat from a previous stream. diff --git a/src/supervision/annotators/utils.py b/src/supervision/annotators/utils.py index 040dd2c6..afb72d05 100644 --- a/src/supervision/annotators/utils.py +++ b/src/supervision/annotators/utils.py @@ -427,7 +427,7 @@ def hex_to_rgba(hex_color: str) -> tuple[int, int, int, int]: ``` """ - hex_color = hex_color.strip().lstrip("#") + hex_color = hex_color.strip().removeprefix("#") if len(hex_color) == 6: hex_color += "FF" # default full opacity if len(hex_color) != 8: diff --git a/tests/annotators/test_utils.py b/tests/annotators/test_utils.py index eb6c1aaf..6376a30a 100644 --- a/tests/annotators/test_utils.py +++ b/tests/annotators/test_utils.py @@ -262,8 +262,18 @@ def test_hex_to_rgba_valid( assert hex_to_rgba(hex_color) == expected_rgba -@pytest.mark.parametrize("hex_color", ["#FF00F", "#GGHHII", "#FFF", "1234567"]) +@pytest.mark.parametrize( + "hex_color", + [ + pytest.param("#FF00F", id="five-digits"), + pytest.param("#GGHHII", id="non-hex-digits"), + pytest.param("#FFF", id="short-form"), + pytest.param("1234567", id="seven-digits"), + pytest.param("##000000", id="multiple-prefixes"), + ], +) def test_hex_to_rgba_invalid(hex_color: str) -> None: + """Invalid formats raise instead of being normalized into valid colors.""" with pytest.raises(ValueError, match="Invalid hex"): hex_to_rgba(hex_color)