Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to have a colored line that starts with transparent 100 and ends with trasparent, let's say, 30.

I tried this code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.markings}


\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start shade/.store in = \startshade,
         end shade/.store in   = \endshade,
}

\pgfdeclaredecoration{shade change}{initial}{
\state{initial}[
width                     = \pgflinewidth,
next state                = line,
persistent precomputation = {\pgfmathdivide{(\startshade-\endshade)/100}{\pgfdecoratedpathlength}%
                             \let\@increment=\pgfmathresult
                             \pgfmathsetmacro\@pitch{\pgfdecoratedpathlength/100}
                             \def\@@n{0}
                             \def\@sh{\startshade}}
]{}

\state{line}[
width                      =  \pgflinewidth,
persistent postcomputation = {\pgfmathsetmacro\@@n{\@@n+1}
                              \pgfmathsetmacro\@sh{\startshade/100-\@increment*\@@n}}]
{
\pgfsetstrokeopacity{\@sh}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{\@pitch pt}{0pt}}
\pgfusepath{stroke}}

\state{final}{
\pgfpathmoveto{\pgfpointorigin}%
\pgfsetstrokeopacity{\@sh}
 \pgfusepath{stroke}%
}
}

\makeatother


\begin{document}
\centering
\begin{tikzpicture}
\draw[
line width = 2pt,
decoration = {shade change,
              start shade = 100,
              end shade   = 30},
decorate] (0,0)--++(3,3)node[midway,above]{$I$};

\end{tikzpicture}
\end{document}

but the result is the following: enter image description here

Where do I do wrong?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Something like that :

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.markings}


\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start shade/.store in = \startshade,
         end shade/.store in   = \endshade,
}

\pgfdeclaredecoration{shade change}{initial}{
\state{initial}[
width                     = \pgflinewidth,
next state                = line,
persistent precomputation = {\pgfmathdivide{(\startshade-\endshade)/100}%
 {\pgfdecoratedpathlength}%
                             \let\@increment=\pgfmathresult
 \pgfmathsetmacro\@pitch{\pgfdecoratedpathlength/100}
                             \def\@@n{0}
                             \def\x{\@@n}
 \def\@sh{\startshade}}
]{}

\state{line}[
width                      =  \pgflinewidth,
persistent postcomputation = {\pgfmathsetmacro\@@n{\@@n+1}
                     \pgfmathsetmacro\@sh{\startshade/100-\@increment*\@@n}}]
{%
\pgfsetstrokecolor{black!\@@n!white}%  
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{\@pitch pt}{0pt}}
\pgfusepath{stroke}}

\state{final}{
\pgfpathmoveto{\pgfpointorigin}%
\pgfsetstrokecolor{black!\@@n!white}%  
 \pgfusepath{stroke}%
}
}

\makeatother


\begin{document}
\centering
\begin{tikzpicture}
\draw[
line width = 2pt,
decoration = {shade change,
              start shade = 100,
              end shade   = 30},
decorate] (0,0)--++(3,3)node[midway,above]{$I$};

\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.