How to create rainbow colormap with violet

99 views (last 30 days)
I am familiar with the 'jet' colormap which is actually the rainbow colormap.
But, I want to add to this colormap (or create a new one), which is still a rainbow colormap, but would start from violet for the low levels (instead of blue).
Any idea how can I achive this?
Would appreciate youe kind response/

Accepted Answer

DGM on 5 Jan 2022
Edited: DGM on 5 Jan 2022
If you have an image of a colormap from some other figure, you can fairly easily just extract the map without having to guess about what path it makes in some colorspace.
% read image and process it
margins = [7 7 15 11]; % N S E W
A = imread('');
A = A(1+margins(1):end-margins(2),1+margins(4):end-margins(3),:);
CT0 = permute(mean(im2double(A),1),[2 3 1]);
CT0 = CT0([true; ~all(diff(CT0,1,1)==0,2)],:); % remove duplicate rows
% CT0 is now a fixed-length color table
% make it whatever length we want
N = 32; % specify the number of colors in table
na = size(CT0,1);
CT = interp1(linspace(0,1,na),CT0,linspace(0,1,N));
x = linspace(-pi/2,pi/2,30);
y = x.';
z = sin(x).*sin(y);
The extracted color table (CT0) can be stored as a .mat file or used by a simple function containing the interpolation routine above. In that manner, you can have a convenient function that can be called like jet() or parula() to return a color table of any desired length. Attached is such a function that will return the above colormap for any user-specified length.
C = mycolortable(16)
C = 16×3
0.1405 0.0072 0.2242 0.5172 0.0102 0.4591 0.8287 0.0235 0.7762 0.1711 0.0201 0.9859 0.0260 0.2198 0.8213 0 0.5438 0.9673 0.0229 0.9820 0.9416 0.0031 0.9081 0 0.0264 0.7446 0.0193 0.5732 0.9149 0.0024
% show color tables of different lengths
CT = [];
for n = [16 32 64 128 256]
thisct = permute(mycolortable(n),[1 3 2]);
CT = [CT imresize(thisct,[256 16],'nearest')];
CT = rot90(imresize(CT,[512 256],'nearest'),1);
The given map appears to be done simply in RGB, and could be reconstructed as a path. The problem is that there are quite a few breakpoints near the LHS of the image, and the compression artifacts pretty much turn any attempt to reconstruct it into an exercise in guessing. If exact replication of the breakpoints isn't an option, then simple interpolation is as good as any guess.
omri r
omri r on 8 Jan 2022
Thank you!
It's almost like a magic...!

Sign in to comment.

More Answers (2)

Turlough Hughes
Turlough Hughes on 4 Jan 2022
Edited: Turlough Hughes on 4 Jan 2022
Violet is approximately [0.5 0 1] in the rgb space.
So we can modify the blue part of the jet colormap, to have a bit more red as follows:
cmap = jet(256);
cmap(1:find(cmap(:,1)==0.5),1) = 0.5;
hf = figure('Units','normalized');
hCB = colorbar('north');
hCB.Position = [0.15 0.3 0.74 0.4]; % edit just removes some whitespace
hf.Position(4) = 0.1000;
omri r
omri r on 5 Jan 2022
Thank you.
I undersatnd the drawbacks of the rainbow colormap.
But I'm trying to compare my results to some other application colormap results and qualative matter.

Sign in to comment.

Image Analyst
Image Analyst on 4 Jan 2022
The MATLAB built-in "turbo" colormap is nice.
cmap = turbo(256);
No violet, but better than jet.
The Odyssea color map is pretty cool. See attached demo.


Find more on Colormaps in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by