# How to colour a 3D image with a continuous spectrum of colours

10 views (last 30 days)
Matlab2010 on 17 Jun 2014
Edited: Cedric Wannaz on 18 Jun 2014
I have a 3D image. I would like to shade it in a continuous colour. I.e. where the colour of the image is conditional on its location in 3D space. How can I do this for for my image? For example:
My code to generate the image is below.
thanks
%www.mathworks.co.uk/matlabcentral/fileexchange/30923-fast-stl-import-function
%www.cs.technion.ac.il/~gershon/EscherForReal/PenroseTriangleStl1.zip
myStr = 'D:\PenroseTriangle.stl';
[vertices, norms] = import_stl_fast(myStr,2);
n = length(vertices)/3;
X = reshape(vertices(:,1),3,n);
Y = reshape(vertices(:,2),3,n);
Z = reshape(vertices(:,3),3,n);
C = zeros(3,n);
%%Render
figure;
set(gcf, 'Color', 'white');
as = 0.95;
strt=1;
stop=6000;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', [0 0 1], ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6001;
stop=6299;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', [0 1 0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6300;
stop=6719;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', [1 0 0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
view([-135 35]);
axis('image');
axis('off');
% colormap('hsv');
material('shiny');

Jason Nicholson on 17 Jun 2014
Edited: Jason Nicholson on 17 Jun 2014
The patch command's fourth argument, C, is the color of the that patch. Your C matrix is C=0. That is the problem. Instead use a function that defines the color matrix C. Matlab will scale C matrix to the default color values.
Try this:
% Euclidean distance in 3d
C = sqrt(X.^2 + Y.^2 + Z.^2);
Here is what it does to your code:
%www.mathworks.co.uk/matlabcentral/fileexchange/30923-fast-stl-import-function
%www.cs.technion.ac.il/~gershon/EscherForReal/PenroseTriangleStl1.zip
myStr = 'PenroseTriangle.stl';
[vertices, norms] = import_stl_fast(myStr,2);
n = length(vertices)/3;
X = reshape(vertices(:,1),3,n);
Y = reshape(vertices(:,2),3,n);
Z = reshape(vertices(:,3),3,n);
C = sqrt(X.^2 + Y.^2 + Z.^2);
%%Render
figure;
set(gcf, 'Color', 'white');
as = 0.95;
strt=1;
stop=6000;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', 'interp', ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6001;
stop=6299;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', 'interp', ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
strt=6300;
stop=6719;
patch(X(:,strt:stop),Y(:,strt:stop),Z(:,strt:stop),C(:,strt:stop),...
'FaceColor', 'interp', ...
'EdgeColor', 'none', ...
'FaceLighting', 'phong', ...
'AmbientStrength', as);
view([-135 35]);
axis('image');
axis('off');
colormap('hsv');
material('shiny');
Cedric Wannaz on 18 Jun 2014
Edited: Cedric Wannaz on 18 Jun 2014
Yes I was wondering as well. Hopefully he is just on a sunny island and was wise enough not to take his keyboard! Speaking of ratio, I built the code attached lately, just for fun (well, I was waiting at a bus stop, so .. "for more fun than just waiting").

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

### Tags

No tags entered yet.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by