How to map value to color using TikZ?

I am trying to draw rectangles, each of which has a corresponding value, which will be mapped into a color from a color map based on the value. This is how I want the write the code. What is missing is the mapping from \myvalue to \mycolor.

\documentclass{standalone}

\usepackage{colortbl}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{compat=1.9}

\newcommand*{\ListOfRectanglesColor}{%
0/0/1/1/blue,
1/1/2/2/red,
}

\newcommand*{\ListOfRectanglesValues}{%
0/0/1/1/0.3,
1/1/2/2/0.6,
}

\begin{document}
\begin{tikzpicture}
\foreach \sXa/\sYa/\sXb/\sYb/\mycolor in \ListOfRectanglesColor {
\fill[\mycolor] (\sXa, \sYa) rectangle (\sXb, \sYb);
}
%\foreach \sXa/\sYa/\sXb/\sYb/\myvalue in \ListOfRectanglesColor {
%  what code will define \mycolor as a color based on \myvalue?
%  and a specified color map?
%  \fill[\mycolor] (\sXa, \sYa) rectangle (\sXb, \sYb);
%}
\end{tikzpicture}
\end{document}


Edited in response to comment

Question: How do I map values to colors? I would appreciate it if the TeX code were commented so that I could understand how the code works.

For simplicity, the mapping function will be of the form

\valuetocolor{value-min}{value-max}{color-min}{color-max}{value}


The floating point numbers value-min and value-max will be mapped to two defined colors color-min and color-max respectively. We can assume that value-minvalue-max. I want to map the floating point number value to the linear interpolated value between color-min and color-max. More precisely, let λ be the value such that value = λ value-min + (1 - λ) value-max. Then I want \valuetocolor{value-min}{value-max}{color-min}{color-max}{value} to return the color λ color-min + (1 - λ) color-max.

Related TeX.SX Question

I am aware that there is a related question Need some help getting started with TikZ, groupplots and color mapping of matrix where there is TeX code that accomplishes a mapping from value to color for a more complicated color map. Unfortunately, with my limited TeX/PGF knowledge, I can't understand how the code works.

• What is the algorithm to determine the color in \mycolor based on a numerical \myvalue? – Peter Grill Feb 8 '14 at 4:20
• I've edited the question based on your comment to clarify how to mathematically determine the color. – I Like to Code Feb 8 '14 at 18:52

The \colorlet macro from the xcolor package can define a mix between two colors (color1 and color2 in this example):

\colorlet{color1}{red}
\colorlet{color2}{blue}
\colorlet{my mixed color}{color1!\lambda!color2}


The \lambda value must be an integer between 0 (my mixed color is blue) and 100 (my mixed color is red).

By default, colors and mixing are in the RGB color space. If the two colors are in the HSB color space, the mixing is in HSB color space:

\colorlet{color1}[hsb]{red}
\colorlet{color2}[hsb]{blue}
\colorlet{my mixed color}[rgb]{color1!\lambda!color2}


Here is a simple example with two colormaps between red and lime (left in RGB color space, right in HSB color space) using values (\myvalue) between 0 (\min) and 20 (\max):

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\colorlet{color min rgb}[rgb]{red}
\colorlet{color max rgb}[rgb]{lime}

\colorlet{color min hsb}[hsb]{red}
\colorlet{color max hsb}[hsb]{lime}

\def\min{0}
\def\max{20}

\foreach \pos in {0,...,20}{
\pgfmathsetmacro\myvalue{\pos}

\pgfmathtruncatemacro\lambda{(\myvalue-\min)/(\max-\min)*100}
\colorlet{my color rgb}[rgb]{color min rgb!\lambda!color max rgb}
\colorlet{my color hsb}[rgb]{color min hsb!\lambda!color max hsb}

\fill[fill=my color rgb,draw=white] (1cm,\pos cm) rectangle +(1cm,1cm);
\fill[fill=my color hsb,draw=white] (2cm,\pos cm) rectangle +(1cm,1cm);
\node at (3.5cm, \pos cm + 5mm) {\myvalue};
}
\end{tikzpicture}
\end{document}

• This is perfect! I like how your example illustrates the mixings for the two different color spaces, and the output demonstrates that the interpolation works correctly. – I Like to Code Feb 8 '14 at 18:56