From 6bd23715b822c49aa1202c8cea626905ba6102d2 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Mon, 24 Jan 2022 11:53:33 +0100 Subject: [PATCH] added multiplication as color repeat functionality --- python/damask/_colormap.py | 10 ++++++++++ python/tests/test_Colormap.py | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/python/damask/_colormap.py b/python/damask/_colormap.py index 1ccc292e6..36f9e2d1b 100644 --- a/python/damask/_colormap.py +++ b/python/damask/_colormap.py @@ -63,6 +63,16 @@ class Colormap(mpl.colors.ListedColormap): """Concatenate (in-place).""" 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': """Reverse.""" return self.reversed() diff --git a/python/tests/test_Colormap.py b/python/tests/test_Colormap.py index 2321745aa..12a26e550 100644 --- a/python/tests/test_Colormap.py +++ b/python/tests/test_Colormap.py @@ -140,6 +140,11 @@ class TestColormap: c += c 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',[ (8,'gray',0.5,[0.5,0.5,0.5]), (17,'gray',0.5,[0.5,0.5,0.5]),