# How to create rainbow colormap with violet

124 views (last 30 days)
omri r on 4 Jan 2022
Commented: omri r on 8 Jan 2022
Hi,
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/
O.

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 = 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);
surf(x,y,z)
colormap(CT)
colorbar
view(-17,50)
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')];
end
CT = rot90(imresize(CT,[512 256],'nearest'),1);
imshow(CT)
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.
DGM on 7 Jan 2022
For what it's worth, this is one way you could do it by interpreting the paths manually.
First start by getting the initial color table from the image. I'm going to go ahead and reduce it to a shorter table for neatness later on, but you could use CT0 just the same.
% read image and process it
margins = [7 7 15 11]; % N S E W
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
N = 512; % specify the number of colors in table
na = size(CT0,1);
CT = interp1(linspace(0,1,na),CT0,linspace(0,1,N));
Then plot the trajectory of each channel in 2D and use the ROI tools to manually trace over each curve one at a time. This will return a list of breakpoint locations. Right-click to finish the polyline, adjust any points manually and hit enter. Move on to the green trace, and so on. Retrieve the three breakpoint lists and tweak them as necessary.
% plot trajectory in 2D
plot(CT(:,1),'r'); hold on
plot(CT(:,2),'g');
plot(CT(:,3),'b');
grid on
h = drawpolyline(gca);
input('Press enter when done adjusting this curve')
R = mat2str(h.Position,3)
h = drawpolyline(gca);
input('Press enter when done adjusting this curve')
G = mat2str(h.Position,3)
h = drawpolyline(gca);
input('Press enter when done adjusting this curve')
B = mat2str(h.Position,3)
Once you are satisfied with the changes to the breakpoint lists, they can be saved and used to create a new color table by interpolation. See the attached file. This is what the trajectory looks like after interpretation.
CT = mycolortable2(512);
figure
plot(CT(:,1),'r'); hold on
plot(CT(:,2),'g');
plot(CT(:,3),'b');
grid on
The compare the noisy table extracted from the image with the one created by manual breakpoint selection.
n = 256;
CT1 = permute(mycolortable(n),[1 3 2]);
CT2 = permute(mycolortable2(n),[1 3 2]);
CT = [imresize(CT1,[256 16],'nearest') imresize(CT2,[256 16],'nearest')];
CT = rot90(imresize(CT,[512 256],'nearest'),1);
figure
imshow(CT)
For seemingly small deviations between the two trajectories, the difference is rather noticeable visually. Make of this what you will.
omri r on 8 Jan 2022
Thank you!
It's almost like a magic...!

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');
colormap(cmap)
hCB = colorbar('north');
set(gca,'Visible',false)
hCB.Position = [0.15 0.3 0.74 0.4]; % edit just removes some whitespace
hf.Position(4) = 0.1000;
Turlough Hughes on 4 Jan 2022
Hmm, yea... on second thought, maybe the answer should be "don't use a rainbow colormap".
The following would be worth reading:
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.

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

### Categories

Find more on Colormaps in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!