TransWikia.com

How to use gray colour mode in fill command in Tikz

TeX - LaTeX Asked by Jarak on April 26, 2021

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?

3 Answers

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.

Correct answer by user238301 on April 26, 2021

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.

Answered by muzimuzhi Z on April 26, 2021

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

Answered by Ignasi on April 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP