# Cycle or specify implicant colors in karnaugh-map

I'm drawing Karnaugh maps using Mattias Jacobsson's excellent karnaugh-map package.

Each implicant is drawn in a different color -- unless there are more than 6. After that, the colors all get stuck at cyan. In some cases, this can obscure the implicants, as they get drawn over each other.

Is there any way to specify the color for an implicant? Or to kick the package to start the color cycling over again?

MWE follows:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{karnaugh-map}

\begin{document}

\begin{karnaugh-map}[4][4][4][EF][CD][AB]
\minterms{0,2,3,5,9,13,14,18,21,22,24,25,26,29,40,41,43,45,48,49,51,53,56,58,59,61}
\terms{16,37,42,50,57}{X}
\autoterms[0]
\implicant{0}{0}[0,1]
\implicant{3}{2}[0]
\implicant{5}{13}[0,1,2,3]
\implicant{13}{9}[0,1,2,3]
\implicant{14}{14}[0]
\implicant{2}{6}[1]
\implicantedge{8}{8}{10}{10}[1,3]
\implicant{8}{10}[2,3]
\implicant{0}{2}[3]
\end{karnaugh-map}

\end{document}


Notice, especially, the color problems on the lower right row.

The command in karnaugh-map responsible for the colors looks as follows:

\newcommand{\@karnaughmap@func@decimaltocolor@}[1]{%
\ifnum#1=0 red\fi
\ifnum#1=1 green\fi
\ifnum#1=2 yellow\fi
\ifnum#1=3 cyan\fi
\ifnum#1=4 blue\fi
\ifnum#1=5 magenta\fi
\ifnum#1>5 cyan\fi
}


To extend the colors, add the following lines after loading the package.

\makeatletter
\renewcommand{\@karnaughmap@func@decimaltocolor@}[1]{%
\ifnum#1=0 red\fi
\ifnum#1=1 green\fi
\ifnum#1=2 yellow\fi
\ifnum#1=3 cyan\fi
\ifnum#1=4 blue\fi
\ifnum#1=5 magenta\fi
\ifnum#1=6 brown\fi
\ifnum#1=7 lime\fi
\ifnum#1=8 orange\fi
\ifnum#1>8 cyan\fi
}
\makeatother


To cycle through the colors, redefine the command as follows:

\makeatletter
\renewcommand{\@karnaughmap@func@decimaltocolor@}[1]{%
\ifnum#1=0 red\fi
\ifnum#1=1 green\fi
\ifnum#1=2 yellow\fi
\ifnum#1=3 cyan\fi
\ifnum#1=4 blue\fi
\ifnum#1=5 magenta\fi
\ifnum#1>5 \expandafter\@karnaughmap@func@decimaltocolor@\expandafter{\numexpr#1-6}\fi
}
\makeatother


You can also combine the two approaches, i.e., extend the colors and cycle. Just make sure to get the numbers right:

• The number after \ifnum#1> must be the same as in the equality check preceding it.
• The number after \numexpr#1- must be larger by one than the number after \ifnum#1>.