From ada55f9aa5ff682005f9b02a1383dd2226f5ec51 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Mon, 16 Nov 2015 15:12:35 +0000
Subject: [PATCH] All plot panels use the same initial plot colour for a given
 overlay.

---
 fsl/fsleyes/views/histogrampanel.py     |  3 +-
 fsl/fsleyes/views/plotpanel.py          | 42 ++++++++++++++++++++++---
 fsl/fsleyes/views/powerspectrumpanel.py |  3 +-
 fsl/fsleyes/views/timeseriespanel.py    |  3 +-
 4 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/fsl/fsleyes/views/histogrampanel.py b/fsl/fsleyes/views/histogrampanel.py
index 67b5acde7..fcc9d63cc 100644
--- a/fsl/fsleyes/views/histogrampanel.py
+++ b/fsl/fsleyes/views/histogrampanel.py
@@ -23,7 +23,6 @@ import fsl.utils.dialog                           as fsldlg
 import fsl.fsleyes.plotting.histogramseries       as histogramseries
 import fsl.fsleyes.controls.histogramcontrolpanel as histogramcontrolpanel
 import fsl.fsleyes.controls.plotlistpanel         as plotlistpanel
-import fsl.fsleyes.colourmaps                     as fslcm
 import                                               plotpanel
 
 
@@ -172,7 +171,7 @@ class HistogramPanel(plotpanel.OverlayPlotPanel):
             strings.messages[self, 'calcHist'].format(overlay.name),
             loadHs).Run()
         
-        hs.colour      = fslcm.randomDarkColour()
+        hs.colour      = self.getOverlayPlotColour(overlay)
         hs.alpha       = 1
         hs.lineWidth   = 1
         hs.lineStyle   = '-'
diff --git a/fsl/fsleyes/views/plotpanel.py b/fsl/fsleyes/views/plotpanel.py
index a721ea09c..b3beeb332 100644
--- a/fsl/fsleyes/views/plotpanel.py
+++ b/fsl/fsleyes/views/plotpanel.py
@@ -29,9 +29,10 @@ import matplotlib.pyplot as plt
 from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
 from matplotlib.backends.backend_wx    import NavigationToolbar2Wx
 
-import                     props
-import                     viewpanel
-import fsl.data.strings as strings
+import                           props
+import                           viewpanel
+import fsl.data.strings       as strings
+import fsl.fsleyes.colourmaps as fslcm
 
 
 log = logging.getLogger(__name__)
@@ -775,7 +776,18 @@ class OverlayPlotPanel(PlotPanel):
     ``none``    Only the ``DataSeries`` that are in the
                 :attr:`.PlotPanel.dataSeries` list will be plotted.
     =========== =====================================================
-    """ 
+    """
+
+    
+    plotColours = {}
+    """This dictionary is used to store a collection of ``{overlay : colour}``
+    mappings. It is shared across all ``OverlayPlotPanel`` instances, so that
+    the same (initial) colour is used for the same overlay, across multiple
+    plots.
+    
+    Sub-classes should use the :meth:`getOverlayPlotColour` method to retrieve
+    the initial colour to use for a given overlay.
+    """
 
 
     def __init__(self, *args, **kwargs):
@@ -853,12 +865,34 @@ class OverlayPlotPanel(PlotPanel):
         specified overlay, or ``None`` if there is no ``DataSeries`` instance.
         """
         return self.__dataSeries.get(overlay)
+
+
+    def getOverlayPlotColour(self, overlay):
+        """Returns an initial colour to use for plots associated with the
+        given overlay. If a colour is present in the  :attr:`plotColours`
+        dictionary, it is returned. Otherwise a random colour is generated,
+        added to ``plotColours``, and returned.
+        """
+
+        colour = self.plotColours.get(overlay)
+
+        if colour is None:
+            colour = fslcm.randomDarkColour()
+            self.plotColours[overlay] = colour
+
+        return colour
  
 
     def createDataSeries(self, overlay):
         """This method must be implemented by sub-classes. It must create and
         return a :class:`.DataSeries` instance for the specified overlay.
 
+        
+        .. note:: Sub-class implementations should set the
+                  :attr:`.DataSeries.colour` property to that returned by
+                  the :meth:`getOverlayPlotColour` method.
+
+        
         Different ``DataSeries`` types need to be re-drawn when different
         properties change. For example, a :class:`.TimeSeries`` instance needs
         to be redrawn when the :attr:`.DisplayContext.location` property
diff --git a/fsl/fsleyes/views/powerspectrumpanel.py b/fsl/fsleyes/views/powerspectrumpanel.py
index 76c901b3e..c9c618483 100644
--- a/fsl/fsleyes/views/powerspectrumpanel.py
+++ b/fsl/fsleyes/views/powerspectrumpanel.py
@@ -21,7 +21,6 @@ import                                                   plotpanel
 import fsl.fsleyes.plotting.powerspectrumseries       as psseries
 import fsl.fsleyes.controls.powerspectrumcontrolpanel as pscontrol
 import fsl.fsleyes.controls.plotlistpanel             as plotlistpanel
-import fsl.fsleyes.colourmaps                         as fslcm
 import fsl.data.image                                 as fslimage
 import fsl.data.melodicimage                          as fslmelimage
 
@@ -179,7 +178,7 @@ class PowerSpectrumPanel(plotpanel.OverlayPlotPanel):
         else:
             return None, None, None
 
-        ps.colour    = fslcm.randomDarkColour()
+        ps.colour    = self.getOverlayPlotColour(overlay)
         ps.alpha     = 1.0
         ps.lineWidth = 1
         ps.lineStyle = '-'
diff --git a/fsl/fsleyes/views/timeseriespanel.py b/fsl/fsleyes/views/timeseriespanel.py
index 0ac508c19..cc89346c1 100644
--- a/fsl/fsleyes/views/timeseriespanel.py
+++ b/fsl/fsleyes/views/timeseriespanel.py
@@ -19,7 +19,6 @@ import                                                plotpanel
 import fsl.data.featimage                          as fslfeatimage
 import fsl.data.melodicimage                       as fslmelimage
 import fsl.data.image                              as fslimage
-import fsl.fsleyes.colourmaps                      as fslcmaps
 import fsl.fsleyes.plotting                        as plotting
 import fsl.fsleyes.controls.timeseriescontrolpanel as timeseriescontrolpanel
 import fsl.fsleyes.controls.plotlistpanel          as plotlistpanel
@@ -243,7 +242,7 @@ class TimeSeriesPanel(plotpanel.OverlayPlotPanel):
             targets   = [self._displayCtx]
             propNames = ['location'] 
 
-        ts.colour    = fslcmaps.randomDarkColour()
+        ts.colour    = self.getOverlayPlotColour(overlay)
         ts.alpha     = 1
         ts.lineWidth = 1
         ts.lineStyle = '-'
-- 
GitLab