From d02336723a810a5ab20eff6b7b3925a711ef6753 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Tue, 3 Sep 2019 14:21:15 +0200
Subject: [PATCH] reintroduce setting colour map

---
 .../atlasViewer.apiService.service.ts         | 20 +++++++++-
 src/plugin_examples/plugin_api.md             | 12 ++++--
 .../nehubaContainer.component.ts              |  6 ++-
 .../nehubaViewer/nehubaViewer.component.ts    | 38 ++++++++++++++-----
 4 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/src/atlasViewer/atlasViewer.apiService.service.ts b/src/atlasViewer/atlasViewer.apiService.service.ts
index 475cb5c09..e17da56ea 100644
--- a/src/atlasViewer/atlasViewer.apiService.service.ts
+++ b/src/atlasViewer/atlasViewer.apiService.service.ts
@@ -1,6 +1,6 @@
 import { Injectable } from "@angular/core";
 import { Store, select } from "@ngrx/store";
-import { ViewerStateInterface, safeFilter, getLabelIndexMap, isDefined } from "src/services/stateStore.service";
+import { ViewerStateInterface, safeFilter, getLabelIndexMap, isDefined, getMultiNgIdsRegionsLabelIndexMap } from "src/services/stateStore.service";
 import { Observable } from "rxjs";
 import { map, distinctUntilChanged, filter } from "rxjs/operators";
 import { ModalHandler } from "../util/pluginHandlerClasses/modalHandler";
