Changing the background color of slice views

I am not being able to change de slice views background colors. I am doing this:

viewNode = slicer.app.layoutManager().sliceWidget('Red').mrmlSliceNode()
viewNode.SetBackgroundColor(1,1,1)
viewNode.SetBackgroundColor2(1,1,1)

But it has no effect. What should I do? I want to change to white. Thank you!

1 Like

Try:
view = slicer.app.layoutManager().sliceWidget('Red').sliceView() view.setBackgroundColor(qt.QColor.fromRgbF(1,1,1))

@jumbojing you forgot to add view.forceRender() to apply the modifications

For all slice view we can write like

layoutManager = slicer.app.layoutManager()
for sliceViewName in layoutManager.sliceViewNames():
  view = layoutManager.sliceWidget(sliceViewName).sliceView()
  view.setBackgroundColor(qt.QColor.fromRgbF(1,1,1))
  view.forceRender()
2 Likes

@Dwij_Mistry Perfect! Thanks :+1:

但是, 如果每个slice有个边框就更好了

However, it would be better if each slice had a balck border.

image

1 Like

If you are ok to use dark mode of slicer then we can get border which is nothing but a background color of central widget of MainWindow

Screenshot (110)

Dark mode can be activated from Menu → Edit → application settings → appearance → style → dark slicer

1 Like

Great! Thanks :+1:

image

1 Like

@jumbojing

Finally I found a way by which we can have border without changing the slicer color mode.


def paint_border(view):
  color = [0.0, 0.0, 0.0] #change color for border 
  lineThickness = 10.0
  viewRenderer = view.renderWindow().GetRenderers().GetItemAsObject(0)
  borderPoints = vtk.vtkPoints()
  borderPoints.InsertNextPoint(  1e-4,   1e-4, 0)
  borderPoints.InsertNextPoint(0.9999,   1e-4, 0)
  borderPoints.InsertNextPoint(0.9999, 0.9999, 0)
  borderPoints.InsertNextPoint(  1e-4, 0.9999, 0)
  borderCells = vtk.vtkCellArray()
  borderCells.InsertNextCell(5)
  for i in range(5):
    borderCells.InsertCellPoint(i%5)
  borderPolyData = vtk.vtkPolyData()
  borderPolyData.SetPoints(borderPoints)
  borderPolyData.SetLines(borderCells)
  borderCoordinate = vtk.vtkCoordinate()
  borderCoordinate.SetCoordinateSystemToNormalizedViewport()
  borderCoordinate.SetViewport(viewRenderer)
  borderPolyDataMapper = vtk.vtkPolyDataMapper2D()
  borderPolyDataMapper.SetInputData(borderPolyData)
  borderPolyDataMapper.SetTransformCoordinate(borderCoordinate)
  borderPolyDataMapper.SetTransformCoordinateUseDouble(True)
  highlightedBorderActor = vtk.vtkActor2D()
  highlightedBorderActor.SetMapper(borderPolyDataMapper)
  highlightedBorderActor.GetProperty().SetColor(color[0], color[1], color[2])
  highlightedBorderActor.GetProperty().SetDisplayLocationToForeground()
  highlightedBorderActor.GetProperty().SetLineWidth(lineThickness)
  viewRenderer.AddActor2D(highlightedBorderActor)
  view.renderWindow().Render()



#calling 
paint_border(slicer.app.layoutManager().threeDWidget(0).threeDView())
for sliceViewName in slicer.app.layoutManager().sliceViewNames():
    paint_border(slicer.app.layoutManager().sliceWidget(sliceViewName).sliceView())

OutPut
Capture

1 Like

@Dwij_Mistry what’s wrong? a black line

I am also facing the same issue, Can any one please share why it is happening? or any alternative solution?

@lassoan @pieper @jamesobutler @jcfr @cpinter

I cannot reproduce this, so I cannot tell what’s wrong, but maybe you need to tweak the 1e-4 and 0.9999 constants.

1 Like