5

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?

9

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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