Unfortunately, the requested feature is unsupported.
In general, your approach works fine: if you write
\usepackage[cmyk]{xcolor}
all color definitions result in cmyk colors. But shadings are special: they are not based on xcolor's drivers but on pgf's drivers. And the pgf drivers for shadings supports RGB, nothing else. I believe that pgf calls colorspace conversion routines in order to convert from cmyk back to RGB whenever it generates shadings.
What you need is a feature request for PGF in order to respect the global xcolor
configuration and to generate shadings in that color space.
There may be work-arounds. These, however, depend on the urgency - if you say that you will file feature requests and will live with the restriction, that is fine.
If you really need a workaround, you can continue reading.
The work-around that I have in mind is to use pgfplots. It has a couple of plot-related shadings and comes with its own related drivers. These, in turn, support cmyk - and most shadings can be expressed as plot-based shadings. The effort to convert these shadings from tikz pictures which have a super embedding into your pictures to pgfplots plots would be medium.
Here is an attempt in this direction:
\pdfcompresslevel=0
\documentclass{minimal}
%%% uncomment to use CMYK
\usepackage[cmyk]{xcolor}
\definecolor{mygreen}{cmyk}{1,0,0.57,0.42}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
%%% uncomment to use RGB
% \usepackage[rgb]{xcolor}
% \definecolor{mygreen}{HTML}{009440}
\usepackage{tikz}
\begin{document}
\color{mygreen}{MY TEXT}
\tikz
\shade [left color=mygreen,right color=mygreen]
(1,1) rectangle (2,2);
%
\tikz
\fill [mygreen]
(1,1) rectangle (2,2);
\begin{tikzpicture}
% this statement is needed for pgfplots v1.8.
% pgfplots 1.9 or newer inherits it from
% \usepackage[cmyk]{xcolor}:
\pgfplotsset{mesh/colorspace explicit color output=cmyk}
\begin{axis}[
x=1cm,y=1cm,
hide axis,
view={0}{90}]
\addplot[surf,mesh/color input=explicit,shader=interp]
table[meta=cdata] {
x y cdata
1 1 color=mygreen
2 1 color=yellow
1 2 color=mygreen
2 2 color=yellow
};
\end{axis}
\end{tikzpicture}
\end{document}
It relies on a surface plot with explicit color using the rectangular coordinates (1cm,1cm) (2cm,2cm) and the colors are listed in the table. The verbose syntax color=<name>
is necessary to distinguish from something like 0,0,1
or gray=0.5
.
How's this?
settings.outformat="pdf";
size(5cm);
import graph;
pen[] pens = new pen[] {white, 0.9*red + 0.1*black, 0.75*black + 0.25*red, black};
path outerpath = ellipse((0,0), 2, 1.5);
path innerpath = (1,0.8);
path midpath(path p1, path p2, real t) {
pair f(real s) {
return (1-t)*relpoint(p1, s) + t*relpoint(p2, s);
}
path p = graph(f, 0, 1, operator ..) -- cycle;
return p;
}
int lastpath = pens.length - 1;
path[] paths = new path[lastpath + 1];
for (int i = 0; i <= lastpath; ++i) {
paths[i] = midpath(innerpath, outerpath, i/lastpath);
}
draw(paths, pens);
Best Answer
Thank you @CharlesStaats for your great tip regarding the
latticeshade
function.After doing more reading at Wikipedia I learned that the HSV color space consists simply of an RGB linear interpolation between the color sequence red--yellow--green--cyan--blue--magenta--red. Trimming that sequence to fit my need gives the following code.