<?php
 
/* PIMG module: represents a single pixel from the image */
 
class pimg_pixel
 
{
 
    /* Resources */
 
    private $pimg;
 
    private $x;
 
    private $y;
 
    private $color;
 
    
 
    // PIMG constructor
 
    function __construct($pimg)
 
    {
 
        $this -> pimg = $pimg;
 
    }
 
    
 
    
 
    
 
    /*
 
        Sets pixel properties
 
        @param: $x - the x position in the image
 
        @param: $y - the y position in the image
 
        @param: $color - a valid pimg_color resource
 
    */
 
    function init($x, $y, $color)
 
    {
 
        /* INPUT VALIDATORS */
 
        if ($x < 0)
 
        {
 
            $x = 0;
 
            $this -> pimg -> setDebug('X position must be >= 0, using 0 as default', 'notice', __CLASS__);
 
        }
 
        if ($y < 0)
 
        {
 
            $y = 0;
 
            $this -> pimg -> setDebug('Y position must be >= 0, using 0 as default', 'notice', __CLASS__);
 
        }
 
        if (!$color instanceOf pimg_color)
 
            $this -> pimg -> setDebug('Passed color is not a valid pimg_color instance', 'error', __CLASS__);
 
        
 
        
 
        // Set pixel properties
 
        $this -> x        = $x;
 
        $this -> y        = $y;
 
        $this -> color    = $color;
 
        
 
        // Return an instance of the class
 
        return $this;
 
    }
 
    
 
    
 
    
 
    /*
 
        If the user wants to view the pixel info for debugging this method will return a string representation of it
 
        @result: string info
 
    */
 
    function __toString()
 
    {
 
        return 'x: ' . $this -> x . ', y: ' . $this -> y . ', color: [ ' . $this -> color . ' ]';
 
    }
 
    
 
    
 
    
 
    /*
 
        Sets or gets the x value
 
        @param: $x
 
        @result: hande of the class or x value
 
    */
 
    public function x($x = null)
 
    {
 
        if (isset($x))
 
        {
 
            $this -> x = $x;
 
            return $this;
 
        }
 
        else
 
            return $this -> x;
 
    }
 
    
 
    
 
    
 
    /*
 
        Sets or gets the y value
 
        @param: $y
 
        @result: hande of the class or y value
 
    */
 
    public function y($y = null)
 
    {
 
        if (isset($y))
 
        {
 
            $this -> y = $y;
 
            return $this;
 
        }
 
        else
 
            return $this -> y;
 
    }
 
    
 
    
 
    
 
    /*
 
        Sets or gets the color value
 
        @param: $color
 
        @result: hande of the class or hande of pimg_color representing pixel's color
 
    */
 
    public function color($color = null)
 
    {
 
        if (isset($color))
 
        {
 
            $this -> color = $color;
 
            return $this;
 
        }
 
        else
 
            return $this -> color;
 
    }
 
    
 
    
 
    
 
    /*
 
        Draws the pixel to the parent pimg image or the given pimg image
 
        @result: handle of the pimg class
 
    */
 
    public function draw($pimg = null)
 
    {
 
        // Draw the pixel
 
        $pimg = (empty($pimg)) ? $this -> pimg : $pimg;
 
        imagesetpixel($pimg -> handle(), $this -> x, $this -> y, $this -> color -> allocate());
 
        
 
        // Return handle of the class
 
        return $pimg;
 
    }
 
}
 
?>
 
 |