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

colorcolormapimage processing

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

Best Answer

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);