Matrix of fading colors

8 次查看(过去 30 天)
Matthew Pearson
Matthew Pearson 2015-10-2
回答: DGM 2024-6-6
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.

回答(1 个)

DGM
DGM 2024-6-6
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)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by