Skip to content

Commit 66f86c9

Browse files
authored
Merge pull request #2604 from PaulHax/color-map-fix
fix(ColorTransferFunction): applyColorMap calls modified in more cases
2 parents 50d2299 + b40e33f commit 66f86c9

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

Sources/Rendering/Core/ColorTransferFunction/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ function vtkColorTransferFunction(publicAPI, model) {
12091209

12101210
//----------------------------------------------------------------------------
12111211
publicAPI.applyColorMap = (colorMap) => {
1212+
const oldColorSpace = JSON.stringify(model.colorSpace);
12121213
if (colorMap.ColorSpace) {
12131214
model.colorSpace = ColorSpace[colorMap.ColorSpace.toUpperCase()];
12141215
if (model.colorSpace === undefined) {
@@ -1218,12 +1219,18 @@ function vtkColorTransferFunction(publicAPI, model) {
12181219
model.colorSpace = ColorSpace.RGB;
12191220
}
12201221
}
1222+
let isModified = oldColorSpace !== JSON.stringify(model.colorSpace);
1223+
1224+
const oldNanColor = isModified || JSON.stringify(model.nanColor);
12211225
if (colorMap.NanColor) {
12221226
model.nanColor = [].concat(colorMap.NanColor);
12231227
while (model.nanColor.length < 4) {
12241228
model.nanColor.push(1.0);
12251229
}
12261230
}
1231+
isModified = isModified || oldNanColor !== JSON.stringify(model.nanColor);
1232+
1233+
const oldNodes = isModified || JSON.stringify(model.nodes);
12271234
if (colorMap.RGBPoints) {
12281235
const size = colorMap.RGBPoints.length;
12291236
model.nodes = [];
@@ -1240,13 +1247,15 @@ function vtkColorTransferFunction(publicAPI, model) {
12401247
});
12411248
}
12421249
}
1243-
// FIXME: not supported ?
1244-
// if (colorMap.IndexedColors) {
1245-
// }
1246-
// if (colorMap.Annotations) {
1247-
// }
12481250

1249-
publicAPI.sortAndUpdateRange();
1251+
const modifiedInvoked = publicAPI.sortAndUpdateRange();
1252+
1253+
const callModified =
1254+
!modifiedInvoked &&
1255+
(isModified || oldNodes !== JSON.stringify(model.nodes));
1256+
if (callModified) publicAPI.modified();
1257+
1258+
return modifiedInvoked || callModified;
12501259
};
12511260
}
12521261

Sources/Rendering/Core/ColorTransferFunction/test/testColorTransferFunction.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,61 @@ test('Test discretized color transfer function', (t) => {
159159

160160
t.end();
161161
});
162+
163+
test('Test applyColorMap calls modified', (t) => {
164+
const ctf = vtkColorTransferFunction.newInstance();
165+
166+
const colorMapA = {
167+
ColorSpace: 'RGB',
168+
RGBPoints: [0, 0, 0, 0, 1, 0, 0, 0],
169+
};
170+
const colorMapB = {
171+
ColorSpace: 'RGB',
172+
RGBPoints: [0, 0, 0, 0, 1, 1, 1, 1],
173+
};
174+
175+
ctf.applyColorMap(colorMapA);
176+
177+
let isModified = false;
178+
ctf.onModified(() => {
179+
isModified = true;
180+
});
181+
182+
ctf.applyColorMap(colorMapA);
183+
t.notOk(
184+
isModified,
185+
`Expect applyColorMap does not call modified with same color map`
186+
);
187+
188+
ctf.applyColorMap(colorMapB);
189+
t.ok(
190+
isModified,
191+
`Expect applyColorMap calls modified with different color map`
192+
);
193+
194+
colorMapB.ColorSpace = 'LAB';
195+
isModified = false;
196+
let modifiedReturn = ctf.applyColorMap(colorMapB);
197+
t.ok(
198+
isModified && modifiedReturn,
199+
`Expect applyColorMap calls modified with different ColorSpace`
200+
);
201+
202+
colorMapB.NanColor = [0, 0, 0, 1];
203+
isModified = false;
204+
modifiedReturn = ctf.applyColorMap(colorMapB);
205+
t.ok(
206+
isModified && modifiedReturn,
207+
`Expect applyColorMap calls modified with different NanColor`
208+
);
209+
210+
colorMapB.RGBPoints = [0, 0, 0, 0, 0.5, 1, 1, 1, 1, 1, 1, 1];
211+
isModified = false;
212+
modifiedReturn = ctf.applyColorMap(colorMapB);
213+
t.ok(
214+
isModified && modifiedReturn,
215+
`Expect applyColorMap calls modified with different RGBPoints`
216+
);
217+
218+
t.end();
219+
});

0 commit comments

Comments
 (0)