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

declare(strict_types=1);

namespace 
InterventionImageColorsCmykDecoders;

use 
InterventionImageColorsCmykColor;
use 
InterventionImageDriversAbstractDecoder;
use 
InterventionImageExceptionsDecoderException;
use 
InterventionImageInterfacesColorInterface;
use 
InterventionImageInterfacesDecoderInterface;
use 
InterventionImageInterfacesImageInterface;

class 
StringColorDecoder extends AbstractDecoder implements DecoderInterface
{
    
/**
     * Decode CMYK color strings
     *
     * @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 '/^cmyk((?P<c>[0-9.]+%?), ?(?P<m>[0-9.]+%?), ?(?P<y>[0-9.]+%?), ?(?P<k>[0-9.]+%?))$/i';
        if (
preg_match($pattern$input$matches) != 1) {
            throw new 
DecoderException('Unable to decode input');
        }

        
$values array_map(function (string $value): int {
            return 
intval(round(floatval(trim(str_replace('%'''$value)))));
        }, [
$matches['c'], $matches['m'], $matches['y'], $matches['k']]);

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