From bb4d741aff4d2390aad02a32c29d994ac10ebe96 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Fri, 19 Jan 2024 00:29:46 +0300 Subject: [PATCH 1/3] =?UTF-8?q?feat(color.py):=20=E2=9C=A8=20add=20color?= =?UTF-8?q?=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds the WHITE, BLACK, RED, GREEN, and BLUE color constants to the Color class in color.py. These constants represent the corresponding colors in the RGB color space. Signed-off-by: Onuralp SEZER --- supervision/annotators/core.py | 2 +- supervision/detection/annotate.py | 2 +- supervision/detection/line_counter.py | 4 +- supervision/detection/tools/polygon_zone.py | 2 +- supervision/draw/color.py | 48 ++++++++++++++++----- supervision/draw/utils.py | 2 +- test/draw/test_color.py | 24 +++++------ 7 files changed, 56 insertions(+), 28 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index fdfb9a58..9ae81c95 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -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, diff --git a/supervision/detection/annotate.py b/supervision/detection/annotate.py index 5e9b379a..1ab1bdc2 100644 --- a/supervision/detection/annotate.py +++ b/supervision/detection/annotate.py @@ -28,7 +28,7 @@ class BoxAnnotator: 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, diff --git a/supervision/detection/line_counter.py b/supervision/detection/line_counter.py index 0b8a1321..8fd3de4c 100644 --- a/supervision/detection/line_counter.py +++ b/supervision/detection/line_counter.py @@ -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, diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index 8e18d8bd..c6f5b80e 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -90,7 +90,7 @@ class PolygonZoneAnnotator: 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, diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 9d1b4ad9..cac00130 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -47,6 +47,12 @@ class Color: g: int b: int + _WHITE = None + _BLACK = None + _RED = None + _GREEN = None + _BLUE = None + @classmethod def from_hex(cls, color_hex: str) -> Color: """ @@ -116,25 +122,40 @@ class Color: """ return self.b, self.g, self.r + @property @classmethod - def white(cls) -> Color: - return Color.from_hex(color_hex="#ffffff") + def WHITE(cls): + if cls._WHITE is None: + cls._WHITE = cls.from_hex("#ffffff") + return cls._WHITE + @property @classmethod - def black(cls) -> Color: - return Color.from_hex(color_hex="#000000") + def BLACK(cls): + if cls._BLACK is None: + cls._BLACK = cls.from_hex("#000000") + return cls._BLACK + @property @classmethod - def red(cls) -> Color: - return Color.from_hex(color_hex="#ff0000") + def RED(cls): + if cls._RED is None: + cls._RED = cls.from_hex("#ff0000") + return cls._RED + @property @classmethod - def green(cls) -> Color: - return Color.from_hex(color_hex="#00ff00") + def GREEN(cls): + if cls._GREEN is None: + cls._GREEN = cls.from_hex("#00ff00") + return cls._GREEN + @property @classmethod - def blue(cls) -> Color: - return Color.from_hex(color_hex="#0000ff") + def BLUE(cls): + if cls._BLUE is None: + cls._BLUE = cls.from_hex("#0000ff") + return cls._BLUE @dataclass @@ -197,3 +218,10 @@ class ColorPalette: 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") diff --git a/supervision/draw/utils.py b/supervision/draw/utils.py index 34c11a9f..10f5c932 100644 --- a/supervision/draw/utils.py +++ b/supervision/draw/utils.py @@ -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, diff --git a/test/draw/test_color.py b/test/draw/test_color.py index eb4c3657..b9dbc440 100644 --- a/test/draw/test_color.py +++ b/test/draw/test_color.py @@ -9,13 +9,13 @@ from supervision.draw.color import Color @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)), @@ -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()), ], ) From 6fee6268280eefe014bfeae9b27a8106cdf8dc36 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Fri, 19 Jan 2024 01:41:31 +0300 Subject: [PATCH 2/3] =?UTF-8?q?refactor(Color):=20=F0=9F=8E=A8=20Deprecate?= =?UTF-8?q?=20color=20methods=20in=20favor=20of=20color=20properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deprecated the `white`, `black`, `red`, `green`, and `blue` methods in the `Color` class. Users should now use the `WHITE`, `BLACK`, `RED`, `GREEN`, and `BLUE` properties instead. This change improves the API and makes it more consistent. Signed-off-by: Onuralp SEZER --- supervision/draw/color.py | 73 +++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index cac00130..2fddb684 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -2,6 +2,7 @@ from __future__ import annotations from dataclasses import dataclass from typing import List, Tuple +from supervision.utils.internal import deprecated DEFAULT_COLOR_PALETTE = [ "#a351fb", @@ -47,12 +48,6 @@ class Color: g: int b: int - _WHITE = None - _BLACK = None - _RED = None - _GREEN = None - _BLUE = None - @classmethod def from_hex(cls, color_hex: str) -> Color: """ @@ -123,39 +118,65 @@ class Color: return self.b, self.g, self.r @property - @classmethod def WHITE(cls): - if cls._WHITE is None: - cls._WHITE = cls.from_hex("#ffffff") - return cls._WHITE + return cls.from_hex("#ffffff") @property - @classmethod def BLACK(cls): - if cls._BLACK is None: - cls._BLACK = cls.from_hex("#000000") - return cls._BLACK + return cls.from_hex("#000000") @property - @classmethod def RED(cls): - if cls._RED is None: - cls._RED = cls.from_hex("#ff0000") - return cls._RED + return cls.from_hex("#ff0000") @property - @classmethod def GREEN(cls): - if cls._GREEN is None: - cls._GREEN = cls.from_hex("#00ff00") - return cls._GREEN + return cls.from_hex("#00ff00") @property - @classmethod def BLUE(cls): - if cls._BLUE is None: - cls._BLUE = cls.from_hex("#0000ff") - return cls._BLUE + 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") + @dataclass From 38bdeff1715147eaf818daccaad486538333499d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:43:26 +0000 Subject: [PATCH 3/3] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/draw/color.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 2fddb684..fe55b25c 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -2,6 +2,7 @@ from __future__ import annotations from dataclasses import dataclass from typing import List, Tuple + from supervision.utils.internal import deprecated DEFAULT_COLOR_PALETTE = [ @@ -136,7 +137,7 @@ class Color: @property def BLUE(cls): return cls.from_hex("#0000ff") - + @classmethod @deprecated( "`Color.white()` is deprecated and will be removed in " @@ -178,7 +179,6 @@ class Color: return Color.from_hex(color_hex="#0000ff") - @dataclass class ColorPalette: colors: List[Color]