"Color Scalar Bar" reworked and upgraded, now it’s called "Color Legend"

Starting from version 30530 build 2022-01-15 “Color Scalar Bar” section from “Colors” module was replaced with “Color Legend” section.

The “Color Legend” section has been also added to the “Volume”, “Model” and “Markups” modules to control visibility and behavior of color legend.

New major features:

  1. More unified API. Each Volume, Model, Markups or any other displayable node now can have an individual color legend by means of vtkMRMLColorLegendDisplayNode.

  2. Displayed LUT is in sync with range of displayed values of primary display node;

  3. Resizable title of color legend;

  4. Control of visibility of color legend on each slice or 3D view.

  5. Color legend can by added to any displayable node (if the node has a primary display node) by means of Color module logic and AddDefaultColorLegendDisplayNode method.

  6. The access to a low level vtkScalarBarActor for developers and experienced users is described here.

There are still some features that must be added. Issue to check the progress is here.

Any other required or recommended features can added to the issue or in this topic.

7 Likes

@Mik Thank you very much! This feature was missing for a very long time and it will make an impact in many projects.

Hi Mik

Thank you for your development about color scalar bar

I try to plot the scalar bar on “View1” (i.e., 3D view).
“View1” was selected in the View panel, but the color bar is not displayed in the 3D view as shown in the example above.

Is there a way to solve this problem ?

Best,

For what data are you trying to display color legend in 3D: scalar volume, model or something else? Currently there is a limitation of showing color legends in 3D.

It is scalar volume.

I made a volume rendering plot using scientific data (as colormap)
And I would like to make color legend of this volume rendering

As far as I search, the color legend in 3D look likt only for model. Is it right?

Correct. Color legend can be shown in 3D for models.

@Mik submitted a pull request that will allow displaying color legend of volumes shown slice views to be shown in 3D views as well. It’ll be available in the Slicer Preview Release within a few days.

If you enable “Synchronize with Volumes module” in Volume rendering module’s Advanced / Volume Properties section then the same color legend applies to volume rendering and slice views. However, if you use any other volume rendering preset then there is no GUI for making the volume rendering’s Scalar Color Mapping appear as a color legend, but you can achieve it by copy-pasting this Python script into the Python console in Slicer:

# Get colormap from first volume rendering property node
colorMapOfVolumeRendering = getNode('*Prop*').GetColor()

# Add color node
colorNode = slicer.mrmlScene.CreateNodeByClass("vtkMRMLProceduralColorNode")
colorNode.UnRegister(None)  # to prevent memory leaks
colorNode.SetHideFromEditors(False)
slicer.mrmlScene.AddNode(colorNode)
colorMap = colorNode.GetColorTransferFunction()
colorMap.DeepCopy(colorMapOfVolumeRendering)

# Add an empty displayable node (you can only show a color legend if it belongs to a displayable node)
displayableNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode", "volume")
displayableNode.CreateDefaultDisplayNodes()
displayNode = displayableNode.GetDisplayNode()

# Display color legend
displayNode.SetAndObserveColorNodeID(colorNode.GetID())
colorLegendDisplayNode = slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(displayableNode)

@mik if you have time you might consider adding display of color legend for volume rendering (but instead of creating a separate color node, take the colormap from the volume property node). However, I’m not sure how often users want to display color legend for volume rendering.

The color legend for volume rendering should be displayed only in 3D?

@Mik I am having an issue with a module that used the old VTKScaleBar, I was wondering if you could help me to update it to Color Legend. The module is BoneThicknessMapping (version f98136b 22.02.2023).

When I try to run it, the phython console shows the following:

Traceback (most recent call last):

File “C:/Users/Asus/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/BoneThicknessMapping/lib/Slicer-5.2/qt-scripted-modules/BoneThicknessMapping.py”, line 521, in click_execute

BoneThicknessMappingLogic.set_scalar_colour_bar_state(0)

File “C:/Users/Asus/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/BoneThicknessMapping/lib/Slicer-5.2/qt-scripted-modules/BoneThicknessMapping.py”, line 936, in set_scalar_colour_bar_state

ctkBar = slicer.util.findChildren(colorWidget, name=‘VTKScalarBar’)[0]

IndexError: list index out of range

[Qt] This function is deprecated. Use lookFromAxis(const ctkAxesWidget::Axis& axis) instead.

The section of code referenced is this (starting from line 933):

@staticmethod
def set_scalar_colour_bar_state(state, color_node_id=None):
    colorWidget = slicer.modules.colors.widgetRepresentation()
    ctkBar = slicer.util.findChildren(colorWidget, name='VTKScalarBar')[0]
    ctkBar.setDisplay(state)
    if state == 0 or color_node_id is None: return
    slicer.util.findChildren(colorWidget, 'ColorTableComboBox')[0].setCurrentNodeID(color_node_id)
    slicer.util.findChildren(colorWidget, 'UseColorNameAsLabelCheckBox')[0].setChecked(True)

And it is called upon in other sections such as this (lines 581 and 582):

    # update scalar bar
    BoneThicknessMappingLogic.set_scalar_colour_bar_state(1, colourNodeId)

Hope you can help and thank you for your time regardless!

Is this module BoneThicknessMapping compile and run without problems? I mean i hope it isn’t abandoned.

You just want to show color legend for a model node or volume, or you want a properly working button within the module?

One more question. Do you need color legend for calculated model node or for the input volume node as well?

I have made a PR to upgrade that module. Can you download and check that this upgrade is that what you need.

Thank you for all your help. I ran the module with the modifications but it doesn’t seem to work. What I get on the console is the following:


Traceback (most recent call last):

File “C:/Users/Asus/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/BoneThicknessMapping/lib/Slicer-5.2/qt-scripted-modules/BoneThicknessMapping.py”, line 521, in click_execute

# BoneThicknessMappingLogic.set_scalar_colour_bar_state(0)

File “C:/Users/Asus/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/BoneThicknessMapping/lib/Slicer-5.2/qt-scripted-modules/BoneThicknessMapping.py”, line 936, in set_scalar_colour_bar_state

colorLegendNode = slicer.modules.colors.logic().GetColorLegendDisplayNode(displayable_node)

IndexError: list index out of range

[Qt] This function is deprecated. Use lookFromAxis(const ctkAxesWidget::Axis& axis) instead.


The IndexError persists, which makes me think that the issue is not due to the change from VTKScalarBar to ColorLegend but lies somewhere else.

This results in the progress bar when using the module not moving from 0% (status:initializing execution). The issue is the same as described on this post BoneThicknessMapping does not work . This other post seems to describe the same problem and according to them it worked properly in 3dslicer 4.1.1 Bone Thickness Mapping not functioning .

Thank you anyway for your help and time.

I’m working with Slicer-5.9. Can you check that update with a newer Slicer version rather then 5.2?