Repository: scikit-learn/scikit-learn
File: sklearn/inspection/_plot/decision_boundary.py (Python)

Code before:
                    warnings.warn(
                        f"'{kwarg}' is ignored in favor of 'multiclass_colors' "
                        "in the multiclass case."
                    )
                    del kwargs[kwarg]

            if self.multiclass_colors is None or isinstance(
                self.multiclass_colors, str
            ):
                if self.multiclass_colors is None:
                    cmap = "tab10" if self.n_classes <= 10 else "gist_rainbow"
                else:
                    cmap = self.multiclass_colors

                # Special case for the tab10 and tab20 colormaps that encode a
                # discrete set of colors that are easily distinguishable
                # contrary to other colormaps that are continuous.
                if cmap == "tab10" and self.n_classes <= 10:
                    colors = plt.get_cmap("tab10", 10).colors[: self.n_classes]
                elif cmap == "tab20" and self.n_classes <= 20:
                    colors = plt.get_cmap("tab20", 20).colors[: self.n_classes]
                else:
                    cmap = plt.get_cmap(cmap, self.n_classes)
                    if not hasattr(cmap, "colors"):
                        # For LinearSegmentedColormap
                        colors = cmap(np.linspace(0, 1, self.n_classes))
                    else:
                        colors = cmap.colors
            elif isinstance(self.multiclass_colors, list):
                colors = [mpl.colors.to_rgba(color) for color in self.multiclass_colors]
            else:
                raise ValueError("'multiclass_colors' must be a list or a str.")

            self.multiclass_colors_ = colors

            if plot_method == "contour":
                # Plot only argmax
                response = (
                    self.response.argmax(axis=2)
                    if self.response.ndim == 3
                    else self.response
                )
                self.surface_ = plot_func(
                    self.xx0, self.xx1, response, colors=colors, **kwargs
                )
            elif plot_method == "pcolormesh" and self.response.ndim == 2:  # predict
                cmap = mpl.colors.ListedColormap(colors)
                self.surface_ = plot_func(
                    self.xx0, self.xx1, self.response, cmap=cmap, **kwargs
                )
            else:

Review comment (suggestion):
Sorry another nit - the colour handling has expanded from subsequent PRs and is now probably a bit more complex than it needs to be.

What about something simpler e.g.,

```python
if isinstance(self.multiclass_colors, list):
    colors = [mpl.colors.to_rgba(color) for color in self.multiclass_colors]
else:
    cmap_name = self.multiclass_colors
    if cmap_name is None:
        # use default if `multiclass_colors` None
        cmap_name = "tab10" if self.n_classes <= 10 else "gist_rainbow"
    elif not isinstance(cmap_name, str):
        raise ValueError("'multiclass_colors' must be a list or a str.")
    
    # Special case for the tab10 and tab20 colormaps that encode a
    # discrete set of colors that are easily distinguishable
    # contrary to other colormaps that are continuous.
    if cmap_name == "tab10" and self.n_classes <= 10:
        colors = plt.get_cmap("tab10", 10).colors[: self.n_classes]
    elif cmap_name == "tab20" and self.n_classes <= 20:
        colors = plt.get_cmap("tab20", 20).colors[: self.n_classes]
    else:
        cmap = plt.get_cmap(cmap_name, self.n_classes)
        if not hasattr(cmap, "colors"):
            # For LinearSegmentedColormap
            colors = cmap(np.linspace(0, 1, self.n_classes))
        else:
            colors = cmap.colors
```
Here we do the shorter condition (list) first, which reads easier than having the long condition first - so people don't forget whats going on.
It's similiar in length but less nesting.

Again, could be done in a separate PR.

Code after:
        check_matplotlib_support("DecisionBoundaryDisplay.plot")
        import matplotlib as mpl
        import matplotlib.pyplot as plt

        if plot_method not in ("contourf", "contour", "pcolormesh"):
            raise ValueError(
                "plot_method must be 'contourf', 'contour', or 'pcolormesh'. "
                f"Got {plot_method} instead."
            )

        if ax is None:
            _, ax = plt.subplots()

        plot_func = getattr(ax, plot_method)
        if self.n_classes == 2:
            self.surface_ = plot_func(self.xx0, self.xx1, self.response, **kwargs)
        else:  # multiclass
            for kwarg in ("cmap", "colors"):
                if kwarg in kwargs:
                    warnings.warn(
                        f"'{kwarg}' is ignored in favor of 'multiclass_colors' "
                        "in the multiclass case."
                    )
                    del kwargs[kwarg]

            if self.multiclass_colors is None or isinstance(
                self.multiclass_colors, str
            ):
                if self.multiclass_colors is None:
                    cmap = "tab10" if self.n_classes <= 10 else "gist_rainbow"
                else:
                    cmap = self.multiclass_colors

                # Special case for the tab10 and tab20 colormaps that encode a
                # discrete set of colors that are easily distinguishable
                # contrary to other colormaps that are continuous.
                if cmap == "tab10" and self.n_classes <= 10:
                    colors = plt.get_cmap("tab10", 10).colors[: self.n_classes]
                elif cmap == "tab20" and self.n_classes <= 20:
                    colors = plt.get_cmap("tab20", 20).colors[: self.n_classes]
                else:
                    cmap = plt.get_cmap(cmap, self.n_classes)
                    if not hasattr(cmap, "colors"):
                        # Get `LinearSegmentedColormap` for non-qualitative cmaps
                        colors = cmap(np.linspace(0, 1, self.n_classes))
                    else:
                        colors = cmap.colors
            elif isinstance(self.multiclass_colors, list):
                colors = [mpl.colors.to_rgba(color) for color in self.multiclass_colors]
            else:
                raise ValueError("'multiclass_colors' must be a list or a str.")