All files / packages/core/src/utilities colormap.ts

86.2% Statements 25/29
80% Branches 8/10
71.42% Functions 5/7
85.18% Lines 23/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112            1x                                 2x                 408x                   408x 48960x     408x       408x     408x 13538x   13538x 9454x     4084x 6522x           3676x       408x     408x       408x 408x 407x         407x           407x 814x             408x                        
import vtkColorMaps from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
 
import { ColormapPublic, ColormapRegistration } from '../types';
import isEqual from './isEqual';
import { actorIsA } from './actorCheck';
 
const _colormaps = new Map();
 
/**
 * Register a colormap
 * @param name - name of the colormap
 * @param colormap - colormap object
 */
function registerColormap(colormap: ColormapRegistration) {
  _colormaps.set(colormap.Name, colormap);
}
 
/**
 * Get a colormap by name
 * @param name - name of the colormap
 * @returns colormap object
 */
function getColormap(name) {
  return _colormaps.get(name);
}
 
/**
 * Get all registered colormap names
 * @returns array of colormap names
 *
 */
function getColormapNames() {
  return Array.from(_colormaps.keys());
}
 
/**
 * Finds a colormap that matches the given RGB points.
 *
 * @param rgbPoints - The RGB points to match against the colormaps.
 * @returns  The matched colormap object or null if no match is found.
 */
function findMatchingColormap(rgbPoints, actor): ColormapPublic | null {
  const colormapsVTK = vtkColorMaps.rgbPresetNames.map((presetName) =>
    vtkColorMaps.getPresetByName(presetName)
  );
 
  const colormapsCS3D = getColormapNames().map((colormapName) =>
    getColormap(colormapName)
  );
 
  const colormaps = colormapsVTK.concat(colormapsCS3D);
 
  // Find the colormap that matches the given RGB points
  const matchedColormap = colormaps.find((colormap) => {
    const { RGBPoints: presetRGBPoints } = colormap;
 
    if (presetRGBPoints.length !== rgbPoints.length) {
      return false;
    }
 
    for (let i = 0; i < presetRGBPoints.length; i += 4) {
      if (
        !isEqual(
          presetRGBPoints.slice(i + 1, i + 4),
          rgbPoints.slice(i + 1, i + 4)
        )
      ) {
        return false;
      }
    }
 
    return true;
  });
 
  Iif (!matchedColormap) {
    return null;
  }
 
  const opacity = [];
  if (actorIsA(actor, 'vtkVolume')) {
    const opacityPoints = actor
      .getProperty()
      .getScalarOpacity(0)
      .getDataPointer();
 
    Iif (!opacityPoints) {
      return {
        name: matchedColormap.Name,
      };
    }
 
    for (let i = 0; i < opacityPoints.length; i += 2) {
      opacity.push({
        value: opacityPoints[i],
        opacity: opacityPoints[i + 1],
      });
    }
  }
 
  return {
    name: matchedColormap.Name,
    opacity,
  };
}
 
export {
  getColormap,
  getColormapNames,
  registerColormap,
  findMatchingColormap,
};