7

I want to model gradients in TikZ for a paper covering color gradients. These are rather special, so a cannot rely on a TikZ shade alone. Thus, I want to model the gradient using consecutive TikZ shaded rectangles, like this:

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\begin{figure}[!h]
\begin{tikzpicture}\shade[left color=red,right color=gray]
  (1.211,0.000) rectangle (0.211,1.000);
\end{tikzpicture}
\end{figure}
\end{document}

However, instead of red and blue I need an RGB color triplet as common in HTML. As per xcolor manual 2.3.3, I figured

\shade[left color=rgb,300:red,51;green,41;blue,5,right color=rgb,300:red,50;green,38;blue,0]

might do the job, but I get ! Package xcolor Error: Undefined color `rgb'.

It is feasibe but really awkward to pregenerate all the colors I need. Is it possible to use xcolor direct colors in a TikZ shade? all the examples I came across either use definecolor (even for HTML colors) or named colors.

Any pointers are appreciated.

5

It is not so difficult to create shortcuts for such recurring commands. And you can create and recreate the same color, here is a quick mock-up; you can get the setting you are trying push rgb syntax to and read off the argument of the assignment and place it to a temporary color name. Then use it for the setting of that key.

I don't know how often you will use this and for what so no promises.

EDIT

\documentclass[tikz]{standalone}
\tikzset{xcolor/.code args={#1=#2}{
     \definecolor{mytemp}{rgb}{#2}
     \tikzset{#1=mytemp}
  }
}
\begin{document}
\begin{tikzpicture}
\shade[xcolor={left color={0.5,0.7,0.1}},xcolor={right color={0.7,0.4,0.8}}] 
(1.211,0.000) rectangle (0.211,1.000);
\shade[xcolor={right color={0.5,0.7,0.1}},xcolor={left color={0.7,0.4,0.8}}] 
(0.211,0.000) rectangle (-1.211,1.000);  
\end{tikzpicture}
\end{document}

enter image description here

the edited image

enter image description here

  • Thank you, this looks very promising! (I'll test in a few hours.) – Simon Thum Aug 20 '14 at 7:12
  • Thanks, this works. In fact I found putting the extended color spec in braces also works, syntactically, but I fail to get RGB on that path, so I ended going with your solution. – Simon Thum Aug 20 '14 at 20:51
  • Or, perhaps not. If looked like it worked but it does not scale as I require. The next parts of the gradient (adjacent tikz shades) do not show up at all. Only one shade seems to work with the trick. – Simon Thum Aug 21 '14 at 20:58
  • @SimonThum I don't know what you require so you need to make a case for me. Otherwise it does work. – percusse Aug 21 '14 at 21:02
  • It's simple; do an adjacent shaded rect like the first, and try to make it show up. There are no latex errors AFAICT but the second shadeed rectangle does not show up in the PDF. – Simon Thum Aug 21 '14 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.