Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Generate Gradient Within Hex Range In PHP

A function that generates an array of hex colors that forms a gradient, starting from a hex color and ending with another hex color. The number of gradient steps can also be defined.

On Monday, September 29th 2008 at 03:12 AM
By Andrew Pociu (View Profile)
*****   (Rated 5 with 5 votes)
Contextual Ads
More PHP Resources
Advertisement
  1. function Gradient($HexFrom, $HexTo, $ColorSteps)
  2. {
  3.         $FromRGB['r'] = hexdec(substr($HexFrom, 0, 2));
  4.         $FromRGB['g'] = hexdec(substr($HexFrom, 2, 2));
  5.         $FromRGB['b'] = hexdec(substr($HexFrom, 4, 2));
  6.        
  7.         $ToRGB['r'] = hexdec(substr($HexTo, 0, 2));
  8.         $ToRGB['g'] = hexdec(substr($HexTo, 2, 2));
  9.         $ToRGB['b'] = hexdec(substr($HexTo, 4, 2));
  10.        
  11.         $StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1);
  12.         $StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1);
  13.         $StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1);
  14.        
  15.         $GradientColors = array();
  16.        
  17.         for($i = 0; $i <= $ColorSteps; $i++)
  18.         {
  19.                 $RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i));
  20.                 $RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i));
  21.                 $RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i));
  22.                
  23.                 $HexRGB['r'] = sprintf('%02x', ($RGB['r']));
  24.                 $HexRGB['g'] = sprintf('%02x', ($RGB['g']));
  25.                 $HexRGB['b'] = sprintf('%02x', ($RGB['b']));
  26.                
  27.                 $GradientColors[] = implode(NULL, $HexRGB);
  28.         }
  29.         return $GradientColors;
  30. }
  31.  
  32. $Gradients = Gradient("FF5B5B", "FFCA5B", 32);
  33. foreach($Gradients as $Gradient)
  34. {
  35.         echo "<div style=\"background-color: #".$Gradient."; width: 100px; height: 25px;\"></div>";
  36. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by R2 on Monday, November 24th 2008 at 09:17 PM

This works perfect! Thanks a lot dude. it produces a valid html :)

by Sicle on Tuesday, July 14th 2009 at 06:31 PM

Hi,

I tryed this code but it didn't work quite well for me.

I tryed a couple of things but the height of every color would always be 25px.

So I deleted the div tag and replaced it with a table:

Replace:
#
foreach($Gradients as $Gradient)
#
{
#
echo "";
#
}

With:
echo '';

foreach($Gradients as $Gradient)

{

echo "";

}

echo '';

now the height of every gradient color can be et in the td tag, make sure the table tag has 'border-collapse:collapse;' in its style

by Will on Tuesday, April 27th 2010 at 02:12 AM

Cool beans!
minor bug:

- for($i = 0; $i

by magician glasgow on Sunday, January 1st 2012 at 11:48 AM

Hi- I am trying to figure out how to make my cd-rom drive open and or close using c (.net) could you give me a hand? Thanks

by on Tuesday, January 24th 2012 at 06:10 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by www.Grant-Applications.org on Friday, February 24th 2012 at 04:51 PM

Resources like the one you mentioned here will be very useful to me! It gives me excellent enjoyment to look at your website and delight in your excellent content here.

by YoOne on Friday, March 9th 2012 at 03:56 AM

Hi, a little update to permit multiple colors to the gradient.


function Gradient_simple($HexFrom, $HexTo, $ColorSteps) {
$HexFrom = preg_replace("/^#/", "", $HexFrom);
$HexTo = preg_replace("/^#/", "", $HexTo);

$FromRGB['r'] = hexdec(substr($HexFrom, 0, 2));
$FromRGB['g'] = hexdec(substr($HexFrom, 2, 2));
$FromRGB['b'] = hexdec(substr($HexFrom, 4, 2));

$ToRGB['r'] = hexdec(substr($HexTo, 0, 2));
$ToRGB['g'] = hexdec(substr($HexTo, 2, 2));
$ToRGB['b'] = hexdec(substr($HexTo, 4, 2));

@$StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1);
@$StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1);
@$StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1);

$GradientColors = array();

for($i = 0; $i < $ColorSteps; $i ) {
$RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i));
$RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i));
$RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i));

$HexRGB['r'] = sprintf('x', ($RGB['r']));
$HexRGB['g'] = sprintf('x', ($RGB['g']));
$HexRGB['b'] = sprintf('x', ($RGB['b']));

$GradientColors[] = '#' . implode(NULL, $HexRGB);
}
return $GradientColors;
}


