Creating a rainbow color macro

You can do this more simply using the wave colour model:

\documentclass{article}    
\usepackage{xcolor}
\usepackage{pgffor}
\begin{document}
\noindent
\foreach \x in {300,320,...,900} {\textcolor[wave]{\x}{\x}\ }
\end{document}

a spectrum of coloured letters

According to the xcolor manual, the argument, λ, is supposed to be a visible-light wavelength, given in nanometers (nm), so that λ ∈ [380, 780]. As my example shows, "invisible" wavelengths are shown as black.


Nested use of \ifnum to define a color via \colorlet does the job:

enter image description here

Code:

\documentclass{article}    
\usepackage{xcolor}
\usepackage{pgffor}

\colorlet{MyColor}{black}%
\newcommand{\MixValue}{0}
\newcommand*{\SetColor}[1]{%
    \ifnum#1<21
        \pgfmathtruncatemacro{\MixValue}{100*#1/20}%
        \colorlet{MyColor}{orange!\MixValue!red}%
    \else
        \ifnum#1<41
            \pgfmathtruncatemacro{\MixValue}{100*(#1-20)/20}%
            \colorlet{MyColor}{yellow!\MixValue!orange}%
        \else
            \ifnum#1<61
                \pgfmathtruncatemacro{\MixValue}{100*(#1-40)/20}%
                \colorlet{MyColor}{green!\MixValue!yellow}%
            \else
                \ifnum#1<81
                    \pgfmathtruncatemacro{\MixValue}{100*(#1-60)/20}%
                    \colorlet{MyColor}{blue!\MixValue!green}%
                \else
                    \ifnum#1<101
                        \pgfmathtruncatemacro{\MixValue}{100*(#1-80)/20}%
                        \colorlet{MyColor}{violet!\MixValue!blue}%
                    \else
                    \fi%
                \fi%
           \fi%
       \fi%
    \fi%
}%

\newcommand*{\ShowInAppropriateColor}[1]{%
    \SetColor{#1}%
    \textcolor{MyColor}{#1}%
}%

\begin{document}
\noindent
\foreach \x in {0,...,100} {%
    \ShowInAppropriateColor{\x}
}%
\end{document}

Here is an approach that, for a couple of reasons, is interesting.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\newcommand{\makemycolor}[2]{%
    \pgfmathsetmacro{\hue}{(#1/100)^1.715*0.8}%
    \definecolor{myhsbcolor}{hsb}{\hue,1,1}%
    \textcolor{myhsbcolor}{#2}%
}

\begin{document}

\noindent\begin{tikzpicture}
    \foreach \k in {0,1,...,100}{%
        \pgfmathsetmacro{\hue}{(\k/100)^1.715*0.79}
        \definecolor{mycolor}{rgb:hsb}{\hue,1,1}
        \node[color=mycolor] () at (\k/10,0) {$\bullet$};
    }%
    \foreach \f in {0,1,...,10}{%
        \pgfmathtruncatemacro{\num}{\f*10}
        \node () at (\f,-.5) {\num};
    }
    \foreach \g/\h in {0/Red,2/Orange,4/Yellow,6/Green,8/Blue,10/Purple}{%
        \pgfmathtruncatemacro{\num}{\g*10}
        \node at (\g,-1) {\makemycolor{\num}{\h}};
    }%
\end{tikzpicture}

\end{document}

Better sample

First, this uses the hsb (hue-saturation-brightness) model, as Thruston suggests.

Second: Normally, TikZ cannot use the hsb model. That problem is solved by specifying \usepackage[rgb]{xcolor} which causes xcolor.sty to convert all colors from whatever color space to the rgb color space which TikZ can use. You could also say \usepackage[cmyk]{xcolor} if you were having this printed -- TikZ also understands cmyk. Also note that you could say \usepackage{xcolor} but define the color with \definecolor{mycolor}{rgb:hsb}{\hue,1,1} or \definecolor{mycolor}{cmyk:hsb}{\hue,1,1} -- again this converts hsb to rgb or cmyk, but on an individual basis.

Note that xcolor.sty must be loaded BEFORE tikz.sty.

Third, the function that actually sets up what the hue is

\pgfmathsetmacro{\hue}{(\k/100)^1.715*0.8}

can be varied at will to get a better spread of colors. The 0.8 determines the ending color, so you can adjust it a little up or down to fine-tune the shade of purple at the end of the spectrum.

So, for a macro that could be called for a specific color, you could try something like this:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\newcommand{\makemycolor}[2]{%
    \pgfmathsetmacro{\hue}{(#1/100)^1.715*0.79}%
    \definecolor{myhsbcolor}{hsb}{\hue,1,1}%
    \textcolor{myhsbcolor}{#2}%
}

\begin{document}

\makemycolor{0}{Red}
\makemycolor{40}{Yellow}
\makemycolor{55}{Green with a touch of yellow}
\makemycolor{100}{Purple}

\end{document}

Sample colors

As an aside, you can also specify \usepackage[gray]{xcolor} in the preamble to get this:

enter image description here

Not sure of the practical use of this, but interesting nonetheless.

Tags:

Color

Tikz Pgf