6

I am involved in a project that includes the creation of arrows.

Making a monocolored figure is a visual problem due to the scarcity of colors, so I came to design a solution that is as automatic as possible with your huge help!

I need to draw n arrows, so I need n colors (*).

What kind of color?

Every color is composed by a single color, it is solid, preferably using RGB mode.

Where is the set of colors?

I do not know :D. Seriously, because of my low knowledge of colors I did not find any place where a range of colors from white to black is available, so I found some alternative ideas:

Spectral colors (Wikipedia):

Spectral colors (Wikipedia)

Monochromatic Color Schemes (w3schools.com):

Monochromatic Color Schemes (w3schools.com)

Rainbow color palette (Pinterest):

Rainbow color palette (Pinterest)

Using ColorBrewer in TikZ and pgfplots:

Using ColorBrewer in TikZ and pgfplots

What I want

I see colors as a (finite) tape where depending on the number of pixels we can have more or less (solid) colors. For example, it would be ideal for me to be able to work (in RGB) with Monochromatic Color Schemes (w3schools.com), but:

  • The colors have to be arranged in a row and not in a circular shape (like a matrix of 1 x n entries).
  • The extremes must be black and white, and in the middle most possible colors are distributed.
  • We need to apply a transition as smooth as possible from one color to another i.e. between color and color there has to be a very little difference in hue.

Suppose we have the palette of colors of MS Paint:

MS Paint palette of colors

Since we do not want colors with opacity we can consider the first row of pixels, something like:

First row of pixels of MS Paint palette of colors

Now, the idea is to create an algorithm that divides the length of that range into n equal parts, in order to maintain a consistency with the use of colors. For example, if we are using 2 colors we could imagine the following:

Example 1 with 2 colors

If we need 5 colors:

Example 2 with 5 colors

and so on.

Another (more viable) option is to use colorbrewer PGFPlots library since it is very easy to have a range of colors, so I prefer this option (if you have another option you can post it, I also will be grateful).

I am fully aware of the creation of these two questions:

but them are adapted to plots, not arrows.

What I have done

MWE:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \foreach \X in {0,...,5} {
        \node[circle,draw,name=a\X] at (0,\X) {};
    }
    \foreach \X in {0,...,5} {
        \node[circle,draw,name=b\X] at (1,\X) {};
    }

    \foreach \X in {0,...,5} {
        \draw[-latex] (a\X) to (b\X);
    }

    \foreach \X in {0,...,5} {
        \draw[-latex] (a\X) to (b\X);
    }
\end{tikzpicture}

\end{document}

What I have done

Here n=5.

My idea is to use \definecolor{mycolor}{rgb}{[R],[G],[B]} inside three loops, where each index is [R], [G] and [B] going from 0 to 255. The problem is that I do not know how to nest them to produce a nice division nor do I know how to add that color to the arrow. I only tried to vary the first parameter in this way:

\foreach \Y in {1,...,255} {
    \definecolor{currentcolor}{rgb}{0, 1, 1} % I need at least one color
    \foreach \X in {0,...,5} { % I go through each arrow and add the color
        \draw[-latex,currentcolor] (a\X) to (b\X);
        \definecolor{currentcolor}{rgb}{\Y, 1, 1} % I recursively add the colors
    }
}

However, this algorithm (which pretends to be recursive) does not do more than to add a single color to the arrows and it always go through the deeper loop without considering the other colors!

Each arrow must have a different color, as distributed as possible within a set of colors (in which this set must contain a high amount of gradient colors).

Is it possible to adapt something from Using ColorBrewer in TikZ and pgfplots to use it in the MWE?

Thanks!!


(*) In practice I need between 30 to 50 arrows, hence, colors.

  • 1
    You could also use the "wave" colour model: tex.stackexchange.com/a/230265/15036 – Thruston Mar 20 at 23:06
  • @Thruston hi! Yes, that could be a possibility, good answer! Could you post an answer drawing some arrows with the rainbow, please? Remember that the number of arrows (hence, colors) can vary. – manooooh Mar 20 at 23:15
4

I'm not entirely sure that I fully understand the question. If the goal is to color the arrows in your MWE with a series of colors, then I suggest this:

\documentclass{article}

%% to make PGF/Tikz use hsb (or any other) color space; load before PGF/TikZ
\usepackage[rgb]{xcolor}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \foreach \X in {0,...,5} {
        \node[circle,draw,name=a\X] at (0,\X) {};
        \node[circle,draw,name=b\X] at (1,\X) {};
        %% \X * 0.75 (limits the final color to purple) * 1/5
        \pgfmathsetmacro{\huenum}{\X*0.75*0.2}
        \definecolor{mycolor}{hsb}{\huenum,1,1}
        \draw[-latex,mycolor] (a\X) to (b\X);
    }
\end{tikzpicture}

\end{document}

This uses the hsb (hue-saturation-brightness) color space to determine the colors. You need to load \usepackage[rgb]{xcolor} before loading any TikZ or PGF packages -- xcolor.sty will perform the conversion from hsb to rgb. This is the result:

enter image description here

Update

You asked about 255 colors. Here is a simple proof-of-concept of how such a thing might be done. Note the very (too?) gradual change of colors. Still, here it is. Notice that this uses the HSB color space (0 to 255).

\documentclass{article}

\usepackage[rgb]{xcolor} %% to make PGF use hsb (or any other) color space
\usepackage{pgfplots}
\usepackage{multicol}

\newcounter{huecount}

\newcommand{\makefig}[1]{%
    \pgfmathsetmacro{\incr}{255/#1}\typeout{>>>>\incr}%
    \foreach \X in {0,\incr,...,255}{%
        \noindent
        \tikz{%
            \node[circle,draw,name=L] at (0,2) {};
            \node[circle,draw,name=R] at (1,2) {};
            \definecolor{mycolor}{HSB}{\X,255,255}
            \draw[-latex,mycolor] (L) to (R);
        }%
    \par
    }%
}%

\begin{document}

\begin{multicols}{6}
\noindent\makefig{255}
\end{multicols}

\end{document}

255 colors

  • Wow, your code is awesome, thanks! How many different colors can we generate (I do not have a PC to test): 255? – manooooh Mar 20 at 12:21
  • 1
    Theoretically any number you like. I'm not at my computer right now, but that would require something like \pgfmathsetmacro{\huenum}{\X*0.75}*1/255} for \foreach \X in {0,1,...,255} OT: Typing TeX on an iPhone is excruciating. – sgmoye Mar 20 at 13:06

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.