Matrix of fading colors
8 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!