# How to make correct shading with hsb color model?

I want to use hsb color model to shading.

First rectangle shows a shading using rgb color model.

Second rectangle shows a shading using hsb color model. But I don't understand why I don't obtain the same shading that with my third rectangle.

\documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture}   % create some hsb colors   \colorlet{redhsb}[hsb]{red}%   \colorlet{bluehsb}[hsb]{blue}%   \colorlet{greenhsb}[hsb]{green}%    % first shading with rgb color model   \fill[left color=red,right color=blue] (0,1) rectangle (2,0);    % second shading with hsb color model   \fill[left color=redhsb,right color=bluehsb] (0,0) rectangle (2,-1);    % third shading with hsb color model   \fill[left color=redhsb,right color=greenhsb] (0,-1) rectangle (1,-2);   \fill[left color=greenhsb,right color=bluehsb] (1,-1) rectangle (2,-2); \end{tikzpicture} \end{document} 


Edit: In fact, TikZ calculates the middle color (using color model of the left color). Then, it converts all three colors to rgb model and make shading...

% fourth shading with rgb color model \fill[left color=red,right color=green] (0,-2) rectangle ++(1,-1); \fill[left color=green,right color=blue] (1,-2) rectangle ++(1,-1); 


So, my question is: Can TikZ do real hsb (HSV) shading?

Replay

The following happens when you do \fill[left color=redhsb,right color=bluehsb] (0,0) rectangle (2,-1);

• TikZ sets three internal colors:
• [email protected]@bottom=bluehsb
• [email protected]@middle=redhsb!50!bluehsb
• [email protected]@top=redhsb

The middle color is calculated by xcolor, which does the mixing in the first color model (HSB in this case).

• TikZ uses a shading declared by
\pgfdeclareverticalshading[[email protected]@top,[email protected]@middle,[email protected]@bottom]{axis}{100bp}{%
color(0bp)=([email protected]@bottom);
color(25bp)=([email protected]@bottom);
color(50bp)=([email protected]@middle);
color(75bp)=([email protected]@top);
color(100bp)=([email protected]@top)}


• pgf basically passes this through to \[email protected].
• The definition of this command depends on the output driver. For PDF it is
\def\[email protected]#1#2#3{%
{%
\[email protected]{#3}%
\pgfmathparse{#2}%
\setbox\[email protected]=\hbox to\pgfmathresult pt{\vbox to\[email protected]{\vfil\[email protected]{/Sh sh}}\hfil}%
\[email protected]{\pgfpoint{#2}{\[email protected]}}%
\pdfxform resources {%
/ColorSpace /DeviceRGB
/Domain [\[email protected]]
/Coords [0 \[email protected]\space0 \[email protected]]
/Function \[email protected]
/Extend [false false] >> >>}\[email protected]% <<
}%
}



I won't pretend to understand everything that is going on, but as far as I can tell, pgf only writes the 3 computed colors and tells the PDF-viewer to do the shading by linear interpolation. In other words, TikZ doesn't seem to do the shading calculations itself.

As you see above, the color space is set to DeviceRGB. Indeed I think that PDF doesn't support HSL (though I haven't checked to PDF documentation).

As far as I understand, the only way to get HSL interpolation is to write a PostScript shader that does the interpolation and then converts the result to RGB.

Category: tikz pgf Time: 2012-06-09 Views: 4
Tags: color tikz pgf