# What's the best way to do overshadow?

I want to have this effect: overshadow an image with a semi-transparent grey/white colored rectangle (say they are the same size and at the same position).

So now the key question is:

Instead of externally creating an empty eps grey/white figure and then do the overlay, is there any better solution??

-
Use inkscape to draw your figure. It is easy to deal with transparencies. –  Sigur Nov 4 '13 at 22:37
This is something like a programming exercise for me, I am writing a beamer slide, I have 7*5 array of images in this slide and I want to create an effect, that each time only one image is highlighted, the others are all overshadowed with a grey but transparent color, so at this slide, I will have one highlighted, and next slide I will have another highlighted, so... I don't want to do it in inkscape. Thanks though –  Daniel Nov 4 '13 at 22:42
Is this helpful stackoverflow.com/a/1837537/1778537 ? –  Sigur Nov 4 '13 at 22:56

I've used the following before (taken from some very old tikz code, and can surely be improved using a node):

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\begin{document}

\noindent
\setbox1=\hbox{\includegraphics[width=\linewidth]{example-image-a}}\usebox1
\vskip-\baselineskip
\smash{%
\tikz{%
\filldraw[blue,semitransparent] (0mm,0mm) -- (0mm,\ht1) -- (\wd1,\ht1) -- (\wd1,0mm) -- cycle;
}%
}

\end{document}


You can set your image, then overlay a \tikz fill that is semitransparent.

The following utilizes the core of Harish's answer, but adds key-value options that can be mixed with that of graphicx's \includegraphics:

