本篇内容主要讲解“php如何实现色彩空间转换”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php如何实现色彩空间转换”吧!
php实现色彩空间转换的方法:首先创建一个PHP示例文件;然后创建“HSL、HSV、RGB色彩空间”;最后通过“protected function tearDown(){...}”等方法实现转换。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php怎么实现色彩空间转换?
PHP实现RGB,HSL,HSV色彩空间转换
<?php class HSL { protected $_hue; protected $_saturation; protected $_lightness; public function __construct($hue=0, $saturation=0, $lightness=0) { $this->_hue = $hue; $this->_saturation = $saturation; $this->_lightness = $lightness; } public function getHue() { return $this->_hue; } public function getSaturation() { return $this->_saturation; } public function getLightness() { return $this->_lightness; } 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; } public function toArray() { return array( 'hue' => $this->getHue(), 'saturation' => $this->getSaturation(), 'lightness' => $this->getLightness() ); } }
<?php class HSV { protected $_hue; protected $_saturation; protected $_value; public function __construct($hue=0, $saturation=0, $value=0) { $this->_hue = $hue; $this->_saturation = $saturation; $this->_value = $value; } public function getHue() { return $this->_hue; } public function getSaturation() { return $this->_saturation; } public function getValue() { return $this->_value; } 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); } public function toArray() { return array( 'hue' => $this->getHue(), 'saturation' => $this->getSaturation(), 'value' => $this->getValue() ); } }
<?php class RGB { protected $_red; protected $_green; protected $_blue; public function __construct($red = 0, $green = 0, $blue = 0) { $this->_red = $red; $this->_green = $green; $this->_blue = $blue; } public function getRed() { return $this->_red; } public function getGreen() { return $this->_green; } public function getBlue() { return $this->_blue; } 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); } 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; } $value = $max; require_once 'HSV.php'; return new HSV($hue, $saturation, $value); } 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'; class HSLTest extends PHPUnit_Framework_TestCase { protected $object; protected function setUp() { $this->object = new HSL(120, 1, 0.75); } protected function tearDown() { } public function testGetHue() { $this->assertEquals(120, $this->object->getHue()); } public function testGetSaturation() { $this->assertEquals(1, $this->object->getSaturation()); } public function testGetLightness() { $this->assertEquals(0.75, $this->object->getLightness()); } public function testToRGB() { $this->assertEquals(new RGB(0.5, 1, 0.5), $this->object->toRGB()); } 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'; class HSVTest extends PHPUnit_Framework_TestCase { protected $object; protected function setUp() { $this->object = new HSV(120, 0.5, 1); } protected function tearDown() { } public function testGetHue() { $this->assertEquals(120, $this->object->getHue()); } public function testGetSaturation() { $this->assertEquals(0.5, $this->object->getSaturation()); } public function testGetValue() { $this->assertEquals(1, $this->object->getValue()); } public function testToRGB() { $this->assertEquals(new RGB(0.5, 1, 0.5), $this->object->toRGB()); } 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'; class RGBTest extends PHPUnit_Framework_TestCase { protected $object; protected function setUp() { $this->object = new RGB(0.5, 1, 0.5); } protected function tearDown() { } public function testGetRed() { $this->assertEquals(0.5, $this->object->getRed()); } public function testGetGreen() { $this->assertEquals(1, $this->object->getGreen()); } public function testGetBlue() { $this->assertEquals(0.5, $this->object->getBlue()); } public function testToHSL() { $this->assertEquals(new HSL(120, 1, 0.75), $this->object->toHSL()); } public function testToHSV() { $this->assertEquals(new HSV(120, 0.5, 1), $this->object->toHSV()); } public function testToArray() { $this->assertEquals(array( 'red' => 0.5, 'green' => 1, 'blue' => 0.5 ), $this->object->toArray()); } }
到此,相信大家对“php如何实现色彩空间转换”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!