added multiplication as color repeat functionality

This commit is contained in:
Philip Eisenlohr 2022-01-24 11:53:33 +01:00
parent efd9b37235
commit 6bd23715b8
2 changed files with 15 additions and 0 deletions

View File

@ -63,6 +63,16 @@ class Colormap(mpl.colors.ListedColormap):
"""Concatenate (in-place).""" """Concatenate (in-place)."""
return self.__add__(other) return self.__add__(other)
def __mul__(self, factor: int) -> 'Colormap':
"""Repeat."""
return Colormap(np.broadcast_to(self.colors,(factor,)+self.colors.shape)
.reshape((factor*self.colors.shape[0],)+self.colors.shape[1:]),
f'{self.name}*{factor}')
def __imul__(self, factor: int) -> 'Colormap':
"""Repeat (in-place)."""
return self.__mul__(factor)
def __invert__(self) -> 'Colormap': def __invert__(self) -> 'Colormap':
"""Reverse.""" """Reverse."""
return self.reversed() return self.reversed()

View File

@ -140,6 +140,11 @@ class TestColormap:
c += c c += c
assert (np.allclose(c.colors[:len(c.colors)//2],c.colors[len(c.colors)//2:])) assert (np.allclose(c.colors[:len(c.colors)//2],c.colors[len(c.colors)//2:]))
def test_mul(self):
c = o = Colormap.from_predefined('jet')
o *= 2
assert c+c == o
@pytest.mark.parametrize('N,cmap,at,result',[ @pytest.mark.parametrize('N,cmap,at,result',[
(8,'gray',0.5,[0.5,0.5,0.5]), (8,'gray',0.5,[0.5,0.5,0.5]),
(17,'gray',0.5,[0.5,0.5,0.5]), (17,'gray',0.5,[0.5,0.5,0.5]),