Page 1 of 1

[SOLVED] horizontal gradient

Posted: 2012-09-29T15:40:19-07:00
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.

Re: horizontal gradient

Posted: 2012-09-29T17:14:39-07:00
by fmw42
Use the 2 pt barycentric method. See http://www.imagemagick.org/Usage/canvas ... _gradients

Re: horizontal gradient

Posted: 2012-09-29T17:57:34-07:00
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

Re: horizontal gradient

Posted: 2012-09-30T03:11:24-07:00
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

Re: horizontal gradient

Posted: 2012-09-30T04:07:11-07:00
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

Re: horizontal gradient

Posted: 2012-10-01T16:51:22-07:00
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.

Re: horizontal gradient

Posted: 2012-10-02T02:52:50-07:00
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.