3

Consider the code

\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage{transparent}
\begin{document}

\begin{figure}[!htb] \centering \includegraphics[width=20em,height=30em]{example-image}\llap{\texttransparent{0.5}{\color{blue!90}\rule{20em}{30em}}} \end{figure} \vspace*{-45pt} \hskip 150pt\includegraphics[scale=4.5]{Blue_Image.png} \end{document}

which produces

enter image description here

Notice the lower blue rectangle is slightly darker than the upper blue image.

I would like them to be the same; and it seems that I may accomplish this by slightly lightening (if possible), the lower includegraphics png image.

I am under the impression that I cannot change the color of an includegraphics image in Latex, but it seems reasonable that one might be able to simply lighten or darken it. Perhaps I am wrong.

QUESTION: Is there a way to lighten or darken a png image via the includegraphics command; and if so, how may one do this?

Thank you.

DDS
  • 8,816
  • 4
  • 9
  • 36
  • Bit ugly, but couldn't you do what you've done with example image but with transparent white rectangle? – David Purton Dec 30 '22 at 04:20
  • @DavidPurton The problem is when I use an online snippit tool---the color does not always transform exactly. So, if I want to match such a cut-out to an includegraphics image in Latex, it would be useful to be able to lighten or shade the imported image. – DDS Dec 30 '22 at 04:34
  • Hmm. I confess I'm not 100% sure exactly what you mean. Do you just mean an interface question? i.e., something like \includegraphics[lighten=0.5]{} should lighten up each pixel by 50%? (which would be equivalent to putting a 50% transparent white rectangle over the top of the image or perhaps doing a screen blend with a grey rectangle.) – David Purton Dec 30 '22 at 05:15
  • https://tex.stackexchange.com/q/252256/87678 – David Purton Dec 30 '22 at 05:20
  • @DavidPurton Yes---\includegraphics[lighten=0.5]{} but lighten=0.5 seems not to work for me. – DDS Dec 30 '22 at 05:23
  • @DavidPurton I saw the link you provided earlier, but wasn't sure if it applies here for it indicates "Beamer" – DDS Dec 30 '22 at 05:25
  • I just made up the lighten thing. It's not a feature – David Purton Dec 30 '22 at 05:25
  • More promising: https://tex.stackexchange.com/a/150219/87678 – David Purton Dec 30 '22 at 05:25
  • @DavidPurton I've seen that one; because of Martin's Sharer's comment, ``No, \includegraphics can't change the image color.'' I indicated earlier that I am under the impression that color cannot be changed with includegraphics. But again, I am not trying to "change" the color---just lighten (or darken it) if possible. – DDS Dec 30 '22 at 05:27
  • What about Bruno's decodearray option? Did you try that? – David Purton Dec 30 '22 at 05:35
  • @DavidPurton I don't know what that is. – DDS Dec 30 '22 at 05:37
  • In the most recent answer I linked to. – David Purton Dec 30 '22 at 05:39

1 Answers1

9

I think using the decodearray option to \includegraphics can suit your purposes. Based on this answer.

For RGB images, decodearray takes 3 pairs of numbers between 0 and 1. The three pairs adjust the red, green and blue channels respectively. In your case you can make each of the three pairs the same since you just want to lighten (or darken) each image rather than actually change colours.

The pairs of numbers map channel levels (which run from 0 to 1) linearly on to the new scale you specify in the two numbers.

So to lighten by 50% you would use decodearray={0.5 1 0.5 1 0.5 1}. In other words, for each channel (0 … 1) → (0.5 … 1).

Caveat: This seems to work with pdftex and xetex, but not luatex (bug?).

MWE

You need the image (blue_square.png):

blue_square

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{blue_square}
\includegraphics[decodearray={0.2 1 0.2 1 0.2 1}]{blue_square}
\includegraphics[decodearray={0.4 1 0.4 1 0.4 1}]{blue_square}
\includegraphics[decodearray={0.6 1 0.6 1 0.6 1}]{blue_square}
\includegraphics[decodearray={0.8 1 0.8 1 0.8 1}]{blue_square}
\includegraphics[decodearray={1 1 1 1 1 1}]{blue_square}

\bigskip

\includegraphics{blue_square} \includegraphics[decodearray={0 0.8 0 0.8 0 0.8}]{blue_square} \includegraphics[decodearray={0 0.6 0 0.6 0 0.6}]{blue_square} \includegraphics[decodearray={0 0.4 0 0.4 0 0.4}]{blue_square} \includegraphics[decodearray={0 0.2 0 0.2 0 0.2}]{blue_square} \includegraphics[decodearray={0 0 0 0 0 0}]{blue_square} \end{document}

output

David Purton
  • 25,884