Skip to content

adityam/context-shadow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

The shadow module

This module draws drop shadows using ImageMagick.

Using the module

Include the module using

\usemodule[shadow]

Pre-defined shadows

The module provides 9 pre-defined shadows and an interface to define new shadows. Each shadow consists of two shadowlayers: an umbra (dark part of the shadow) and a penumbra (light part of the shadow).

  • Three umbras are pre-defined: soft, hard, and flat
  • Three penumbras are pre-defined: light, medium, dark

The pre-defined shadows consist of all combinations of these, so

  • soft:light, soft:medium, soft:dark
  • hard:light, hard:medium, hard:dark
  • flat:light, flat:medium, flat:dark

Behind the scenes, the module calls ImageMagick to generate the shadow as a PNG. Once the shadow is generated, it is cached and regenerated only when some parameter changes. You can force shadows to be regenerated by setting

\setupexternalshadow[force=yes]

The default value of force is no.

The generated PNGs are stored in the current directory. To store them in a different directory, say shadow-cache, set

\setupexternalshadow[directory=shadow-cache]

Using shadows in frames

A shadow can be used anywhere an overlay is accepted (so any command which has a background key). The most common use case is a frame. To use a shadow behind a frame, use

\framed[background=soft:light, backgroundcolor=white]{...}

The backgroundcolor=white is needed as we rely on ConTeXt to fill the background color.

The shadow adapts to the corner of the frame; for instance, if we set

\framed[background=soft:light, backgroundcolor=white,
        corner=round, radius=3mm]{...}

then, we will get a shadow which has the same shape as the frame (so rounded corners with a radius of 3mm).

Using shadows in MetaPost

The module defines a macro externalshadow[...] to draw a shadow behind closed paths in MetaPost. For example,

\startMPcode
    newpath p ;
    p := (fullcircle scaled 3cm) randomized 3bp;
    draw externalshadow [
      path = p,
      preset = "soft:light",
      fillcolor = "white",
    ];
\stopMPcode

If the fillcolor is omitted, then the path is not filled.

Currently, open paths are not supported, but it is possible to get the same effect by obtaining the envelope of the path using envelope and treating that as a closed path. For example:

\startMPcode
    newpath p ;
    p := (0,0) -- (1cm, 1cm) -- (0, 1cm) -- (1cm, 0) ;

    draw externalshadow [
      path = envelope (makepen fullcircle) scaled 1bp of p,
      preset = soft:light, 
      fillcolor = "black",
    ];
\stopMPcode

Two things are worth noting. First, we create the envelope of the path using makepen fullcircle rather than pencircle (because envelope pencircle scaled 1bp of p is not implmeneted and gives an error). Second, we set fillcolor to "black"; filling the envelope of a path is effectively the same as drawing it.

Defining new shadows

A shadow consists of two shadowlayers: umbra and penumbra. Both are defined using

\defineshadowlayer[name][spread=, blur=, transparency=]
  • spread is the amount by ...
  • spread controls how far the shadow extends out from the object.
  • blur controls how soft the shadow edges look.
  • transparency is between 0 and 1: 0 is solid shadow, 1 is very light shadow.

The default umbra layers are defined as

\defineshadowlayer [soft] [blur=0.85mm,spread=0mm,transparency=0.40]
\defineshadowlayer [hard] [blur=0.35mm,spread=0mm,transparency=0.40]
\defineshadowlayer [flat] [blur=0mm,   spread=0mm,transparency=0.40]

And the default penumbra layers are defined as

\defineshadowlayer [light]  [blur=1.35mm,spread=0.5mm,transparency=0.70]
\defineshadowlayer [medium] [blur=1.50mm,spread=0.5mm,transparency=0.60]
\defineshadowlayer [dark]   [blur=1.85mm,spread=0.5mm,transparency=0.50]

A shadow is defined by combining an umbra and a penumbra. For example:

\defineexternalshadow[soft:light][umbra=soft, penumbra=light]

If you only want one layer, simply don't specify the other. For instance

\defineexternalshadow[umbraonly][umbra=soft, penumbra=]

Other parameters of the shadow are

\defineexternalshadow
    [
       direction=-45,
       offset=1.7mm,
       shadowcolor=shadowcolor,
    ]

Though these are defined as default values for all shadows, they can be changed for individual shadows well.

  • direction specifies the angle (in degrees) at which the shadow is cast and offset is the distance of the shadow.
  • shadowcolor is the color of the shadow. The default shadow color is a cool dark blue, which gives a softer shadow than pure black.

About

Module for drawing shadows

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages