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

declare(strict_types=1);

namespace 
InterventionImageColorsRgbDecoders;

use 
InterventionImageColorsRgbColor;
use 
InterventionImageDriversAbstractDecoder;
use 
InterventionImageExceptionsDecoderException;
use 
InterventionImageInterfacesColorInterface;
use 
InterventionImageInterfacesDecoderInterface;
use 
InterventionImageInterfacesImageInterface;

class 
HexColorDecoder extends AbstractDecoder implements DecoderInterface
{
    
/**
     * Decode hexadecimal rgb colors with and without transparency
     *
     * @param mixed $input
     * @return ImageInterface|ColorInterface
     */
    
public function decode(mixed $input): ImageInterface|ColorInterface
    
{
        if (!
is_string($input)) {
            throw new 
DecoderException('Unable to decode input');
        }

        
$pattern = '/^#?(?P<hex>[a-fd]{3}(?:[a-fd]?|(?:[a-fd]{3}(?:[a-fd]{2})?)?)b)$/i';
        if (
preg_match($pattern, $input, $matches) != 1) {
            throw new 
DecoderException('Unable to decode input');
        }

        
$values = match (strlen($matches['hex'])) {
            
3, 4 => str_split($matches['hex']),
            
6, 8 => str_split($matches['hex'], 2),
            default => throw new 
DecoderException('Unable to decode input'),
        };

        
$values = array_map(function (string $value): float|int {
            return 
match (strlen($value)) {
                
1 => hexdec($value . $value),
                
2 => hexdec($value),
                default => throw new 
DecoderException('Unable to decode input'),
            };
        }, 
$values);

        return new 
Color(...$values);
    }
}
Онлайн: 3
Реклама