Using Colormaps
Colormaps play a crucial role in data visualization by mapping image values to display colors. NiiVue offers a range of built-in colormaps and supports user-defined custom maps. Many of the included schemes are designed to be perceptually uniform and robust to common variations in human color vision. The following are examples of popular colormaps viridis, batlow, cubehelix, bone and hot.
Setting an Initial Colormap
When loading volumes, you can specify the initial colormap for each volume directly in the configuration object.
// Assuming 'nv' is your initialized Niivue instance (e.g., const nv = new Niivue();)
// Ensure nv is attached to a canvas element.
const volumeList = [
{
url: "/images/mni152.nii.gz", // Replace with your image path
colormap: "gray", // Set the initial colormap here ('gray' is the default)
opacity: 1,
visible: true,
},
// You can add more volumes with different initial colormaps
];
await nv.loadVolumes(volumeList);
Listing Available Colormaps
Niivue comes with several predefined colormaps. You can retrieve a list of their names using the colormaps()
method.
// Assuming 'nv' is your Niivue instance
const availableColormaps = nv.colormaps();
console.log(availableColormaps);
// Example Output: ['gray', 'warm', 'cool', 'plasma', 'viridis', 'inferno', ...]
// You can use this list to populate UI elements like dropdowns or buttons.
Changing the Colormap
After a volume is loaded, you can dynamically change its colormap using the setColormap
method. You need the ID of the volume you want to modify. Often, for the first loaded volume, this is nv.volumes[0].id
. NiiVue sets the ID to a unique value when it is loaded so you don't have to manage this.
// Assuming 'nv' is your Niivue instance and at least one volume is loaded
const firstVolumeId = nv.volumes[0].id;
// Change the colormap of the first volume to 'viridis'
nv.setColormap(firstVolumeId, 'viridis');
// Change it to another map, e.g., 'plasma'
// nv.setColormap(firstVolumeId, 'plasma');
Inverting a Colormap
You can invert the current colormap applied to a volume. This reverses the mapping, so low intensities map to colors previously used for high intensities, and vice-versa.
// Assuming 'nv' is your Niivue instance and at least one volume is loaded
// Get the first volume
const volume = nv.volumes[0];
// Set the inversion status (true to invert, false for normal)
volume.colormapInvert = true;
// Important: You must call updateGLVolume() to apply the change visually
nv.updateGLVolume();
// To revert back:
// volume.colormapInvert = false;
// nv.updateGLVolume();