<commit_before><?php

/**
 * This file is part of the Rainbow package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license MIT License
 */

namespace Rainbow\Converter;

use Rainbow\ColorInterface;
use Rainbow\Hsl;
use Rainbow\Rgb;
use Rainbow\Unit\RgbComponent;

final class RgbConverter
{
    /**
     * @var Rgb
     */
    private $color;

    public function __construct(Rgb $color)
    {
        $this->color = $color;
    }

    public function getColor()
    {
        return $this->color->copy();
    }

    /**
     * @return Rgb
     */
    public function toRgb()
    {
        return $this->color->copy();
    }

    public function toHsl()
    {
        list($hue, $saturation, $lightness) = $this->calculateHslValues();

        return new Hsl($hue, $saturation, $lightness);
    }

    /**
     * @url https://hg.python.org/cpython/file/2.7/Lib/colorsys.py
     * @return array
     */
    private function calculateHslValues()
    {
        $red = $this->color->getRed()->getValue() / RgbComponent::MAX_VALUE;
        $green = $this->color->getGreen()->getValue() / RgbComponent::MAX_VALUE;
        $blue = $this->color->getBlue()->getValue() / RgbComponent::MAX_VALUE;

        $max = max($red, $green, $blue);
        $min = min($red, $green, $blue);
        $delta = $max - $min;
        $lightness = ($max + $min) / 2;
        if (0 == $delta) {
            return array(0, 0, $lightness * 100);
        }
        $saturation = (0.5 >= $lightness) ?
            ($delta / ($max + $min)) :
            ($delta / (2.0 - $max - $min));
        switch ($max) {
            case $red:
                $hue = (($green - $blue) / $delta);
                break;
            case $green:
                $hue = (($blue - $red) / $delta) + 2;
                break;
            default:
                $hue = (($red - $green) / $delta) + 4;
        }
        $hue = $hue * (360 / 6);

        return array($hue, $saturation * 100, $lightness * 100);
    }

    public static function create(ColorInterface $color)
    {
        $className = sprintf('Rainbow\Converter\%sConverter', ucfirst($color->getName()));
        if (!class_exists($className)) {
            throw new \UnexpectedValueException("{$className} does not exist");
        }
        $converter = new $className($color);

        return new RgbConverter($converter->toRgb());<commit_msg>optimize rgbConverter creation<commit_after><?php

/**
 * This file is part of the Rainbow package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license MIT License
 */

namespace Rainbow\Converter;

use Rainbow\ColorInterface;
use Rainbow\Hsl;
use Rainbow\Rgb;
use Rainbow\Unit\RgbComponent;

final class RgbConverter
{
    /**
     * @var Rgb
     */
    private $color;

    public function __construct(Rgb $color)
    {
        $this->color = $color;
    }

    public function getColor()
    {
        return $this->color->copy();
    }

    /**
     * @return Rgb
     */
    public function toRgb()
    {
        return $this->color->copy();
    }

    public function toHsl()
    {
        list($hue, $saturation, $lightness) = $this->calculateHslValues();

        return new Hsl($hue, $saturation, $lightness);
    }

    /**
     * @url https://hg.python.org/cpython/file/2.7/Lib/colorsys.py
     * @return array
     */
    private function calculateHslValues()
    {
        $red = $this->color->getRed()->getValue() / RgbComponent::MAX_VALUE;
        $green = $this->color->getGreen()->getValue() / RgbComponent::MAX_VALUE;
        $blue = $this->color->getBlue()->getValue() / RgbComponent::MAX_VALUE;

        $max = max($red, $green, $blue);
        $min = min($red, $green, $blue);
        $delta = $max - $min;
        $lightness = ($max + $min) / 2;
        if (0 == $delta) {
            return array(0, 0, $lightness * 100);
        }
        $saturation = (0.5 >= $lightness) ?
            ($delta / ($max + $min)) :
            ($delta / (2.0 - $max - $min));
        switch ($max) {
            case $red:
                $hue = (($green - $blue) / $delta);
                break;
            case $green:
                $hue = (($blue - $red) / $delta) + 2;
                break;
            default:
                $hue = (($red - $green) / $delta) + 4;
        }
        $hue = $hue * (360 / 6);

        return array($hue, $saturation * 100, $lightness * 100);
    }

    public static function create(ColorInterface $color)
    {
        if ("rgb" === $color->getName()) {
            return new self($color);
        }
        $className = sprintf('Rainbow\Converter\%sConverter', ucfirst($color->getName()));
        if (!class_exists($className)) {
            throw new \UnexpectedValueException("{$className} does not exist");
        }
        $converter = new $className($color);

        return new RgbConverter($converter->toRgb());
{'fraction_non_alphanumeric': 0.15740187288304444, 'fraction_numerical': 0.009962143853357242, 'mean_word_length': 1.9863176680547294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '657494', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1522, 'n_words': 371}