Rounded rectangle with shadow and gradient filling

Viewed 53

Using Magick I managed to create a simple rounded rectangle filled with a solid color and having a shadow:

magick montage -background none -size 300x200 xc:none -fill green -draw "roundRectangle 0,0 300,200 8,8" -shadow  -geometry +10+10

The result is the following:

enter image description here

Is there an easy way to slighty modify the command in order to have a gradient fill (for instance from green to red) instead of the solid green, mantaining all other aspects (rounded corners and shadow) ?

The ideal result is the following:

enter image description here

1 Answers

Here's a way to do that.

magick start.png \
  \( +clone -size "%Hx%W" +delete gradient:green-red -rotate -90 -write MPR:gradient +delete \) \
  \( +clone -alpha off -fill black +opaque green -fill white -opaque green -write MPR:alpha +delete \) \
  \( MPR:gradient MPR:alpha -compose copyalpha -composite \) \
  +compose -composite result.png

enter image description here

It works like this:

line 1: Load original image

line 2: Clone original image (just to get its size) then set the size for the forthcoming -gradient the same but rotated 90 degrees so it runs left-right instead of top-bottom. Make the gradient you want, rotate to correct orientation, and save in MPR "Magick Persistent Register" called gradient for later. Delete it for now.

line 3: Clone the original image. Make all non-green pixels black and all green pixels white. Save in another MPR called alpha. Delete it for now.

line 4: Reload gradient and alpha from MPRs and force the alpha channel into the gradient. The gradient will now be transparent wherever the original image was not green.

line 5: Reset back to default compose method and composite the new layer (containing gradient transparent where original was not green) over the original image and save.

Related