18

I'm trying to fill half of my rectangle with a green color. I would like the transition to be along a diagonal of the rectangle.

Here is the code I have currently

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw,thick,text width=1.5cm,minimum height=1.5cm,
        text centered,rounded corners, drop shadow, fill=yellow, name = re] {Test};
\end{tikzpicture}
\end{document}

This (of course) gives me an only yellow rectangle. I would like something like this instead.

Desired output

I have looked at http://www.texample.net/tikz/examples/rectangle-node-with-diagonal-fill/, but I didn't get it to work with the drop shadow (and it seems like a lot of code for a simple task).

I'm considering drawing an invisible triangle under the rectangle with clipping, but I'm not sure how to do that, or if it's the smartest way.

3 Answers 3

22

A short answer with some trick. Rectangle is drawn twice:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw,text width=1.5cm,minimum height=1.5cm,
        text centered,rounded corners,name = re] {};
       \filldraw[yellow!80,drop shadow][] (re.south west)
        [rounded corners=4pt] -- (re.south east)
        [rounded corners=4pt] -- (re.north east)--cycle
        ;
        \filldraw[green!80][] (re.south west)
        [rounded corners=4pt] -- (re.north west)
        [rounded corners=4pt] -- (re.north east)--cycle
        ;
\node [rectangle,draw,thick, text width=1.5cm,minimum height=1.5cm,
        text centered,rounded corners,name = re] {Text};      
\end{tikzpicture}
\end{document}

enter image description here

EDIT: This edit offers by OP. He/she edit my answer and found a new solution.

Now only drawing one rectangle, and diagonal is completely straight.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
\tikzset{
diagonal fill/.style 2 args={fill=#2, path picture={
\fill[#1, sharp corners] (path picture bounding box.south west) -|
                         (path picture bounding box.north east) -- cycle;}},
reversed diagonal fill/.style 2 args={fill=#2, path picture={
\fill[#1, sharp corners] (path picture bounding box.north west) |- 
                         (path picture bounding box.south east) -- cycle;}}
}
% (reversed) diagonal fill={lower color}{upper color}
\begin{document}
\begin{tikzpicture}
\node[diagonal fill={yellow}{green!80},
      text width=1.5cm, minimum height=1.5cm,
      text centered, rounded corners, draw, drop shadow]{Text};
\end{tikzpicture}
\end{document}

enter image description here

5
  • 1
    If you move the drop shadow to the first node, instead of only the lower part it looks more correct :)
    – Atnas
    Dec 10, 2016 at 19:16
  • 1
    I ended up using this in a slightly modified version (to make the diagonal completely straight). I placed the one of the filldraws in a "path picture" under the first node, and simply deleted the second node. Should I post it as a separate answer or edit this one?
    – Atnas
    Dec 11, 2016 at 22:36
  • @Atnas, sure, I let you do edit my answer.
    – user31034
    Dec 11, 2016 at 23:27
  • @Atnas, I have edited my answer according to your solution. Your answer is better.
    – user31034
    Dec 12, 2016 at 6:47
  • 1
    @Atnas, that's much (much) better than what I did, way simpler. I think you should've answered yourself. I just made it into a style so it can be used more easily, but great solution, really. Dec 12, 2016 at 9:18
12

Stumbling upon How to shade text in different colors? I remembered this question and thought it could be done using shading, so stealing the idea from TikZ Fading Speed we can define a sharp shading and fill the node with it. That gives total control over the angle and the node shape.

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{shadows,shadings,shapes.symbols}
\tikzset{
    double color fill/.code 2 args={
        \pgfdeclareverticalshading[%
            tikz@axis@top,tikz@axis@middle,tikz@axis@bottom%
        ]{diagonalfill}{100bp}{%
            color(0bp)=(tikz@axis@bottom);
            color(50bp)=(tikz@axis@bottom);
            color(50bp)=(tikz@axis@middle);
            color(50bp)=(tikz@axis@top);
            color(100bp)=(tikz@axis@top)
        }
        \tikzset{shade, left color=#1, right color=#2, shading=diagonalfill}
    }
}

\begin{document}
\begin{tikzpicture}[my node/.style={draw, cloud, cloud ignores aspect, drop shadow, double color fill={green}{blue}}]
\foreach \angle[count=\i] in {0,15,...,75} \node[my node, shading angle=\angle] (a\i) at (0,\i) {Text};
\foreach \angle[count=\i] in {105,120,...,180} \node[my node, shading angle=\angle] (b\i) at (3,\i) {Text};
\path (a6) -- node[my node, shading angle=90]{Text} (b1);
\end{tikzpicture}
\end{document}

enter image description here

2

Putting a node inside a multi-parameters pic is a way. The options [rounded corners], [save path], [use path], and [clip] are just good to be together.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
% #1 and #2 are colors; 
% #3 is node name (note: no space in node name)
% #4 is text of the node. It can be empty   
\tikzset{pics/colorfulbox/.style args=
{#1|#2|#3|#4}{code={%
\path (0,0) node[minimum size=1cm,rounded corners,save path=\temppath] (#3) {};
\begin{scope}
\clip[use path=\temppath];
\fill[#1] (#3.45)-|(#3.-135)--cycle;
\fill[#2] (#3.45)|-(#3.-135)--cycle;
\end{scope}
\path (0,0) node[draw,minimum size=1cm,rounded corners] (#3) {#4};
}}} 
        
\path
(0,0)  pic{colorfulbox= cyan | magenta |A|}
(3,2)  pic{colorfulbox=violet|yellow|B|}
(-1,3) pic{colorfulbox=brown|orange|C|C}
;
\draw (A)--(B);
\draw[->] (A) -| (B);
\draw[stealth-] (A) to[out=120,in=80] (B); 
\end{tikzpicture}
\end{document}  

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.