Collection of Squares RGB Matrix
14 views (last 30 days)
Show older comments
I'm trying to figure out how to code a 4*5 matrix of squares that have no space between them. I wanted to be able to adjust each square's color based on an RGB matrix without it being random. So far I've mostly seen random RGB code and don't know how to hard code it in. Any kind of help woudl be appreciated (extreme novice lol) Thanks!
Accepted Answer
John D'Errico
on 19 Feb 2023
Edited: John D'Errico
on 19 Feb 2023
Really pretty simple.
You have a list of colors. 24 of them, apparently. The thing is, all the examples you would see will have someone generating a set of colors. And since nobody knows what colors you might want, they might just generate random colors. Sorry, but I probably can't win here. I'll give you a simple code that will work, in the form of showpatchgrid.
I'm not feeling very creative, so I'll use the colormap jet to define the colors in my array. Other colormaps would work as nicely.
mycolors = jet(24)
showpatchgrid(mycolors,[4 6],50)
function colorgrid = showpatchgrid(RGBset,colorgriddim,patchsize)
% takes an array of colors, reshapes it into a rectangular color array, then expands each patch
colorgrid = reshape(RGBset,[colorgriddim,3]);
colorgrid = repelem(colorgrid,patchsize,patchsize,1);
imshow(colorgrid)
if nargout < 1
clear colorgrid
end
end
All that is required is an array of size 24x3, where each row represents one color. You will need to decide which colors you want, of course. Then just call that function, which can even return the grid it generates if you desire.
2 Comments
John D'Errico
on 19 Feb 2023
Edited: John D'Errico
on 20 Feb 2023
A grey is simply an RGB that has each channel the same value. So if you want a grey scale set of patches in some specific order, just choose a list of levels in the order you want, then replicate them to 3 channels.
greylevels = linspace(0,1,24);
greylevels = repmat(greylevels(:),[1,3]);
showpatchgrid(greylevels,[4 6],50)
function colorgrid = showpatchgrid(RGBset,colorgriddim,patchsize)
% takes an array of colors, reshapes it into a rectangular color array, then expands each patch
colorgrid = reshape(RGBset,[colorgriddim,3]);
colorgrid = repelem(colorgrid,patchsize,patchsize,1);
imshow(colorgrid)
if nargout < 1
clear colorgrid
end
end
Your question about trying how to sequence the colors so they are not in a certain order is impossible to answer, since I have no idea what order you want! Only you know that. And it requires nothing more than choosing a squence for the vector of grey levels, instead of using linspace as I did. I chose a vector that has the grey levels incrementing from black to white. Choose as you wish.
Anyway, why do I feel it necessary to respond to your comment, with a Calvin & Hobbs reference? (There were actually several about the world being in black and white...) Sadly, this single comment at the end will probably get my post flagged as potential spam by the website bots. Lets see if it does. :)
More Answers (2)
Sulaymon Eshkabilov
on 19 Feb 2023
If understood correctly, here is one example:
M = uint8(ones(10));
R = [255*M M M 255*M M;
M 255*M M M 255*M;
M M 255*M M M;
255*M M M 255*M M;];
G = [M 255*M M M 255*M;
M M 255*M M M;
255*M M M 255*M M;
M 255*M M M 255*M;];
B = [M M 255*M M M;
255*M M M 255*M M;
M 255*M M M 255*M;
M M 255*M M M;];
RGB(:,:,1)=R;
RGB(:,:,2)=G;
RGB(:,:,3)=B;
imshow(RGB)
0 Comments
DGM
on 20 Feb 2023
If you're drawing from a set of colors in a color table, then one simple way would be to just create an indexed image.
CT = hsv(6); % a color table to draw from
% construct the image by specifying row indices into CT
% this can be done randomly or in any arbitrary pattern
outpict = [1 2 3 4 5;
2 1 4 3 6;
6 5 3 4 2;
5 6 4 3 1];
% convert to RGB if desired
outpict = ind2rgb(outpict,CT);
% display it
imshow(outpict)
If you want a larger image composed of the same number of colored blocks, simply resize it.
outpict = imresize(outpict,10,'nearest'); % expand by 10x
imshow(outpict)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



