These two PHP functions will allow you to create a gradient of text, without having to calculate all the colours yourself (similar to my user title)
PHP Code:
function gradientComposite( $text, $minr, $ming, $minb, $maxr, $maxg, $maxb) {
// check given values are correct
if( $minr < 0 || $minr > 255) trigger_error( "Given minimal Red value is out of range" E_USER_WARNING);
if( $ming < 0 || $ming > 255) trigger_error( "Given minimal Green value is out of range" E_USER_WARNING);
if( $minb < 0 || $minb > 255) trigger_error( "Given minimal Blue value is out of range" E_USER_WARNING);
if( $maxr < 0 || $maxr > 255) trigger_error( "Given maximal Red value is out of range" E_USER_WARNING);
if( $maxg < 0 || $maxg > 255) trigger_error( "Given maximal Green value is out of range" E_USER_WARNING);
if( $maxb < 0 || $maxb > 255) trigger_error( "Given maximal Blue value is out of range" E_USER_WARNING);
// get length of $text
$length = strlen( $text);
// calculate range of each component
$difr = $maxr-$minr;
$difg = $maxg-$ming;
$difb = $maxb-$minb;
// calculate step for each component
$stepr = $difr/($length-1);
$stepg = $difg/($length-1);
$stepb = $difb/($length-1);
// process each character
$return = "";
for( $i=0; $i<$length; $i++) {
// calculate components for this character
$thisr = sprintf( "%02s", dechex( floor( $minr+$stepr*$i)));
$thisg = sprintf( "%02s", dechex( floor( $ming+$stepg*$i)));
$thisb = sprintf( "%02s", dechex( floor( $minb+$stepb*$i)));
$thiscol = $thisr.$thisg.$thisb;
// append new SPAN to $return
$return .= "<span style=\"color: #{$thiscol}\">".$text[$i]."</span>";
}
// return gradiented string
return $return;
}
This function takes seven arguments:
string $text: The string to be parsed
int $minr: The red component of the left side (0-255)
int $ming: The green component of the left side (0-255)
int $minb: The blue component of the left side (0-255)
int $maxr: The red component of the right side (0-255)
int $maxg: The green component of the right side (0-255)
int $maxb: The blue component of the right side (0-255)
A string is returned containing the HTML code required for the gradient. The code is HTML5, compatible HTML4.01 Strict and Transitional.
PHP Code:
function gradientHue( $text, $minh, $maxh) {
// check given values are correct
if( $minh < 0 || $minh > 360) trigger_error( "Given minimal Hue value is out of range", E_USER_WARNING);
if( $maxh < 0 || $maxh > 360) trigger_error( "Given maximal Hue value is out of range", E_USER_WARNING);
// get length of text
$length = strlen( $text);
// calculate hue range
$dif = $maxh-$minh;
// calculate step
$step = $dif/($length-1);
// process each character
$return = "";
for( $i=0; $i<$length; $i++) {
// calculate hue for this character
$thish = floor( $minh+$step*$i);
// calculate rgb from hue
if( $thish <= 60) {
$thisr = 255;
$thisg = 255*$thish/60;
$thisb = 0;
}
if( $thish > 60 && $thish <= 120) {
$thisr = 255*(120-$thish)/60;
$thisg = 255;
$thisb = 0;
}
if( $thish > 120 && $thish <= 180) {
$thisr = 0;
$thisg = 255;
$thisb = 255*($thish-120)/60;
}
if( $thish > 180 && $thish <= 240) {
$thisr = 0;
$thisg = 255*(240-$thish)/60;
$thisb = 255;
}
if( $thish > 240 && $thish <= 300) {
$thisr = 255*($thish-240)/60;
$thisg = 0;
$thisb = 255;
}
if( $thish > 300) {
$thisr = 255;
$thisg = 0;
$thisb = 255*(360-$thish)/60;
}
// to hex
$thisr = sprintf( "%02s", dechex( $thisr));
$thisg = sprintf( "%02s", dechex( $thisg));
$thisb = sprintf( "%02s", dechex( $thisb));
$thiscol = $thisr.$thisg.$thisb;
// append new SPAN to $return
$return .= "<span style=\"color: #{$thiscol}\">".$text[$i]."</span>";
}
return $return;
}
This function takes five arguments:
string $text: The string to be parsed
int $minh: The hue of the left side (0-360)
int $maxh: The hue of the right side (0-360)
float $sat: The saturation of the text (0-1)
float $val: The value of the text (0-1)
A string is returned containing the HTML code required for the gradient. The code is HTML5, compatible HTML4.01 Strict and Transitional.
ERRATA:
- I accidentally typed $thish instead of $thisr in the second script.
- The gradient started in the wrong place.
- gradientHue() did not take the saturation and value into account. For now they are removed.
_______
Last Edited by
FreezeWarp with
Title Change on
May 1st