img= ExampleData[{"TestImage3D", "MRknee"}];
Manipulate[
colors = {Black, Blue, Purple, Red, Brown, Gray, Green, Orange, Pink,
Cyan, Yellow, White};
w = List @@@ ColorConvert[colors, "RGB"].{0.2989, 0.5870, 0.1140};
opacities = {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12};
co = Table[AppendTo[colors[[i]], opacities[[i]]], {i, 12}];
colorfn = Evaluate[Blend[{w, co}\[Transpose], #]] &;
Image3D[img,
ColorFunction -> colorfn], {x1, 0, 1}, {x2, 0, 1}, {x3, 0, 1}, {x4,
0, 1}, {x5, 0, 1}, {x6, 0, 1}, {x7, 0, 1}, {x8, 0, 1}, {x9, 0,
1}, {x10, 0, 1}, {x11, 0, 1}, {x12, 0, 1}]
The above code snippet works fine. However, 'AppendTo' adds an element on each iteration. I want to append the opacity values to be appended to the color values only once (during the execution of the Manipulate for the first time) and from the next time onwards (for any change in the parameters x1, x2, ..) it will just replace the last the opacity value.
[
Table[AppendTo[colors[[i]], opacities[[i]]], {i, 12}]
results
{GrayLevel[0, 0], RGBColor[0, 0, 1, 0], RGBColor[0.5, 0, 0.5, 0],
RGBColor[1, 0, 0, 0], RGBColor[0.6, 0.4, 0.2, 0], GrayLevel[0.5, 0],
RGBColor[0, 1, 0, 0], RGBColor[1, 0.5, 0, 0], RGBColor[1, 0.5, 0.5, 0],
RGBColor[0, 1, 1, 0], RGBColor[1, 1, 0, 0], GrayLevel[1, 0]}
(*when it is first called*)
Next iteration ...
{GrayLevel[0, 0, 0], RGBColor[0, 0, 1, 0, 0], RGBColor[0.5, 0, 0.5, 0, 0],
RGBColor[1, 0, 0, 0, 0], RGBColor[0.6, 0.4, 0.2, 0, 0], GrayLevel[0.5, 0, 0],
RGBColor[0, 1, 0, 0, 0], RGBColor[1, 0.5, 0, 0, 0], RGBColor[1, 0.5, 0.5, 0, 0],
RGBColor[0, 1, 1, 0, 0], RGBColor[1, 1, 0, 0, 0], GrayLevel[1, 0, 0]}
and so on. ]
How can I modify the above code to serve the purpose?


Manipulateand what does not. – Mr.Wizard Feb 22 '17 at 13:46Manipulateand friends are rather tricky and it's important to see multiple examples that work as well as some that don't, IMHO. I hope this one is helpful to you. – Mr.Wizard Feb 22 '17 at 18:41