Drawing Grids with different colors

\definegrid[rows,cols] defines \grows and \gcols and proceeds to define all the cells of the grid \grid[i,j] where i and j are row and column indices as \wh (white). Note that \grid[i,j] is defined with \csname since i and j are catcode 12.

Then, \setgrid[i,j]{color} is used to redefine the cell \grid[i,j] to the desired color.

Finally, \drawgrid regurgitates the \grid[i,j] cells in the form of a stack.

Each cell is an \fbox, by the way, dimensions can be set as needed.

Revised for standalone:

\documentclass{standalone}
\usepackage{xcolor,stackengine,pgffor}
\def\block#1{\kern-\fboxrule\fboxsep=0pt\fbox{\color{#1}\rule{1ex}{1ex}}}
\def\wh{\block{white}}
\setstackgap{S}{-\fboxrule}
\setstackEOL{\\}

\makeatletter
\def\definegrid[#1,#2]{%
  \def\grows{#1}%
  \def\gcols{#2}%
  \foreach\i in {1,...,#1}%
  {%
    \foreach\j in {1,...,#2}%
    {%
      \expandafter\gdef\csname grid[\i,\j]\endcsname{\wh}%
    }%
  }%
\ignorespaces}

\newcommand\drawgrid{%
  \def\stackbuild{}%
  \foreach\i in {1,...,\grows}%
  {%
    \ifnum\i=1\else\g@addto@macro\stackbuild{\\}\fi%
    \foreach\j in {1,...,\gcols}%
    {%
      \expandafter\g@addto@macro\expandafter\stackbuild\expandafter{%
        \csname grid[\i,\j]\endcsname}%
    }%
  }%
  \kern\fboxrule%
  \expandafter\Shortstack\expandafter{\stackbuild}%
}

\def\setgrid[#1,#2]#3{\expandafter\gdef\csname grid[#1,#2]\endcsname{\block{#3}}%
  \ignorespaces}
\makeatother
\begin{document}
\definegrid[4,4]
%
\setgrid[1,1]{blue}
\setgrid[3,2]{red}
\setgrid[4,4]{green}
%
\drawgrid
\end{document} 

enter image description here


Here is a version in which \definegrid[rows,cols]{default-color} takes a mandatory trailing argument that sets the default color of the grid.

\documentclass{standalone}
\usepackage{xcolor,stackengine,pgffor}
\def\block#1{\kern-\fboxrule\fboxsep=0pt\fbox{\color{#1}\rule{1ex}{1ex}}}
\setstackgap{S}{-\fboxrule}
\setstackEOL{\\}

\makeatletter
\def\definegrid[#1,#2]#3{%
  \def\grows{#1}%
  \def\gcols{#2}%
  \foreach\i in {1,...,#1}%
  {%
    \foreach\j in {1,...,#2}%
    {%
      \expandafter\gdef\csname grid[\i,\j]\endcsname{\block{#3}}%
    }%
  }%
\ignorespaces}

\newcommand\drawgrid{%
  \def\stackbuild{}%
  \foreach\i in {1,...,\grows}%
  {%
    \ifnum\i=1\else\g@addto@macro\stackbuild{\\}\fi%
    \foreach\j in {1,...,\gcols}%
    {%
      \expandafter\g@addto@macro\expandafter\stackbuild\expandafter{%
        \csname grid[\i,\j]\endcsname}%
    }%
  }%
  \kern\fboxrule%
  \expandafter\Shortstack\expandafter{\stackbuild}%
}

\def\setgrid[#1,#2]#3{\expandafter\gdef\csname grid[#1,#2]\endcsname{\block{#3}}%
  \ignorespaces}
\makeatother
\begin{document}
\definegrid[4,4]{yellow}
%
\setgrid[1,1]{blue}
\setgrid[3,2]{red}
\setgrid[4,4]{green}
%
\drawgrid
\end{document} 

enter image description here


This is based on the first edit. Note: I assigned the name name to the grid so that you can use coordinates (name.south east) etc.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\GridSize}{4}

\newcommand*{\ColorCells}[1]{% #1 = list of x/y/color
  \foreach \x/\y/\color in {#1} {
    \node [fill=\color, draw=none, thick, minimum size=1cm] 
      at (\x-.5,\GridSize+0.5-\y) {};
    }%
}%

%%%%%


%\listfiles
\begin{document}
\begin{tikzpicture}[scale=1]

    \begin{scope}[thick,local bounding box=name]
        \ColorCells{1/1/blue, 2/3/red, 3/2/green, 4/4/yellow}
        \draw (0, 0) grid (\GridSize, \GridSize);
    \end{scope}


\end{tikzpicture}
\end{document}

demo

Tags:

Tikz Pgf