Skip to content

Commit

Permalink
Merge pull request roboflow#756 from roboflow/q1-2024/color-properties
Browse files Browse the repository at this point in the history
feat(color.py): ✨ add color constants/properties
  • Loading branch information
SkalskiP authored Jan 19, 2024
2 parents d503e3a + 38bdeff commit 4e4fad9
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
2 changes: 1 addition & 1 deletion supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ class LabelAnnotator:
def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.default(),
text_color: Color = Color.black(),
text_color: Color = Color.BLACK,
text_scale: float = 0.5,
text_thickness: int = 1,
text_padding: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion supervision/detection/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.default(),
thickness: int = 2,
text_color: Color = Color.black(),
text_color: Color = Color.BLACK,
text_scale: float = 0.5,
text_thickness: int = 1,
text_padding: int = 10,
Expand Down
4 changes: 2 additions & 2 deletions supervision/detection/line_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ class LineZoneAnnotator:
def __init__(
self,
thickness: float = 2,
color: Color = Color.white(),
color: Color = Color.WHITE,
text_thickness: float = 2,
text_color: Color = Color.black(),
text_color: Color = Color.BLACK,
text_scale: float = 0.5,
text_offset: float = 1.5,
text_padding: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion supervision/detection/tools/polygon_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(
zone: PolygonZone,
color: Color,
thickness: int = 2,
text_color: Color = Color.black(),
text_color: Color = Color.BLACK,
text_scale: float = 0.5,
text_thickness: int = 1,
text_padding: int = 10,
Expand Down
49 changes: 49 additions & 0 deletions supervision/draw/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from dataclasses import dataclass
from typing import List, Tuple

from supervision.utils.internal import deprecated

DEFAULT_COLOR_PALETTE = [
"#a351fb",
"#e6194b",
Expand Down Expand Up @@ -116,23 +118,63 @@ def as_bgr(self) -> Tuple[int, int, int]:
"""
return self.b, self.g, self.r

@property
def WHITE(cls):
return cls.from_hex("#ffffff")

@property
def BLACK(cls):
return cls.from_hex("#000000")

@property
def RED(cls):
return cls.from_hex("#ff0000")

@property
def GREEN(cls):
return cls.from_hex("#00ff00")

@property
def BLUE(cls):
return cls.from_hex("#0000ff")

@classmethod
@deprecated(
"`Color.white()` is deprecated and will be removed in "
"`supervision-0.20.0`. Use `Color.WHITE` instead."
)
def white(cls) -> Color:
return Color.from_hex(color_hex="#ffffff")

@classmethod
@deprecated(
"`Color.black()` is deprecated and will be removed in "
"`supervision-0.20.0`. Use `Color.BLACK` instead."
)
def black(cls) -> Color:
return Color.from_hex(color_hex="#000000")

@classmethod
@deprecated(
"`Color.red()` is deprecated and will be removed in "
"`supervision-0.20.0`. Use `Color.RED` instead."
)
def red(cls) -> Color:
return Color.from_hex(color_hex="#ff0000")

@classmethod
@deprecated(
"`Color.green()` is deprecated and will be removed in "
"`supervision-0.20.0`. Use `Color.GREEN` instead."
)
def green(cls) -> Color:
return Color.from_hex(color_hex="#00ff00")

@classmethod
@deprecated(
"`Color.blue()` is deprecated and will be removed in "
"`supervision-0.20.0`. Use `Color.BLUE` instead."
)
def blue(cls) -> Color:
return Color.from_hex(color_hex="#0000ff")

Expand Down Expand Up @@ -197,3 +239,10 @@ def by_idx(self, idx: int) -> Color:
raise ValueError("idx argument should not be negative")
idx = idx % len(self.colors)
return self.colors[idx]


Color.WHITE = Color.from_hex("#ffffff")
Color.BLACK = Color.from_hex("#000000")
Color.RED = Color.from_hex("#ff0000")
Color.GREEN = Color.from_hex("#00ff00")
Color.BLUE = Color.from_hex("#0000ff")
2 changes: 1 addition & 1 deletion supervision/draw/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def draw_text(
scene: np.ndarray,
text: str,
text_anchor: Point,
text_color: Color = Color.black(),
text_color: Color = Color.BLACK,
text_scale: float = 0.5,
text_thickness: int = 1,
text_padding: int = 10,
Expand Down
24 changes: 12 additions & 12 deletions test/draw/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
@pytest.mark.parametrize(
"color_hex, expected_result, exception",
[
("fff", Color.white(), DoesNotRaise()),
("#fff", Color.white(), DoesNotRaise()),
("ffffff", Color.white(), DoesNotRaise()),
("#ffffff", Color.white(), DoesNotRaise()),
("f00", Color.red(), DoesNotRaise()),
("0f0", Color.green(), DoesNotRaise()),
("00f", Color.blue(), DoesNotRaise()),
("fff", Color.WHITE, DoesNotRaise()),
("#fff", Color.WHITE, DoesNotRaise()),
("ffffff", Color.WHITE, DoesNotRaise()),
("#ffffff", Color.WHITE, DoesNotRaise()),
("f00", Color.RED, DoesNotRaise()),
("0f0", Color.GREEN, DoesNotRaise()),
("00f", Color.BLUE, DoesNotRaise()),
("#808000", Color(r=128, g=128, b=0), DoesNotRaise()),
("", None, pytest.raises(ValueError)),
("00", None, pytest.raises(ValueError)),
Expand All @@ -35,11 +35,11 @@ def test_color_from_hex(
@pytest.mark.parametrize(
"color, expected_result, exception",
[
(Color.white(), "#ffffff", DoesNotRaise()),
(Color.black(), "#000000", DoesNotRaise()),
(Color.red(), "#ff0000", DoesNotRaise()),
(Color.green(), "#00ff00", DoesNotRaise()),
(Color.blue(), "#0000ff", DoesNotRaise()),
(Color.WHITE, "#ffffff", DoesNotRaise()),
(Color.BLACK, "#000000", DoesNotRaise()),
(Color.RED, "#ff0000", DoesNotRaise()),
(Color.GREEN, "#00ff00", DoesNotRaise()),
(Color.BLUE, "#0000ff", DoesNotRaise()),
(Color(r=128, g=128, b=0), "#808000", DoesNotRaise()),
],
)
Expand Down

0 comments on commit 4e4fad9

Please sign in to comment.