![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/853390/image.png)
How to create rainbow colormap with violet
112 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
DGM
2022-1-5
编辑:DGM
2022-1-5
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);
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)
% 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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/853390/image.png)
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.
2 个评论
DGM
2022-1-7
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 = 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
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.
更多回答(2 个)
Turlough Hughes
2022-1-4
编辑:Turlough Hughes
2022-1-4
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;
5 个评论
Turlough Hughes
2022-1-4
Hmm, yea... on second thought, maybe the answer should be "don't use a rainbow colormap".
The following would be worth reading:
Image Analyst
2022-1-4
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/853055/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!