Heim  >  Artikel  >  Backend-Entwicklung  >  So erreichen Sie eine Farbraumkonvertierung in PHP

So erreichen Sie eine Farbraumkonvertierung in PHP

藏色散人
藏色散人Original
2021-07-22 09:29:261547Durchsuche

So erreichen Sie die Farbraumkonvertierung in PHP: Erstellen Sie zunächst eine PHP-Beispieldatei. Erstellen Sie dann „HSL-, HSV- und RGB-Farbraum“.

So erreichen Sie eine Farbraumkonvertierung in PHP

Die Betriebsumgebung dieses Artikels: Windows7-System, PHP7.1-Version, DELL G3-Computer

Wie erreicht PHP die Farbraumkonvertierung?

PHP implementiert die RGB-, HSL- und HSV-Farbraumkonvertierung

<?php
 
/**
 * HSL色彩空间描述
 * @author shizhuolin
 */
class HSL {
 
    /**
     * 色相 0-360
     * @var float 
     */
    protected $_hue;
 
    /**
     * 饱和度 0-1
     * @var float 
     */
    protected $_saturation;
 
    /**
     * 亮度 0-1
     * @var float 
     */
    protected $_lightness;
 
    /**
     * 构造HSL色彩空间描述
     * @param float $hue
     * @param float $saturation
     * @param float $lightness 
     */
    public function __construct($hue=0, $saturation=0, $lightness=0) {
        $this->_hue = $hue;
        $this->_saturation = $saturation;
        $this->_lightness = $lightness;
    }
 
    /**
     * 获取色相
     * @return float 
     */
    public function getHue() {
        return $this->_hue;
    }
 
    /**
     * 获取饱和度
     * @return float 
     */
    public function getSaturation() {
        return $this->_saturation;
    }
 
    /**
     * 获取亮度
     * @return float 
     */
    public function getLightness() {
        return $this->_lightness;
    }
 
    /**
     * 获取RGB形式色彩空间描述
     * @return RGB 
     */
    public function toRGB() {
        $h = $this->getHue();
        $s = $this->getSaturation();
        $l = $this->getLightness();
 
        if ($s == 0) {
            require_once &#39;RGB.php&#39;;
            return new RGB($l, $l, $l);
        }
 
        $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - ($l * $s);
        $p = 2 * $l - $q;
        $hk = $h / 360;
        $tR = $hk + (1 / 3);
        $tG = $hk;
        $tB = $hk - (1 / 3);
 
        $tR = $this->getTC($tR);
        $tG = $this->getTC($tG);
        $tB = $this->getTC($tB);
        $tR = $this->getColorC($tR, $p, $q);
        $tG = $this->getColorC($tG, $p, $q);
        $tB = $this->getColorC($tB, $p, $q);
 
        require_once &#39;RGB.php&#39;;
        return new RGB($tR, $tG, $tB);
    }
 
    private function getColorC($tc, $p, $q) {
        if ($tc < (1 / 6)) {
            return $p + (($q - $p) * 6 * $tc );
        } else if ((1 / 6) <= $tc && $tc < 0.5) {
            return $q;
        } else if (0.5 <= $tc && $tc < (2 / 3)) {
            return $p + (($q - $p) * 6 * (2 / 3 - $tc) );
        } else {
            return $p;
        }
    }
 
    private function getTC($c) {
        if ($c < 0)
            $c++;
        if ($c > 1)
            $c--;
        return $c;
    }
 
    /**
     * 获取 array形式HSL色彩描述
     * @return array 
     */
    public function toArray() {
        return array(
            &#39;hue&#39; => $this->getHue(),
            &#39;saturation&#39; => $this->getSaturation(),
            &#39;lightness&#39; => $this->getLightness()
        );
    }
 
}

<?php
 
/**
 * HSV色彩空间描述
 * @author shizhuolin
 */
class HSV {
 
    /**
     * 色相 0-260
     * @var float 
     */
    protected $_hue;
 
    /**
     * 饱和度 0-1
     * @var float 
     */
    protected $_saturation;
 
    /**
     * 色调 0-1
     * @var float 
     */
    protected $_value;
 
    /**
     * 构造
     * @param float $hue 色相
     * @param float $saturation 饱和度
     * @param float $value 色调
     */
    public function __construct($hue=0, $saturation=0, $value=0) {
        $this->_hue = $hue;
        $this->_saturation = $saturation;
        $this->_value = $value;
    }
 
    /**
     * 获取色相 0-360
     * @return float 
     */
    public function getHue() {
        return $this->_hue;
    }
 
    /**
     * 获取饱和度 0-1
     * @return float 
     */
    public function getSaturation() {
        return $this->_saturation;
    }
 
    /**
     * 获取色调 0-1
     * @return float 
     */
    public function getValue() {
        return $this->_value;
    }
 
    /**
     * 返回该色彩在RGB色彩空间的描述
     * @return RGB
     */
    public function toRGB() {
        $hue = $this->getHue();
        $saturation = $this->getSaturation();
        $value = $this->getValue();
        $hi = floor($hue / 60) % 6;
        $f = $hue / 60 - $hi;
        $p = $value * (1 - $saturation);
        $q = $value * (1 - $f * $saturation);
        $t = $value * (1 - (1 - $f) * $saturation);
        switch ($hi) {
            case 0:
                $red = $value;
                $green = $t;
                $blue = $p;
                break;
            case 1:
                $red = $q;
                $green = $value;
                $blue = $p;
                break;
            case 2:
                $red = $p;
                $green = $value;
                $blue = $t;
                break;
            case 3:
                $red = $p;
                $green = $q;
                $blue = $value;
                break;
            case 4:
                $red = $t;
                $green = $p;
                $blue = $value;
                break;
            case 5:
                $red = $value;
                $green = $p;
                $blue = $q;
                break;
            default:
                throw new ErrorException(&#39;HSV Conversion RGB failure!&#39;);
                break;
        };
        require_once &#39;RGB.php&#39;;
        return new RGB($red, $green, $blue);
    }
 
    /**
     * 返回数组形式表达
     * @return array
     */
    public function toArray() {
        return array(
            &#39;hue&#39; => $this->getHue(),
            &#39;saturation&#39; => $this->getSaturation(),
            &#39;value&#39; => $this->getValue()
        );
    }
 
}

Empfohlenes Lernen: „

PHP Video-Tutorial

Das obige ist der detaillierte Inhalt vonSo erreichen Sie eine Farbraumkonvertierung in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn