7

I need to get the list of predefined colors in xcolor. Hard-coding as follows is not a good coding practice.

\documentclass[dvipsnames,cmyk]{minimal}
\parindent=0pt
\usepackage{pstricks} 

\begin{document}
    \psforeach{\nA}
    {
        Tan, 
        RedOrange, 
        Magenta,
        Yellow,
        Green,
        Red,
        Blue,
        BrickRed,
        DarkOrchid,
        Rhodamine,
        OliveGreen,
        Mahogany,
        Goldenrod%      
    }{{\color{\nA}\nA} \endgraf}
\end{document}

enter image description here


My real scenario is to create an animation as follows.

\documentclass[pstricks,dvipsnames,cmyk]{standalone}

\usepackage{etoolbox}

\def\temp{}

\begingroup
\renewcommand\DefineNamedColor[4]{\listgadd{\temp}{#2}}
\input{dvipsnam.def}
\endgroup
\def\do#1{#1,}
\edef\MyNames{\dolistloop{\temp}end}

\begin{document}
\psforeach{\x}{\MyNames}
{
   \expandafter\ifstrequal\expandafter{\x}{end}{}
   {
        \pspicture(\LL,\BB)(\RR,\TT)
        \pscircle*[linecolor=\x](0,0){\RR}
        \rput(0,0){\tiny\x}
        \endpspicture
        \newpage
   }
}
\end{document}

enter image description here

  • You find the macros in the updated answer. – egreg Jul 24 '11 at 18:44
  • @xport: Nice one :-) – Marco Daniel Jul 24 '11 at 19:23
  • @xport: I edited my answer. – Marco Daniel Jul 25 '11 at 9:28
7

In the following example I use the orig dvipsnm.def where the colors are definied:

EDIT 2:

Macro to use psforeach.

\documentclass[dvipsnames,cmyk]{minimal}
\parindent=0pt
\usepackage{pstricks} 
\usepackage{xcolor}
\usepackage{etoolbox,xstring}
\def\mydvipsnamecolors{}
\begingroup
\renewcommand\DefineNamedColor[4]{%
  \listgadd{\mydvipsnamecolors}{#2}%
  }
\input{dvipsnam.def}
\endgroup
\begin{document}
All names:
\def\do#1{#1, }
\dolistloop{\mydvipsnamecolors}

Package \verb+xstring+
\begin{flushleft}
\def\do#1{#1,}
\edef\MyNames{\dolistloop{\mydvipsnamecolors}notcolor}
\psforeach{\nA}{\MyNames}{%
    \IfStrEq{\nA}{notcolor}{}{\mbox{\color{\nA}++\nA++} \quad}
}
\end{flushleft}

Package \verb+etoolbox+
\begin{flushleft}
\def\do#1{#1,}
\edef\MyNames{\dolistloop{\mydvipsnamecolors}notcolor}
\psforeach{\nA}{\MyNames}{%
   \expandafter\ifstrequal\expandafter{\nA}{notcolor}{}{\mbox{\color{\nA}++\nA++} \quad}
}
\end{flushleft}
\end{document}

EDIT 3

The following should also work. You don't need any psforeach:

\documentclass[dvipsnames,cmyk]{pst-xport}
\SetCan{1}
\SetPad{0cm}
\usepackage{etoolbox}
\def\mydvipsnamecolors{}
\begingroup
\renewcommand\DefineNamedColor[4]{%
  \listgadd{\mydvipsnamecolors}{#2}%
  }
\input{dvipsnam.def}
\endgroup
\begin{document}

\def\do#1{%
        \pspicture(\LL,\BB)(\RR,\TT)
        \pscircle*[linecolor=#1](0,0){\RR}
        \rput(0,0){\tiny#1}
        \endpspicture
        \newpage

}%
\dolistloop{\mydvipsnamecolors}

\end{document}
  • There's no need to redefine the colors, because they have already been defined. It was a bit trickier doing the same for svgnames. – egreg Jul 24 '11 at 18:05
  • @xport: I edited my code -- Now it works. – Marco Daniel Jul 24 '11 at 18:20
  • @egreg: I saw your solution and "great". – Marco Daniel Jul 24 '11 at 18:21
  • \listgadd does not produce a list usable with \psforeach, as it is not comma separated. – egreg Jul 24 '11 at 18:41
  • @egreg: Use do -- I edit my post – Marco Daniel Jul 24 '11 at 18:46
9
\documentclass[a4paper]{minimal}
\usepackage[dvipsnames,svgnames]{xcolor}
\def\showdvipsnamescolors{%
  \begingroup\renewcommand\DefineNamedColor[4]{\color{##2}##2\par}%
  \input{dvipsnam.def}\endgroup}
\def\showsvgnamescolors{%
  \begingroup\renewcommand\preparecolorset[4]{\process##4;,,,;}
  \def\process##1,##2,##3,##4;{%
    \if!##1!\else\color{##1}##1\par\expandafter\process\fi}%
  \input{svgnam.def}\endgroup}

\begin{document}
DVIPSNAMES

\showdvipsnamescolors
\newpage
SVGNAMES

\showsvgnamescolors
\end{document}

It's trivial to modify the code in such a way that it produces a list of colors:

\usepackage{etoolbox}
\def\dvipscolorslist{}
\def\showdvipsnamescolors{%
  \begingroup\renewcommand\DefineNamedColor[4]{\gappto\dvipscolorslist{##2,}}%
  \input{dvipsnam.def}\endgroup}

A similar redefinition can be used for \showsvgnamescolors:

\def\svgnamescolorslist{}
\def\showsvgnamescolors{%
  \begingroup\renewcommand\preparecolorset[4]{\process##4;,,,;}
  \def\process##1,##2,##3,##4;{%
    \if!##1!\else\gappto\svgnamescolorslist{##1,}\expandafter\process\fi}%
  \input{svgnam.def}\endgroup}
  • 1
    Sometimes I should use the refresh button :-) – Marco Daniel Jul 24 '11 at 17:59
  • 2
    My code shows all the predefined color with the options dvipsnames and svgnames. Why bother with an inappropriate macro? Without knowing the "real scenario" what do you think we should do? – egreg Jul 24 '11 at 18:02
3

The etoolbox package usually helps me when I have to iterate elements in a list.

\documentclass{article}

\usepackage{etoolbox}
\usepackage[usenames,dvipsnames]{color}

\DeclareListParser*{\myforeach}{,}

\newcommand*\mycolor[1]{%
\color{#1}#1\par%
}

\newcommand*\drawcolors[1]{%
\myforeach{\mycolor}{#1}%
}

\begin{document}

\drawcolors{Tan,RedOrange,Magenta,Yellow,Green,Red,Blue}

\end{document}

Note that I'm a casual etoolbox user, I'm certainly unaware of the advanced stuff. =)

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.