6

Is it possible to calculate a color value like this in-place with tikz and its calc library?

\filldraw[fill=black!{50-95.7/2},draw=black] (1,0) rectangle (2,1); %Gives a 'Missing number, treated as zero.' error

I am aware that I can define a color using a macro, but I have many lines like these and I want to keep the code concise and readable.

1 Answer 1

5

Oooo this is ugly...

\documentclass[tikz,border=5]{standalone}
\def\atchar{@}
\tikzset{set color/.style 2 args={.. set color={#2!@!@;0;;#1;}},
.. set color/.code args={#1!#2;#3;#4;#5;}{
  \def\tmp{#1}\ifx\tmp\atchar%
     \tikzset{#5=#4}%
  \else%
    \ifcase#3\relax
      \tikzset{.. set color/.expanded={#2;1;#1;#5;}}%
    \or
      \pgfmathparse{max(min(100,#1),0)}%
      \tikzset{.. set color/.expanded={#2;2;#4!\pgfmathresult;#5;}}%
    \else
      \tikzset{.. set color/.expanded={#2;1;#4!#1;#5;}}%
    \fi
  \fi%
},
fill color/.style={set color={fill}{#1}},
draw color/.style={set color={draw}{#1}},
shade left color/.style={set color={left color}{#1}},
shade right color/.style={set color={right color}{#1}}
}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0, 10, ..., 360}
  \filldraw [fill color=cyan!sin(\i)*50 + 50, draw color=red!\i/3.6!yellow, 
    ultra thick] (\i:2) circle [radius=1/10];
\shade [shade left color=purple!pi*25, shade right color=orange!50 * 1.5] 
   circle [radius=1]; 
\end{tikzpicture}
\end{document}

enter image description here

1
  • Wow, that's indeed ugly. I can live with a different syntax and/or some limitations if this makes the code a little less ugly. Commented Apr 20, 2016 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .