CoreFiles by mySites.guru
HsTrait.php90 lines, 2,066 bytes, from Joomla_4.0.0-beta1-Beta-Full_Package.zip
Raw History Compare with 3.9.0-beta4
md5
16d07641d096f016d100fb403bd3820a
sha1
2367af8af6105c6048202f6d292ad488adf8cb6a
sha256
a6fbc5379ee84e88c6bfe12a35ccc4e52b2f2c5a1f84dbf634f98cf9cc4fd560
<?php

namespace OzdemirBurak\Iris\Traits;

use OzdemirBurak\Iris\Helpers\DefinedColor;

trait HsTrait
{
    /**
     * @var int
     */
    protected $hue;

    /**
     * @var int
     */
    protected $saturation;

    /**
     * @param string $code
     *
     * @return string|bool
     */
    protected function validate($code)
    {
        list($class, $index) = property_exists($this, 'lightness') ? ['hsl', 2] : ['hsv', 3];
        $color = str_replace([$class, '(', ')', ' ', '%'], '', DefinedColor::find($code, $index));
        if (preg_match('/^(\d{1,3}),(\d{1,3}),(\d{1,3})$/', $color, $matches)) {
            if ($matches[1] > 360 || $matches[2] > 100 || $matches[3] > 100) {
                return false;
            }
            return $color;
        }
        return false;
    }

    /**
     * @param int|string $hue
     *
     * @return int|$this
     */
    public function hue($hue = null)
    {
        if (is_numeric($hue)) {
            $this->hue = $hue >= 0 && $hue <= 360 ? $hue : $this->hue;
            return $this;
        }
        return (int) $this->hue;
    }

    /**
     * @param int|string $saturation
     *
     * @return int|$this
     */
    public function saturation($saturation = null)
    {
        if (is_numeric($saturation)) {
            $this->saturation = $saturation >= 0 && $saturation <= 100 ? $saturation : $this->saturation;
            return $this;
        }
        return (int) $this->saturation;
    }

    /**
     * @return array
     */
    public function getValues()
    {
        return [
            $this->hue(),
            $this->saturation() . '%',
            (property_exists($this, 'lightness') ? $this->lightness() : $this->value()) . '%'
        ];
    }

    /**
     * Values in [0, 1] range
     *
     * @return array
     */
    public function valuesInUnitInterval()
    {
        return [
            $this->hue() / 360,
            $this->saturation() / 100,
            (property_exists($this, 'lightness') ? $this->lightness() : $this->value()) / 100
        ];
    }
}

The same content is in 9 releases, Joomla 4.0.0-alpha11 to 4.0.0-beta7. See them all, or every version of this file.