TikZ: Rectangle with diagonal fill (two colors)

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


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

Tags:

Tikz Pgf