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

declare(strict_types=1);

namespace 
InterventionImageColorsRgb;

use 
InterventionImageColorsHsvColor as HsvColor;
use 
InterventionImageColorsHslColor as HslColor;
use 
InterventionImageColorsCmykColor as CmykColor;
use 
InterventionImageExceptionsColorException;
use 
InterventionImageInterfacesColorChannelInterface;
use 
InterventionImageInterfacesColorInterface;
use 
InterventionImageInterfacesColorspaceInterface;

class 
Colorspace implements ColorspaceInterface
{
    
/**
     * Channel class names of colorspace
     *
     * @var array<string>
     */
    
public static array $channels = [
        
ChannelsRed::class,
        
ChannelsGreen::class,
        
ChannelsBlue::class,
        
ChannelsAlpha::class
    ];

    
/**
     * {@inheritdoc}
     *
     * @see ColorspaceInterface::colorFromNormalized()
     */
    
public function colorFromNormalized(array $normalized): ColorInterface
    
{
        return new 
Color(...array_map(
            
fn($classnamefloat $value_normalized) => (new $classname(normalized$value_normalized))->value(),
            
self::$channels,
            
$normalized,
        ));
    }

    
/**
     * @param ColorInterface $color
     * @return ColorInterface
     * @throws ColorException
     */
    
public function importColor(ColorInterface $color): ColorInterface
    
{
        return 
match ($color::class) {
            
CmykColor::class => $this->importCmykColor($color),
            
HsvColor::class => $this->importHsvColor($color),
            
HslColor::class => $this->importHslColor($color),
            default => 
$color,
        };
    }

    
/**
     * @param ColorInterface $color
     * @return ColorInterface
     * @throws ColorException
     */
    
protected function importCmykColor(ColorInterface $color): ColorInterface
    
{
        if (!(
$color instanceof CmykColor)) {
            throw new 
ColorException('Unabled to import color of type ' $color::class . '.');
        }

        return new 
Color(
            (int) (
255 * ($color->cyan()->normalize()) * ($color->key()->normalize())),
            (int) (
255 * ($color->magenta()->normalize()) * ($color->key()->normalize())),
            (int) (
255 * ($color->yellow()->normalize()) * ($color->key()->normalize())),
        );
    }

    
/**
     * @param ColorInterface $color
     * @return ColorInterface
     * @throws ColorException
     */
    
protected function importHsvColor(ColorInterface $color): ColorInterface
    
{
        if (!(
$color instanceof HsvColor)) {
            throw new 
ColorException('Unabled to import color of type ' $color::class . '.');
        }

        
$chroma $color->value()->normalize() * $color->saturation()->normalize();
        
$hue $color->hue()->normalize() * 6;
        
$x $chroma * (abs(fmod($hue2) - 1));

        
// connect channel values
        
$values match (true) {
            
$hue => [$chroma$x0],
            
$hue => [$x$chroma0],
            
$hue => [0$chroma$x],
            
$hue => [0$x$chroma],
            
$hue => [$x0$chroma],
            default => [
$chroma0$x],
        };

        
// add to each value
        
$values array_map(fn(float|int $value): float => $value $color->value()->normalize() - $chroma$values);
        
$values[] = 1// append alpha channel value

        
return $this->colorFromNormalized($values);
    }

    
/**
     * @param ColorInterface $color
     * @return ColorInterface
     * @throws ColorException
     */
    
protected function importHslColor(ColorInterface $color): ColorInterface
    
{
        if (!(
$color instanceof HslColor)) {
            throw new 
ColorException('Unabled to import color of type ' $color::class . '.');
        }

        
// normalized values of hsl channels
        
[$h$s$l] = array_map(
            
fn(ColorChannelInterface $channel): float => $channel->normalize(),
            
$color->channels()
        );

        
$c = (abs($l 1)) * $s;
        
$x $c * (abs(fmod($h 62) - 1));
        
$m $l $c 2;

        
$values match (true) {
            
$h => [$c$x0],
            
$h => [$x$c0],
            
$h => [0$c$x],
            
$h => [0$x$c],
            
$h => [$x0$c],
            default => [
$c0$x],
        };

        
$values array_map(fn(float|int $value): float => $value $m$values);
        
$values[] = 1// append alpha channel value

        
return $this->colorFromNormalized($values);
    }
}
Онлайн: 5
Реклама