9

Consider the following:

\colorlet{mycolour1}{black}

AND

\colorlet{mycolour2}{black}

How do I use a command like

\ifthenelse{\equal{mycolour1}{mycolour2}}{TRUE}{FALSE}

In other words, how can I check equality between two colors?

  • Please accept one of the answers so that your question doesn't remain in the unanswered list. – jub0bs Aug 30 '13 at 06:09

2 Answers2

5

I worked it out.

\makeatletter
   \extractcolorspec{mycolor1}{\@spec@A}
   \extractcolorspec{mycolor2}{\@spec@B}
   \ifthenelse{\equal{\@spec@A}{\@spec@B}}{
      %TRUE
   }{
      %FALSE
   }
\makeatother
4

As Werner points out, etoolbox's e-TeX powerful funtionalities are preferable to what ifthen offers. The \ifdefequal is particularly handy here.

enter image description here

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}

\makeatletter
\newcommand\ifcolorsequalthenelse[4]{%
    \extractcolorspec{#1}{\@spec@A}
    \extractcolorspec{#2}{\@spec@B}
    \ifdefequal{\@spec@A}{\@spec@B}%
    {%
        #3%
    }{%
        #4%
    }
}
\makeatother

\begin{document}

\colorlet{mycolor1}{red}
\definecolor{mycolor2}{rgb}{1 0 0.1}

\noindent
Test 1:\ifcolorsequalthenelse{mycolor1}{red}{equal}{not equal}\\
Test 2:\ifcolorsequalthenelse{mycolor1}{mycolor2}{equal}{not equal}

\end{document}
David Carlisle
  • 757,742
jub0bs
  • 58,916