hoops_ai.insights.utils.ColorPalette
- class hoops_ai.insights.utils.ColorPalette(mapping)
Bases:
objectContainer for label-to-color mappings with convenient access methods.
Stores both RGB colors and text descriptions for each label, providing a clean interface for visualization workflows.
Examples
>>> labels_desc = {0: "background", 17: "through hole", 23: "blind hole"} >>> palette = ColorPalette.from_labels( ... labels_desc, ... cmap_name='hsv', ... reserved_colors={17: (255, 0, 0), 23: (0, 0, 255)} ... ) >>> palette.get_color(17) (255, 0, 0) >>> palette.get_description(17) 'through hole' >>> len(palette) 3 >>> 17 in palette True
- classmethod from_labels(labels, cmap_name='tab20', reserved_colors=None, use_distinct_colors=True)
Create ColorPalette from label descriptions with automatic color generation.
- Parameters:
labels (Dict[int, str]) – Dict mapping label_id -> description string
cmap_name (str) – Matplotlib colormap name (‘tab20’, ‘hsv’, ‘Set3’, ‘viridis’, etc.) Ignored if use_distinct_colors=True
reserved_colors (Dict[int, Tuple[int, int, int]] | None) – Optional dict to force specific colors for certain labels Example: {0: (200, 200, 200), 17: (255, 0, 0)}
use_distinct_colors (bool) – If True, use algorithm to maximize color distinction (recommended)
- Returns:
ColorPalette instance with colors assigned to all labels
- Raises:
ImportError – If matplotlib is not available
- Return type:
Examples
>>> labels = {0: "no-label", 1: "hole", 2: "pocket"} >>> palette = ColorPalette.from_labels( ... labels, ... reserved_colors={0: (200, 200, 200)}, ... use_distinct_colors=True ... )
- get_all_colors()
Get dict of all label -> color mappings.
- get_all_descriptions()
Get dict of all label -> description mappings.
- get_color(label)
Get RGB color for a label.
- get_description(label)
Get text description for a label.
- get_label(label)
Alias for get_description() for more intuitive API.
- items()
Iterate over (label_id, (color, description)) pairs.
- Yields:
Tuples of (label_id, (color_rgb, description))