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

declare(strict_types=1);

namespace 
InterventionImageColorsRgb;

use 
InterventionImageColorsAbstractColor;
use 
InterventionImageColorsRgbChannelsBlue;
use 
InterventionImageColorsRgbChannelsGreen;
use 
InterventionImageColorsRgbChannelsRed;
use 
InterventionImageColorsRgbChannelsAlpha;
use 
InterventionImageInputHandler;
use 
InterventionImageInterfacesColorChannelInterface;
use 
InterventionImageInterfacesColorInterface;
use 
InterventionImageInterfacesColorspaceInterface;

class 
Color extends AbstractColor
{
    
/**
     * Create new instance
     *
     * @param int $r
     * @param int $g
     * @param int $b
     * @param int $a
     * @return ColorInterface
     */
    
public function __construct(int $rint $gint $bint $a 255)
    {
        
/** @throws void */
        
$this->channels = [
            new 
Red($r),
            new 
Green($g),
            new 
Blue($b),
            new 
Alpha($a),
        ];
    }

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

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::create()
     */
    
public static function create(mixed $input): ColorInterface
    
{
        return 
InputHandler::withDecoders([
            
DecodersHexColorDecoder::class,
            
DecodersStringColorDecoder::class,
            
DecodersTransparentColorDecoder::class,
            
DecodersHtmlColornameDecoder::class,
        ])->
handle($input);
    }

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

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

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

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

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::toHex()
     */
    
public function toHex(string $prefix ''): 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(
                
'rgba(%d, %d, %d, %.1F)',
                
$this->red()->value(),
                
$this->green()->value(),
                
$this->blue()->value(),
                
$this->alpha()->normalize(),
            );
        }

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

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

        return 
count(array_unique($valuesSORT_REGULAR)) === 1;
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::isTransparent()
     */
    
public function isTransparent(): bool
    
{
        return 
$this->alpha()->value() < $this->alpha()->max();
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::isClear()
     */
    
public function isClear(): bool
    
{
        return 
$this->alpha()->value() == 0;
    }
}
Онлайн: 0
Реклама