How to create rainbow colormap with violet
135 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/
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('https://www.mathworks.com/matlabcentral/answers/uploaded_files/853000/image.png');
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)
% 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.
More Answers (2)
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;