### 用Matlab繪制三維RGB立方體模型。

#### [英]Drawing 3-D RGB cube model with Matlab

I wrote this code to draw an RGB cube, but it's color not exact as true?

``````%Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8]

%Define an eight row by three column matrix to define the vertices at which
%the faces meet
vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1]

%Plot the cube ----- gives each face a different color and creates the cube at a convenient viewing angle
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',hsv(8),'FaceColor','interp');
view(3);
``````

## 2 个解决方案

### #1

2

By the time, I finished the code, @horchler's answer was online already. It looks perfect. Anyway posting mine as well.

To understand what colors you are applying, I printed values of hsv(8) as follows.

``````1.0000         0         0
1.0000    0.7500         0
0.5000    1.0000         0
0    1.0000    0.2500
0    1.0000    1.0000
0    0.2500    1.0000
0.5000         0    1.0000
1.0000         0    0.7500
``````

But what you want to apply actually is red, green, blue, white, and cyan, magenta, yellow, black. Please refer to this link to know about Matlab color codes. Hence we can apply the colors to each vertex manually based on your requirement. I changed your code as follows.

``````% Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8];

% Define an eight row by three column matrix to define the vertices at which
% the faces meet
vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1];

% Plot the cube ----- gives each face a different color and creates the
% cube at a convenient viewing angle
clear cdata;
cdata = [
0 0 0; % black
1 0 0; % red
1 0 1; % magenta
0 0 1; % blue
0 1 0; % green
1 1 0; % yellow
1 1 1; % white
0 1 1; % cyan
];

patch('Vertices',vm,'Faces',fm,'FaceVertexCData',cdata,'FaceColor','interp');

axis equal;
axis off;
view(3);
``````

Output:

### #2

3

It is the color map that needs to be updated to get your plot to look like the one in your link. You can't simply use a built-in function to directly generate the right sequence. Additionally, calling `hsv(8)` produces additional colors that you don't want (print it out in the Command Window to see), but doesn't include pure white or black. You can use `hsv(6)` and append `[0 0 0]` and `[1 1 1]`, but you'll need to make sure the ordering aligns with the rest of your code (`fm` and `vm`).

Here's a revised version of your code – the `cm` matrix encodes the pattern of colors for each vertex:

``````% Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5;
2 3 7 6;
3 4 8 7;
4 1 5 8;
1 2 3 4;
5 6 7 8];

% Define an eight row by three column matrix to define the vertices at which the faces meet
vm = [0 0 0;
1 0 0;
1 1 0;
0 1 0;
0 0 1;
1 0 1;
1 1 1;
0 1 1];

% RGB colors for each vertex
cm = [0 0 0;
0 1 0;
1 1 0;
1 0 0;
0 0 1;
0 1 1;
1 1 1;
1 0 1];

% Plot the cube - gives each face a different color and creates the cube at a convenient viewing angle
figure('Color','w')
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',cm,'FaceColor','interp');
view(120,30);

% Plot axes
axis equal;
axis off;
d1 = 1.25;
line([0 0 0;d1 0 0],[0 0 0;0 d1 0],[0 0 0;0 0 d1],'Color','k','LineWidth',2);

% Label axes
d2 = 0.1;
text([0 1 0],[1 -d2 -d2],[-d2 0 1],'255','FontSize',11,'HorizontalAlignment','center');
text([0 d1 0],[d1 d2 d2],[d2 0 d1],{'R','G','B'},'FontSize',16);
``````

This results in a figure that looks like this