Calculate number of colors in an image.

@param resource $im the image.

@return int
6200
private function colorsTotal($im)
    {
        if (imageistruecolor($im)) {
            $this->log("Colors as true color.");
            $h = imagesy($im);
            $w = imagesx($im);
            $c = array();
            for ($x=0; $x < $w; $x++) {
                for ($y=0; $y < $h; $y++) {
                    @$c['c'.imagecolorat($im, $x, $y)]++;
                }
            }
            return count($c);
        } else {
            $this->log("Colors as palette.");
            return imagecolorstotal($im);
        }
    }
1140748
def get_colors(image):
    
    colors = Counter(pixel for row in image for pixel in row)
    if len(colors) > 256:
        msg = (
            "The maximum number of distinct colors in a GIF is 256 but "
            "this image has {} colors and can't be encoded properly."
        )
        raise RuntimeError(msg.format(len(colors)))
    return colors
393557
final protected function _getColorAt($resource,$x,$y){
		$color=imagecolorat($resource,(int)$x,(int)$y);
		if($color===false){
			throw new Exception("Cannot get color");
		}
		if(imagecolorstotal($resource)){
			$rgba=imagecolorsforindex($resource,$color);
			if(!is_array($rgba)){
				throw new Exception("Cannot get colors for specified index");
			}
			return array($rgba['red'],$rgba['green'],$rgba['blue'],$rgba['alpha']);
		}
		else{
			return array(
				($color>>16)&0xFF,
				($color>>8)&0xFF,
				$color&0xFF,
				($color&0x7F000000)>>24
			);
		}
	}
1257963
protected function imageColor($image, string $css_color): int
    {
        return imagecolorallocate(
            $image,
            (int) hexdec(substr($css_color, 0, 2)),
            (int) hexdec(substr($css_color, 2, 2)),
            (int) hexdec(substr($css_color, 4, 2))
        );
    }
713607
function setImageColorspaceGray( $imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
    {
        $colorScale = array( 1.0, 1.0, 1.0 );
        return $this->setImageLuminanceColorScale( $imageObject, $filterData, $sourceMimeData, $destinationMimeData,
                                                   $colorScale );
    }
940805
public function colorLiteCommand(): int
    {
        if (!$this->output->supportColor()) {
            $this->write('Current terminal is not support output color text.');
            return -2;
        }

        $this->output->startBuffer();

        foreach (Color::getStyles() as $style) {
            $this->output->write(Color::render("color text(style:$style)", $style));
        }

        $this->output->flush();

        return 0;
    }
122874
protected function fontColor($i) {
        if (empty($this->configs['fontColor'])) {
            return [mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)];
        }
        if (!is_array($this->configs['fontColor'])) {
            return $this->configs['fontColor'];
        }
        if (count($this->configs['fontColor']) <= $i) {
            return [mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)];
        }
        return $this->configs['fontColor'][$i];
    }
1200312
private function colorMeanValue($resource) {
		$colorList = array();
		$colorSum = 0;
		for ($a = 0; $a<8; $a++) {
			for ($b = 0; $b<8; $b++) {
				$rgb = imagecolorat($resource, $a, $b);
				$colorList[] = $rgb & 0xFF;
				$colorSum += $rgb & 0xFF;
			}
		}
		
		return array($colorSum/64,$colorList);
	}
430946
public function draw($im) {
        $this->drawBars($im);
        $this->drawText($im, 0, 0, $this->positionX, $this->thickness);

        if ($this->isDefaultEanLabelEnabled()) {
            $dimension = $this->labelCenter1->getDimension();
            $this->drawExtendedBars($im, $dimension[1] - 2);
        }
    }
250110
def get_colors(img):
    
    w, h = img.size
    return [color[:3] for count, color in img.convert('RGB').getcolors(w * h)]
1323880
@Override
    protected Integer theCalculation(Bitmap image) {
        Map histogram = new HashMap();

        for(int i = 0; i < image.getWidth(); i++)
            for(int j = 0; j < image.getHeight(); j++) {
                int rgb = image.getPixel(i,j);

                int rgbArray[] = getRGBArrayFromPixel(rgb);

                if(!isGray(rgbArray)) {

                    Integer counter = (Integer) histogram.get(rgb);

                    if (counter == null)
                        counter = 0;
                    counter++;

                    histogram.put(rgb, counter);
                }
            }

        return Color.parseColor("#"+getMostCommonColor(histogram));
    }
1028966
public function draw($im) {
        $c = count($this->data);
        for ($i = 0; $i < $c; $i++) {
            $this->drawChar($im, $this->data[$i], true);
        }

        $this->drawChar($im, '1', true);
        $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
    }
362176
protected function OnImage()
    {
        $im = imagecreate($this->x, $this->y);
        $bg = imagecolorallocate($im, 200, 255, 200);
        $textcolor = imagecolorallocate($im, rand(0, 255), rand(0, 200), rand(0, 255));

        imagestring($im, 5, 2, 2, $this->code, $textcolor);

        for ($i = 0; $i < 5; $i++) {
            $color = imagecolorallocate($im, rand(0, 255), rand(0, 200), rand(0, 255));
            imageline($im, rand(0, 10), rand(1, 20), rand(50, 80), rand(1, 50), $color);
        }
        return $im;
    }
