4

This seems like a really stupid question, but I cannot find the answer...

I am trying to make a contiguous grid of coloured blocks using Tikz, much like in this question, but in my case, the colours to be filled in are grayscale values in the real number range [0.0,1.0], i.e. the 'gray' colour model listed in the xcolor package documentation. The values (and, in theory, the figures) are generated programmatically and could be any arbitrary number between 0 and 1 (inclusive), so it won't make sense to use a handful of definecolor commands at the start. Instead, my current plan is to overwrite the fill for each node as my script writes it out to the file.

For a minimum working example, the below produces something that looks like what I want (see the image below it), except I cannot work out how to get the correct sort of grayscale settings. Using the approach I thought might work, the first three cells just produce white (I suspect it is using the Gray system, with an integer range of [0,255]). The latter six create the sort of results I'm looking for, but the fill colours are not specified in a useful way.

\documentclass[tikz]{standalone} \usetikzlibrary{positioning} \begin{document} \begin{tikzpicture}[outer sep=0pt,minimum size=1cm] \selectcolormodel{gray} \node [fill={gray!0.1}] at (0,0) {}; \node [fill={gray!0.2}] at (0,-1) {}; \node [fill={gray!0.3}] at (0,-2) {}; \node [fill={rgb:black,4;white,8}] at (1,0) {}; \node [fill={rgb:black,5;white,7}] at (1,-1) {}; \node [fill={rgb:black,6;white,6}] at (1,-2) {}; \node [fill={rgb:black,7;white,5}] at (2,0) {}; \node [fill={rgb:black,8;white,4}] at (2,-1) {}; \node [fill={rgb:black,9;white,3}] at (2,-2) {}; \end{tikzpicture} \end{document} 

Output from the MWE

I have also tried out such commands as gray:0.1, or using \PassOptionsToPackage{gray}{xcolor} and then something like just 0.1, but all of those seem to hit compilation errors.

Alternatively, I also tried using the 'matrix' option of Tikz, as below, but that didn't seem to permit any colouring of the grid cells (the visible lines come from using the node=draw option).

\documentclass[tikz]{standalone} \usetikzlibrary{positioning} \begin{document} \begin{tikzpicture}[nodes=draw] \matrix { \node { }; [fill={rgb:black,2;white,10}]; & \node {}; [fill={rgb:black,3;white,9}]; & \node {}; [fill={rgb:black,4;white,8}]; \\ \node { }; [fill={rgb:black,5;white,7}]; & \node {}; [fill={rgb:black,6;white,6}]; & \node {}; [fill={rgb:black,7;white,5}]; \\ \node { }; [fill={rgb:black,2;white,10}]; & \node {}; [fill={rgb:black,3;white,9}]; & \node {}; [fill={rgb:black,4;white,8}]; \\ }; \end{tikzpicture} \end{document} 

Result from using the second MWE's code

How can I create a grid of grayscale coloured blocks using Tikz, with gray colour values in the real number range [0.0,1.0] specified individually for each cell?

2
  • You are confusing the gray model of xcolor (used in colorlet for example) with the color mixing (the color!number!color2 notation). To have a grey scale with the second one you need to use black!xx where xx is in the range 0-100 (the second color is white when not specified) Commented Mar 31, 2021 at 7:09
  • I wouldn't even say it's as advanced as confusing the two. I couldn't find any explanation in the Tikz documentation about specifying grayscale colours inline, so I was just imitating what I saw elsewhere. Your comment is literally the first explanation of that I have seen... Commented Mar 31, 2021 at 21:27

3 Answers 3

4

Maybe this goes in the right direction. It sets the gray level via black!<level>, where <level> is an integer that emerges from multiplying the input, which is in [0,1], by 100.

\documentclass[tikz]{standalone} \usetikzlibrary{matrix} \begin{document} \begin{tikzpicture}[g/.style={minimum size=1cm,fg=#1}, fg/.code={\pgfmathtruncatemacro{\iFill}{100*#1}% \tikzset{fill=black!\iFill}}] \matrix[matrix of nodes,column sep=0pt,row sep=0pt,nodes in empty cells] { |[g=0.3]| & |[g=0.6]|\\ |[g=0.1]| & |[g=0.9]|\\ |[g=0.5]| & |[g=0.2]|\\ }; \end{tikzpicture} \end{document} 

enter image description here

There are many more possibilities, including using a matrix plot from pgfplots.

2

I'm not sure about the use of .code and the code inside it, but it seems to work to automatically fix a filling color for every cell in matrix.

\documentclass[tikz,border=2mm]{standalone} \usetikzlibrary{matrix} \begin{document} \begin{tikzpicture}[ mycolor/.code={ \pgfmathsetmacro{\percent}{(\pgfmatrixcurrentrow-1)*20+(\pgfmatrixcurrentcolumn-1)*5}, \tikzset{fill=gray!\percent}, } ] \matrix[matrix of nodes, column sep=0pt, row sep=0pt, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, mycolor} ]{ & & & \\ & & & \\ & & & \\ & & & \\ }; \end{tikzpicture} \end{document} 

enter image description here

1
  • this should be accepted answer: plain use of TikZ's library matrix Commented Mar 31, 2021 at 11:34
1

Guess what you need is gray!10 instead of gray!.1.

In xcolor's color mix expression, the number is in percents, hence gray!<n> means the color from mixing <n>% of gray and 1 - <n>% of white.

In your gray!.1, the resulting color is the mix of 0.1% gray and 99.9% white, which is indistinguishable with 100% white.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.