How can I retrieve RGB array from a colormap given a CData?
Show older comments
Hello,
If I have a designated colormap, jet for example, and the caxis is set to [0 1] and I want to return the RGB values (array of three numbers) of CData 0.5, how can I do this?
Thanks!
Accepted Answer
More Answers (1)
By default (with CDataMapping property is set to 'scale'), CData is scaled to "map" to colormap range. So if you somehow convert the CData, colormap to index image, you can then convert indexed image to RGB image using ind2rgb function. The example follows.
% Example plot with colormap
h = surf(peaks);
Cdata = h.CData;
cmap = colormap;
% make it into a index image.
cmin = min(Cdata(:));
cmax = max(Cdata(:));
m = length(cmap);
index = fix((Cdata-cmin)/(cmax-cmin)*m)+1; %A
% Then to RGB
RGB = ind2rgb(index,cmap)
Please note that if you specify index as an array of class single or double, then the value 1 corresponds to the first color in the colormap (see +1 at %A above).
Related doc pages:
1 Comment
Walter Roberson
on 18 Nov 2016
Edited: Walter Roberson
on 18 Nov 2016
Or on the last step instead of ind2rgb, just
RGB = cmap(index,:)
It depends whether you want something x 3 (my version) or something by something x 3 (Michio's code)
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!