Вход Регистрация
Файл: vendor/intervention/image/src/Colors/Rgb/Color.php
Строк: 237
<?php

declare(strict_types=1);

namespace 
InterventionImageColorsRgb;

use 
InterventionImageColorsAbstractColor;
use 
InterventionImageColorsRgbChannelsAlpha;
use 
InterventionImageColorsRgbChannelsBlue;
use 
InterventionImageColorsRgbChannelsGreen;
use 
InterventionImageColorsRgbChannelsRed;
use 
InterventionImageColorsRgbDecodersHexColorDecoder;
use 
InterventionImageColorsRgbDecodersNamedColorDecoder;
use 
InterventionImageColorsRgbDecodersStringColorDecoder;
use 
InterventionImageExceptionsColorException;
use 
InterventionImageExceptionsDriverException;
use 
InterventionImageExceptionsInvalidArgumentException;
use 
InterventionImageExceptionsNotSupportedException;
use 
InterventionImageInputHandler;
use 
InterventionImageInterfacesColorChannelInterface;
use 
InterventionImageInterfacesColorspaceInterface;

class 
Color extends AbstractColor
{
    
/**
     * Create new instance.
     *
     * @throws InvalidArgumentException
     */
    
public function __construct(int|Red $rint|Green $gint|Blue $bfloat|Alpha $a 1)
    {
        
$this->channels = [
            
is_int($r) ? new Red($r) : $r,
            
is_int($g) ? new Green($g) : $g,
            
is_int($b) ? new Blue($b) : $b,
            
is_float($a) ? new Alpha($a) : $a,
        ];
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::create()
     *
     * @throws InvalidArgumentException
     */
    
public static function create(int|Red $rint|Green $gint|Blue $bfloat|Alpha $a 1): self
    
{
        return new 
self($r$g$b$a);
    }

    
/**
     * Parse RGB color from string.
     *
     * @throws InvalidArgumentException
     * @throws ColorException
     */
    
public static function parse(string $input): self
    
{
        try {
            
$color InputHandler::usingDecoders([
                
StringColorDecoder::class,
                
NamedColorDecoder::class,
                
HexColorDecoder::class,
            ])->
handle($input);
        } catch (
NotSupportedException DriverException $e) {
            throw new 
InvalidArgumentException(
                
'Unable to parse RGB color from input "' $input '"',
                
previous$e,
            );
        }

        if (!
$color instanceof self) {
            throw new 
ColorException('Result must be instance of ' self::class);
        }

        return 
$color;
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::colorspace()
     */
    
public function colorspace(): ColorspaceInterface
    
{
        return new 
Colorspace();
    }

    
/**
     * Return the RGB red color channel.
     */
    
public function red(): ColorChannelInterface
    
{
        
/** @throws void */
        
return $this->channel(Red::class);
    }

    
/**
     * Return the RGB green color channel.
     */
    
public function green(): ColorChannelInterface
    
{
        
/** @throws void */
        
return $this->channel(Green::class);
    }

    
/**
     * Return the RGB blue color channel.
     */
    
public function blue(): ColorChannelInterface
    
{
        
/** @throws void */
        
return $this->channel(Blue::class);
    }

    
/**
     * Return the colors alpha channel.
     */
    
public function alpha(): ColorChannelInterface
    
{
        
/** @throws void */
        
return $this->channel(Alpha::class);
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::toHex()
     */
    
public function toHex(bool $prefix false): string
    
{
        if (
$this->isTransparent()) {
            return 
sprintf(
                
'%s%02x%02x%02x%02x',
                
$prefix '#' '',
                
$this->red()->value(),
                
$this->green()->value(),
                
$this->blue()->value(),
                
$this->alpha()->value(),
            );
        }

        return 
sprintf(
            
'%s%02x%02x%02x',
            
$prefix '#' '',
            
$this->red()->value(),
            
$this->green()->value(),
            
$this->blue()->value(),
        );
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::toString()
     */
    
public function toString(): string
    
{
        if (
$this->isTransparent()) {
            return 
sprintf(
                
'rgb(%d %d %d / %s)',
                
$this->red()->value(),
                
$this->green()->value(),
                
$this->blue()->value(),
                
$this->alpha()->toString(),
            );
        }

        return 
sprintf(
            
'rgb(%d %d %d)',
            
$this->red()->value(),
            
$this->green()->value(),
            
$this->blue()->value(),
        );
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::isGrayscale()
     */
    
public function isGrayscale(): bool
    
{
        
$values = [$this->red()->value(), $this->green()->value(), $this->blue()->value()];

        return 
count(array_unique($valuesSORT_REGULAR)) === 1;
    }
}
Онлайн: 4
Реклама