@@ -60,8 +60,11 @@ export class AtlasViewerAPIServices{
 
         loadedTemplates : [],
 
+        // TODO deprecate
         regionsLabelIndexMap : new Map(),
 
+        layersRegionLabelIndexMap: new Map(), 
+
         datasetsBSubject : this.store.pipe(
           select('dataStore'),
           safeFilter('fetchedDataEntries'),
@@ -133,7 +136,10 @@ export class AtlasViewerAPIServices{
 
   private init(){
     this.loadedTemplates$.subscribe(templates=>this.interactiveViewer.metadata.loadedTemplates = templates)
-    this.selectParcellation$.subscribe(parcellation => this.interactiveViewer.metadata.regionsLabelIndexMap = getLabelIndexMap(parcellation.regions))
+    this.selectParcellation$.subscribe(parcellation => {
+      this.interactiveViewer.metadata.regionsLabelIndexMap = getLabelIndexMap(parcellation.regions)
+      this.interactiveViewer.metadata.layersRegionLabelIndexMap = getMultiNgIdsRegionsLabelIndexMap(parcellation)
+    })
   }
 }
 
@@ -145,6 +151,7 @@ export interface InteractiveViewerInterface{
     selectedRegionsBSubject : Observable<any[]|null>
     loadedTemplates : any[]
     regionsLabelIndexMap : Map<number,any> | null
+    layersRegionLabelIndexMap: Map<string, Map<number, any>>
     datasetsBSubject : Observable<any[]>
   },
 
@@ -157,8 +164,17 @@ export interface InteractiveViewerInterface{
     hideSegment : (labelIndex : number)=>void
     showAllSegments : ()=>void
     hideAllSegments : ()=>void
+
+    // TODO deprecate
     segmentColourMap : Map<number,{red:number,green:number,blue:number}>
+
+    getLayersSegmentColourMap: () => Map<string, Map<number, {red:number, green:number, blue: number}>>
+
+    // TODO deprecate
     applyColourMap : (newColourMap : Map<number,{red:number,green:number,blue:number}>)=>void
+
+    applyLayersColourMap: (newLayerColourMap: Map<string, Map<number, {red:number, green: number, blue: number}>>) => void
+
     loadLayer : (layerobj:NGLayerObj)=>NGLayerObj
     removeLayer : (condition:{name : string | RegExp})=>string[]
     setLayerVisibility : (condition:{name : string|RegExp},visible:boolean)=>void
diff --git a/src/plugin_examples/plugin_api.md b/src/plugin_examples/plugin_api.md
index 45aa8ef15..f997fd378 100644
--- a/src/plugin_examples/plugin_api.md
+++ b/src/plugin_examples/plugin_api.md
@@ -15,7 +15,9 @@ window.interactiveViewer
 
   - *loadedTemplates* : Array of TemplateDescriptor objects. Loaded asynchronously onInit.
 
-  - *regionsLabelIndexMap* Map of labelIndex (used by neuroglancer and nehuba) to the corresponding RegionDescriptor object.
+  - **Deprecated** ~~*regionsLabelIndexMap* Map of labelIndex (used by neuroglancer and nehuba) to the corresponding RegionDescriptor object.~~
+
+  - *layersRegionLabelIndexMap* Map of layer name to Map of labelIndex (used by neuroglancer and nehuba) to the corresponding RegionDescriptor object.
 
 - viewerHandle
 
@@ -36,9 +38,13 @@ window.interactiveViewer
 
   - *hideAllSegments()* Function that hides all segments. Will trigger *selectRegionBSubject*
 
-  - *segmentColourMap* : Map of *labelIndex* to an object with the shape of `{red: number, green: number, blue: number}`.
+  - **Deprecated** ~~*segmentColourMap* : Map of *labelIndex* to an object with the shape of `{red: number, green: number, blue: number}`.~~
+
+  - *getLayersSegmentColourMap* : Call to get Map of layer name to Map of label index to colour map
+
+  - **Deprecated**  ~~*applyColourMap(colourMap)* Function that applies a custom colour map (Map of number to and object with the shape of `{red: number , green: number , blue: number}`)~~
 
-  - *applyColourMap(colourMap)* Function that applies a custom colour map (Map of number to and object with the shape of `{red: number , green: number , blue: number}`)
+  - *applyLayersColourMap* Function that applies a custom colour map.
 
   - *loadLayer(layerObject)* Function that loads *ManagedLayersWithSpecification* directly to neuroglancer. Returns the values of the object successfully added. **n.b.** advanced feature, will likely break other functionalities. **n.b.** if the layer name is already taken, the layer will not be added.
   
diff --git a/src/ui/nehubaContainer/nehubaContainer.component.ts b/src/ui/nehubaContainer/nehubaContainer.component.ts
index d3dbca836..daf61453f 100644
--- a/src/ui/nehubaContainer/nehubaContainer.component.ts
+++ b/src/ui/nehubaContainer/nehubaContainer.component.ts
@@ -1104,8 +1104,12 @@ export class NehubaContainer implements OnInit, OnDestroy{
         })
       },
       segmentColourMap : new Map(),
+      getLayersSegmentColourMap: () => this.nehubaViewer.multiNgIdColorMap,
       applyColourMap : (map)=>{
-        /* TODO to be implemented */
+        throw new Error(`apply color map has been deprecated. use applyLayersColourMap instead`)
+      },
+      applyLayersColourMap: (map) => {
+        this.nehubaViewer.setColorMap(map)
       },
       loadLayer : (layerObj)=>this.nehubaViewer.loadLayer(layerObj),
       removeLayer : (condition)=>this.nehubaViewer.removeLayer(condition),
diff --git a/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts b/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts
index 30b1d9d6c..75311ee37 100644
--- a/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts
+++ b/src/ui/nehubaContainer/nehubaViewer/nehubaViewer.component.ts
@@ -340,7 +340,15 @@ export class NehubaViewerUnit implements OnDestroy{
 
   viewerState : ViewerState
 
-  private multiNgIdColorMap: Map<string, Map<number, {red: number, green:number, blue: number}>>
+  private _multiNgIdColorMap: Map<string, Map<number, {red: number, green:number, blue: number}>>
+  get multiNgIdColorMap(){
+    return this._multiNgIdColorMap
+  }
+
+  set multiNgIdColorMap(val) {
+    this._multiNgIdColorMap = val
+  }
+
   public mouseOverSegment: number | null
   public mouseOverLayer: {name:string,url:string}| null
 
@@ -350,6 +358,10 @@ export class NehubaViewerUnit implements OnDestroy{
     ? window['export_nehuba'].getNgHash()
     : null
 
+  redraw(){
+    this.nehubaViewer.redraw()
+  }
+
   loadNehuba(){
     this.nehubaViewer = window['export_nehuba'].createNehubaViewer(this.config, (err)=>{
       /* print in debug mode */
@@ -367,7 +379,7 @@ export class NehubaViewerUnit implements OnDestroy{
       if (layer) layer.setVisible(true)
       else console.log('layer unavailable', ngId)
     })
-    this.nehubaViewer.redraw()
+    this.redraw()
 
     /* creation of the layout is done on next frame, hence the settimeout */
     setTimeout(() => {
@@ -938,16 +950,11 @@ export class NehubaViewerUnit implements OnDestroy{
       ]
     }) as [string, Map<number, {red:number, green: number, blue: number}>][]
 
-    this.multiNgIdColorMap = new Map(obj)
+    const multiNgIdColorMap = new Map(obj)
 
     /* load colour maps */
-
-    Array.from(this.multiNgIdColorMap.entries()).forEach(([ngId, map]) => {
-
-      this.nehubaViewer.batchAddAndUpdateSegmentColors(
-        map,
-        { name : ngId })
-    })
+    
+    this.setColorMap(multiNgIdColorMap)
 
     this._s$.forEach(_s$=>{
       if(_s$) _s$.unsubscribe()
@@ -971,6 +978,17 @@ export class NehubaViewerUnit implements OnDestroy{
     }
   }
 
+  public setColorMap(map: Map<string, Map<number,{red:number, green:number, blue:number}>>){
+    this.multiNgIdColorMap = map
+    
+    Array.from(map.entries()).forEach(([ngId, map]) => {
+
+      this.nehubaViewer.batchAddAndUpdateSegmentColors(
+        map,
+        { name : ngId })
+    })
+  }
+
   private getRgb(labelIndex:number,rgb?:number[]):{red:number,green:number,blue:number}{
     if(typeof rgb === 'undefined' || rgb === null){
       const arr = intToColour(Number(labelIndex))
-- 
GitLab