How to export color node as color table

I would like to convert a slicer .seg.nrrd segmentation into a labelmap + color table, so I can use it downstream in other applications. I figured out how to convert the segmentation to a labelmap and I can see the color node under the Data (All nodes) section. Is there a way I can export it to the color table format, described e.g. here?

The slicer color table format is easy to parse (and already supported by the tool I am using).

I guess I could access the labelmap node via getNode("name of node"). I didn’t see what api I could use to export the color table (or more importantly the index and name mapping).

As a workaround I used the slicerio module to export the segmentation as a labelmap and save the color table (by hand):

import slicerio
import nrrd
import random
import numpy as np
from pathlib import Path

input_filename = "/Users/lloyd/Downloads/segIEMap.seg.nrrd"
output_filename = "/Users/lloyd/Downloads/segIEMap.nrrd"

voxels, header =
segmentation_info = slicerio.read_segmentation_info(input_filename)

number_of_segments = len(segmentation_info["segments"])
print(f"Number of segments: {number_of_segments}")

segment_names = slicerio.segment_names(segmentation_info)

segment_names_to_labels = []
for idx, name in enumerate(segment_names, start=1):
    segment_names_to_labels.append((name, idx))


voxels3, map = slicerio.extract_segments(voxels, header, segmentation_info, segment_names_to_labels)
nrrd.write(output_filename, voxels3)

with open(Path(output_filename).with_suffix(".txt"), "w") as f:
    print("# Color table file", file=f)
    print(f"# {len(segment_names_to_labels) + 1} values", file=f)
    print("0 Background 0 0 0 0", file=f)
    for (name, idx) in segment_names_to_labels:
        print(f"{idx} {name} {random.randint(0, 255)} {random.randint(0, 255)} {random.randint(0, 255)} 255", file=f)