def _validate_colormap_config(colormap_config, ptype):
    """Validate the colormap configuration provided by the user."""

    # Ensure colormap_config has the necessary attributes
    required_attrs = ["cmap", "norm", "clevs"]
    missing_attrs = [
        attr for attr in required_attrs if not hasattr(colormap_config, attr)
    ]
    if missing_attrs:
        raise ValueError(
            f"colormap_config is missing required attributes: {', '.join(missing_attrs)}"
        )

    # Ensure that ptype is appropriate when colormap_config is provided
    if ptype not in ["intensity", "depth"]:
        raise ValueError(
            "colormap_config is only supported for ptype='intensity' or 'depth'"
        )

    cmap = colormap_config.cmap
    clevs = colormap_config.clevs

    # Validate that the number of colors matches len(clevs)
    if isinstance(cmap, colors.ListedColormap):
        num_colors = len(cmap.colors)
    else:
        num_colors = cmap.N

    expected_colors = len(clevs)
    if num_colors != expected_colors:
        raise ValueError(
            f"Number of colors in colormap (N={num_colors}) does not match len(clevs) (N={expected_colors})."
        )

    return colormap_config.cmap, colormap_config.norm, colormap_config.clevs
Validate the colormap configuration provided by the user.
pysteps/visualization/precipfields.py