# Is there another better way to have a region with gradient opacity?

My objective is to get a region with gradient opacity. In the following MWE, the region is a quarter of a disk on the top a bird. I made the gradient opacity by partitioning the sector into 45 infinitesimal sub-sectors.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido,pst-fun}
\SpecialCoor
\begin{document}
\begin{pspicture}(5,5)
\psParrot{.75}
\psset{linestyle=none,linewidth=0,fillstyle=solid,fillcolor=green}
\multido{\i=0+2,\r=.00000+.01111}{45}{\pswedge[opacity=\r](0,0){5}{\i}{!\i\space 2 add}}
\end{pspicture}
\end{document}


My question: Is there another better way to get the same effect? For example without partitioning the region into several infinitesimal sub-regions? Any solution with PSTricks (preferred), Asymptote, TikZ, Metapost are welcome.

• @Donut E. Knot: Hmm... it looks that in the MWE the opacity value of the last sector is 0.01111*44=0.48884. Nov 23, 2013 at 5:42

## 2 Answers

I would suggest using pst-slpe:

\documentclass{article}
\usepackage{pstricks}% http://tug.org/PSTricks/main.cgi/
\usepackage{graphicx,pst-slpe}% http://ctan.org/pkg/{graphicx,pst-slpe}
\newsavebox{\imagebox}
\begin{document}

\savebox{\imagebox}{\includegraphics[width=4cm]{tiger}}

\begin{pspicture}(\wd\imagebox,\ht\imagebox)
\psclip{
\usebox{\imagebox}
}
\psframe[linestyle=none,%
fillstyle=slope,slopebegin=green,slopeend=green,slopeangle=-45,
fading,startfading=0,endfading=1](0,0)(\wd\imagebox,\ht\imagebox)
\endpsclip
\end{pspicture}
\end{document}


The fillstyle can be adjusted to a variety of other options, all specified in the pst-slpe documentation.

Perhaps more true to your image:

\documentclass{article}
\usepackage{pstricks}% http://tug.org/PSTricks/main.cgi/
\usepackage{graphicx,pst-slpe}% http://ctan.org/pkg/{graphicx,pst-slpe}
\makeatletter
\def\psfs@customfill{%
\addto@pscode{%
\psx@slopecolors\space
\psslopesteps\psx@slopecenter\space\psx@sloperadius\space\psx@slopeangle
\ifPST@fading \psk@startfading \psk@endfading true \else false \fi
tx@PstSlopeDict begin CustomFill end}}
\makeatother
\pstVerb{
/CustomFill {
/Fading ED        % do we have fading?
Fading {
/FadingEnd ED % the last opacity value
dup /FadingStart ED % the first opacity value
/Opacity ED % the opacity start value
} if
gsave
rotate
/Radius ED
/CenterY ED
/CenterX ED
/NumSteps ED
Fading { /dOpacity FadingEnd FadingStart sub NumSteps div def } if
clip
pathbbox
/h ED /w ED
2 copy translate
h sub neg /h ED
w sub neg /w ED
w CenterX mul h CenterY mul translate
PatchRadius
/AngleIncrement 90 NumSteps div def %/AngleIncrement 360 NumSteps div neg def
/dY AngleIncrement sin AngleIncrement cos div Radius mul def
/DrawStep {
Fading {            % do we have a fading?
Opacity .setopacityalpha  % set opacity value
Opacity++         % increase opacity
} if
0 0 moveto
Radius 0 rlineto
0 dY rlineto
closepath fill
AngleIncrement rotate
} bind def
Iterate
grestore
} def
}
\newsavebox{\imagebox}
\begin{document}

\savebox{\imagebox}{\includegraphics[width=4cm]{tiger}}

\begin{pspicture}(\wd\imagebox,\ht\imagebox)
\psclip{
\usebox{\imagebox}
}
\psframe[linestyle=none,%
fillstyle=customfill,slopecolors={0 0 1 0 1 0 1 0 2},slopecenter=0 0,
fading,startfading=0,endfading=1](0,0)(\wd\imagebox,\ht\imagebox)
\endpsclip
\end{pspicture}
\end{document}


The above uses an updated version of the radslopes fill style and it's accompanying PostScript definition. The only thing that has changed is the cycle/angle, which now spans 90 degrees clockwise, rather than 360 anti-clockwise. Additionally, the slope center is specified in the lower left-hand corner using slopecenter=0 0.

The following code snippet uses the above customfill to make a radar-effect:

\multido{\i=0+5}{72}{
\newpage
\begin{pspicture}(\wd\imagebox,\ht\imagebox)
\psclip{
\usebox{\imagebox}
}
\rput{\i}(.5\wd\imagebox,.5\ht\imagebox){\pscircle[linestyle=none,%
fillstyle=customfill,slopecolors={0 0 1 0 1 0 1 0 2},%slopecenter=0 0,
fading,startfading=0,endfading=1](0,0){.5\wd\imagebox}}
\endpsclip
\end{pspicture}
}

• It should be like this. The center of shading should always be at the origin rather than the bottom left corner. Nov 23, 2013 at 14:09
• @DonutE.Knot: I've added something to that effect...
– Werner
Nov 23, 2013 at 17:49

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{fadings}
\pgfdeclarefunctionalshading{angular}
{\pgfpointorigin}{\pgfpoint{50bp}{50bp}}{}
{exch atan 90 div dup dup}
\pgfdeclarefading{angular}{\pgfuseshading{angular}}
\begin{document}
\begin{tikzpicture}
\fill[green!70!blue,path fading=angular]
(0,0) -- (right:2) arc[radius=2, start angle=0, delta angle=90] -- cycle;
\end{tikzpicture}
\end{document}


## Output

• For any sector of various angles your code produces different center of shading. Nov 23, 2013 at 13:40