Skip to content

Commit

Permalink
Adressing changes requested
Browse files Browse the repository at this point in the history
- fixed a bug with the text color on draw_boxes.py and draw_points.py
- renamed params on color.py
- added mising typehints in color.py
- typos in color.py
- removed unused imports in absolute_grid.y
  • Loading branch information
javiber committed Nov 21, 2022
1 parent df9d897 commit d567ec6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
1 change: 0 additions & 1 deletion norfair/drawing/absolute_grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import lru_cache
from typing import Optional, Tuple

import numpy as np

Expand Down
38 changes: 22 additions & 16 deletions norfair/drawing/color.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import re
from typing import Any, Iterable, Tuple, Union
from typing import Any, Hashable, Iterable, Tuple, Union

# types

ColorType = Tuple[int, int, int]
ColorLike = Union[ColorType, str]


def hex_to_bgr(string: str) -> ColorType:
"""Converts convencional 6 digits hex colors to BGR tuples
def hex_to_bgr(hex_value: str) -> ColorType:
"""Converts conventional 6 digits hex colors to BGR tuples
Parameters
----------
Expand All @@ -25,10 +25,16 @@ def hex_to_bgr(string: str) -> ColorType:
ValueError
if the string is invalid
"""
# TODO: support 3-digits hex
if re.match("#[a-f0-9]{6}$", string):
return int(string[3:5], 16), int(string[5:7], 16), int(string[1:3], 16)
raise ValueError(f"'{string}' is not a valid color")
if re.match("#[a-f0-9]{6}$", hex_value):
return int(hex_value[3:5], 16), int(hex_value[5:7], 16), int(hex_value[1:3], 16)

if re.match("#[a-f0-9]{3}$", hex_value):
return (
int(hex_value[3] * 2, 16),
int(hex_value[2] * 2, 16),
int(hex_value[1] * 2, 16),
)
raise ValueError(f"'{hex_value}' is not a valid color")


class Color:
Expand Down Expand Up @@ -223,12 +229,12 @@ class Color:
cb10 = hex_to_bgr("#56b4e9")


def parse_color(value: ColorLike) -> ColorType:
def parse_color(color_like: ColorLike) -> ColorType:
"""Makes best effort to parse the given value to a Color
Parameters
----------
value : ColorLike
color_like : ColorLike
Can be one of:
1. a string with the 6 digits hex value (`"#ff0000"`)
Expand All @@ -240,13 +246,13 @@ def parse_color(value: ColorLike) -> ColorType:
Color
The BGR tuple.
"""
if isinstance(value, str):
if value.startswith("#"):
return hex_to_bgr(value)
if isinstance(color_like, str):
if color_like.startswith("#"):
return hex_to_bgr(color_like)
else:
return getattr(Color, value)
# TODO: validate value?
return tuple([int(v) for v in value])
return getattr(Color, color_like)
# TODO: validate?
return tuple([int(v) for v in color_like])


PALETTES = {
Expand Down Expand Up @@ -355,7 +361,7 @@ def set_default_color(cls, color: ColorLike):
cls._default_color = parse_color(color)

@classmethod
def choose_color(cls, hashable):
def choose_color(cls, hashable: Hashable) -> ColorType:
if hashable is None:
return cls._default_color
return cls._colors[abs(hash(hashable)) % len(cls._colors)]
5 changes: 4 additions & 1 deletion norfair/drawing/draw_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def draw_boxes(
if drawables is None:
return frame

if text_color is not None:
text_color = parse_color(text_color)

for obj in drawables:
d = Drawable(obj)

Expand All @@ -150,7 +153,7 @@ def draw_boxes(
if text_color is None:
obj_text_color = obj_color
else:
obj_text_color = color
obj_text_color = text_color
# the anchor will become the bottom-left of the text,
# we select-top left of the bbox compensating for the thickness of the box
text_anchor = (
Expand Down
6 changes: 5 additions & 1 deletion norfair/drawing/draw_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def draw_points(

if drawables is None:
return

if text_color is not None:
text_color = parse_color(text_color)

if color is None:
color = "by_id"
if thickness is None:
Expand All @@ -135,7 +139,7 @@ def draw_points(
if text_color is None:
obj_text_color = obj_color
else:
obj_text_color = color
obj_text_color = text_color

if draw_points:
for point, live in zip(d.points, d.live_points):
Expand Down

0 comments on commit d567ec6

Please sign in to comment.