# Colored lines inside the rectangles of respective colors

I have to draw below figure. But I am not able to put the lines inside the rectangular boxes, of respective colors as written inside the boxes. I also would like to have text, maybe color name here, but without any lines crossing the text(as I try to show it in case of RED and YELLOW). My MWE

\documentclass[border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,3);
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
\draw (0,0) rectangle (3,1);
\draw (2,0) rectangle (5,1);
\draw (2,0) rectangle (3,3);
\end{tikzpicture}
\end{document}


Easy with the patterns library (section 60 of the pgfmanual).

\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
\draw[pattern=north east lines,pattern color=red] (0,0) rectangle (1,3);
\draw[pattern=north west lines,pattern color=blue] (0,0) rectangle (3,1);
\draw[pattern=vertical lines] (2,0) rectangle (5,1);
\draw[pattern=horizontal lines,pattern color=yellow] (2,0) rectangle (3,3);
\end{tikzpicture}
\end{document}


EDIT: With annotations. You only need to modify the mynode style to change the behavior. I also made the boundaries have the same color as the lines.

\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\tikzset{mynode/.style args={#1 | #2}{midway,%
node contents={\contour{white}{\textcolor{#1}{#2}}},%
font=\small,inner sep=0pt}}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
\draw[pattern=north east lines,pattern color=red,draw=red] (0,0) rectangle (1,3)
node[mynode=red | hello];
\draw[pattern=north west lines,pattern color=blue,draw=blue] (0,0) rectangle (3,1)
node[mynode=blue | ducks];
\draw[pattern=vertical lines] (2,0) rectangle (5,1)
node[mynode=black | hello];
\draw[pattern=horizontal lines,pattern color=yellow,draw=yellow] (2,0) rectangle (3,3)
node[mynode=yellow | world];
\end{tikzpicture}
\end{document}


• Great one! I also would like to have text, maybe color name here, but without any lines crossing the text. Can it be done? – Shamina Jul 17 '18 at 11:43
• @Shamina Sure. Text color black or the same color as the lines? – marmot Jul 17 '18 at 11:50
• @Shamina Done. If you change your mind, you only need to adjust the mynode style. – marmot Jul 17 '18 at 12:00
• I added another version which takes two arguments. – marmot Jul 17 '18 at 12:19
• @Shamina That's easy, just add draw=red, say. I updated my answer accordingly. – marmot Jul 17 '18 at 13:27