2

I have chosen a specific color that I am using for my letters. The color is used in headings and titles. I also have a black and white image that I want to use as a background, but I want it to have my chosen color instead of black. Is there a simple way to do this, without tweaking the parameters by hand?

MWE

\documentclass[12pt]{article}

\usepackage[top=0.78in, left=1in, right=1in, bottom=1in]{geometry}  
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{scrpage2}
\usepackage{background}
\usepackage{lipsum}

\colorlet{MyColor1}{RoyalPurple}
\newcommand{\HRule}{\rule{\textwidth}{1.5pt}}
\pagestyle{scrheadings}
\clearscrheadfoot
\chead{{\color{MyColor1} \textsc{\Huge{My Name} \\[-1.5pt] \Large{some tagline}}\\ \Large{\vspace{-8.5pt}} \HRule}}
\footskip = 0pt
\cfoot{\newcommand{\NL}{ $\ast$ }
{\color{MyColor1} \HRule \textsc{\footnotesize{\\ My address} \footnotesize{ \\ my.email@server.com \NL webpage.com \\ +44 123 456 789}}}}

\backgroundsetup{contents={\includegraphics[decodearray={MyColor1}, width=0.1\textwidth]{example-image}}, angle=0, position=current page.south, anchor=above, vshift=10pt}


\begin{document}
{\color{MyColor1}\Huge Title}\\[1cm]
\lipsum[1-5]
\end{document}

Resulting file

3
  • 1
    Option decodearray will not help, because it does not allow to change the color model, i.e., it does not convert gray images to the RGB color model. The PDF specification explains the feature and the syntax of the value for decodearray. – Heiko Oberdiek Mar 29 '18 at 16:52
  • @HeikoOberdiek , Well decodearray={0.38 1 0.25 1 0.6 1} turns the black in my figure into the purple color I want. I'm just looking for an automatic way of doing it. – Kajsa Mar 29 '18 at 17:19
  • 1
    Then, the image is not a "black and white" image with the color model "gray". – Heiko Oberdiek Mar 29 '18 at 17:25
3

You can play with the blend modes offered by TikZ.

\documentclass[12pt]{article}

\usepackage[top=0.78in, left=1in, right=1in, bottom=1in]{geometry}  
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{scrpage2}
\usepackage{background}
\usepackage{lipsum}
\usepackage{tikz}

\colorlet{MyColor1}{RoyalPurple}
\newcommand{\HRule}{\rule{\textwidth}{1.5pt}}
\pagestyle{scrheadings}
\clearscrheadfoot
\chead{{\color{MyColor1} \textsc{\Huge{My Name} \\[-1.5pt] \Large{some tagline}}\\ \Large{\vspace{-8.5pt}} \HRule}}
\footskip = 0pt
\cfoot{\newcommand{\NL}{ $\ast$ }
{\color{MyColor1} \HRule \textsc{\footnotesize{\\ My address} \footnotesize{ \\ my.email@server.com \NL webpage.com \\ +44 123 456 789}}}}

\backgroundsetup{contents={\tikz[blend mode=screen]{\node (img){%
\includegraphics[width=0.1\textwidth]{example-image-a}};
\fill[MyColor1](img.south east) rectangle (img.north west);}},%
 angle=0, position=current page.south, anchor=above, vshift=10pt}


\begin{document}
{\color{MyColor1}\Huge Title}\\[1cm]
\lipsum[1-5]
\end{document}

enter image description here

3
  • @HeikoOberdiek Thanks! I just copied it from the OP's code, not knowing what it does, and you're right I've should have looked it up. – user121799 Mar 29 '18 at 16:53
  • If I am understanding this correctly, this puts a rectangle with my color on top of the other image and blends them together. If so, it will not solve my problem as my image isn't rectangular and it has white parts in the middle. – Kajsa Mar 29 '18 at 17:24
  • @Kajsa You can fill all sorts of shapes with TikZ. I was just working with what you had in your MWE. – user121799 Mar 29 '18 at 17:30
3

Package xcolor provides useful macros for extracting and converting color specifications. The following example uses them to convert the color into the color model rgb. The values are parsed to construct the decodearray.

Requirement:

  • The image must be a bitmap image in the RGB color space.

The image is generated with the following TeX code:

\documentclass{standalone}
\begin{document}
  \setlength{\fboxrule}{1mm}
  \fbox{\sffamily\bfseries ABC}
\end{document}

It is converted via Ghostscript and the device pngalpha to abc.png:

Image, black and white, RGB colors, bitmap

Now the document that includes the image:

\documentclass{article}

\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{xcolor}
\colorlet{MyColor1}{RoyalPurple}

\makeatletter
\newcommand*{\ParseColorToRgbDecodeArray}[2]{%
  \extractcolorspecs{#1}\ParseResultModel\ParseResultColor
  \convertcolorspec\ParseResultModel\ParseResultColor{rgb}\ParseRgb
  \let#2\@empty
  \@for\ParseValue:=\ParseRgb\do{%
    \edef#2{%
      #2%
      \ifx#2\@empty
      \else
        \space
      \fi
      \ParseValue\space 1%
    }%
  }%
}
\makeatother

\ParseColorToRgbDecodeArray{MyColor1}\MyColorOneDecodeArray

\begin{document}
  \includegraphics[decodearray={\MyColorOneDecodeArray}]{abc.png}
\end{document}

Result with image and decodearray

Caveat:

The original color was given as CMYK color. The image uses RGB. Therefore, the colors in the different models can be displayed differently.

5
  • Very helpful, though the color doesn't come out quite right, might be do to the conversion. What should I remove to get the CMYK version of the code? – Kajsa Mar 29 '18 at 19:51
  • Actually, the CMYK code doesn't come out right either. – Kajsa Mar 29 '18 at 20:15
  • @Kajsa PNG does not support CMYK. – Heiko Oberdiek Mar 29 '18 at 21:41
  • I meant the decode array. The numbers for that aren't the correct ones for the color. RoyalPurple has the RGB code {0.38 0.25 0.6}, but the numbers that I get from this are {0.25 0.1 1}, excluding the 1's for the white. – Kajsa Mar 30 '18 at 18:06
  • Fantastic, this is exactly what I was looking for. Had to convert a svg style signature in black to blue and this trick worked like a charm. Thanks again. – theOne Jul 30 '20 at 15:14

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.