567904
public function colorDifference(Color $color): int
    {
        $color2 = $color->getRgb();

        return (int)abs($this->colors['r'] - $color2['r'])
            + (int)abs($this->colors['g'] - $color2['g'])
            + (int)abs($this->colors['b'] - $color2['b']);
    }
1304376
public function apply(Picture $picture)
    {
        $resource = $picture->getResource();

        if($picture->isGd())
        {
            imagesavealpha($resource, TRUE);
            imagealphablending($resource, FALSE);

            $w = imagesx($resource);
            $h = imagesy($resource);

            // FIND SMALLEST ALPHA VALUE
            $minimalAlpha = 127;

            for($x = 0; $x < $w; $x++) {

                for($y = 0; $y < $h; $y++)
                {
                    $alpha = (imagecolorat($resource, $x, $y) >> 24 ) & 0xFF;

                    if($alpha < $minimalAlpha)
                    {
                        $minimalAlpha = $alpha;
                    }
                }
            }

            // MODIFY IMAGE PIXELS
            for($x = 0; $x < $w; $x++)
            {
                for($y = 0; $y < $h; $y++)
                {
                    $colorXY = imagecolorat($resource, $x, $y);
                    $alpha = ($colorXY >> 24) & 0xFF;

                    if($minimalAlpha !== 127)
                    {
                        $alpha = 127 + 127 * $this->opacity * ($alpha - 127) / (127 - $minimalAlpha);
                    }
                    else
                    {
                        $alpha += 127 * $this->opacity;
                    }

                    $alphaColorXY = imagecolorallocatealpha(

                        $resource,
                        ($colorXY >> 16) & 0xFF,
                        ($colorXY >> 8) & 0xFF,
                        $colorXY & 0xFF,
                        $alpha
                    );

                    // MODIFY SINGLE PIXEL VALUE
                    imagesetpixel($resource, $x, $y, $alphaColorXY);
                }
            }

            $picture->setResource($resource);
        }
        else
        {
            if(!$resource->getImageAlphaChannel())
            {
                $resource->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
            }

            $resource->evaluateImage(Imagick::EVALUATE_MULTIPLY, $this->opacity, Imagick::CHANNEL_ALPHA);
        }
    }
18072
public function draw($im) {
        $temp_text = $this->text;

        // Checksum
        if ($this->checksum === true) {
            $this->calculateChecksum();
            $temp_text .= $this->keys[$this->checksumValue];
        }

        // Starting Code
        $this->drawChar($im, '0000', true);

        // Chars
        $c = strlen($temp_text);
        for ($i = 0; $i < $c; $i += 2) {
            $temp_bar = '';
            $c2 = strlen($this->findCode($temp_text[$i]));
            for ($j = 0; $j < $c2; $j++) {
                $temp_bar .= substr($this->findCode($temp_text[$i]), $j, 1);
                $temp_bar .= substr($this->findCode($temp_text[$i + 1]), $j, 1);
            }

            $this->drawChar($im, $this->changeBars($temp_bar), true);
        }

        // Ending Code
        $this->drawChar($im, $this->changeBars('100'), true);
        $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
    }
935670
protected function applyFilter($im, $filterType, $arg0 = 0, $arg1 = 0, $arg2 = 0)
    {
        $imgW = imagesx($im);
        $imgH = imagesy($im);

        $out = imagecreatetruecolor($imgW, $imgH);
        imagesavealpha($out, true);
        $trans = imagecolorallocatealpha($out, 0, 0, 0, 127);
        imagefill($out, 0, 0, $trans);
        imagecolortransparent($out, $trans);

        for ($x = 0; $x < $imgW; $x++) {
            for ($y = 0; $y < $imgH; $y++) {
                $rgb = imagecolorat($im, $x, $y);
                $rgba = imagecolorsforindex($im, $rgb);
                $a = $rgba['alpha'];
                $r = $rgba['red'];
                $g = $rgba['green'];
                $b = $rgba['blue'];
                switch ($filterType) {
                    case self::IMG_COL:
                        $r = $arg0;
                        $g = $arg1;
                        $b = $arg2;
                        break;
                    case self::IMG_MUL:
                        $rgba2 = imagecolorsforindex($arg0, imagecolorat($arg0, $x, $y));
                        $r = $r * $rgba2['red'] / 255;
                        $g = $g * $rgba2['green'] / 255;
                        $b = $b * $rgba2['blue'] / 255;
                        break;
                    case self::IMG_ADD_NEG:
                        $rgba2 = imagecolorsforindex($arg0, imagecolorat($arg0, $x, $y));
                        // cap color values to 255
                        $r = min(255, $r + 255 - $rgba2['red']);
                        $g = min(255, $g + 255 - $rgba2['green']);
                        $b = min(255, $b + 255 - $rgba2['blue']);
                        break;
                }
                $val = imagecolorallocatealpha($im, $r, $g, $b, $a);
                imagesetpixel($out, $x, $y, $val);
            }
        }
        return $out;
    }