Merge pull request #1372 from roboflow/fix/from_matplotlib_when_colormap_does_not_provide_colors_attribute

fix `from_matplotlib` when Matplotlib colormaps does not provide `colors` property
This commit is contained in:
Piotr Skalski 2024-07-17 10:03:11 +02:00 committed by GitHub
commit 31db574e63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 5 deletions

View File

@ -352,11 +352,15 @@ class ColorPalette:
supervision-annotator-examples/visualized_color_palette.png)
""" # noqa: E501 // docs
mpl_palette = plt.get_cmap(palette_name, color_count)
colors = [
Color(int(r * 255), int(g * 255), int(b * 255))
for r, g, b, _ in mpl_palette.colors
]
return cls(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]
)
def by_idx(self, idx: int) -> Color:
"""