# Custom Color Map .txt file Implementation via code

**URL:** <https://discourse.slicer.org/t/custom-color-map-txt-file-implementation-via-code/29761>\
**Category:** Support\
**Created:** [May 31, 2023, 11:59pm UTC](https://discourse.slicer.org/t/custom-color-map-txt-file-implementation-via-code/29761 "2023-05-31T23:59:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanchoi0519](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/seanchoi0519/32/10354_2.png) [@seanchoi0519](https://discourse.slicer.org/u/seanchoi0519)\
**Post date:** [May 31, 2023, 11:59pm UTC](https://discourse.slicer.org/t/custom-color-map-txt-file-implementation-via-code/29761/1 "2023-05-31T23:59:44Z")

</div>

Hello,

I have a custom color map .txt file that I’d like to apply to a displayNode.  
I have the resourcePath to my .txt file as follows:

customColorMapPath = moduleDir +‘/Resources/CustomColorMaps/custom.txt’

It would be perfect if I could find a way to fit this into the code below to replace the ColdToHotRainbow.txt.

node.GetDisplayNode().SetAndObserveColorNodeID(‘vtkMRMLColorTableNodeFileColdToHotRainbow.txt’)

What would be the best way to do this?  
I’ve tried looking up the documentation however could not find anything related to a custom .txt file - [Install Slicer — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository/gui.html#create-custom-color-map-and-display-color-legend)

Please note that I’m looking to get this done all programmatically (no editing via the user interface or dragging the file in). Ideally the .txt file is imported by code on the backend.

I’m fairly new to 3DSlicer, so a simple, clear answer would be appreciated!

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [June 1, 2023, 12:31am UTC](https://discourse.slicer.org/t/custom-color-map-txt-file-implementation-via-code/29761/2 "2023-06-01T00:31:44Z")

</div>

If using python, there are a lot of common functions available in `slicer.util`. Take a look at the `loadColorTable` method below which will allow you to programmatically load the custom color table file. This method returns the node from which you can `GetID()` for use in the `SetAndObserveColorNodeID` method that you mentioned.

> <https://github.com/Slicer/Slicer/blob/f331600faffb462295cbe2775e10728b88dab13d/Base/Python/slicer/util.py#L735-L743>

Or if you want to create the color table programmatically rather than from file:

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository/gui.html#create-custom-color-table](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository/gui.html#create-custom-color-table)

Color Table file format:  
[https://slicer.readthedocs.io/en/latest/developer\_guide/modules/colors.html](https://slicer.readthedocs.io/en/latest/developer_guide/modules/colors.html)

---

<div class="post-metadata">

**Author:** ![seanchoi0519](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/seanchoi0519/32/10354_2.png) [@seanchoi0519](https://discourse.slicer.org/u/seanchoi0519)\
**Post date:** [June 1, 2023, 12:57am UTC](https://discourse.slicer.org/t/custom-color-map-txt-file-implementation-via-code/29761/3 "2023-06-01T00:57:40Z")

</div>

> [@jamesobutler](#):
>
> SetAndObserveColorNodeID

Thanks so much, this worked great.
