Repository: matplotlib/matplotlib
File: lib/matplotlib/colors.py (Python)

Code before:

    origin : (float, float)
        The relative origin of the colormap. Typically (0, 0), for colormaps
        that are linear on both axis, and (.5, .5) for circular colormaps.
        Used when getting 1D colormaps from 2D colormaps.

    name : str, optional
        The name of the colormap.
    """

    def __init__(self, patch, N=256, shape='square', origin=(0, 0),
                 name='segmented bivariate colormap'):
        _api.check_shape((None, None, 3), patch=patch)
        self.patch = patch
        super().__init__(N, N, shape, origin, name=name)

    def _init(self):
        # Perform bilinear interpolation

        s = self.patch.shape

        # Indices (whole and fraction) of the new grid points
        row = np.linspace(0, s[0] - 1, self.N)[:, np.newaxis]
        col = np.linspace(0, s[1] - 1, self.N)[np.newaxis, :]
        left = np.floor(row).astype(int)
        top = np.floor(col).astype(int)
        row_frac = (row - left)[:, :, np.newaxis]
        col_frac = (col - top)[:, :, np.newaxis]

        # Indices of the next edges, clipping where needed
        right = np.clip(left + 1, 0, s[0] - 1)
        bottom = np.clip(top + 1, 0, s[1] - 1)

        # Values at the corners
        tl = self.patch[left, top, :]
        tr = self.patch[right, top, :]
        bl = self.patch[left, bottom, :]
        br = self.patch[right, bottom, :]

        # Interpolate between the corners
        lut = (tl * (1 - row_frac) * (1 - col_frac) +
               tr * row_frac * (1 - col_frac) +
               bl * (1 - row_frac) * col_frac +
               br * row_frac * col_frac)

        # Add the alpha channel
        self._lut = np.concatenate([lut, np.ones((self.N, self.N, 1))], axis=2)

        self._isinit = True



Review comment (suggestion):
astype already floors the values for you here I think.
```suggestion
        left = row.astype(int)
        top = col.astype(int)
```

Code after:

    origin : (float, float)
        The relative origin of the colormap. Typically (0, 0), for colormaps
        that are linear on both axis, and (.5, .5) for circular colormaps.
        Used when getting 1D colormaps from 2D colormaps.

    name : str, optional
        The name of the colormap.
    """

    def __init__(self, patch, N=256, shape='square', origin=(0, 0),
                 name='segmented bivariate colormap'):
        _api.check_shape((None, None, 3), patch=patch)
        self.patch = patch
        super().__init__(N, N, shape, origin, name=name)

    def _init(self):
        # Perform bilinear interpolation

        s = self.patch.shape

        # Indices (whole and fraction) of the new grid points
        row = np.linspace(0, s[0] - 1, self.N)[:, np.newaxis]
        col = np.linspace(0, s[1] - 1, self.N)[np.newaxis, :]
        left = row.astype(int)  # floor not needed because all values are nonnegative
        top = col.astype(int)  # floor not needed because all values are nonnegative
        row_frac = (row - left)[:, :, np.newaxis]
        col_frac = (col - top)[:, :, np.newaxis]

        # Indices of the next edges, clipping where needed
        right = np.clip(left + 1, 0, s[0] - 1)
        bottom = np.clip(top + 1, 0, s[1] - 1)

        # Values at the corners
        tl = self.patch[left, top, :]
        tr = self.patch[right, top, :]
        bl = self.patch[left, bottom, :]
        br = self.patch[right, bottom, :]

        # Interpolate between the corners
        lut = (tl * (1 - row_frac) * (1 - col_frac) +
               tr * row_frac * (1 - col_frac) +
               bl * (1 - row_frac) * col_frac +
               br * row_frac * col_frac)

        # Add the alpha channel
        self._lut = np.concatenate([lut, np.ones((self.N, self.N, 1))], axis=2)

        self._isinit = True