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

declare(strict_types=1);

namespace 
InterventionImageColors;

use 
InterventionImageExceptionsColorException;
use 
InterventionImageInterfacesColorChannelInterface;
use 
InterventionImageInterfacesColorInterface;
use 
InterventionImageInterfacesColorspaceInterface;
use 
ReflectionClass;
use 
Stringable;

abstract class 
AbstractColor implements ColorInterfaceStringable
{
    
/**
     * Color channels
     *
     * @var array<ColorChannelInterface>
     */
    
protected array $channels;

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::channels()
     */
    
public function channels(): array
    {
        return 
$this->channels;
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::channel()
     */
    
public function channel(string $classname): ColorChannelInterface
    
{
        
$channels array_filter(
            
$this->channels(),
            
fn(ColorChannelInterface $channel): bool => $channel::class === $classname,
        );

        if (
count($channels) == 0) {
            throw new 
ColorException('Color channel ' $classname ' could not be found.');
        }

        return 
reset($channels);
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::normalize()
     */
    
public function normalize(): array
    {
        return 
array_map(
            
fn(ColorChannelInterface $channel): float => $channel->normalize(),
            
$this->channels(),
        );
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::toArray()
     */
    
public function toArray(): array
    {
        return 
array_map(
            
fn(ColorChannelInterface $channel): int => $channel->value(),
            
$this->channels()
        );
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::convertTo()
     */
    
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
    
{
        
$colorspace match (true) {
            
is_object($colorspace) => $colorspace,
            default => new 
$colorspace(),
        };

        return 
$colorspace->importColor($this);
    }

    
/**
     * Show debug info for the current color
     *
     * @return array<string, int>
     */
    
public function __debugInfo(): array
    {
        return 
array_reduce($this->channels(), function (array $resultColorChannelInterface $item) {
            
$key strtolower((new ReflectionClass($item))->getShortName());
            
$result[$key] = $item->value();
            return 
$result;
        }, []);
    }

    
/**
     * {@inheritdoc}
     *
     * @see ColorInterface::__toString()
     */
    
public function __toString(): string
    
{
        return 
$this->toString();
    }
}
Онлайн: 0
Реклама