Azzera filtri
Azzera filtri

Matrix of fading colors

2 visualizzazioni (ultimi 30 giorni)
Matthew Pearson
Matthew Pearson il 2 Ott 2015
Risposto: DGM il 6 Giu 2024
I'm trying to make a 256x256 matrix of colors fading into each other. The top left corner is red, the top right is purple, the bottom right is blue, and the bottom left is green. The blue and the red fade into each other, which is easy to figure out. The problem is the green fades out radially, in both directions, and I can't get my image to do that. My code so far is:
mat3d(:,:,1) = zeros(256,256);
mat3d(:,:,2) = zeros(256,256);
mat3d(:,:,3) = zeros(256,256);
increase = linspace(0,1,256);
decrease = linspace(1,0,256);
for i = 1:256
mat3d(i,:,1) = decrease(i);
mat3d(:,i,3) = increase(i);
for j = 1:256
dist = sqrt((256-i)^2+(256-j)^2);
end
end
I can't figure out how to write the last for loop.

Risposte (1)

DGM
DGM il 6 Giu 2024
Loops aren't needed.
% don't embed parameters into the code
outsize = [256 256]; % [y x]
% i'm assuming we live prior to R2016b
x = linspace(0,1,outsize(2));
y = linspace(1,0,outsize(1)); % because the image origin is at the top
[X Y] = meshgrid(x,y);
R = 1 - sqrt(X.^2 + Y.^2)/sqrt(2);
% assemble the image
outpict = cat(3,Y,R,X);
% show it
imshow(outpict)

Community Treasure Hunt

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

Start Hunting!

Translated by