Matrix of fading colors

조회 수: 7 (최근 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