<?php
   function gradient($txt, $from=0x000000, $to=0xffffff)
        {
                // count of all characters
                $strlen = strlen($txt);

                // count of nonblank characters
                $grdlen = $strlen - preg_match_all('/\s/', $txt, $null) - 1;

                // skip a colorizing of whitespace line
                if ( ! $grdlen ) {
                        return $txt;
                }

                // skip a single coloring
                if ( $from == $to ) {
                        return sprintf('<span style="color: #%06x">%s</span>', $from, $txt);
                }

                // represent the start and the end colors as arrays
                $f = array(
                        floor($from / 0x10000),
                        floor(($from & 0xffff) / 0x100),
                        $from & 0xff,
                );
                $t = array(
                        floor($to / 0x10000),
                        floor(($to & 0xffff) / 0x100),
                        $to & 0xff,
                );

                // current value of red, green and blue colors separately
                $r = $f[0];
                $g = $f[1];
                $b = $f[2];

                // deltas per each color
                $rd = ($t[0] - $f[0]) / $grdlen;
                $gd = ($t[1] - $f[1]) / $grdlen;
                $bd = ($t[2] - $f[2]) / $grdlen;

                $result = '';
                for ($i = 0; $i < $strlen; $i++) {
                        // skip colorizing of whitespaces
                        if ( preg_match('/\s/', $txt{$i}) ) {
                                $result .= $txt{$i};
                                continue;
                        }

                        // colorize one character
                        $result .= sprintf('<span style="color: #%02x%02x%02x">%s</span>', $r, $g, $b, $txt{$i});

                        // next color
                        $r += $rd;
                        $g += $gd;
                        $b += $bd;
                }

                return $result;
        }
?>