12

Using TikZ, the shade option is very convenient to produce color shadings, but it produces only linear color gradients. Is it possible to obtain other transitions, and especially a logarithmic one?

Thanks!

Edit:

Here is an example for the linear case:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}


\begin{tikzpicture}
\shade[left color=black, right color=white] (0,0) rectangle (10,1);
\end{tikzpicture}

\end{document}

linear shading

4
  • You can with \pgfdeclarefunctionalshading. Check the manual or tex.stackexchange.com/questions/54193/… for examples.
    – percusse
    Apr 10, 2015 at 9:54
  • Thank you @percusse, I am pretty sure this is what I need, but the code I have to put into the function is puzzling to me! Any help would be appreciated.
    – Tobard
    Apr 10, 2015 at 10:20
  • 1
    Can you include a MWE and the details about the shading direction etc. ? Then we can look at it together.
    – percusse
    Apr 10, 2015 at 10:30
  • I have added a MWE. I would like something very simple : a horizontal band changing from black to white with a logarithmic progression. Thanks.
    – Tobard
    Apr 10, 2015 at 11:42

1 Answer 1

17

Here are some examples.

  • R=G=B=x (your MWE)
  • R=G=B=x
  • R=G=B=x²
  • R=G=B=√x
  • R=G=B=log(1+x)
  • R=G=B=log(1+9x)
  • R=G=B= ... well ...

\documentclass{article}
\usepackage[a3paper]{geometry}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[left color=black,right color=white](-10,-1)rectangle(10,1);
\end{tikzpicture}

\pgfdeclarefunctionalshading{LBRW}{\pgfpoint{0bp}{0bp}}{\pgfpoint{100bp}{100bp}}{}{
    pop 50 div .5 sub % u
    dup dup % u u u
}
\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[shading=LBRW](-10,-1)rectangle(10,1);
\end{tikzpicture}

\pgfdeclarefunctionalshading{dup mul LBRW}{\pgfpoint{0bp}{0bp}}{\pgfpoint{100bp}{100bp}}{}{
    pop 50 div .5 sub % u
    dup mul % u²
    dup dup % u² u² u²
}
\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[shading=dup mul LBRW](-10,-1)rectangle(10,1);
\end{tikzpicture}

\pgfdeclarefunctionalshading{sqrt LBRW}{\pgfpoint{0bp}{0bp}}{\pgfpoint{100bp}{100bp}}{}{
    pop 50 div .5 sub % u
    sqrt % √u
    dup dup % √u √u √u
}
\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[shading=sqrt LBRW](-10,-1)rectangle(10,1);
\end{tikzpicture}

\pgfdeclarefunctionalshading{1 add log LBRW}{\pgfpoint{0bp}{0bp}}{\pgfpoint{100bp}{100bp}}{}{
    pop 50 div .5 sub % u
    1 add log % ㏒(1+u)
    dup dup % ㏒(1+u) ㏒(1+u) ㏒(1+u)
}
\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[shading=1 add log LBRW](-10,-1)rectangle(10,1);
\end{tikzpicture}

\pgfdeclarefunctionalshading{9 mul 1 add log LBRW}{\pgfpoint{0bp}{0bp}}{\pgfpoint{100bp}{100bp}}{}{
    pop 50 div .5 sub % u
    9 mul 1 add log % ㏒(1+9u)
    dup dup % ㏒(1+9u) ㏒(1+9u) ㏒(1+9u)
}
\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[shading=9 mul 1 add log LBRW](-10,-1)rectangle(10,1);
\end{tikzpicture}

\pgfdeclarefunctionalshading{logistic}{\pgfpoint{0bp}{0bp}}{\pgfpoint{100bp}{100bp}}{}{
    50 div .5 sub exch % v U
    50 div .5 sub 4 mul exch % 4u v
    dup 1 exch sub % 4u v 1-v
    2 index % 4u v (1-v) 4u
    mul mul % u 4uv(1-v)
    dup 1 exch sub 2 index mul mul
    dup 1 exch sub 2 index mul mul
    dup 1 exch sub 2 index mul mul
    dup 1 exch sub 2 index mul mul
    dup 1 exch sub 2 index mul mul
    exch pop dup dup
}
\begin{tikzpicture}
    \fill(-11,0)|-(0,-2)|-(11,2)|-cycle;
    \shade[shading=logistic](-10,-1)rectangle(10,1);
\end{tikzpicture}

\end{document}

Remarks

  • dup duplicates the topmost element.
  • The dup dup at every very end is actually unnecessary.
  • If you leave only a single number in the stack, PDF-renderer will treat it as the grayscale.
  • If there are three, they are R, G, B respectively.
  • If there are 0 or 2 or 4, 5, 6, ... I do not know.
  • pop discards the topmost element.
  • The pop at every very beginning throws away the y-coordinate, which is useless except the last case.
  • Replacing pop by swap, you can leave the y-coordinate at the bottom alone. But then dup dup becomes necessary.
  • For more information, see TikZ manual IX.110.2.3 General (Functional) Shadings.
  • For ever more information, see PDF manual 8.7.4.5.2 Type 1 (Function-Based) Shadings.
  • See also Annex B (normative) Operators in Type 4 Functions.
4
  • Thank you! Could you just explain what pop mean, and why dup commands are needed?
    – Tobard
    Apr 15, 2015 at 13:22
  • @Tobard I updated.
    – Symbol 1
    Apr 15, 2015 at 15:19
  • @Symbol1, I notice you say that dup dup at the end is unnecessary, but I'm not sure about this. When the shading is created, the colour model needs to be specified (and by default pgf uses RGB). The dup dup is missing in your logistic shading and the resulting PDF gives an error in Adobe Reader for me. Sep 13, 2020 at 11:59
  • @DavidPurton If Adobe reader complains, then it is right/I am wrong. Don't skip dup dup.
    – Symbol 1
    Sep 13, 2020 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .