[SOLVED] horizontal gradient

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

[SOLVED] horizontal gradient

Post by Draoidh »

How to create a horizontal gradient ?

Create a vertical gradient:

Code: Select all

convert  -size 400x50 gradient:    vertical.png
Ok, I could create a horizontal gradient like this:

Code: Select all

convert  -size 50x400 gradient: -rotate 90  horizontal.png
But how can I do it without transposing width and height?

The reason I am asking is: my geometry parameter is in a shell variable and ideally I would like to avoid faffing around with width and height in my bash script.
Last edited by Draoidh on 2012-10-02T02:54:15-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: horizontal gradient

Post by fmw42 »

Use the 2 pt barycentric method. See http://www.imagemagick.org/Usage/canvas ... _gradients
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: horizontal gradient

Post by anthony »

Or generate a diagonal gradient using a braycentric sparse color gradient. That will generate a diagonal gradient.
http://www.imagemagick.org/Usage/canvas ... _gradients
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: horizontal gradient

Post by Draoidh »

I think I might have oversimplified the example.

What I am really after is a multi-colour gradient.
Vertical gradient example:

Code: Select all

convert \( -size 400x50 gradient: -interpolate Bicubic \
\( +size  xc:blue xc:yellow xc:red -reverse +append \) -clut \) vertical.png
The following parts of the code have been auto-generated elsewhere and therefore are a given:

Code: Select all

400x300

Code: Select all

xc:blue xc:yellow xc:red
Do it via Shell Script:

Vertical gradient :

Code: Select all

convert \( -size $dim gradient: -interpolate Bicubic  \
\( +size  $colours -reverse  +append \) -clut \) vertical.png
Horizontal gradient :

Code: Select all

#transpose dimension

hdim=${dim#*x}x${dim%x*}
convert \( -size $hdim gradient: -interpolate Bicubic  -rotate 90 \
\( +size  $colours -reverse  +append \) -clut \) horizontal.png
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: horizontal gradient

Post by Draoidh »

Likewise, based on the same generated code pieces, I need to produce diagonal gradients.

Diagonal gradient, first attempt:

Code: Select all

convert \( -size $dim gradient: -interpolate Bicubic -distort SRT 45  \
\( +size  $colours -reverse  +append \) -clut \) diagonal.png
But when the target dimension is a sliver (e.g. 400x50), the gradient is not the desired result:
Image

Second attempt:
The best way I can figure is produce a big square gradient, distort and resize it:

Code: Select all

convert \( -size 1000x1000 gradient: -interpolate Bicubic -distort SRT 45 \
\( +size  $colours -reverse  +append \) -clut \) -resize ${dim}! diagonal2.png
Image

For reference, here are my vertical and horizontal gradients:

Image

Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: horizontal gradient

Post by anthony »

Take a look at Sparse Color Gradients...
http://www.imagemagick.org/Usage/canvas/#barycentric

This just takes ANY image, and overwrites it with the gradient specified. Positions of the colors can be set using percent escapes.
However adding 3 colors in a linear line will likely cause a fault, as it technically needs a triangle of points, 2 of whcih may be the same color to specify the angle of the gradient.

However using it to generate the grayscale gradient which is then recolored will make things very versatile, in terms of angles, specified using points.

That means you can generate the 'desired size', and then just use a separate 'sparse color' argument variable to specify the gradient type, and finally recolor it using the appropriate CLUT image. Each step a completely separate step to the others.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: horizontal gradient

Post by Draoidh »

Anthony, thanks for pointing me to the Barycentric Gradients. I'll keep that in mind for future reference. So many ways to skin a cat with IM!

I don't think it's quite the right thing for linear gradients (which is all that I am after albeit at different angles):

Code: Select all

convert -size 100x100 xc: -sparse-color  Barycentric '10,10 red   50,10 blue   90,10 lime'  sparse_barycentric.png
I'll mark this thread as SOLVED now as I am getting the desired gradients.
Post Reply