@param resource $image
@param int|null $backgroundColor

@return Palette

@throws \InvalidArgumentException
184407
public static function fromGD($image, $backgroundColor = null)
    {
        if (!is_resource($image) || get_resource_type($image) != 'gd') {
            throw new \InvalidArgumentException('Image must be a gd resource');
        }
        if ($backgroundColor !== null && (!is_numeric($backgroundColor) || $backgroundColor < 0 || $backgroundColor > 16777215)) {
            throw new \InvalidArgumentException(sprintf('"%s" does not represent a valid color', $backgroundColor));
        }

        $palette = new self();

        $areColorsIndexed = !imageistruecolor($image);
        $imageWidth = imagesx($image);
        $imageHeight = imagesy($image);
        $palette->colors = [];

        $backgroundColorRed = ($backgroundColor >> 16) & 0xFF;
        $backgroundColorGreen = ($backgroundColor >> 8) & 0xFF;
        $backgroundColorBlue = $backgroundColor & 0xFF;

        for ($x = 0; $x < $imageWidth; ++$x) {
            for ($y = 0; $y < $imageHeight; ++$y) {
                $color = imagecolorat($image, $x, $y);
                if ($areColorsIndexed) {
                    $colorComponents = imagecolorsforindex($image, $color);
                    $color = ($colorComponents['alpha'] * 16777216) +
                             ($colorComponents['red'] * 65536) +
                             ($colorComponents['green'] * 256) +
                             ($colorComponents['blue']);
                }

                if ($alpha = $color >> 24) {
                    if ($backgroundColor === null) {
                        continue;
                    }

                    $alpha /= 127;
                    $color = (int) (($color >> 16 & 0xFF) * (1 - $alpha) + $backgroundColorRed * $alpha) * 65536 +
                             (int) (($color >> 8 & 0xFF) * (1 - $alpha) + $backgroundColorGreen * $alpha) * 256 +
                             (int) (($color & 0xFF) * (1 - $alpha) + $backgroundColorBlue * $alpha);
                }

                isset($palette->colors[$color]) ?
                    $palette->colors[$color] += 1 :
                    $palette->colors[$color] = 1;
            }
        }

        arsort($palette->colors);

        return $palette;
    }
1151886
protected function background($color = null)
    {
        if (!is_null($color)) {

            $this->background = $this->getColorID($color);
            extract($this->getBackgroundCoordinates($this->getGravity()));

            imagefill($this->resource, $x1, $y1, $this->background);
            imagefill($this->resource, $x2, $x2, $this->background);
        }

        return $this;
    }
20620
public function match($sourceImage) : GD
    {
        if( ! is_resource($sourceImage) )
        {
            throw new InvalidArgumentException(NULL, '[resource]');
        }

        imagecolormatch($this->canvas, $sourceImage);

        return $this;
    }
376426
public function rotate($angle, $backgroundColor = -1) {
		if($backgroundColor === -1) {
			$backgroundColor = hexdec($this->getBackgroundColor()->getHexValue());
		}
		$this->image = imagerotate($this->image, $angle, (int) $backgroundColor);
		$this->getSize();
		return $this;
	}
489578
protected static function ensureImageInterfaceInstance($image)
    {
        if ($image instanceof ImageInterface) {
            return $image;
        }

        if (is_resource($image)) {
            return static::getImagine()->read($image);
        }

        if (is_string($image)) {
            return static::getImagine()->open(Yii::getAlias($image));
        }

        throw new InvalidParamException('File should be either ImageInterface, resource or a string containing file path.');
    }
787673
public static function getBackgroundCode($backgroundColour)
    {
        if (!isset(self::$background_colours[$backgroundColour])) {
            throw new \Exception('Invalid background colour.');
        }
        return "\033[" . self::$background_colours[$backgroundColour] . 'm';
    }
896882
public static function mergeStyleAndBackground( $background, $style )
    {
        $style = trim( $style, '; ' ) . "; background-image:url({$background});";
        $style = trim( $style, '; ' );

        return $style;
    }
864996
public function _roundedCorners(int $radius, string $background_color, int $aa_level = 4)
    {
        if ($aa_level > 1) {
            $radius *= $aa_level;
        }

        $ih_corner = imagecreatetruecolor($radius, $radius);

        $r_dec = hexdec($background_color[0] . $background_color[1]);
        $g_dec = hexdec($background_color[2] . $background_color[3]);
        $b_dec = hexdec($background_color[4] . $background_color[5]);

        $cr_clear = imagecolorallocate($ih_corner, ($r_dec ? $r_dec - 1 : 1), ($g_dec ? $g_dec - 1 : 1), ($b_dec ? $b_dec - 1 : 1));
        $cr_solid = imagecolorallocate($ih_corner, $r_dec, $g_dec, $b_dec);
        imagecolortransparent($ih_corner, $cr_clear);

        imagefill($ih_corner, 0, 0, $cr_solid);
        imagefilledellipse($ih_corner, $radius, $radius, $radius * 2, $radius * 2, $cr_clear);

        if ($aa_level > 1) {
            $new_radius = $radius / $aa_level;
            $res = imagecreatetruecolor($new_radius, $new_radius);

            if ($this->image_type == 3) {
                imagealphablending($res, false);
                imagesavealpha($res, true);
            }

            imagecolortransparent($res, $cr_clear);
            imagecopyresampled($res, $ih_corner, 0, 0, 0, 0, $new_radius, $new_radius, $radius, $radius);
            imagedestroy($ih_corner);
            $ih_corner = $res;
            $radius = $new_radius;
        }

        $this->image_height = imagesy($this->image_resource);
        $this->image_width = imagesx($this->image_resource);

        imagecopymerge($this->image_resource, $ih_corner, 0, 0, 0, 0, $radius, $radius, 100);

        $ih_corner = imagerotate($ih_corner, 90, 0);
        imagecopymerge($this->image_resource, $ih_corner, 0, $this->image_height - $radius, 0, 0, $radius, $radius, 100);

        $ih_corner = imagerotate($ih_corner, 90, 0);
        imagecopymerge($this->image_resource, $ih_corner, $this->image_width - $radius, $this->image_height - $radius, 0, 0, $radius, $radius, 100);

        $ih_corner = imagerotate($ih_corner, 90, 0);
        imagecopymerge($this->image_resource, $ih_corner, $this->image_width - $radius, 0, 0, 0, $radius, $radius, 100);

        return $this;
    }
