All files / packages/tools/src/stateManagement/segmentation/config segmentationColor.ts

21.73% Statements 5/23
10% Branches 1/10
25% Functions 1/4
21.73% Lines 5/23

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                                                                                                                                                        9x         9x           9x     9x 9x                                                                  
import { utilities } from '@cornerstonejs/core';
import * as SegmentationState from '../../../stateManagement/segmentation/segmentationState';
import { Color } from '../../../types/SegmentationStateTypes';
import { ColorLUT } from '../../../types/SegmentationStateTypes';
import { triggerSegmentationRepresentationModified } from '../triggerSegmentationEvents';
 
/**
 * addColorLUT - Adds a new color LUT to the state at the given colorLUTIndex.
 * If no colorLUT is provided, a new color LUT is generated.
 *
 * @param colorLUTIndex - the index of the colorLUT in the state
 * @param colorLUT - An array of The colorLUT to set.
 * @returns
 */
function addColorLUT(colorLUT: ColorLUT, colorLUTIndex: number): void {
  if (!colorLUT) {
    throw new Error('addColorLUT: colorLUT is required');
  }
 
  // Append the "zero" (no label) color to the front of the LUT, if necessary.
  if (!utilities.isEqual(colorLUT[0], [0, 0, 0, 0])) {
    console.warn(
      'addColorLUT: [0, 0, 0, 0] color is not provided for the background color (segmentIndex =0), automatically adding it'
    );
    colorLUT.unshift([0, 0, 0, 0]);
  }
 
  SegmentationState.addColorLUT(colorLUT, colorLUTIndex);
}
 
/**
 * It sets the toolGroup's segmentationRepresentation to use the provided
 * colorLUT at the given colorLUTIndex.
 * @param toolGroupId - the id of the toolGroup that renders the representation
 * @param segmentationRepresentationUID - the representationUID for the segmentation
 * @param colorLUTIndex - the index of the colorLUT to use
 */
function setColorLUT(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  colorLUTIndex: number
): void {
  const segRepresentation =
    SegmentationState.getSegmentationRepresentationByUID(
      toolGroupId,
      segmentationRepresentationUID
    );
 
  if (!segRepresentation) {
    throw new Error(
      `setColorLUT: could not find segmentation representation with UID ${segmentationRepresentationUID}`
    );
  }
 
  if (!SegmentationState.getColorLUT(colorLUTIndex)) {
    throw new Error(
      `setColorLUT: could not find colorLUT with index ${colorLUTIndex}`
    );
  }
 
  segRepresentation.colorLUTIndex = colorLUTIndex;
 
  triggerSegmentationRepresentationModified(
    toolGroupId,
    segmentationRepresentationUID
  );
}
 
/**
 * Given a tool group UID, a segmentation representationUID, and a segment index, return the
 * color for that segment. It can be used for segmentation tools that need to
 * display the color of their annotation.
 *
 * @param toolGroupId - The Id of the tool group that owns the segmentation representation.
 * @param segmentationRepresentationUID - The uid of the segmentation representation
 * @param segmentIndex - The index of the segment in the segmentation
 * @returns A color.
 */
function getColorForSegmentIndex(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  segmentIndex: number
): Color {
  const segmentationRepresentation =
    SegmentationState.getSegmentationRepresentationByUID(
      toolGroupId,
      segmentationRepresentationUID
    );
 
  Iif (!segmentationRepresentation) {
    throw new Error(
      `segmentation representation with UID ${segmentationRepresentationUID} does not exist for tool group ${toolGroupId}`
    );
  }
 
  const { colorLUTIndex } = segmentationRepresentation;
 
  // get colorLUT
  const colorLUT = SegmentationState.getColorLUT(colorLUTIndex);
  return colorLUT[segmentIndex];
}
 
function setColorForSegmentIndex(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  segmentIndex: number,
  color: Color
): void {
  // Get the reference to the color in the colorLUT.
  const colorReference = getColorForSegmentIndex(
    toolGroupId,
    segmentationRepresentationUID,
    segmentIndex
  );
 
  // Modify the values by reference
  for (let i = 0; i < color.length; i++) {
    colorReference[i] = color[i];
  }
 
  triggerSegmentationRepresentationModified(
    toolGroupId,
    segmentationRepresentationUID
  );
}
 
export {
  getColorForSegmentIndex,
  addColorLUT,
  setColorLUT,
  setColorForSegmentIndex,
};