How can I get RGB color of a surface?

조회 수: 2 (최근 30일)
KAE
KAE 2017년 8월 4일
편집: KAE 2017년 8월 4일
How can I retrieve the displayed RGB color value of a surface? The following code draws a "line" colored to indicating a chosen value. In fact the line is really a surface, but it has a constant color as long as the caxis and colormap aren't changed. How do I retrieve this RGB color value, which is green in the example below? (My motivation is to use this RGB color for a text label).
figure;
x = 1:20;
y = sin(x*pi/8);
vr = 200*ones(size(x)); % This value is used to assign the surface's color
% Unlike typical surfaces, the height doesn't vary
z = zeros(size(x));
% Make a very skinny surface that looks like a line
h = surface([x;x],[y;y],[z;z],[vr;vr],'facecolor','none',...
'edgecolor','flat','linewidth',1) ;
colormap(jet); % This affects the color of the surface
caxis([0 400]); % This also affects the color of the surface
colorbar;
% h is a surface (matlab.graphics.primitive.Surface)
get(h, 'color'); % Can't get the color of a surface so this fails here
The plot below shows that the surface appears as a green "line", and I'd like to retrieve the green color.

채택된 답변

Image Analyst
Image Analyst 2017년 8월 4일
편집: Image Analyst 2017년 8월 4일
I don't know what h is, since I didn't write that File Exchange code. Is h a 2-d matrix? If so, can't you just use the value of h as an index for your colormap, jet, to get the color for a particular height?
  댓글 수: 5
Image Analyst
Image Analyst 2017년 8월 4일
Well I can't see any surface, so I assume you have some surface where the color of the surface depends on the height of the surface, like this gives you:
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
C = X.*Y;
surf(X,Y,Z,C)
colorbar
KAE
KAE 2017년 8월 4일
I have a very funny surface: it is so skinny it appears like a green line in the example plot, and the values are all the same height (200 in the example). Why do this? Because the "line" color can indicate some relevant information about the plotted data. For example, you could plot price time series for various stocks and the color of each "line" indicates the number of people following it on social media. I think the lookup table recommendation is straightforward so I will pursue that!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by