PHP Classes

File: color.pimg.php

Recommend this page to a friend!
  Classes of Tony Bogdanov   PIMG   color.pimg.php   Download  
File: color.pimg.php
Role: Class source
Content type: text/plain
Description: Class for handling colors
Class: PIMG
Process images using multiple operations
Author: By
Last change: 1.1
Date: 13 years ago
Size: 4,695 bytes
 

Contents

Class file image Download
<?php
# NEW STUFF, NOT DOCUMENTED

/* PIMG module: handles colors */
class pimg_color
{
   
/* Resources */
   
private $pimg;
    private
$red = 0;
    private
$green = 0;
    private
$blue = 0;
    private
$alpha = 0;
   
   
   
   
// PIMG constructor
   
function __construct($pimg)
    {
       
$this -> pimg = $pimg;
    }
   
   
   
   
/*
        Creates a color
        @param: $red - the red RGBA amount (0 - 255)
        @param: $green - the green RGBA amount (0 - 255)
        @param: $blue - the blue RGBA amount (0 - 255)
        @param: $alpha - the alpha RGBA amount (0 - 127)
        @result: pimg_color resource
    */
   
function init($red = null, $green = null, $blue = null, $alpha = null)
    {
       
/* INPUT VALIDATORS */
       
$red = (int) $red;
        if (
$red < 0 || $red > 255)
        {
           
$red = abs($red) % 255;
           
$this -> pimg -> setDebug('Red channel must be 0-255, using closest value of ' . $red, 'notice', __CLASS__);
        }
       
$green = (int) $green;
        if (
$green < 0 || $green > 255)
        {
           
$green = abs($green) % 255;
           
$this -> pimg -> setDebug('Green channel must be 0-255, using closest value of ' . $green, 'notice', __CLASS__);
        }
       
$blue = (int) $blue;
        if (
$blue < 0 || $blue > 255)
        {
           
$blue = abs($blue) % 255;
           
$this -> pimg -> setDebug('Blue channel must be 0-255, using closest value of ' . $blue, 'notice', __CLASS__);
        }
       
$alpha = (int) $alpha;
        if (
$alpha < 0 || $alpha > 127)
        {
           
$alpha = abs($alpha) % 127;
           
$this -> pimg -> setDebug('Alpha channel must be 0-127, using closest value of ' . $alpha, 'notice', __CLASS__);
        }
       
       
// Get colors
       
$this -> red = $red;
       
$this -> green = $green;
       
$this -> blue = $blue;
       
$this -> alpha = $alpha;
       
       
// Return an instance of the class
       
return $this;
    }
   
   
   
   
/*
        If the user wants to view the color for debugging this method will return a string representation of the RGBA color
        @result: string color
    */
   
function __toString()
    {
        return
'red: ' . $this -> red . ', green: ' . $this -> green . ', blue: ' . $this -> blue . ', alpha: ' . $this -> alpha;
    }
   
   
   
   
/*
        Sets or gets the red color value
        @param: $red
        @result: color handle or red value
    */
   
public function red($red = null)
    {
        if (isset(
$red))
        {
           
$this -> red = $red;
            return
$this;
        }
        else
            return
$this -> red;
    }
   
   
   
   
/*
        Sets or gets the green color value
        @param: $green
        @result: color handle or green value
    */
   
public function green($green = null)
    {
        if (isset(
$green))
        {
           
$this -> green = $green;
            return
$this;
        }
        else
            return
$this -> green;
    }
   
   
   
   
/*
        Sets or gets the blue color value
        @param: $blue
        @result: color handle or blue value
    */
   
public function blue($blue = null)
    {
        if (isset(
$blue))
        {
           
$this -> blue = $blue;
            return
$this;
        }
        else
            return
$this -> blue;
    }
   
   
   
   
/*
        Sets or gets the alpha color value
        @param: $alpha
        @result: color handle or alpha value
    */
   
public function alpha($alpha = null)
    {
        if (isset(
$alpha))
        {
           
$this -> alpha = $alpha;
            return
$this;
        }
        else
            return
$this -> alpha;
    }
   
   
   
   
/*
        Allocates the color for the parent PIMG
        @result: valid allocated image color
    */
   
public function allocate()
    {
        return
imagecolorallocatealpha($this -> pimg -> handle(), $this -> red, $this -> green, $this -> blue, $this -> alpha);
    }
   
   
   
   
/*
        Mixes two or more colors and sets the current to the result
        @param: $colors - an array with arrays containing a valid pimg_color color as first and amount if mixing as second argument
        @result: pimg_color resource
    */
   
public function mix($colors)
    {
       
// Reset the color to black
       
$this -> red = $this -> green = $this -> blue = $this -> alpha = 0;
       
$totalAmount = 0;
       
       
// Go through all colors
       
if (is_array($colors))
        {
            foreach(
$colors as $color)
            {
               
$this -> red += $color[0] -> red() * $color[1] / 100;
               
$this -> green += $color[0] -> green() * $color[1] / 100;
               
$this -> blue += $color[0] -> blue() * $color[1] / 100;
               
$this -> alpha += $color[0] -> alpha() * $color[1] / 100;
               
$totalAmount += $color[1];
            }
           
            if (
$totalAmount <> 100)
               
$this -> pimg -> setDebug('The total color mix amount is <b>' . $totalAmount . '</b>, but should be exactly <b>100</b>!', 'error', __CLASS__);
           
           
$this -> red = round($this -> red);
           
$this -> green = round($this -> green);
           
$this -> blue = round($this -> blue);
           
$this -> alpha = round($this -> alpha);
           
           
// Return a color class instance
           
return $this;
        } else
           
$this -> pimg -> setDebug('There are no valid colors to mix', 'error', __CLASS__);
    }
}
?>