4

Situation

Let's say I have defined some colors. I'd like to produce a test page that functions as a color-key for developers.

Example

The defined colors colora, colorb, and colorc should be iterated without mentioning them explicitly again (happens automatically). This is efficient programming in that I can have an arbitrary number of colors (perhaps 50 or so) and create a time-saving color key.

\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usepackage{pgffor}

\definecolor{colora}{cmyk}{0,.5,1,.3}
\definecolor{colorb}{cmyk}{.4,.6,.5,.1}
\definecolor{colorc}{cmyk}{.6,.2,.5,.2}

\begin{document}

%\foreach \definedcolor in {defined colors} % pseudo-code
%   \tikz \node [fill=definedcolor, minimum width=2cm, minimum height=1cm,text=white] {name of defined color}; % pseudo-code
\color{colora} COLORA

\color{colorb} COLORB

\color{colorc} COLORC
\end{document}
  • Should any pre-defined named colors (such as blue, red, cyan, etc.) be included or excluded from this loop? What if your color definitions overwrite a pre-defined color name? – Paul Gessler Mar 29 '15 at 19:32
8

One possible way is to intercept the color definition with your own macro.

\documentclass{article}
\usepackage{tikz}


\def\appendcolor#1#2#3{%
\csname pgfutil@ifundefined\endcsname{definedcolorsofar}{\def\definedcolorsofar{#1}}{%
\expandafter\def\expandafter\definedcolorsofar\expandafter{\definedcolorsofar,#1}}%
\definecolor{#1}{#2}{#3}%
}

\appendcolor{colora}{cmyk}{0,.5,1,.3}
\appendcolor{colorb}{cmyk}{.4,.6,.5,.1}
\appendcolor{colorc}{rgb}{1,0,0}
\appendcolor{colord}{rgb}{0,0,1}

\begin{document}
\tikz{
\foreach \x[count=\xi] in \definedcolorsofar 
   \node at (0,\xi)[fill=\x, minimum width=2cm, 
           minimum height=1cm,text=white] {\x}; 
}

\end{document}

enter image description here

  • Could you please explain \csname pgfutil@ifundefined\endcsname? – Jonathan Komar Apr 9 '15 at 13:29
  • 1
    @macmadness86 Normally if a control sequence has @ character in it you have to put the code inside \makeatletter...\makeatother pair (if it is a package LaTeX handles behind the scene). But if you only have a few of those you can place it within \csname...\endcsname and it will handle it automatically such that you don't need to change the catcode of @. \pgfutil@ifdefined is an if syntax and checks whether the macro has been defined already or not. So if not defined I create, if already exists I append there. – percusse Apr 9 '15 at 13:42
  • I know this question is old, but I just had a request for putting the cmyk values explicitly on the page. Is that also possible with a minor adjustment to the code? – Jonathan Komar Aug 14 '15 at 12:42
  • @macmadness86 I think I didn't get exactly the additional requirement can you elaborate a bit? – percusse Aug 14 '15 at 18:13
  • For example, if I wanted not just "colora" to be printed, but also cymk and its values: {cmyk}{0,.5,1,.3} either decimally like that or in percentages. The reason for this is that I am designing a style sheet, where the outputted PDF can be used as a reference for other users who might want to user a color. They can then easily reference the cmyk values. – Jonathan Komar Aug 16 '15 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.