# How to retrieve color from a specific location and use it?

Please save your time and energy on this question, this is not an important one.

What's the color in the middle (or anywhere else) in these pictures? How to retrieve/compute that color to be used for drawing later?

\documentclass[a4paper]{article}
\pagestyle{empty}
\parindent=0pt
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{pgfplots.colormaps}

\begin{document}
\centering
\begin{tikzpicture}
\begin{axis}[colormap={mal-map}{[1cm] rgb255(0cm)=(50,50,10) color(1cm)=(white) rgb255(5cm)=(200,100,150)}, colorbar horizontal, colorbar/width=2cm, hide axis]
%\addplot[mesh, point meta=y, line width=4mm, samples=150] {x^2}; % Some graph to show...
\end{axis}
\end{tikzpicture}\bigskip

\begin{tikzpicture}
\begin{axis}[colormap/bright, colorbar horizontal, colorbar/width=2cm, hide axis]
%\addplot[mesh, point meta=y, line width=4mm, samples=150] {x^2}; % Some graph to show...
\end{axis}
\end{tikzpicture}\bigskip

\begin{tikzpicture}
lower left=cyan, lower right=black!50]
(0,0) rectangle (4,4);
%\draw (0,0) grid (4,4); % help lines
\end{tikzpicture}\bigskip

\begin{tikzpicture}
\shade[ball color=red, opacity=0.50] (1,0) circle (2.0cm);
\shade[ball color=black, opacity=0.50] (0.5,0) circle (1.5cm);
%\draw (-1,-2) grid (3,2); % help lines
\end{tikzpicture}
\end{document}

I am aware of the \extractcolorspecs command from xcolor package, but this is not the case. First two pictures were prepared in pgfplots by using colormap/colorbar, next two were prepared in tikz by using \shade command from shadings library.

We tried to duplicate and use colors from the colorbar from specific locations, for more details about that experiment, please, see this question.

We could guess color in the first picture, sort of, we could compute color in the second picture, because we know the distance between two key colors. Well, it looks that the difficult cases are the remaining two. The third example uses both directions to place the colors (not only horizontal or vertical direction is used as seen on examples from pgfplots) and the last example uses opacity and layers. I was wondering if somebody needed to solve this problem.

There is a conversion from colormap to shadingspec, it is mentioned on page 173 of the pgfplots manual.

I was thinking of this command (if I omit opacity for a minute): \getmecolor{x-coordinate}{y-coordinate} and the result would be color in RGB or CMYK.

-
Do you want to retrieve the color interactively with a tool? Or from inside the code? –  alfC Mar 30 at 20:13
@alfC From inside the code, otherwise GIMP could be used (I am sure there are many more tools of this kind). –  Malipivo Mar 30 at 20:17

You can use the pgfplots macro \pgfplotscolormapaccess:

\documentclass{standalone}
\parindent=0pt
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
\pgfplotsset{
colormap={mal-map}{[1cm] rgb255(0cm)=(50,50,10) color(1cm)=(white) rgb255(5cm)=(200,100,150)}
}

\begin{tikzpicture}
\begin{axis}[colormap name={mal-map}, colorbar horizontal, colorbar/width=2cm, hide axis]
%\addplot[mesh, point meta=y, line width=4mm, samples=150] {x^2}; % Some graph to show...
\end{axis}
\end{tikzpicture}\bigskip

\begin{tikzpicture}

\foreach \x in {0,0.25,0.5,0.75,1} {
% \pgfplotscolormapaccess[<input min>:<input max>]{<input>}{<colormap name>}
\pgfplotscolormapaccess[0:1]{\x}{mal-map}
\message{GOT \meaning\pgfmathresult^^J}%
\def\TEMP{\definecolor{my color}{rgb}}
\expandafter\TEMP\expandafter{\pgfmathresult}
\fill[my color] (\x*3cm,0) rectangle ++ (0.2cm,1cm);
}
\end{tikzpicture}
\end{document}

It expects three arguments: the first is the domain of the input argument. In our case, the input argument is \x and we assume that \x=0 is the lower end and \x=1 is the upper end.

The output is assigned to \pgfmathresult; it will contain a triple {<r>,<g>,<b>}.

Then we use the xcolor command \definecolor{<name>}{rgb}{<r>,<g>,<b>} to define a color named <name> using these coordinates.

The \expandafter and \def instructions are TeX commands to ensure that xcolor receives the expanded values rather than the token "\pgfmathresult" (please refer to Where do I start LaTeX programming? for details about these programming constructs).

The macro \message{...\meaning\pgfmathresult} is just a debug instruction; \meaning shows the value of the following macro.

Naturally, this will only work for color map stuff. I believe there is no tikz solution which allows to grab some arbitrary color given an input vector. But you may be able to formulate some of the simpler tikz shadings as colormap (at least the rectangular ones).

-
This is great! It didn't cross my mind to start digging in the code, I was always checking the pgfplots manual. It solves first two figures. To TikZ: I think there will be some solution at the postscript level, but I am guessing here. Maybe we could externalize that particular picture, use ImageMagick at a pixel level and load the color back on-the-fly... –  Malipivo Mar 30 at 20:41