262326
public static function fromIndex($image, int $index): Color {
        if ($image instanceof Image) {
            $image = $image->getResource();
        }

        $colors = imagecolorsforindex($image, $index);

        return new Color(
            $colors['red'],
            $colors['green'],
            $colors['blue'],
            $colors['alpha']
        );
    }
275509
public function setResource( $resource )
	{
		if( !is_resource( $resource ) )
			throw new InvalidArgumentException( 'Must be a valid image resource' );
		if( $this->resource )
			imagedestroy( $this->resource );

		$this->resource	= $resource;
		$this->width	= imagesx( $resource );
		$this->height	= imagesy( $resource );

		if( function_exists( 'imageantialias' ) )
			imageantialias( $this->resource, TRUE );

		imagealphablending( $this->resource, FALSE );											//  disable alpha blending in favour to
		imagesavealpha( $this->resource, TRUE );											//  copying the complete alpha channel
	}
1192799
public function getJPEGImage($quality, $backgroundColor){

        if($this->owner->exists()) {

            $pathinfo = pathinfo($this->owner->Name);

            $cacheFile = $this->owner->cacheToJPEGFilename($quality, $backgroundColor, $pathinfo);

            list($width, $height, $type, $attr) = getimagesize($this->owner->getFullPath());


            if($type == IMAGETYPE_JPEG && $quality == 100) return $this->owner;

            if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) {
                // convert the current file to jpeg
                $this->owner->generateToJPEGImage($quality, $backgroundColor, $pathinfo, $type);
            }

			if($this->owner instanceof SecureImage) $cached = new SecureImage_Cached($cacheFile);
			else $cached = new Image_Cached($cacheFile);

			// Pass through the title so the templates can use it
			$cached->Title = $this->owner->Title;
			$cached->ID = $this->owner->ID;
			$cached->ParentID = $this->owner->ParentID;
			return $cached;
		}
	}
42108
public function gdResourceToTruecolor(&$resource)
    {
        $width = imagesx($resource);
        $height = imagesy($resource);

        // new canvas
        $canvas = imagecreatetruecolor($width, $height);

        // fill with transparent color
        imagealphablending($canvas, false);
        $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
        imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent);
        imagecolortransparent($canvas, $transparent);
        imagealphablending($canvas, true);

        // copy original
        imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height);
        imagedestroy($resource);

        $resource = $canvas;

        return 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));
    }
383332
function set_background_image($val)
    {
        //see __set and __get, on all assignments clear cache, not needed on direct set through __set
        $this->_prop_cache["background_image"] = null;
        $this->_props["background_image"] = $this->_image($val);
    }
236812
public function setImage($image)
    {
        if (!is_null($image) && !$image instanceof ComActivitiesActivityMedialinkInterface) {
            throw new InvalidArgumentException('Invalid image type.');
        }

        $this->image = $image;
        return $this;
    }
898717
protected function setColor(ColorInterface $color, bool $background = null): FormatterInterface
    {
        $name = $color->getName();
        $colors = $this->getColors();
        if (array_key_exists($name, $colors) === false) {
            throw new ColorNotSupported($color);
        }

        if ($background === true) {
            $this->background = $colors[$name] + 10;
        } else {
            $this->foreground = $colors[$name];
        }

        return $this;
    }
337511
private BufferedImage create_BACKGROUND_Image(final int WIDTH, BufferedImage image) {
        if (WIDTH <= 0) {
            return null;
        }
        if (image == null) {
            image = UTIL.createImage(WIDTH, WIDTH, Transparency.TRANSLUCENT);
        }
        final Graphics2D G2 = image.createGraphics();
        G2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        switch (getFrameType()) {
            case SQUARE:
                BACKGROUND_FACTORY.createLinearBackground(WIDTH, WIDTH, getBackgroundColor(), getCustomBackground(), getModel().getTextureColor(), image);
                break;
            case ROUND:

            default:
                BACKGROUND_FACTORY.createRadialBackground(WIDTH, getBackgroundColor(), getCustomBackground(), getModel().getTextureColor(), image);
                break;
        }
        G2.dispose();

        return image;
    }