Вход Регистрация
Файл: gapps/vendor/intervention/image/src/Intervention/Image/Gd/Color.php
Строк: 193
<?php

namespace InterventionImageGd;

use 
InterventionImageAbstractColor;

class 
Color extends AbstractColor
{
    
/**
     * RGB Red value of current color instance
     *
     * @var integer
     */
    
public $r;

    
/**
     * RGB Green value of current color instance
     *
     * @var integer
     */
    
public $g;

    
/**
     * RGB Blue value of current color instance
     *
     * @var integer
     */
    
public $b;

    
/**
     * RGB Alpha value of current color instance
     *
     * @var float
     */
    
public $a;

    
/**
     * Initiates color object from integer
     *
     * @param  integer $value
     * @return InterventionImageAbstractColor
     */
    
public function initFromInteger($value)
    {
        
$this->= ($value >> 24) & 0xFF;
        
$this->= ($value >> 16) & 0xFF;
        
$this->= ($value >> 8) & 0xFF;
        
$this->$value 0xFF;
    }

    
/**
     * Initiates color object from given array
     *
     * @param  array $value
     * @return InterventionImageAbstractColor
     */
    
public function initFromArray($array)
    {
        
$array array_values($array);

        if (
count($array) == 4) {

            
// color array with alpha value
            
list($r$g$b$a) = $array;
            
$this->$this->alpha2gd($a);

        } elseif (
count($array) == 3) {

            
// color array without alpha value
            
list($r$g$b) = $array;
            
$this->0;

        }

        
$this->$r;
        
$this->$g;
        
$this->$b;
    }

    
/**
     * Initiates color object from given string
     *
     * @param  string $value
     * @return InterventionImageAbstractColor
     */
    
public function initFromString($value)
    {
        if (
$color $this->rgbaFromString($value)) {
            
$this->$color[0];
            
$this->$color[1];
            
$this->$color[2];
            
$this->$this->alpha2gd($color[3]);
        }
    }

    
/**
     * Initiates color object from given R, G and B values
     *
     * @param  integer $r
     * @param  integer $g
     * @param  integer $b
     * @return InterventionImageAbstractColor
     */
    
public function initFromRgb($r$g$b)
    {
        
$this->intval($r);
        
$this->intval($g);
        
$this->intval($b);
        
$this->0;
    }

    
/**
     * Initiates color object from given R, G, B and A values
     *
     * @param  integer $r
     * @param  integer $g
     * @param  integer $b
     * @param  float   $a
     * @return InterventionImageAbstractColor
     */
    
public function initFromRgba($r$g$b$a 1)
    {
        
$this->intval($r);
        
$this->intval($g);
        
$this->intval($b);
        
$this->$this->alpha2gd($a);
    }

    
/**
     * Initiates color object from given ImagickPixel object
     *
     * @param  ImagickPixel $value
     * @return InterventionImageAbstractColor
     */
    
public function initFromObject($value)
    {
        throw new 
InterventionImageExceptionNotSupportedException(
            
"GD colors cannot init from ImagickPixel objects."
        
);
    }

    
/**
     * Calculates integer value of current color instance
     *
     * @return integer
     */
    
public function getInt()
    {
        return (
$this-><< 24) + ($this-><< 16) + ($this-><< 8) + $this->b;
    }

    
/**
     * Calculates hexadecimal value of current color instance
     *
     * @param  string $prefix
     * @return string
     */
    
public function getHex($prefix '')
    {
        return 
sprintf('%s%02x%02x%02x'$prefix$this->r$this->g$this->b);
    }

    
/**
     * Calculates RGB(A) in array format of current color instance
     *
     * @return array
     */
    
public function getArray()
    {
        return array(
$this->r$this->g$this->bround($this->1272));
    }

    
/**
     * Calculates RGBA in string format of current color instance
     *
     * @return string
     */
    
public function getRgba()
    {
        return 
sprintf('rgba(%d, %d, %d, %.2f)'$this->r$this->g$this->bround($this->1272));
    }

    
/**
     * Determines if current color is different from given color
     *
     * @param  AbstractColor $color
     * @param  integer       $tolerance
     * @return boolean
     */
    
public function differs(AbstractColor $color$tolerance 0)
    {
        
$color_tolerance round($tolerance 2.55);
        
$alpha_tolerance round($tolerance 1.27);

        
$delta = array(
            
'r' => abs($color->$this->r),
            
'g' => abs($color->$this->g),
            
'b' => abs($color->$this->b),
            
'a' => abs($color->$this->a)
        );

        return (
            
$delta['r'] > $color_tolerance or
            
$delta['g'] > $color_tolerance or
            
$delta['b'] > $color_tolerance or
            
$delta['a'] > $alpha_tolerance
        
);
    }

    
/**
     * Convert rgba alpha (0-1) value to gd value (0-127)
     *
     * @param  float $input
     * @return int
     */
    
private function alpha2gd($input)
    {
        
$oldMin 0;
        
$oldMax 1;

        
$newMin 127;
        
$newMax 0;

        return 
ceil(((($input$oldMin) * ($newMax $newMin)) / ($oldMax $oldMin)) + $newMin);
    }
}
Онлайн: 0
Реклама