PGF/TikZ Manual | PGFplots | TikZ Editor

TikZ and PGF Manual

The Basic Layer

114 Shadings

114.1 Overview

A shading is an area in which the color changes smoothly between different colors. Similarly to an image, a shading must first be declared before it can be used. Also similarly to an image, a shading is put into a -box. Hence, in order to include a shading in a {pgfpicture}, you have to use \pgftext around it.

There are different kinds of shadings: horizontal, vertical, radial, and functional shadings. However, you can rotate and clip shadings like any other graphics object, which allows you to create more complicated shadings. Horizontal shadings could be created by rotating a vertical shading by 90 degrees, but explicit commands for creating both horizontal and vertical shadings are included for convenience.

Once you have declared a shading, you can insert it into the text using the command \pgfuseshading. This command cannot be used directly in a {pgfpicture}, you have to put a \pgftext around it. The second command for using shadings, \pgfshadepath, on the other hand, can only be used inside {pgfpicture} environments. It will “fill” the current path with the shading.

A horizontal shading is a horizontal bar of a certain height whose color changes smoothly. You must at least specify the colors at the left and at the right end of the bar, but you can also add color specifications for points in between. For example, suppose you wish to create a bar that is red at the left end, green in the middle, and blue at the end, and you would like the bar to be 4cm long. This could be specified as follows:


rgb(0cm)=(1,0,0); rgb(2cm)=(0,1,0); rgb(4cm)=(0,0,1)

This line means that at 0cm (the left end) of the bar, the color should be red, which has red-green-blue (rgb) components (1,0,0). At 2cm, the bar should be green, and at 4cm it should be blue. Instead of rgb, you can currently also specify cmyk as color model, in which case four values are needed, gray as color model, in which case only one value is needed, or color, in which case you must provide the name of a color in parentheses. In a color specification the individual specifications must be separated using a semicolon, which may be followed by a whitespace (like a space or a newline). Individual specifications must be given in increasing order.

114.1.1 Color models

by David Purton

An attempt is made to produce shadings consistent with the currently selected xcolor package color model. The rgb, cmyk, and gray color models from the xcolor package are supported.

Note: The color model chosen for a shading is based on the xcolor color model at the time the shading is created. This is either when \pgfdeclare*shading is called with no optional argument or when \pgfuseshading is called if \pgfdeclare*shading was called with an optional argument.

If the xcolor package natural color model is in use then the shading color model will be rgb by default. In practice this means that if you are using the natural color model of the xcolor package you can get mismatched colors if you, for example, create a shading from green (which is defined as rgb) to magenta (which is defined as cmyk). The shading will finish with rgb magenta which will look different to the cmyk magenta used in solid colors.

You can avoid mismatched colors by loading the xcolor package first with an explicit color model (rgb, cmyk, or gray).


\begin{tikzpicture}
\fill[green] (0,0) rectangle (1,1);
\shade[left color=green, right color=magenta] (1.25,0) rectangle (3.75,1);
\fill[magenta] (4,0) rectangle (5,1);
\end{tikzpicture}

xcolor natural color model:

(-tikz- diagram)

xcolor cmyk color model:

(-tikz- diagram)

xcolor rgb color model:

(-tikz- diagram)

xcolor gray color model:

(-tikz- diagram)

114.2 Declaring Shadings
114.2.1 Horizontal and Vertical Shadings
  • \pgfdeclarehorizontalshading[color list]{shading name}{shading height}{color specification}

  • Declares a horizontal shading named shading name of the specified height with the specified colors. The width of the bar is deduced automatically from the maximum dimension in the specification.


    \pgfdeclarehorizontalshading{myshadingA}
    {1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
    \pgfuseshading{myshadingA}

    The effect of the color list, which is a comma-separated list of colors, is the following: Normally, when this list is empty, once a shading has been declared, it becomes “frozen”. This means that even if you change a color that was used in the declaration of the shading later on, the shading will not change. By specifying a color list you can specify that the shading should be recalculated whenever one of the colors listed in the list changes (this includes effects like color mixins and xcolor color models). Thus, when you specify a color list, whenever the shading is used, pgf first converts the colors in the list to tuples in the current xcolor color model using the current values of the colors and taking any mixins and blends into account. If the resulting tuples have not yet been used, a new shading is internally created and used. Note that if the option color list is used, then no shading is created until the first use of \pgfuseshading. In particular, the colors mentioned in the shading need not be defined when the declaration is given.

    When a shading is recalculated because of a change in the colors mentioned in color list, the complete shading is recalculated. Thus even colors not mentioned in the list will be used with their current values, not with the values they had upon declaration.


    \pgfdeclarehorizontalshading[mycolor]{myshadingB}
    {1cm}{rgb(0cm)=(1,0,0); color(2cm)=(mycolor)}
    \colorlet{mycolor}{green}
    \pgfuseshading{myshadingB}
    \colorlet{mycolor}{blue}
    \pgfuseshading{myshadingB}

114.2.2 Radial Shadings
114.2.3 General (Functional) Shadings

Color model independent functional shadings.

By nature, the PostScript code used in functional shadings must output one of rgb, cmyk, or grayscale data. Therefore, \pgfdeclarefunctionalshading is not portable across color models.

Take particular care that the same color model is in use at declaration time and use time for functional shadings declared with an optional argument as otherwise the PostScript data will not match the declared color space and you will end up with a malformed PDF.

Having said this, it is possible to create portable functional shadings by providing conditional code to append color transformations to the PostScript data. A variety of \pgffuncshading*to* (e.g., \pgffuncshadingrgbtocmyk) macros along with \ifpgfshadingmodel* (e.g., \ifpgfshadingmodelcmyk) conditionals are provided to assist with these transformations. Obviously, this will make the PostScript code less efficient than if you work in your intended color model.

xcolor rgb model:

xcolor cmyk model:

xcolor gray model:


\pgfdeclarefunctionalshading[black]{portabletwospots}{\pgfpointorigin}{\pgfpoint{3.5cm}{3.5cm}}{}{
2 copy
45 sub dup mul exch
40 sub dup mul 0.5 mul add sqrt
dup mul neg 1.0005 exch exp 1.0 exch sub
3 1 roll
70 sub dup mul .5 mul exch
70 sub dup mul add sqrt
dup mul neg 1.002 exch exp 1.0 exch sub
1.0 3 1 roll
\ifpgfshadingmodelcmyk
\pgffuncshadingrgbtocmyk
\fi
\ifpgfshadingmodelgray
\pgffuncshadingrgbtogray
\fi
}
  • \pgffuncshadingrgbtogray

  • Within the type 4 function argument of \pgfdeclarefunctionalshading, this command can be used to convert the top 3 elements on the stack from rgb to grayscale. In combination with the \ifpgfshadingmodelgray conditional this macro can be used to make functional shading declarations more portable across color models.

  • \pgffuncshadingcmyktorgb

  • Within the type 4 function argument of \pgfdeclarefunctionalshading, this command can be used to convert the top 4 elements on the stack from cmyk to rgb. In combination with the \ifpgfshadingmodelrgb conditional this macro can be used to make functional shading declarations more portable across color models.

114.3 Using Shadings
  • \pgfsetadditionalshadetransform{transformation}

  • This command allows you to specify an additional transformation that should be applied to shadings when the \pgfshadepath command is used. The transformation should be transformation code like \pgftransformrotate{20}.