From e6f0ae8514c20b8f19e95281eb9dfb6c618e3c02 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Sun, 26 Nov 2023 11:25:59 +0100 Subject: [PATCH 1/2] Add 'as_hex' method and corresponding test to Color class. --- supervision/draw/color.py | 17 ++++++++++++++++- test/draw/test_color.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 69bcc9c5..dd9a5e82 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -68,9 +68,24 @@ class Color: color_hex = color_hex.lstrip("#") if len(color_hex) == 3: color_hex = "".join(c * 2 for c in color_hex) - r, g, b = (int(color_hex[i : i + 2], 16) for i in range(0, 6, 2)) + r, g, b = (int(color_hex[i: i + 2], 16) for i in range(0, 6, 2)) return cls(r, g, b) + def as_hex(self) -> str: + """ + Converts the Color instance to a hex string. + + Returns: + str: The hexadecimal color string. + + Example: + ``` + >>> Color(r=255, g=0, b=255).as_hex() + '#ff00ff' + ``` + """ + return f"#{self.r:02x}{self.g:02x}{self.b:02x}" + def as_rgb(self) -> Tuple[int, int, int]: """ Returns the color as an RGB tuple. diff --git a/test/draw/test_color.py b/test/draw/test_color.py index 723f7043..732e841e 100644 --- a/test/draw/test_color.py +++ b/test/draw/test_color.py @@ -30,3 +30,22 @@ def test_color_from_hex( with exception: result = Color.from_hex(color_hex=color_hex) assert result == expected_result + + +@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(r=128, g=128, b=0), "#808000", DoesNotRaise()), + ] +) +def test_color_as_hex( + color: Color, expected_result: Optional[str], exception: Exception +) -> None: + with exception: + result = color.as_hex() + assert result == expected_result From 242ac5f7db9e924f47d7c8662f8dd18f60d8922f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 10:27:43 +0000 Subject: [PATCH 2/2] =?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 | 2 +- test/draw/test_color.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index dd9a5e82..9d1b4ad9 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -68,7 +68,7 @@ class Color: color_hex = color_hex.lstrip("#") if len(color_hex) == 3: color_hex = "".join(c * 2 for c in color_hex) - r, g, b = (int(color_hex[i: i + 2], 16) for i in range(0, 6, 2)) + r, g, b = (int(color_hex[i : i + 2], 16) for i in range(0, 6, 2)) return cls(r, g, b) def as_hex(self) -> str: diff --git a/test/draw/test_color.py b/test/draw/test_color.py index 732e841e..eb4c3657 100644 --- a/test/draw/test_color.py +++ b/test/draw/test_color.py @@ -41,7 +41,7 @@ def test_color_from_hex( (Color.green(), "#00ff00", DoesNotRaise()), (Color.blue(), "#0000ff", DoesNotRaise()), (Color(r=128, g=128, b=0), "#808000", DoesNotRaise()), - ] + ], ) def test_color_as_hex( color: Color, expected_result: Optional[str], exception: Exception