PokeZam Forums  

Go Back   PokeZam Forums > Off-Topic > Technology > Tutorials

Tutorials Here are technology tutorials. Feel free to post your own, but they may be taken down.

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-17-2008
Kaboom Kaboom is offline
Twilight Blaziken
 
Join Date: Aug 2007
Location: The Dark World, for Darkness is the leading power!
Age: 17
Posts: 1,028
Default PHP Graident Script

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 || $minr 255trigger_error"Given minimal Red value is out of range" E_USER_WARNING);
 if( 
$ming || $ming 255trigger_error"Given minimal Green value is out of range" E_USER_WARNING);
 if( 
$minb || $minb 255trigger_error"Given minimal Blue value is out of range" E_USER_WARNING);
 if( 
$maxr || $maxr 255trigger_error"Given maximal Red value is out of range" E_USER_WARNING);
 if( 
$maxg || $maxg 255trigger_error"Given maximal Green value is out of range" E_USER_WARNING);
 if( 
$maxb || $maxb 255trigger_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"dechexfloor$minr+$stepr*$i)));
  
$thisg sprintf"%02s"dechexfloor$ming+$stepg*$i)));
  
$thisb sprintf"%02s"dechexfloor$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 || $minh 360trigger_error"Given minimal Hue value is out of range"E_USER_WARNING);
 if( 
$maxh || $maxh 360trigger_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

Last edited by Kaboom; 06-19-2008 at 03:18 PM.
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 06:07 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Template-Modifications by TMS
© 1999-2009 PokeZam. All Rights Reserved.