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

24.13% Statements 7/29
14.28% Branches 2/14
25% Functions 1/4
25% Lines 7/28

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 134 135 136 137 138 139                                                                                                                                                                      11x         11x           11x     11x 11x 11x           11x                                                                  
import { utilities } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import * as SegmentationState from '../../../stateManagement/segmentation/segmentationState';
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: Types.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
): Types.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);
  let colorValue = colorLUT[segmentIndex];
  Iif (!colorValue) {
    if (typeof segmentIndex !== 'number') {
      throw new Error(`Can't create colour for LUT index ${segmentIndex}`);
    }
    colorValue = colorLUT[segmentIndex] = [0, 0, 0, 0];
  }
  return colorValue;
}
 
function setColorForSegmentIndex(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  segmentIndex: number,
  color: Types.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,
};