3

I would like to draw an orange box with green diagonal stripes in it, where the whole thing should be a bit transparent. I used this as a baseline for creating the pattern. Now, I'm wondering, why the green stripes do not get transparent if I write opacity=0.35 as in the last line of the tikzpicture-block. I already tried to define my own transparent green color like this:

\definecolor{my_green_opac}{RGBA}{181,230,29,0.35};

But then I get an error. I already had a look at the xcolor package, but it seems like there is no RGBA definition. Is there another way to get the stripes as transparent as the orange box?

My current result

\documentclass{scrbook}

\usepackage{tikz}
\usepackage{tkz-graph}
%\usepackage{tikz,fullpage}
\usetikzlibrary{arrows,arrows.meta, matrix, shapes, calc, fit, decorations.pathmorphing, quotes, shadows,intersections, patterns}
%\usepackage{tkz-berge}
\usepackage{mwe,tikz}\usepackage[percent]{overpic}



% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}

% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}


% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north west lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
    \pgfusepath{stroke}
   }




%---------------------------------------------------------
\def\cHeight{0.5}
\def\cWidth{4}
\newcommand{\cigarOne}[2]{
    \pgfmathtruncatemacro\result{\cWidth/2}
    \fill[draw=orange,pattern=north east lines,pattern color=black, pattern width= 4pt, opacity=0.35] (#1,#2) rectangle (\result+#1,#2-\cHeight);
    % \filldraw[pattern=crosshatch dots] (\result+#1,#2) rectangle (#1+\cWidth,#2-\cHeight);
}
%---------------------------------------------------------
\begin{document}
\begin{tikzpicture}
    \definecolor{my_orange}{RGB}{243, 171, 0};  
    \definecolor{my_green}{RGB}{181,230,29};

    \draw[preaction={fill=my_orange, opacity=0.35},pattern=custom north west lines,hatchspread=10pt,hatchcolor=my_green, hatchthickness=3pt, opacity=0.35] (0,0) rectangle +(3,4);
\end{tikzpicture}
\end{document}  

1 Answer 1

1

Welcome! I think your pattern is transparent. The problem is that you add the green lines on top of the orange background. So you want to add two patterns instead.

Please notice that your pattern can be obtained much more conveniently with the patterns.meta library. The only problem is that at this point we need to engineer the shift in a peculiar way to have an alternating pattern. However, in future versions of pgf the pattern transformations will become more intuitive.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns.meta}
\begin{document}
\begin{tikzpicture}
    \definecolor{my_orange}{RGB}{243, 171, 0};  
    \definecolor{my_green}{RGB}{181,230,29};
    \draw[blue,very thick] (0,0) -- (7,4);
    \draw[preaction={fill=my_orange, opacity=0.35},
    pattern={Lines[angle=-45,distance={sqrt(0.5)*10pt},
       line width=3pt]},pattern color=my_green,opacity=0.35]
    (0,0) rectangle +(3,4);
    \draw[
        preaction={pattern={Lines[angle=-45,distance={sqrt(0.5)*10pt},
       line width={sqrt(0.5)*10pt-3pt},yshift={1.5*(sqrt(0.5)*10pt)}]},pattern color=my_orange, opacity=0.35},
    pattern={Lines[angle=-45,distance={sqrt(0.5)*10pt},
       line width=3pt]},pattern color=my_green,
       opacity=0.35]
    (4,0) rectangle +(3,4);
\end{tikzpicture}
\end{document}

enter image description here

You could also just define a pattern for alternating stripes. Here is a proposal for that. In addition to the keys supported by Lines, which you can find on p. 733 of pgfmanual v3.1.5, the pattern Alternating Lines has the keys

  • color 1, which sets the first line color,
  • color 2, which sets the second line color, and
  • fraction, which controls the relative widths of the two lines.

If fraction is larger than 0.5, the stripes of the first color will be wider. The combined width of both stripes is determined by distance.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns.meta}
\pgfdeclarepattern{
  name=Alternating Lines,
  type=colored,
  parameters={
      \pgfkeysvalueof{/pgf/pattern keys/distance},
      \pgfkeysvalueof{/pgf/pattern keys/angle},
      \pgfkeysvalueof{/pgf/pattern keys/xshift},
      \pgfkeysvalueof{/pgf/pattern keys/yshift},
      \pgfkeysvalueof{/pgf/pattern keys/line width},
      \pgfkeysvalueof{/pgf/pattern keys/color 1},
      \pgfkeysvalueof{/pgf/pattern keys/color 2},
      \pgfkeysvalueof{/pgf/pattern keys/fraction},
  },
  bottom left={%
    \pgfpoint
      {-.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}%
      {-.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}},
  top right={%
    \pgfpoint
      {.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}%
      {.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}},
  tile size={%
    \pgfpoint
      {\pgfkeysvalueof{/pgf/pattern keys/distance}}%
      {\pgfkeysvalueof{/pgf/pattern keys/distance}}},
  tile transformation={%
    \pgftransformshift{%
      \pgfpoint
        {\pgfkeysvalueof{/pgf/pattern keys/xshift}}%
        {\pgfkeysvalueof{/pgf/pattern keys/yshift}}}%
    \pgftransformrotate{\pgfkeysvalueof{/pgf/pattern keys/angle}}%
        },
  defaults={
    distance/.initial=3pt,
    angle/.initial=0,
    xshift/.initial=0pt,
    yshift/.initial=0pt,
    line width/.initial=\the\pgflinewidth,
    fraction/.initial=0.5,
    color 1/.initial=black,
    color 2/.initial=white,
  },
  code={%
    \pgfsetlinewidth{\pgfkeysvalueof{/pgf/pattern keys/fraction}*\pgfkeysvalueof{/pgf/pattern keys/distance}}%
    \pgfsetstrokecolor{\pgfkeysvalueof{/pgf/pattern keys/color 1}}%
    \pgfpathmoveto{\pgfpoint{-.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}{0pt}}%
    \pgfpathlineto{\pgfpoint{.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}{0pt}}%
    \pgfusepath{stroke}%
    \pgfsetlinewidth{(1-\pgfkeysvalueof{/pgf/pattern keys/fraction})*\pgfkeysvalueof{/pgf/pattern keys/distance}}%
    \pgfsetstrokecolor{\pgfkeysvalueof{/pgf/pattern keys/color 2}}%
    \pgfpathmoveto{\pgfpoint{-.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}{.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}}%
    \pgfpathlineto{\pgfpoint{.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}{.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})}}%
    \pgfusepath{stroke}%
  },
}

\begin{document}
\begin{tikzpicture}
 \definecolor{my_orange}{RGB}{243, 171, 0};  
 \definecolor{my_green}{RGB}{181,230,29};
 \path[pattern={Alternating Lines[angle=-45,distance={sqrt(0.5)*10pt},
    color 1=my_orange,color 2=my_green,fraction=0.7]},opacity=0.5]
 (0,0) rectangle +(3,4);
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thank you very much. I probably never would have thought that the green overlay on the orange background could be the reason. But now, it totally makes sense. However, it is also about my editor: Texmaker. Because your solution doesn't look transparent in Texmaker, but it does in the final pdf file. But I think this is a different problem :) May 30, 2020 at 22:24
  • For me it works after replacement {0pt} by {-0.25*(\pgfkeysvalueof{/pgf/pattern keys/distance})} and {.5*(\pgfkeysvalueof{/pgf/pattern keys/distance})} by {0.25*(\pgfkeysvalueof{/pgf/pattern keys/distance})}.
    – forrest
    Jan 31, 2021 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .