Fix hex parser accepting multiple leading prefixes (#2421)

`hex_to_rgba` previously stripped every leading `#`, so invalid inputs such as `##000000` were accepted despite `is_valid_hex` rejecting them.

Remove only one optional prefix and add regression coverage for the minimized failing input.

Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
This commit is contained in:
Teïlo M
2026-07-11 00:55:51 +02:00
committed by GitHub
co-authored by Jirka Borovec
parent 39c015c0ff
commit 14c3c86e22
3 changed files with 13 additions and 2 deletions
+1
View File
@@ -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.
+1 -1
View File
@@ -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:
+11 -1
View File
@@ -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)