www.RoBorg.co.uk - Javascript and PHP Projects :: Colours (or colors) with PHP

Colours (or colors) in PHP

This is a small class to help you use colours with GD in PHP. It allows you to mix RGB and HSL easily so you can create cool pictures like these (or do something useful):

Colour block     Colour Wheel

What are RGB and HSL?

RBG stands for "Red, Green, Blue", while HSL stands for "Hue, Saturation, Luminance". They're both ways of describing colours. Most computer systems, including PHP's GD functions use RGB, because that's the way monitors work. However, some operation like "make this colour lighter" are hard to do in RGB. HSL is a more natural colour space - lightening a colour is very easy, you just change the luminance! For more information on RGB and HSL, see the Wikipedia category on Colour Spaces



PHP Code

You are free to modify and use this code, but please link back to http://www.roborg.co.uk if you do.

<?php
header
("Content-type: text/html");

/*
* "HSL<->RGB provided by Bob Powell. http://www.bobpowell.net" 
*
* Usage Examples:
* $c = new Colour();
* $c->setHTMLColour("#FF0000");  //Set to red
* $c->setB(1);                   //$c->getHTMLColour() is now #FF00FF
* $c->setL($c->l * 0.5);         //Set luminance to 50% of its current value
* print($c->getHTMLColour());
*/