\documentclass{article}
\usepackage{xkeyval,graphicx,tikz}% http://ctan.org/pkg/{xkeyval,graphicx,pgf}
\makeatletter
\define@key{tikznode}{opacity}{\def\tikznode@opacity{#1}}
\define@key{tikznode}{fill}{\def\tikznode@fill{#1}}
\newcommand{\customizedIncludeGraphics}[2][,]{%
\begin{tikzpicture}
\setkeys*{Gin,tikznode}{fill = blue!50, opacity = 0.7, #1}% Set defaults
\node [inner sep=0pt, outer sep = 0pt, fill = \tikznode@fill, opacity = \tikznode@opacity]
{\includegraphics{#2}};
\end{tikzpicture}%
}
\makeatother
\begin{document}

\customizedIncludeGraphics[width=50pt]{example-image-a}
\customizedIncludeGraphics[width=100pt, fill=brown]{example-image-a}
\customizedIncludeGraphics[width=123pt, opacity=0.2]{example-image-a}
\customizedIncludeGraphics[height=2\baselineskip, fill=green, opacity=0.9]{example-image-a}

\end{document}


Defaults are fill=blue!50 (50% blue fill) and opacity=0.7 (70% transparent; 0 = fully opaque).

-
Is it possible to wrap these up, so that I could just use something like \customizedIncludeGraphics{example-image-a}? –  Daniel Nov 5 '13 at 1:36
@Daniel: Yes, it's always a good idea. I've updated my answer with that detail, taking some lead from Harish's answer, but adding some key-value processing. –  Werner Nov 5 '13 at 5:44
I think the concept of opacity you said is wrong. opacity=0.7 represents 30% transparent rather than 70% transparent. So opacity=1 represents fully opaque or 0% transparent. What do you think? –  Weirdstress Function Nov 5 '13 at 6:40

Using tikz again.

\documentclass{article}
\usepackage{tikz}
\newcommand{\myimage}[4]{%
\begin{tikzpicture}
\node [inner sep=0pt, outer sep = 0pt,fill = #1,opacity=#2]{\includegraphics[width=#3]{#4}};
\end{tikzpicture}%
}
\begin{document}
%% \myimage{<color>}{<transparency>}{<width>}{<image file name>}
\noindent\myimage{blue!40}{.7}{.4\linewidth}{example-image-a}
\hfill
\myimage{magenta!40}{.7}{.4\linewidth}{example-image-b}

\noindent\myimage{green!40}{.7}{.4\linewidth}{example-image-c}
\hfill
\myimage{blue!40!green!30}{.7}{.4\linewidth}{example-image}

\end{document}


-
See, I don't have much tikz-foo... –  Werner Nov 5 '13 at 0:39
@Werner: You are surely joking Werner ;-) –  Harish Kumar Nov 5 '13 at 0:40

With PSTricks. Please compile it with pdflatex -shell-escape inputfilename.tex.

% this filename is inputfilename.tex
% compile it with pdflatex -shell-escape inputfilename.tex
\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{image.tex}
\documentclass[pstricks]{standalone}
\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics{example-image-a}}

\psset{xunit=\wd\IBox,yunit=\ht\IBox}
\begin{document}
\begin{pspicture}(1,1)
\rput[bl](0,0){\usebox\IBox}
\psframe[fillstyle=solid,fillcolor=orange,opacity=.25](1,1)
\end{pspicture}
\end{document}
\end{filecontents*}

\usepackage{pgffor,graphicx}
\foreach \compiler/\ext in {latex/tex,dvips/dvi,ps2pdf/ps}{\immediate\write18{\compiler\space image.\ext}}

\begin{document}
Is this the image, with a yellow transparent layer, you are looking for?

\section*{Original}
\includegraphics[scale=.5]{example-image-a}

\section*{With a yellow transparent layer}
\includegraphics[scale=.5]{image}
\end{document}


% this filename is inputfilename.tex
% compile it with pdflatex -shell-escape inputfilename.tex
\documentclass{article}
\usepackage[a4paper,hmargin=2cm,vmargin=1cm]{geometry}
\usepackage{filecontents}
\begin{filecontents*}{images.tex}
\documentclass[pstricks,dvipsnames,cmyk]{standalone}
\usepackage{etoolbox}

\def\temp{}

\begingroup
\input{dvipsnam.def}
\endgroup
\def\do#1{#1,}
\edef\ColorList{\dolistloop{\temp}end}

\usepackage{graphicx,bera}
\newsavebox\IBox
\savebox\IBox{\includegraphics{example-image-a}}
\psset{xunit=\wd\IBox,yunit=\ht\IBox}

\begin{document}
\fontsize{40pt}{15pt}\selectfont
\psforeach{\x}{\ColorList}
{
\expandafter\ifstrequal\expandafter{\x}{end}{}
{
\begin{pspicture}(1,1)
\rput[bl](0,0){\usebox\IBox}
\psframe[fillstyle=solid,fillcolor=\x,opacity=.75](1,1)
\rput(0.5,.85){\x}
\end{pspicture}
}
}
\end{document}
\end{filecontents*}

\usepackage{pgffor,graphicx}
\foreach \compiler/\ext in {latex/tex,dvips/dvi,ps2pdf/ps}{\immediate\write18{\compiler\space images.\ext}}

\pdfximage{images.pdf}
\begin{document}
\noindent\foreach \x in {1,...,\the\pdflastximagepages}{\includegraphics[page=\x,scale=.25]{images} }
\end{document}


-
By the way, why is the original bigger than the modified one even though I use the same scaling factor? What is wrong? –  Weirdstress Function Nov 4 '13 at 23:57

For comparison, this is how you can achieve the same effect in ConTeXt

% Search figures in TEXMF tree
\setupexternalfigures
[location={local,global,default}]

\definecolor[transparentblue][b=1,t=.5,a=1]

\starttext

\startcombination[nx=2]
{\externalfigure[mill.png]]}
{Normal figure}
{\externalfigure[mill.png][background={foreground,color}, backgroundcolor=transparentblue]}
\stopcombination


The idea is pretty straight forward. Each externalfigure is a frame, and one of the properties of the frame is that you can set the background. One of the default backgrounds is color, which fills the background with backgroundcolor.

Now what you want can be thought of as a background that is placed over the content, rather than under it. Specifying background={foreground,color} tells ConTeXt to first place the foreground layer (that is the layer containing the content) and then place the color layer on top of it.

This idea works with any ConTeXt environment that accepts a background key: framed, framedtext, TABLE, page backgrounds, etc.

If you want to apply this to multiple images, then you can define

\defineexternalfigure[tint][background={foreground,color}, backgroundcolor=transparentblue]


and use it as follows

\externalfigure[filename][tint]


This will add the overshadow to the figure. If you want, you can override the shadow color using

\externalfigure[filename][tint][backgroundcolor=transparentred]


Here is a complete example showing this.

% Search figures in TEXMF tree
\setupexternalfigures
[location={local,global,default}]

\definecolor[transparentblue][b=1,t=.5,a=1]
\definecolor[transparentred][r=1,t=.5,a=1]

\defineexternalfigure[tint][background={foreground,color}, backgroundcolor=transparentblue]

\starttext

\startcombination[nx=2]
{\externalfigure[mill.png][tint]}
{Default tint}
{\externalfigure[mill.png][tint][backgroundcolor=transparentred]}