13

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.

15

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

  • 1
    If you move the drop shadow to the first node, instead of only the lower part it looks more correct :) – Atnas Dec 10 '16 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 '16 at 22:36
  • @Atnas, sure, I let you do edit my answer. – ferahfeza Dec 11 '16 at 23:27
  • @Atnas, I have edited my answer according to your solution. Your answer is better. – ferahfeza Dec 12 '16 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. – Guilherme Zanotelli Dec 12 '16 at 9:18
9

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

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.