Gradient on icon, drop shadow

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
Avo
Posts: 2
Joined: 2013-11-23T18:08:31-07:00
Authentication code: 6789

Gradient on icon, drop shadow

Post by Avo »

Hi people,

In my company we have a HUGE iconpackage with white or black icons. And now the design for our product is changing somewhat, so our icons now need to have a bit of gradient and some drop shadow.
It's really easy to do in photoshop, but I would really like to make a script for it, so no manual work is needed.

I've made a small example of the process:
Image

I've made a script that "kind of" works. But my guess is that it's very inefficient because of all the seperate calls to convert (that doesn't really matter though, but for the sake of learning, pointers are welcome). The real problem is that I lose some precious detail while going through the process.

This is how I do it:

0)The original icon (48x48 pixels):
Image

1) Making a suitable gradient ("convert -size 48x48 gradient:'#c2cfdc'-'#849db8' gradient.png")
Image

2) Making a completely red background ("convert -size 48x48 xc:red red_background.png")
Image

3) Merging the icon with the red background ("convert red_background.png resized.png -gravity center -composite icon_with_red_background.png")
Image

4) Making the black icon transparent ("convert icon_with_red_background.png -fuzz 55% -transparent black icon_transparent.png")
Image

5) Merging with the gradient background ("convert gradient.png icon_transparent.png -gravity center -composite +repage with_gradient.png")
Image

6) Removing the red color ("convert with_gradient.png -fuzz 55% -transparent red with_gradient_no_red.png")
Image

7) Creating a shadow ("convert with_gradient_no_red.png -background black -shadow 30x1+5+5 with_shadow.png")
Image

8) Merging with the previous icon ("convert with_shadow.png with_gradient_no_red.png -composite +repage finalicon_normal.png")
Image

9) Then resizing to the final icon size ("convert finalicon_normal.png -resize 24x24 finalicon_resized.png")
Image

As you can see, in step 4 (and probably also step 6), I lose a lot of the "anti-alias" detail. I'm pretty sure if the steps that make colors transparent could be improved, then it would all be okay. But I've googled a lot and so far found no answer, so I really hope someone here can help.

Thanks in advance! :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Gradient on icon, drop shadow

Post by fmw42 »

Put the alpha channel of the icon into the alpha channel of the gradient and then make the shadow. The background of the icon is totally black. The alpha channel is doing all the work in your icon.

try this --- adjust the shadow as desired.

convert gradient.png \( test2.png -alpha extract \) \
-alpha off -compose copy_opacity -composite -compose over \
\( -clone 0 -background black -shadow 70x1+3+3 \) \
+swap -background none -layers merge test2_new.png


see shadow examples at http://www.imagemagick.org/Usage/blur/#shadow
Avo
Posts: 2
Joined: 2013-11-23T18:08:31-07:00
Authentication code: 6789

Re: Gradient on icon, drop shadow

Post by Avo »

You my dear sir, have just saved my day. :)
Post Reply