Read color information from a certain position

@param  \Intervention\Image\Image $image
@return boolean
325291
public function execute($image)
    {
        $x = $this->argument(0)->type('digit')->required()->value();
        $y = $this->argument(1)->type('digit')->required()->value();
        $format = $this->argument(2)->type('string')->value('array');

        // pick color
        $color = imagecolorat($image->getCore(), $x, $y);

        if ( ! imageistruecolor($image->getCore())) {
            $color = imagecolorsforindex($image->getCore(), $color);
            $color['alpha'] = round(1 - $color['alpha'] / 127, 2);
        }

        $color = new Color($color);

        // format to output
        $this->setOutput($color->format($format));

        return true;
    }
477185
public function run(Image $image)
    {
        $coordinates = $this->getCoordinates($image);

        if ($coordinates) {
            $coordinates = $this->limitToImageBoundaries($image, $coordinates);

            $image->crop(
                $coordinates[0],
                $coordinates[1],
                $coordinates[2],
                $coordinates[3]
            );
        }

        return $image;
    }
793950
public function runSepiaFilter(Image $image)
    {
        $image->greyscale();
        $image->brightness(-10);
        $image->contrast(10);
        $image->colorize(38, 27, 12);
        $image->brightness(-10);
        $image->contrast(10);

        return $image;
    }
333270
public function run(Image $image)
    {
        $brightness = $this->getBrightness();

        if ($brightness !== null) {
            $image->brightness($brightness);
        }

        return $image;
    }
1319943
protected function postEffect($image)
    {
        if (!function_exists('imagefilter')) {
            return;
        }

        if ($this->backgroundColor != null || $this->textColor != null) {
            return;
        }

        // Negate ?
        if ($this->rand(0, 1) == 0) {
            imagefilter($image, IMG_FILTER_NEGATE);
        }

        // Edge ?
        if ($this->rand(0, 10) == 0) {
            imagefilter($image, IMG_FILTER_EDGEDETECT);
        }

        // Contrast
        imagefilter($image, IMG_FILTER_CONTRAST, $this->rand(-50, 10));

        // Colorize
        if ($this->rand(0, 5) == 0) {
            imagefilter($image, IMG_FILTER_COLORIZE, $this->rand(-80, 50), $this->rand(-80, 50), $this->rand(-80, 50));
        }
    }
298249
public function execute($image)
    {
        $size = $this->argument(0)->type('digit')->value(10);

        return imagefilter($image->getCore(), IMG_FILTER_PIXELATE, $size, true);
    }
300418
protected function randomColor($image)
    {
        if($this->ismono) {
            return imagecolorallocatealpha($image, $this->monocolor[0], $this->monocolor[1], $this->monocolor[2], $this->rand(0, 96));
        }
        return imagecolorallocate($image, $this->rand(0, 255), $this->rand(0, 255), $this->rand(0, 255));
    }
1350616
public function getPixel($image, $x, $y)
    {
        return imagecolorsforindex($image, imagecolorat($image, $x, $y));
    }
944762
public function execute($image)
    {
        $count = $this->argument(0)->value();
        $matte = $this->argument(1)->value();

        // get current image size
        $size = $image->getSize();

        // create empty canvas
        $resource = imagecreatetruecolor($size->width, $size->height);

        // define matte
        if (is_null($matte)) {
            $matte = imagecolorallocatealpha($resource, 255, 255, 255, 127);
        } else {
            $matte = $image->getDriver()->parseColor($matte)->getInt();
        }

        // fill with matte and copy original image
        imagefill($resource, 0, 0, $matte);

        // set transparency
        imagecolortransparent($resource, $matte);

        // copy original image
        imagecopy($resource, $image->getCore(), 0, 0, 0, 0, $size->width, $size->height);

        if (is_numeric($count) && $count <= 256) {
            // decrease colors
            imagetruecolortopalette($resource, true, $count);
        }

        // set new resource
        $image->setCore($resource);

        return true;
    }
