PGF supports several types of shadings. The most advanced type of shading is "functional shadings". For such shadings the color of each point is calculated by calling a user specified function for each point in the shaded area. The shading function is specified using a simplified form of a subset of the PostScript language.

In this impressive example a functional shading is used to draw a slice of the cone shaped HSV color space. Note the smooth color transitions. The most interesting part of the code is the conversion from HSV to RGB.


hsv-shading

Edit and compile if you like:

% HSV shading
% Author: Ken Stars
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfdeclarefunctionalshading{HSVsweep}
{\pgfpoint{-2cm}{-2cm}}
{\pgfpoint{2cm}{2cm}}
{}
{ % x y
2 copy % ... x y x y
2 copy 0 eq exch 0 eq and
{ pop pop 0.0 } % silently deal with error: return
% arbitrary heading of zero for origin
{atan 360.0 div} % ... x y heading; heading being in
%the interval [0, 1.0]
ifelse % because we will use it for 'Hue'
dup 360 eq { pop 0.0 }{} ifelse % if heading is 360
%degrees, make it zero instead
3 1 roll % ... heading x y
dup mul % ... heading x y*y
exch dup mul % ... heading y*y x*x
add sqrt % ... heading ra_pt (distance from origin in points)
56 div % scale it means a ra of just under 2 cm
dup 1.0 ge % BOOLEAN. ready to clamp to interval [0, 1.0]
{ pop 1.0 }{} ifelse % We shall use the scaled ra as 'Saturation'
2.5 mul 0.25 sub % now, Ra in [0.1, 0.5] --> Saturation
% in [0.0, 1.0]. Saturation varies between the two radii
1 % ... H S V ( with 'Value' set to literal constant of 1.0 )
1 index 1.0 eq % TEST I [ S == 1.0 ]
{ % BLOCK A [ take stack to V V V ] achromatic case
3 1 roll pop pop dup dup
}
{ % BLOCK B take stack to V T Q P i
% C version to use as model:
% H' = H * 6
% i = floor(H')
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Click to download: hsv-shading.texhsv-shading.pdf
Open in Overleaf: hsv-shading.tex


Comments

#1 Kjell Magne Fauske, October 29, 2008 at 8:33 a.m.
Update: Added a workaround for Apple’s PDF viewer shading bug. Thanks Berteun Damman for the patch.

#2 hakan tiftikci, November 11, 2009 at 8:40 a.m.
can alpha channel (~transparency) be specified in “functional shadings”?

#3 Kjell Magne Fauske, November 13, 2009 at 10:41 a.m.
I checked the PGF documentation, but as far as I can see, you can’t specify alpha channel when using functional shadings, only RGB-values. There are probably workarounds since PGF supports fadings with varying alpha-values.