How to get the rgb code from Matlab figure

31 次查看(过去 30 天)
Hello Everyone,
I have attached a matlab figure here. This plot was generated from Matlab and I need to know the RGB color code for each color used in this picture. I know that there are exactly 5 colors have been used for this plot, I need to extract these 5 colors. Please help me to solve this problem.
Thank you.
Khalid
  2 个评论
Adam Danz
Adam Danz 2020-4-29
编辑:Adam Danz 2020-4-29
Do you have the fig file (if so, attach it) and do you know which function was used to generate the plot?
Khalid Ibne Masood
Khalid Ibne Masood 2020-4-29
Hello Adam,
Thank you, here I have attached the .fig file.
I believe 'surf' was used to generate the plot.
Thank you.
Khalid

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-4-29
编辑:Adam Danz 2020-4-29
Retreiving the colors from a surface plot can be trickly due to the interaction between the color properties of a surface object. If you plan to apply this solution to a variety of surface plots with varying color properties, you'll need to carefully read about the color properties and how they interact with eachother: https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.surface-properties.html#d120e1097631
See inline comments for details.
% Open the figure
fig = openfig('Question.fig');
% Find the axis handle
ax = findobj(fig, 'Type', 'axes');
% Find the surface handle
surfObj = ax.Children;
% Get the colormap
cmap = ax.Colormap;
% scale the color data values to the range of the
% colormap to match the color data with the colormap rows.
cval = round((surfObj.CData - ax.CLim(1))./range(ax.CLim) .* (size(cmap,1)-1)) +1;
% List the unique colors in order from left to right.
uniqueRGB = cmap(unique(cval(:),'stable'),:);
Results:
uniqueRGB =
0.5 0 0
0.9375 1 0.0625
0.5625 1 0.4375
0.4375 1 0.5625
0 0 0.5625
Plot a large dot above each color section to check the results
% Add dots to plot to confirm color
hold on
h = scatter3([.1 .35 .6 .80 .95], repmat(1,1,size(uniqueRGB,1)), repmat(max(ax.ZLim),1,size(uniqueRGB,1)), ...
200, uniqueRGB, 'filled', 'MarkerEdgeColor', 'k', 'LineWidth', 2);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by