Skip to content

Commit

Permalink
Merge pull request #140 from sigmama/feature/support-custom-color-map
Browse files Browse the repository at this point in the history
Support user defined color map preset
  • Loading branch information
floryst authored Nov 21, 2024
2 parents 0633dbc + c9aa1a1 commit 02c9c68
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/core/Geometry2DRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData';
import vtkActor2D, {
IActor2DInitialValues,
} from '@kitware/vtk.js/Rendering/Core/Actor2D';
import { IColorMapPreset } from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import { ICoordinateInitialValues } from '@kitware/vtk.js/Rendering/Core/Coordinate';
import { Coordinate } from '@kitware/vtk.js/Rendering/Core/Coordinate/Constants';
import vtkMapper2D, {
Expand Down Expand Up @@ -55,9 +56,9 @@ export interface Geometry2DRepresentationProps extends PropsWithChildren {
property?: IProperty2DInitialValues;

/**
* Preset name for the lookup table color map
* Preset name for the lookup table color map or user provided color map preset
*/
colorMapPreset?: string;
colorMapPreset?: string | IColorMapPreset;

/**
* Data range use for the colorMap
Expand Down
5 changes: 3 additions & 2 deletions src/core/GeometryRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData';
import vtkActor, {
IActorInitialValues,
} from '@kitware/vtk.js/Rendering/Core/Actor';
import { IColorMapPreset } from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import vtkMapper, {
IMapperInitialValues,
} from '@kitware/vtk.js/Rendering/Core/Mapper';
Expand Down Expand Up @@ -52,9 +53,9 @@ export interface GeometryRepresentationProps extends PropsWithChildren {
property?: IPropertyInitialValues;

/**
* Preset name for the lookup table color map
* Preset name for the lookup table color map or user provided color map preset
*/
colorMapPreset?: string;
colorMapPreset?: string | IColorMapPreset;

/**
* Data range use for the colorMap
Expand Down
5 changes: 3 additions & 2 deletions src/core/SliceRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AbstractImageMapper, {
vtkAbstractImageMapper,
} from '@kitware/vtk.js/Rendering/Core/AbstractImageMapper';
import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction';
import { IColorMapPreset } from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import vtkImageArrayMapper from '@kitware/vtk.js/Rendering/Core/ImageArrayMapper';
import vtkImageMapper, {
IImageMapperInitialValues,
Expand Down Expand Up @@ -64,9 +65,9 @@ export interface SliceRepresentationProps extends PropsWithChildren {
property?: IImagePropertyInitialValues;

/**
* Preset name for the lookup table color map
* Preset name for the lookup table color map or user provided color map preset
*/
colorMapPreset?: string;
colorMapPreset?: string | IColorMapPreset;

/**
* Data range use for the colorMap
Expand Down
5 changes: 3 additions & 2 deletions src/core/VolumeRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import vtkPiecewiseFunction from '@kitware/vtk.js/Common/DataModel/PiecewiseFunction';
import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction';
import { IColorMapPreset } from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import vtkVolume, {
IVolumeInitialValues,
} from '@kitware/vtk.js/Rendering/Core/Volume';
Expand Down Expand Up @@ -63,9 +64,9 @@ export interface VolumeRepresentationProps extends PropsWithChildren {
property?: IVolumePropertyInitialValues;

/**
* Preset name for the lookup table color map
* Preset name for the lookup table color map or user provided color map preset
*/
colorMapPreset?: string;
colorMapPreset?: string | IColorMapPreset;

/**
* Data range use for the colorMap
Expand Down
15 changes: 12 additions & 3 deletions src/core/modules/useColorTransferFunction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction';
import vtkColorMaps from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import vtkColorMaps, {
IColorMapPreset,
} from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import { Vector2 } from '@kitware/vtk.js/types';
import { compareVector2 } from '../../utils/comparators';
import deletionRegistry from '../../utils/DeletionRegistry';
Expand All @@ -9,7 +11,7 @@ import useGetterRef from '../../utils/useGetterRef';
import useUnmount from '../../utils/useUnmount';

export default function useColorTransferFunction(
presetName: string,
colorMapPreset: string | IColorMapPreset,
range: Vector2,
trackModified: BooleanAccumulator
) {
Expand All @@ -19,11 +21,18 @@ export default function useColorTransferFunction(
return func;
});

const isPassedInPresetName = typeof colorMapPreset === 'string';
const presetName = isPassedInPresetName
? colorMapPreset
: colorMapPreset.Name;

useComparableEffect(
() => {
if (!presetName || !range) return;
const lut = getLUT();
const preset = vtkColorMaps.getPresetByName(presetName);
const preset = isPassedInPresetName
? vtkColorMaps.getPresetByName(presetName)
: colorMapPreset;
lut.applyColorMap(preset);
lut.setMappingRange(range[0], range[1]);
lut.updateRange();
Expand Down

0 comments on commit 02c9c68

Please sign in to comment.