910628
public function apply($image)
    {

        // Localize vars
        $width  = $image->getWidth();
        $height = $image->getHeight();
        $old     = $image->getCore();

        $pixels = array();
        $new    = imagecreatetruecolor($width, $height);
        for ($y = 0; $y < $height; $y++) {
            for ($x = 0; $x < $width; $x++) {
                // row 0
                if ($x > 0 and $y > 0) {
                    $matrix[0][0] = $this->getColor($old, $pixels,$x - 1, $y - 1);
                } else {
                    $matrix[0][0] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($y > 0) {
                    $matrix[1][0] = $this->getColor($old, $pixels, $x, $y - 1);
                } else {
                    $matrix[1][0] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($x + 1 < $width and $y > 0) {
                    $matrix[2][0] = $this->getColor($old, $pixels, $x + 1, $y - 1);
                } else {
                    $matrix[2][0] = $this->getColor($old, $pixels, $x, $y);
                }

                // row 1
                if ($x > 0) {
                    $matrix[0][1] = $this->getColor($old, $pixels, $x - 1, $y);
                } else {
                    $matrix[0][1] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($x + 1 < $width) {
                    $matrix[2][1] = $this->getColor($old, $pixels, $x + 1, $y);
                } else {
                    $matrix[2][1] = $this->getColor($old, $pixels, $x, $y);
                }

                // row 1
                if ($x > 0 and $y + 1 < $height) {
                    $matrix[0][2] = $this->getColor($old, $pixels, $x - 1, $y + 1);
                } else {
                    $matrix[0][2] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($y + 1 < $height) {
                    $matrix[1][2] = $this->getColor($old, $pixels, $x, $y + 1);
                } else {
                    $matrix[1][2] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($x + 1 < $width and $y + 1 < $height) {
                    $matrix[2][2] = $this->getColor($old, $pixels, $x + 1, $y + 1);
                } else {
                    $matrix[2][2] = $this->getColor($old, $pixels, $x, $y);
                }

                $edge = $this->convolve($matrix);
                $edge = intval($edge / 2);
                if ($edge > 255) {
                    $edge = 255;
                }
                $color = imagecolorallocate($new, $edge, $edge, $edge);
                imagesetpixel($new, $x, $y, $color);

            }
        }
        imagedestroy($old); // Free resource
        // Create and return new image with updated core
        return new Image(
            $new,
            $image->getImageFile(),
            $width,
            $height,
            $image->getType()
        );
    }
79416
public function getInfo($image)
    {
        if (DriverInterface::IMAGE_STORAGE_STRING == $this->getImageStorage($image)) {
            $image = 'data://application/octet-stream;base64,'  . base64_encode($image);
        }
        return getimagesize($image);
    }
535913
public function execute($image)
    {
        $filling = $this->argument(0)->value();
        $x = $this->argument(1)->type('digit')->value();
        $y = $this->argument(2)->type('digit')->value();

        $width = $image->getWidth();
        $height = $image->getHeight();
        $resource = $image->getCore();

        try {

            // set image tile filling
            $source = new Decoder;
            $tile = $source->init($filling);
            imagesettile($image->getCore(), $tile->getCore());
            $filling = IMG_COLOR_TILED;

        } catch (\Hail\Image\Exception\NotReadableException $e) {

            // set solid color filling
            $color = new Color($filling);
            $filling = $color->getInt();
        }

        imagealphablending($resource, true);

        if (is_int($x) && is_int($y)) {

            // resource should be visible through transparency
            $base = $image->getDriver()->newImage($width, $height)->getCore();
            imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);

            // floodfill if exact position is defined
            imagefill($resource, $x, $y, $filling);

            // copy filled original over base
            imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);

            // set base as new resource-core
            $image->setCore($base);
            imagedestroy($resource);

        } else {
            // fill whole image otherwise
            imagefilledrectangle($resource, 0, 0, $width - 1, $height - 1, $filling);
        }

        isset($tile) ? imagedestroy($tile->getCore()) : null;

        return true;
    }
284169
public function run(Image $image)
    {
        $contrast = $this->getContrast();

        if ($contrast !== null) {
            $image->contrast($contrast);
        }

        return $image;
    }
163232
private function _computeResolution($image)
    {
        $this->res = "MED";
        if(preg_match("/HiRes/i", $image))
            $this->res = "HIGH";
        elseif (preg_match("/LowRes/i", $image))
            $this->res = "LOW";
        elseif (preg_match("/Thumb/i", $image))
            $this->res = "THUMB";
        elseif (preg_match("/MediumRes/i", $image))
            $this->res = "MED";
    }
496615
public function getImageCaption($image): ?string
    {
        if (null === ($file = $this->container->get('huh.utils.file')->getFileFromUuid($image))) {
            return null;
        }

        $meta = StringUtil::deserialize($file->getModel()->meta, true);

        if (!isset($meta[$GLOBALS['TL_LANGUAGE']]['caption'])) {
            return null;
        }

        return $meta[$GLOBALS['TL_LANGUAGE']]['caption'];
    }
396527
public function applyToImage(Image $image, $x = 0, $y = 0)
    {
        $background = new Color($this->background);
        imagefilledrectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $background->getInt());

        if ($this->hasBorder()) {
            $border_color = new Color($this->border_color);
            imagesetthickness($image->getCore(), $this->border_width);
            imagerectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $border_color->getInt());
        }

        return true;
    }
681661
private function diffusion( $image ){
        $pixels = array();

        // Localize vars
        $width = $image->getWidth();
        $height = $image->getHeight();

        // Loop using image1
        $pixelIterator = $image->getCore()->getPixelIterator();
        foreach ($pixelIterator as $y => $rows) { /* Loop through pixel rows */
            foreach ( $rows as $x => $px ) { /* Loop through the pixels in the row (columns) */
                /**
                 * @var $px \ImagickPixel */
                $rgba = $px->getColor();

                $gray = round($rgba['r'] * 0.3 + $rgba['g'] * 0.59 + $rgba['b'] * 0.11);

                if(isset($pixels[$x][$y])){ // Add errors to color if there are
                    $gray += $pixels[$x][$y];
                }

                if ( $gray <= 127 ) { // Determine if black or white. Also has the benefit of clipping excess val due to adding the error
                    $blackOrWhite = 0;
                } else {
                    $blackOrWhite = 255;
                }

                $oldPixel = $gray;
                $newPixel = $blackOrWhite;

                // Current pixel
                $px->setColor("rgb($newPixel,$newPixel,$newPixel)");

                $qError = $oldPixel - $newPixel; // Quantization error

                // Propagate error on neighbor pixels
                if ( $x + 1 < $width ) {
                    $pixels[$x+1][$y] = (isset($pixels[$x+1][$y]) ? $pixels[$x+1][$y] : 0) + ($qError * (7 / 16));
                }

                if ( $x - 1 > 0 and $y + 1 < $height ) {
                    $pixels[$x-1][$y+1] = (isset($pixels[$x-1][$y+1]) ? $pixels[$x-1][$y+1] : 0) + ($qError * (3 / 16));
                }

                if ( $y + 1 < $height ) {
                    $pixels[$x][$y+1] = (isset($pixels[$x][$y+1]) ? $pixels[$x][$y+1] : 0) + ($qError * (5 / 16));
                }

                if ( $x + 1 < $width and $y + 1 < $height ) {
                    $pixels[$x+1][$y+1] = (isset($pixels[$x+1][$y+1]) ? $pixels[$x+1][$y+1] : 0) + ($qError * (1 / 16));
                }

            }
            $pixelIterator->syncIterator(); /* Sync the iterator, this is important to do on each iteration */
        }

        $type = $image->getType();
        $file = $image->getImageFile();
        $image = $image->getCore();

        return new Image( $image, $file, $width, $height, $type ); // Create new image with updated core

    }