function Gradient($cfg, $steps=100) {
$nb_gradients = count($cfg) -1;
$ret = array();
for($i=1; $i'#000000'),
array('hex'=>'#FF0000', 'pc'=>2/5),
array('hex'=>'#FF00FF', 'pc'=>2.3/5),
array('hex'=>'#FFFF00', 'pc'=>4/5),
array('hex'=>'#0000FF', 'pc'=>4.5/5),
array('hex'=>'#00FF00')
),
100
);

by YoOne on Friday, March 9th 2012 at 03:56 AM

Hi, a little update to permit multiple colors to the gradient.


function Gradient_simple($HexFrom, $HexTo, $ColorSteps) {
$HexFrom = preg_replace("/^#/", "", $HexFrom);
$HexTo = preg_replace("/^#/", "", $HexTo);

$FromRGB['r'] = hexdec(substr($HexFrom, 0, 2));
$FromRGB['g'] = hexdec(substr($HexFrom, 2, 2));
$FromRGB['b'] = hexdec(substr($HexFrom, 4, 2));

$ToRGB['r'] = hexdec(substr($HexTo, 0, 2));
$ToRGB['g'] = hexdec(substr($HexTo, 2, 2));
$ToRGB['b'] = hexdec(substr($HexTo, 4, 2));

@$StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1);
@$StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1);
@$StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1);

$GradientColors = array();

for($i = 0; $i < $ColorSteps; $i ) {
$RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i));
$RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i));
$RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i));

$HexRGB['r'] = sprintf('x', ($RGB['r']));
$HexRGB['g'] = sprintf('x', ($RGB['g']));
$HexRGB['b'] = sprintf('x', ($RGB['b']));

$GradientColors[] = '#' . implode(NULL, $HexRGB);
}
return $GradientColors;
}


function Gradient($cfg, $steps=100) {
$nb_gradients = count($cfg) -1;
$ret = array();
for($i=1; $i'#000000'),
array('hex'=>'#FF0000', 'pc'=>2/5),
array('hex'=>'#FF00FF', 'pc'=>2.3/5),
array('hex'=>'#FFFF00', 'pc'=>4/5),
array('hex'=>'#0000FF', 'pc'=>4.5/5),
array('hex'=>'#00FF00')
),
100
);

by this site on Tuesday, March 13th 2012 at 06:27 AM

Thanks for the blog. The blog is very helpful and informative. Keep up the good work.
this site

by raheja gurgaon on Monday, April 9th 2012 at 07:06 AM

I will be back to look at out more of your articles later!

by mortgage loans on Thursday, May 24th 2012 at 10:24 PM

Various people in all countries receive the loan from various creditors, just because that is simple.

by Pro Travel Network on Monday, June 18th 2012 at 11:48 AM

Pro Travel Network is the best travel agency in town. They have been dealing in all kind of travel from train travel to cruise travel and air travel !

by Pro Travel Network on Monday, June 18th 2012 at 11:48 AM

Pro Travel Network is the best travel agency in town. They have been dealing in all kind of travel from train travel to cruise travel and air travel !

by Pro Travel Network on Monday, June 18th 2012 at 11:48 AM

Pro Travel Network is the best travel agency in town. They have been dealing in all kind of travel from train travel to cruise travel and air travel !

by Pro Travel Network on Monday, June 18th 2012 at 11:48 AM

Pro Travel Network is the best travel agency in town. They have been dealing in all kind of travel from train travel to cruise travel and air travel !

by montreal web design on Thursday, July 19th 2012 at 10:53 AM

Now I need to compare mine with yours too. Mine here is a different code but have the same output.

by Acne Treatment Reviews on Thursday, September 6th 2012 at 07:38 AM

You have really interesting blog, keep up posting such informative posts!

by Best Acne Treatment on Thursday, September 6th 2012 at 08:05 AM

You have really interesting blog, keep up posting such informative posts!

by Acne Scar Treatment on Thursday, September 6th 2012 at 08:23 AM

You have really interesting blog, keep up posting such informative posts!

by Hemocyl on Thursday, September 6th 2012 at 10:42 AM

You have really interesting blog, keep up posting such informative posts!

by Teeth Whitening Reviews on Thursday, September 6th 2012 at 10:57 AM

You have really interesting blog, keep up posting such informative posts!

by Diet Pills on Thursday, September 6th 2012 at 12:17 PM

You have really interesting blog, keep up posting such informative posts!

by Best Diet Pills on Thursday, September 6th 2012 at 01:08 PM

You have really interesting blog, keep up posting such informative posts!

by spideroak on Tuesday, October 2nd 2012 at 02:26 AM

They have been dealing in all kind of travel from train travel to cruise travel and air travel

by on Wednesday, October 24th 2012 at 07:00 AM

They have been dealing in all kind of travel from train travel to cruise travel and air travel

by Argo on Sunday, April 21st 2013 at 09:01 PM

How can I use this to make a word display with gradient?


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources