溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php如何實現(xiàn)色彩空間轉(zhuǎn)換

發(fā)布時間:2021-07-22 09:35:04 來源:億速云 閱讀:137 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“php如何實現(xiàn)色彩空間轉(zhuǎn)換”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“php如何實現(xiàn)色彩空間轉(zhuǎn)換”吧!

php實現(xiàn)色彩空間轉(zhuǎn)換的方法:首先創(chuàng)建一個PHP示例文件;然后創(chuàng)建“HSL、HSV、RGB色彩空間”;最后通過“protected function tearDown(){...}”等方法實現(xiàn)轉(zhuǎn)換。

本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

php怎么實現(xiàn)色彩空間轉(zhuǎn)換?

PHP實現(xiàn)RGB,HSL,HSV色彩空間轉(zhuǎn)換

<?php
 
/**
 * HSL色彩空間描述
 * @author shizhuolin
 */
class HSL {
 
    /**
     * 色相 0-360
     * @var float 
     */
    protected $_hue;
 
    /**
     * 飽和度 0-1
     * @var float 
     */
    protected $_saturation;
 
    /**
     * 亮度 0-1
     * @var float 
     */
    protected $_lightness;
 
    /**
     * 構(gòu)造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 'RGB.php';
            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 'RGB.php';
        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(
            'hue' => $this->getHue(),
            'saturation' => $this->getSaturation(),
            'lightness' => $this->getLightness()
        );
    }
 
}
<?php
 
/**
 * HSV色彩空間描述
 * @author shizhuolin
 */
class HSV {
 
    /**
     * 色相 0-260
     * @var float 
     */
    protected $_hue;
 
    /**
     * 飽和度 0-1
     * @var float 
     */
    protected $_saturation;
 
    /**
     * 色調(diào) 0-1
     * @var float 
     */
    protected $_value;
 
    /**
     * 構(gòu)造
     * @param float $hue 色相
     * @param float $saturation 飽和度
     * @param float $value 色調(diào)
     */
    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;
    }
 
    /**
     * 獲取色調(diào) 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('HSV Conversion RGB failure!');
                break;
        };
        require_once 'RGB.php';
        return new RGB($red, $green, $blue);
    }
 
    /**
     * 返回數(shù)組形式表達
     * @return array
     */
    public function toArray() {
        return array(
            'hue' => $this->getHue(),
            'saturation' => $this->getSaturation(),
            'value' => $this->getValue()
        );
    }
 
}
<?php
 
/**
 * RGB色彩空間描述
 * @author shizhuolin
 */
class RGB {
 
    /**
     * 紅色 0-1
     * @var float 
     */
    protected $_red;
 
    /**
     * 綠色 0-1
     * @var float 
     */
    protected $_green;
 
    /**
     * 藍色 0-1
     * @var float 
     */
    protected $_blue;
 
    /**
     * 以初始值構(gòu)造
     * @param float $red 紅色0-1
     * @param float $green 綠色0-1
     * @param float $blue 藍色0-1
     */
    public function __construct($red = 0, $green = 0, $blue = 0) {
        $this->_red = $red;
        $this->_green = $green;
        $this->_blue = $blue;
    }
 
    /**
     * 獲取紅色分量
     * @return float
     */
    public function getRed() {
        return $this->_red;
    }
 
    /**
     * 獲取綠色分量
     * @return float 
     */
    public function getGreen() {
        return $this->_green;
    }
 
    /**
     * 獲取藍色分量
     * @return float 
     */
    public function getBlue() {
        return $this->_blue;
    }
 
    /**
     * 返回該色彩的HSL空間描述
     * @return HSL
     */
    public function toHSL() {
        $r = $this->getRed();
        $g = $this->getGreen();
        $b = $this->getBlue();
        $rgb = array($r, $g, $b);
        $max = max($rgb);
        $min = min($rgb);
        $diff = $max - $min;
        if ($max == $min) {
            $h = 0;
        } else if ($max == $r && $g >= $b) {
            $h = 60 * (($g - $b) / $diff);
        } else if ($max == $r && $g < $b) {
            $h = 60 * (($g - $b) / $diff) + 360;
        } else if ($max == $g) {
            $h = 60 * (($b - $r) / $diff) + 120;
        } else if ($max == $b) {
            $h = 60 * (($r - $g) / $diff) + 240;
        } else {
            throw new ErrorException("RGB conversion HSL failure!");
        }
        $l = ($max + $min) / 2;
        if ($l == 0 || $max == $min) {
            $s = 0;
        } else if (0 < $l && $l <= 0.5) {
            $s = $diff / (2 * $l);
        } else if ($l > 0.5) {
            $s = $diff / (2 - 2 * $l);
        } else {
            throw new ErrorException("RGB conversion HSL failure!");
        }
        require_once 'HSL.php';
        return new HSL($h, $s, $l);
    }
 
    /**
     * 返回此色彩的HSV空間描述
     * @return HSV 
     */
    public function toHSV() {
        $red = $this->getRed();
        $green = $this->getGreen();
        $blue = $this->getBlue();
 
        $rgb = array($red, $green, $blue);
        $max = max($rgb);
        $min = min($rgb);
        $diff = $max - $min;
 
        /* 計算色相 */
        if ($max == $min) {
            $hue = 0;
        } else if ($max == $red && $green >= $blue) {
            $hue = 60 * (($green - $blue) / $diff);
        } else if ($max == $red && $green < $blue) {
            $hue = 60 * (($green - $blue) / $diff) + 360;
        } else if ($max == $green) {
            $hue = 60 * (($blue - $red) / $diff) + 120;
        } else if ($max == $blue) {
            $hue = 60 * (($red - $green) / $diff) + 240;
        } else {
            throw new ErrorException("compute hue failure!");
        }
 
        /* 計算飽和度 */
        if ($max == 0) {
            $saturation = 0;
        } else {
            $saturation = 1 - $min / $max;
        }
 
        /* 計算色調(diào) */
        $value = $max;
 
        require_once 'HSV.php';
        return new HSV($hue, $saturation, $value);
    }
 
    /**
     * 返回該色彩的數(shù)組表現(xiàn)形式
     */
    public function toArray() {
        return array(
            'red' => $this->getRed(),
            'green' => $this->getGreen(),
            'blue' => $this->getBlue()
        );
    }
 
}

效果測試(需要phpunit支持)

<?php
 
require_once dirname(__FILE__) . '/../../color/RGB.php';
require_once dirname(__FILE__) . '/../../color/HSL.php';
require_once dirname(__FILE__) . '/../../color/HSV.php';
 
/**
 * Test class for HSL.
 * Generated by PHPUnit on 2011-11-29 at 16:56:17.
 */
class HSLTest extends PHPUnit_Framework_TestCase {
 
    /**
     * @var HSL
     */
    protected $object;
 
    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp() {
        $this->object = new HSL(120, 1, 0.75);
    }
 
    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown() {
        
    }
 
    /**
     * @todo Implement testGetHue().
     */
    public function testGetHue() {
        $this->assertEquals(120, $this->object->getHue());
    }
 
    /**
     * @todo Implement testGetSaturation().
     */
    public function testGetSaturation() {
        $this->assertEquals(1, $this->object->getSaturation());
    }
 
    /**
     * @todo Implement testGetLightness().
     */
    public function testGetLightness() {
        $this->assertEquals(0.75, $this->object->getLightness());
    }
 
    /**
     * @todo Implement testToRGB().
     */
    public function testToRGB() {
        $this->assertEquals(new RGB(0.5, 1, 0.5), $this->object->toRGB());
    }
 
    /**
     * @todo Implement testToArray().
     */
    public function testToArray() {
        $this->assertEquals(array(
            'hue' => 120,
            'saturation' => 1,
            'lightness' => 0.75
                ), $this->object->toArray());
    }
 
}
<?php
 
require_once dirname(__FILE__) . '/../../color/RGB.php';
require_once dirname(__FILE__) . '/../../color/HSL.php';
require_once dirname(__FILE__) . '/../../color/HSV.php';
 
/**
 * Test class for HSV.
 * Generated by PHPUnit on 2011-11-29 at 16:49:00.
 */
class HSVTest extends PHPUnit_Framework_TestCase {
 
    /**
     * @var HSV
     */
    protected $object;
 
    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp() {
        $this->object = new HSV(120, 0.5, 1);
    }
 
    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown() {
        
    }
 
    /**
     * @todo Implement testGetHue().
     */
    public function testGetHue() {
        $this->assertEquals(120, $this->object->getHue());
    }
 
    /**
     * @todo Implement testGetSaturation().
     */
    public function testGetSaturation() {
        $this->assertEquals(0.5, $this->object->getSaturation());
    }
 
    /**
     * @todo Implement testGetValue().
     */
    public function testGetValue() {
        $this->assertEquals(1, $this->object->getValue());
    }
 
    /**
     * @todo Implement testToRGB().
     */
    public function testToRGB() {
        $this->assertEquals(new RGB(0.5, 1, 0.5), $this->object->toRGB());
    }
 
    /**
     * @todo Implement testToArray().
     */
    public function testToArray() {
        $this->assertEquals(array(
            'hue' => 120,
            'saturation' => 0.5,
            'value' => 1
                ), $this->object->toArray());
    }
 
}
<?php
 
require_once dirname(__FILE__) . '/../../color/RGB.php';
require_once dirname(__FILE__) . '/../../color/HSL.php';
require_once dirname(__FILE__) . '/../../color/HSV.php';
 
/**
 * Test class for RGB.
 * Generated by PHPUnit on 2011-11-29 at 16:38:54.
 */
class RGBTest extends PHPUnit_Framework_TestCase {
 
    /**
     * @var RGB
     */
    protected $object;
 
    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp() {
        $this->object = new RGB(0.5, 1, 0.5);
    }
 
    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown() {
        
    }
 
    /**
     * @todo Implement testGetRed().
     */
    public function testGetRed() {
        $this->assertEquals(0.5, $this->object->getRed());
    }
 
    /**
     * @todo Implement testGetGreen().
     */
    public function testGetGreen() {
        $this->assertEquals(1, $this->object->getGreen());
    }
 
    /**
     * @todo Implement testGetBlue().
     */
    public function testGetBlue() {
        $this->assertEquals(0.5, $this->object->getBlue());
    }
 
    /**
     * @todo Implement testToHSL().
     */
    public function testToHSL() {
        $this->assertEquals(new HSL(120, 1, 0.75), $this->object->toHSL());
    }
 
    /**
     * @todo Implement testToHSV().
     */
    public function testToHSV() {
        $this->assertEquals(new HSV(120, 0.5, 1), $this->object->toHSV());
    }
 
    /**
     * @todo Implement testToArray().
     */
    public function testToArray() {
        $this->assertEquals(array(
            'red' => 0.5,
            'green' => 1,
            'blue' => 0.5
                ), $this->object->toArray());
    }
 
}

到此,相信大家對“php如何實現(xiàn)色彩空間轉(zhuǎn)換”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI