From 83dd92726ef2d0d93e534e1f38409a1f5bd9aadf Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 17 Jul 2024 09:42:37 +0200 Subject: [PATCH 1/2] fix `from_matplotlib` when Matplotlib colormaps does not provide colors property --- supervision/draw/color.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index f3c1474a..7b481875 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -352,11 +352,17 @@ class ColorPalette: supervision-annotator-examples/visualized_color_palette.png) """ # noqa: E501 // docs mpl_palette = plt.get_cmap(palette_name, color_count) - colors = [ + + if hasattr(mpl_palette, 'colors'): + colors = mpl_palette.colors + else: + colors = [mpl_palette(i / (color_count - 1)) for i in range(color_count)] + + return cls([ Color(int(r * 255), int(g * 255), int(b * 255)) - for r, g, b, _ in mpl_palette.colors - ] - return cls(colors) + for r, g, b, _ + in colors + ]) def by_idx(self, idx: int) -> Color: """ From c930dd06ec7248450474b190dda39342ce231e54 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Jul 2024 07:45:10 +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 | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 7b481875..8e99ac6c 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -353,16 +353,14 @@ class ColorPalette: """ # noqa: E501 // docs mpl_palette = plt.get_cmap(palette_name, color_count) - if hasattr(mpl_palette, 'colors'): + if hasattr(mpl_palette, "colors"): colors = mpl_palette.colors else: colors = [mpl_palette(i / (color_count - 1)) for i in range(color_count)] - return cls([ - Color(int(r * 255), int(g * 255), int(b * 255)) - for r, g, b, _ - in colors - ]) + return cls( + [Color(int(r * 255), int(g * 255), int(b * 255)) for r, g, b, _ in colors] + ) def by_idx(self, idx: int) -> Color: """