All files / tools/src/utilities/segmentation getSVGStyleForSegment.ts

75.75% Statements 25/33
35.13% Branches 13/37
100% Functions 1/1
75.75% Lines 25/33

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                                                                                                    1307x           1307x               1307x 1307x     1307x             1307x 1307x 1307x 1307x 1307x   1307x         1307x 1307x 1307x 1307x 1307x                 1307x 1107x     1307x 1307x   1307x 1307x   1307x         1307x   1307x                        
import { SegmentationRepresentations } from '../../enums';
 
import { getSegmentIndexColor } from '../../stateManagement/segmentation/config/segmentationColor';
import { getActiveSegmentation } from '../../stateManagement/segmentation/getActiveSegmentation';
import { getActiveSegmentIndex } from '../../stateManagement/segmentation/getActiveSegmentIndex';
import { getSegmentationRepresentationVisibility } from '../../stateManagement/segmentation/getSegmentationRepresentationVisibility';
import { internalGetHiddenSegmentIndices } from '../../stateManagement/segmentation/helpers/internalGetHiddenSegmentIndices';
import { segmentationStyle } from '../../stateManagement/segmentation/SegmentationStyle';
import type { ContourStyle } from '../../types';
 
export interface SVGStyleForSegmentParams {
  segmentationId: string;
  segmentIndex: number;
  viewportId: string;
  autoGenerated?: boolean;
}
 
/**
 * Gets the SVG style properties for a segmentation segment based on its state and configuration.
 * It is used for tools that draw svg and want to match the style of the segmentation.
 *
 * The style is determined by merging configurations from different levels based on precedence:
 * - Global segmentation style
 * - Per-segmentation style
 * - Per-segment style
 *
 * The style properties include:
 * - Line width, dash pattern, and opacity
 * - Fill opacity
 * - Colors for outline and fill
 * - Visibility
 *
 * The style varies based on whether the segment is:
 * - Auto-generated
 * - Active/inactive
 * - Currently being hovered/selected
 *
 * @param params - The parameters for getting the segment style
 * @param params.segmentationId - The ID of the segmentation
 * @param params.segmentIndex - The index of the segment within the segmentation
 * @param params.viewportId - The ID of the viewport
 * @param params.autoGenerated - Whether the segment was auto-generated
 * @returns The SVG style properties for the segment
 */
export function getSVGStyleForSegment({
  segmentationId,
  segmentIndex,
  viewportId,
  autoGenerated = false,
}: SVGStyleForSegmentParams) {
  const segmentColor = getSegmentIndexColor(
    viewportId,
    segmentationId,
    segmentIndex
  );
 
  const segmentationVisible = getSegmentationRepresentationVisibility(
    viewportId,
    {
      segmentationId,
      type: SegmentationRepresentations.Contour,
    }
  );
 
  const activeSegmentation = getActiveSegmentation(viewportId);
  const isActive = activeSegmentation?.segmentationId === segmentationId;
 
  // Merge the configurations from different levels based on its precedence
  const style = segmentationStyle.getStyle({
    viewportId,
    segmentationId,
    type: SegmentationRepresentations.Contour,
    segmentIndex,
  });
 
  const mergedConfig = style as ContourStyle;
  let lineWidth = 1;
  let lineDash = undefined;
  let lineOpacity = 1;
  let fillOpacity = 0;
 
  Iif (autoGenerated) {
    lineWidth = mergedConfig.outlineWidthAutoGenerated ?? lineWidth;
    lineDash = mergedConfig.outlineDashAutoGenerated ?? lineDash;
    lineOpacity = mergedConfig.outlineOpacity ?? lineOpacity;
    fillOpacity = mergedConfig.fillAlphaAutoGenerated ?? fillOpacity;
  } else if (isActive) {
    lineWidth = mergedConfig.outlineWidth ?? lineWidth;
    lineDash = mergedConfig.outlineDash ?? lineDash;
    lineOpacity = mergedConfig.outlineOpacity ?? lineOpacity;
    fillOpacity = mergedConfig.fillAlpha ?? fillOpacity;
  } else E{
    lineWidth = mergedConfig.outlineWidthInactive ?? lineWidth;
    lineDash = mergedConfig.outlineDashInactive ?? lineDash;
    lineOpacity = mergedConfig.outlineOpacityInactive ?? lineOpacity;
    fillOpacity = mergedConfig.fillAlphaInactive ?? fillOpacity;
  }
 
  // Change the line thickness when the mouse is over the contour segment
  if (getActiveSegmentIndex(segmentationId) === segmentIndex) {
    lineWidth += mergedConfig.activeSegmentOutlineWidthDelta;
  }
 
  lineWidth = mergedConfig.renderOutline ? lineWidth : 0;
  fillOpacity = mergedConfig.renderFill ? fillOpacity : 0;
 
  const color = `rgba(${segmentColor[0]}, ${segmentColor[1]}, ${segmentColor[2]}, ${lineOpacity})`;
  const fillColor = `rgb(${segmentColor[0]}, ${segmentColor[1]}, ${segmentColor[2]})`;
 
  const hiddenSegments = internalGetHiddenSegmentIndices(viewportId, {
    segmentationId,
    type: SegmentationRepresentations.Contour,
  });
 
  const isVisible = !hiddenSegments.has(segmentIndex);
 
  return {
    color,
    fillColor,
    lineWidth,
    fillOpacity,
    lineDash,
    textbox: {
      color,
    },
    visibility: segmentationVisible && isVisible,
  };
}