如何使 tikz 图案变得透明?

如何使 tikz 图案变得透明?

我想画一个橙色的盒子,里面有绿色的斜条纹,整个盒子应该有点透明。我用作为创建图案的基线。现在,我想知道,为什么如果我opacity=0.35在 tikzpicture 块的最后一行中写入绿色条纹,它就不会变得透明。我已经尝试像这样定义我自己的透明绿色:

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

但随后我收到错误。我已经查看了 xcolor 包,但似乎没有 RGBA 定义。还有其他方法可以让条纹像橙色框一样透明吗?

我目前的结果

\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

欢迎!我认为您的图案是透明的。问题是您在橙色背景上添加了绿线。所以您想添加两个图案。

请注意,使用该库可以更方便地获取您的模式patterns.meta。唯一的问题是,此时我们需要以特殊的方式设计移位以获得交替模式。然而,在 pgf 的未来版本中,模式转换将变得更加直观

\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}

在此处输入图片描述

Lines您也可以只定义一个交替条纹的图案。这里有一个建议。除了 支持的键(您可以在 pgfmanual v3.1.5 的第 733 页找到)之外,该图案Alternating Lines还具有以下键

  • color 1,设置第一条线的颜色,
  • color 2,设置第二条线的颜色,以及
  • fraction,控制两条线的相对宽度。

如果fraction大于0.5,则第一种颜色的条纹会更宽。两种条纹的总宽度由决定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}

在此处输入图片描述

相关内容