That’s a wonderful use case for Luatex!
(In fact, when I read the about the bee colors yesterday I was hoping
for this question to pop up on TeX-SE.)
So, without further ado,
here is the necessary code.
This requires Luaotfload
, which Latex users do not have to load
explicitly if they are using the fontspec package.
The beecolors code is split into a TeX and a Lua file.
The Lua part (beegradients.lua
) implements a node processor for the
post_linebreak_filter
callback.
The principle is quite simple:
It scans the horizontal lists that constitute a paragraph for glyphs
and ligatures which it surrounds with the appropriate PDF color
whatsits, recursing into any vlists an hlists it encounters along the
way.
The second file (beegradients.tex
) contains wrapper macros for
defining gradients and toggling the callback.
A gradient group is a list of color expressions and can be defined like
this:
\definegradientgroup [<name>][<col1>,<col2>, ... ,<coln>]
E.g.
\definegradientgroup [blackwhite][0x000000, 0xFFFFFF]
installs a gradient list consisting of two colors, black and white.
Likewise,
\definegradientgroup [red-green-blue][255*0*0, 0*255*0, 0*0*255]
defines a list of three colors, red, green and blue.
These gradient groups can afterwards be referred to by their names.
The callback can be activated by means of an environment
beegradients:
\startbeegradients [<name>]
... ... ...
\stopbeegradients
Where <name>
refers to a previously defined gradient group.
E.g., in order to use the groups blackwhite and red-green-blue we
defined above:
\startbeegradients [blackwhite]
\input knuth
\stopbeegradients
\startbeegradients [red-green-blue]
\input knuth
\stopbeegradients
Without the optional argument, \startbeegradients
will choose the
last active group.
\startbeegradients [blackwhite]
\input knuth
\stopbeegradients
foo bar baz %% <- no gradient
\startbeegradients
\input knuth %% <- black and white again
\stopbeegradients
Here is a complete example for the Plain format that shows a couple
definitions:
\input luaotfload.sty
\input beegradients.tex
\font \mainfont = file:Iwona-Regular.otf at 10pt
\mainfont
\definegradientgroup [mygradients][
42*11*242, %% decimal notation, separated by “*”
83*242*55,
0xf00ba7, %% hex notation
0x1ec001,
g:23*b:42*r:133, %% rgb notation, also separated by “*”
b:53*g:184*r:10,
]
\definegradientgroup [blackwhite][0x000000, 0xFFFFFF]
\definegradientgroup [red-green-blue][255*0*0, 0*255*0, 0*0*255]
\definegradientgroup [red][255*0*0, 0*0*0]
\definegradientgroup [green][0*100*0, 0*255*0]
\definegradientgroup [blue][0*0*20, 0*0*210]
\input knuth
\startbeegradients [mygradients] \input knuth \stopbeegradients
\startbeegradients [blackwhite] \input knuth \stopbeegradients
\startbeegradients [red-green-blue] \input knuth \stopbeegradients
\startbeegradients [red] \input knuth \stopbeegradients
\startbeegradients [green] \input knuth \stopbeegradients
\startbeegradients [blue] \input knuth \stopbeegradients
\bye
Result:

Of course the code is compatible with Latex, you can load it directly:
\documentclass {scrartcl}
\usepackage {fontspec} %% this loads luaotfload as well
\setmainfont {Antykwa Poltawskiego}
\input beegradients.tex
\definegradientgroup [red-green-blue][255*0*0, 0*255*0, 0*0*255]
\begin {document}
\startbeegradients [red-green-blue]
\input knuth
\stopbeegradients
\end {document}
Which leads to this colorful document:

For texlive 2016 see this post: https://tex.stackexchange.com/a/321962/90087