2D colormaps. MxN matrix of RGB values for 4 colors gradient

4 views (last 30 days)
Hi Dear Community
Matlab colormaps support 1D color gradients (Nx3 arrays for RGB values)
I would like to obtain a 2D matrix of RGB values (MxNx3) like this:
(6 plots are only examples. Im looking to generate the 4 colors gradien)
Look that there's no a single gradient moving over x-axis (something easy to do with matlab). Each corner correspond to a different color.
Some ideas?
Thanks a lot

Accepted Answer

Ameer Hamza
Ameer Hamza on 17 Sep 2020
Edited: Ameer Hamza on 17 Sep 2020
Try this
c{1} = [1 0 0]; % specify 4 colors
c{2} = [0 1 0];
c{3} = [0 0 1];
c{4} = [1 0.5 0.2];
C = reshape(vertcat(c{:}), 2, 2, []);
n = 700; % number of pixels
img = zeros(n, n, 3);
for i=1:3
img(:,:,i) = interp2([0 1], [0 1], C(:,:,i), linspace(0,1,n), linspace(0,1,n).');
end
imshow(img);
  1 Comment
Felipe Bayona
Felipe Bayona on 17 Sep 2020
Ameer
Thx for your help.
The clue is this interp2 function I didn't know. Works perfect

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 17 Sep 2020
If you do something like this:
[hsvImg] = rgb2hsv(Imrgb);
x_lims = [1 184;
199 383;
397 581;
593 779;
791 977;
991 size(Imrgb,2)];
for i1 = 6:-1:1,
subplot(4,6,i1)
imagesc(Imrgb(:,x_lims(i1,1):x_lims(i1,2),:))
subplot(4,6,i1+6*1)
imagesc(hsvImg(:,x_lims(i1,1):x_lims(i1,2),1))
subplot(4,6,i1+6*2)
imagesc(hsvImg(:,x_lims(i1,1):x_lims(i1,2),2))
subplot(4,6,i1+6*3)
imagesc(hsvImg(:,x_lims(i1,1):x_lims(i1,2),3))
end
subplot(4,6,1)
ylabel('RGB-Images')
subplot(4,6,1+6*1)
ylabel('Hue')
subplot(4,6,1+6*2)
ylabel('Saturation')
subplot(4,6,1+6*3)
ylabel('Intensity')
figure
for i1 = 6:-1:1,
subplot(4,6,i1)
imagesc(Imrgb(:,x_lims(i1,1):x_lims(i1,2),:))
subplot(4,6,i1+6*1)
imagesc(Imrgb(:,x_lims(i1,1):x_lims(i1,2),1))
subplot(4,6,i1+6*2)
imagesc(Imrgb(:,x_lims(i1,1):x_lims(i1,2),2))
subplot(4,6,i1+6*3)
imagesc(Imrgb(:,x_lims(i1,1):x_lims(i1,2),3))
end
subplot(4,6,1)
ylabel('RGB-Images')
subplot(4,6,1+6*1)
ylabel('Red')
subplot(4,6,1+6*2)
ylabel('Green')
subplot(4,6,1+6*3)
ylabel('Blue')
You get to look at the Hue, Saturation and Intensity variation of the 6 different sub-images, and their respective red, green and blue image-planes. From there you should see that some of them are "reasonably" simple, and you should be able to reproduce them.
For mapping 2 data-sets, lets say I1 and I2, it should simplify to at most three 2-D interpolations, perhaps something like this:
I1_linear = linspace(min(I1(:)),max(I1(:)),suitable_nr4size1);
I2_linear = linspace(min(I2(:)),max(I2(:)),suitable_nr4size2);
hsvI2I2(:,:,3) = interp2(I1_linear,I2_linear,hsvImg(:,x_lims(i1,1):x_lims(i1,2),3),I1,I2);
hsvI2I2(:,:,2) = interp2(I1_linear,I2_linear,hsvImg(:,x_lims(i1,1):x_lims(i1,2),2),I1,I2);
hsvI2I2(:,:,2) = interp2(I1_linear,I2_linear,hsvImg(:,x_lims(i1,1):x_lims(i1,2),1),I1,I2);
rgbI1I2 = hsv2rgb(hsvI1I2);
Or even simpler if you interpolate over the RGB-planes.
HTH
  2 Comments
Felipe Bayona
Felipe Bayona on 17 Sep 2020
Edited: Felipe Bayona on 17 Sep 2020
HiBjorn
Thx for your answer. I realized I didn't make the disclaimer that the 6 images are only examples. Im lloking to generate one four-color gradien matrix.
I'll check to the interpolations you mention.
Bjorn Gustavsson
Bjorn Gustavsson on 17 Sep 2020
Well, if you look at how the Hue, Saturation and Intensity or the Red, Green and Blue varies over your examples, you will start to understand how to design your 2-D colour-maps. I can only assume that there will be quite a bit of tinkering before you manage to generate visually pleasing maps (which are at least two interesting fields of work: what's a visually pleasing map for a given purpose? Does different peoples color-vision/perception vary enough to make big differences in what's good?)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!