//Start the class
class Colour
{
    
//Declare some properties
    
var $r;  //Red
    
var $g;  //Green
    
var $b;  //Blue
    
    
var $h;  //Hue
    
var $s;  //Saturation
    
var $l;  //Luminance
    
    
var $a;  //Alpha (transparency)
    
    
function Colour()
    {
        
//Initialise all the values to 0
        //They all range between 0 and 1
        
$this->0;
        
$this->0;
        
$this->0;
        
        
$this->0;
        
$this->0;
        
$this->0;
        
        
$this->0;
    }
    
    
//Set the Hue (0-1)
    
function setH($h)
    {
        
$h max(0min(1$h));
        
$this->$h;
        
$this->updateRGB();
    }
    
    
//Set the Saturation (0-1)
    
function setS($s)
    {
        
$s max(0min(1$s));
        
$this->$s;
        
$this->updateRGB();
    }
    
    
//Set the Luminance (0-1)
    
function setL($l)
    {
        
$l max(0min(1$l));
        
$this->$l;
        
$this->updateRGB();
    }
    
    
//Set the Red value (0-1)
    
function setR($r)
    {
        
$r max(0min(1$r));
        
$this->$r;
        
$this->updateHSL();
    }
    
    
//Set the Green value (0-1)
    
function setG($g)
    {
        
$g max(0min(1$g));
        
$this->$g;
        
$this->updateHSL();
    }
    
    
//Set the Blue value (0-1)
    
function setB($b)
    {
        
$b max(0min(1$b));
        
$this->$b;
        
$this->updateHSL();
    }
    
    
//Update the r, g, b (if you change h, s or l)
    
function updateRGB()
    {
        
$col $this->HSL2RGB();
        
$this->$col[0];
        
$this->$col[1];
        
$this->$col[2];
    }
    
    
//Update the h, s, l (if you change r, g, or b)
    
function updateHSL()
    {
        
$col $this->RGB2HSL();
        
$this->$col[0];
        
$this->$col[1];
        
$this->$col[2];
    }
    
    
//Convert HSL to RGB
    
function HSL2RGB()
    {
        
$r 0;
        
$g 0;
        
$b 0;
        
        if(
$this->== 0)
        {
            
$r 0;
            
$g 0;
            
$b 0;
        }
        elseif(
$this->== 0)
        {
            
$r $this->l;
            
$g $this->l;
            
$b $this->l;
        }
        else
        {
            
$tmp2 = ($this->l<=0.5)?$this->l*(1+$this->s):$this->l+$this->s-($this->l*$this->s);
            
$tmp1 2*$this->l-$tmp2;
            
$t3 = array($this->h+(1/3), $this->h$this->h-(1/3));
            
$clr = array(000);
            for(
$i=0$i<3$i++)
            {
                if(
$t3[$i]<0$t3[$i] += 1;
                if(
$t3[$i]>1$t3[$i] -= 1;
                
                if(
6*$t3[$i] < 1$clr[$i] = $tmp1 + ($tmp2-$tmp1)*$t3[$i]*6;
                elseif(
2*$t3[$i] < 1$clr[$i] = $tmp2;
                elseif(
3*$t3[$i] < 2$clr[$i] = ($tmp1 + ($tmp2-$tmp1) * ((2/3)-$t3[$i])*6);
                else 
$clr[$i] = $tmp1;
            }
            
            
$r $clr[0];
            
$g $clr[1];
            
$b $clr[2];
        }
        
        return array(
$r$g$b);
    }
    
    
//Convert RGB to HSL
    
function RGB2HSL()
    {
        
$min min(array($this->r$this->g$this->b));
        
$max max(array($this->r$this->g$this->b));
        
        
$deltamax $max $min;
        
$summax $max $min;
        
        
$L $summax 2;
        
        if(
$deltamax == 0)
        {
            
$H 0;
            
$S 0;
        }
        else
        {
            if((
$L 2) < 1
                
$S $deltamax $summax;
            else
                
$S $deltamax / ($summax);
            
            
$deltaR = ((($max $this->r) / 6.0) + ($deltamax 2.0)) / $deltamax;
            
$deltaG = ((($max $this->g) / 6.0) + ($deltamax 2.0)) / $deltamax;
            
$deltaB = ((($max $this->b) / 6.0) + ($deltamax 2.0)) / $deltamax;
            
            if (
$this->== $max)
                
$H $deltaB $deltaG;
            else if (
$this->== $max)
                
$H = (3.0) + $deltaR $deltaB;
            else if (
$this->== $max)
                
$H = (3.0) + $deltaG $deltaR;
            
            if(
$H 0$H++;
            elseif(
$H 1$H--;
        }
        
        return array(
$H$S$L);
    }
    
    
//Allocate the colour on an image
    
function allocate(&$img)
    {
        if(
$this->a) return imagecolorallocatealpha($img255*$this->r255*$this->g255*$this->b127*$this->a);
        else return 
imagecolorallocate($img255*$this->r255*$this->g255*$this->b);
    }
    
    
//Get an HTML hex colour (e.g. #FF0000)
    
function getHTMLColour()
    {
        
$str "#";
        
$str .= str_pad(dechex(255*$this->r), 2"0"STR_PAD_LEFT);
        
$str .= str_pad(dechex(255*$this->g), 2"0"STR_PAD_LEFT);
        
$str .= str_pad(dechex(255*$this->b), 2"0"STR_PAD_LEFT);
        return 
$str;
    }
    
    
//Set the values using an HTML hex colour (e.g. #FF0000 - the # is optional)
    
function setHTMLColour($str)
    {
        
$str str_replace("#"""$str);
        
$this->setR(hexdec($str{0}.$str{1})/255);
        
$this->setG(hexdec($str{2}.$str{3})/255);
        
$this->setB(hexdec($str{4}.$str{5})/255);
    }
}


//======================================================
//The rest of this file is an example of using the class
//======================================================

//Create a new image of this size with a white background
$width 200;
$height 200;

$im imagecreatetruecolor($width$height);
$bg imagecolorallocate($im255255255);
imagefilledrectangle($im00$width-1$height-1$bg);

//Create a new Colour object
$c = new Colour();

$halfWidth $width 2;
$halfHeight $height 2;

//Fill in the blank image

//This creates the wheel image
for($y=0$y<$height$y++)
{
    for(
$x=0$x<$width$x++)
    {
        
$pxX $x $halfWidth;
        
$pxY $y $halfHeight;
        
$r sqrt(($pxX $pxX) + ($pxY $pxY)); //Radius r
        
        
if($r $halfWidth)
            continue;
        
        
$t atan2($pxY$pxX); //Angle theta
        
        //Set the colour of the Colour object
        //Do it this way instead of $c->setH(...); $c->setS(...); $c->setL(...); because that 
        // would call updateRGB 3 times, when we only need to call it once
        
$c->$t / (M_PI); //The hue changes as you go around the wheel
        
$c->= ($r $width);   //The luminance decreases from the middle of the wheel to the outside
        
$c->0.5;
        
$c->updateRGB();
        
        
//Allocate the colour onto the image
        
$col $c->allocate($im);
        
        
//Fill in the pixel
        
imagesetpixel($im$x$y$col);
    }
}

/*
//This creates the block image
for($y=0; $y<$height; $y++)
{
    for($x=0; $x<$width; $x++)
    {
        $c->s = 1;
        $c->l = 1 - ($y / $height);
        $c->h = $x / $width;
        
        $c->updateRGB();
        $col = $c->allocate($im);
        imagesetpixel($im, $x, $y, $col);
    }
}
*/

imagepng($im"temp.png");
header("Content-type: image/png");
imagepng($im);
?>

This is a graphing script that I made using this colour class.
PHP Graph