How to create a blue color gradient
227 views (last 30 days)
Show older comments
Hello, I want to create a 4K image (3840 x 2160 pixels) that has the blue color gradient from 0 to 255, with its 255 levels. Something like this as an example but well done with MATLAB. The goal is to make a linear gradient from 0 to 255 in blue.
Thank you very much in advance.
0 Comments
Accepted Answer
Image Analyst
on 25 May 2020
0 to 255 is 256 values, not 255 values. Anyway, try this:
rows = 3840;
columns = 2160;
black = zeros(rows, columns, 'uint8');
blueRamp = uint8(linspace(0, 255, columns));
% Make into 2-D image.
blueRamp = repmat(blueRamp, [rows, 1]);
rgbImage = cat(3, black, black, blueRamp);
imshow(rgbImage);
2 Comments
Image Analyst
on 7 Nov 2022
@Minjeong magenta means blue and red are the same, so:
rows = 3840;
columns = 2160;
black = zeros(rows, columns, 'uint8');
ramp = uint8(linspace(0, 255, columns));
% Make into 2-D image.
ramp = repmat(ramp, [rows, 1]);
rgbImage = cat(3, ramp, black, ramp);
imshow(rgbImage);
More Answers (2)
Ameer Hamza
on 25 May 2020
Edited: Ameer Hamza
on 25 May 2020
An alternate approach using indexed image
cols = 2160;
rows = 3840;
left_color = [1 1 1]; % white
right_color = [0 0.25 0.5]; % color according to the attached image
cmap = interp1([0, 1], [left_color; right_color], linspace(0, 1, cols));
img = repmat(1:cols, rows, 1);
imshow(img, cmap)
To get rgb image, you can use ind2rgb()
DGM
on 7 Nov 2022
Edited: DGM
on 7 Nov 2022
There are various ways to make a gradient image. I'm going to ignore specific colors, as it should be fairly obvious how to adapt between colors in most of these examples -- except the first one.
The most common suggestion is to build the image from its component channels, using 2D orthogonal gradients and solid fields of black or white. This works, but it may be cumbersome. Bear in mind that if the 2D gradients you use are full-swing, the only colors you can reach at the ends of your gradient will be the corners of the RGB cube. Scaling and offsetting the individual channels can allow you to reach other endpoint colors, but this is hardly intuitive when a particular tuple is in mind.
% basic channel stacking
% any combination of orthogonal linear gradients and solid fields
sz = [200 200];
gradx = repmat(linspace(0,1,sz(2)),[sz(1) 1]);
grady = repmat(linspace(0,1,sz(1)).',[1 sz(2)]);
bk = zeros(sz);
wh = ones(sz);
grp1 = cat(3,gradx,bk,gradx);
grp2 = cat(3,wh,gradx,grady);
grp3 = cat(3,gradx,grady,wh);
grp4 = cat(3,gradx*0.7,bk+0.5,0.3+grady*0.7);
outpict = [grp1 grp2; grp3 grp4]; % display all examples
imshow(outpict)
If you have a pair of colors in mind, you can just do basic 1D interpolation to build the gradient. You could adapt this to a horizontal gradient, or you could just transpose the result.
% basic 2-point interpolation
colorA = [0 1 0];
colorB = [1 0 0];
sz = [200 200]; % [y x]
x0 = 1/sz(1);
xq = linspace(x0,1,sz(1));
outpict = interp1([x0 1],[colorA; colorB],xq,'linear','extrap');
outpict = repmat(permute(outpict,[1 3 2]),[1 sz(2) 1]);
imshow(outpict)
Note that the gradient is dark in the middle. That's what happens when you try to do this operation in a nonlinear space. You could (approximately) linearize the same operation like so.
% basic 2-point interpolation (linear RGB approximation)
colorA = [0 1 0];
colorB = [1 0 0];
sz = [200 200]; % [y x]
x0 = 1/sz(1);
xq = linspace(x0,1,sz(1));
outpict = interp1([x0 1],[colorA; colorB].^2.4,xq,'linear','extrap');
outpict = repmat(permute(outpict,[1 3 2]),[1 sz(2) 1]);
outpict = outpict.^(1/2.4);
imshow(outpict)
Now consider that all of the above are strictly orthogonal gradients with a linear ease curve. If you want something else, or if you want something that's not so cumbersome, there are existing third-party tools that are made for creating color gradient images.
% using MIMT lingrad()
colorA = [0 0 1];
colorB = [1 0 1];
sz = [200 200]; % [y x]
% any two point or multipoint gradient
% user-selectable ease curves
% operation in linear rgb or srgb
% selectable output class
op1 = lingrad([sz 3],[0 0; 1 0],[colorA; colorB]*255,'linear','linrgb'); % a linear ramp
op2 = lingrad([sz 3],[0 0; 1 0],[colorA; colorB]*255,'cosine','linrgb'); % a cosine ramp
op3 = lingrad([sz 3],[0 0; 1 1],[colorA; colorB]*255,'linear','linrgb'); % doesn't have to be orthogonal
op4 = lingrad([sz 3],[0.5 0; 1 0.5],[colorA; colorB]*255,'cosine','linrgb'); % points can be anywhere
outpict = [op1 op2; op3 op4]; % display all examples
imshow(outpict)
MIMT lingrad() should be easy enough to use. It'll handle any 2-point gradient in sRGB or linear RGB. Also included in MIMT is radgrad(), which is a similar tool for creating radial gradients.
This comment demonstrates one method for creating arbitrarily-shaped gradients.
This answer includes various methods for color image generation, including gradients.
0 Comments
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!