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

declare(strict_types=1);

namespace 
InterventionImageColorsHsv;

use 
InterventionImageColorsCmykColor as CmykColor;
use 
InterventionImageColorsRgbColor as RgbColor;
use 
InterventionImageColorsHslColor as HslColor;
use 
InterventionImageColorsRgbColorspace as RgbColorspace;
use 
InterventionImageExceptionsColorException;
use 
InterventionImageInterfacesColorChannelInterface;
use 
InterventionImageInterfacesColorInterface;
use 
InterventionImageInterfacesColorspaceInterface;

class 
Colorspace implements ColorspaceInterface
{
    
/**
     * Channel class names of colorspace
     *
     * @var array<string>
     */
    
public static array $channels = [
        
ChannelsHue::class,
        
ChannelsSaturation::class,
        
ChannelsValue::class
    ];

    
/**
     * {@inheritdoc}
     *
     * @see ColorspaceInterface::colorFromNormalized()
     */
    
public function colorFromNormalized(array $normalized): ColorInterface
    
{
        return new 
Color(...array_map(
            
fn(string $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->importRgbColor($color->convertTo(RgbColorspace::class)),
            
RgbColor::class => $this->importRgbColor($color),
            
HslColor::class => $this->importHslColor($color),
            default => 
$color,
        };
    }

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

        
// normalized values of rgb channels
        
$values array_map(fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels());

        
// take only RGB
        
$values array_slice($values03);

        
// calculate chroma
        
$min min(...$values);
        
$max max(...$values);
        
$chroma $max $min;

        
// calculate value
        
$v 100 $max;

        if (
$chroma == 0) {
            
// greyscale color
            
return new Color(00intval(round($v)));
        }

        
// calculate saturation
        
$s 100 * ($chroma $max);

        
// calculate hue
        
[$r$g$b] = $values;
        
$h match (true) {
            (
$r == $min) => - (($g $b) / $chroma),
            (
$b == $min) => - (($r $g) / $chroma),
            default => 
- (($b $r) / $chroma),
        } * 
60;

        return new 
Color(
            
intval(round($h)),
            
intval(round($s)),
            
intval(round($v))
        );
    }

    
/**
     * @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()
        );

        
$v $l $s min($l$l);
        
$s = ($v == 0) ? * ($l $v);

        return 
$this->colorFromNormalized([$h$s$v]);
    }
}
Онлайн: 1
Реклама