From 9b9f1ce617e2d75188398a4ab84575a533d4987b Mon Sep 17 00:00:00 2001 From: prolic Date: Wed, 12 Dec 2012 17:32:37 +0100 Subject: [PATCH 01/10] reverted changes in Console Response --- src/Response.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Response.php b/src/Response.php index 84b13ea..4ca17b1 100644 --- a/src/Response.php +++ b/src/Response.php @@ -29,24 +29,13 @@ class Response extends Message implements ResponseInterface * Check if content was sent * * @return bool + * @deprecated */ public function contentSent() { return $this->contentSent; } - /** - * Set content sent - * - * @param $flag - * @return Response - */ - public function setContentSent($flag) - { - $this->contentSent = (bool) $flag; - return $this; - } - /** * Set the error level that will be returned to shell. * From 8efdf7e950b76ca9897a389adaf73a14be4eb801 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Thu, 3 Jan 2013 20:27:22 +0100 Subject: [PATCH 02/10] cs fix --- src/Adapter/Posix.php | 3 ++- src/Color/Xterm256.php | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index 83efbaa..392ba03 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -370,7 +370,8 @@ protected function setTTYMode($mode) * @throws Exception\BadMethodCallException * @return string */ - protected function getColorCode($color, $type = 'fg') { + protected function getColorCode($color, $type = 'fg') + { if($color instanceof Xterm256) { $r = new \ReflectionClass($color); $code = $r->getStaticPropertyValue('color'); diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index fa5a0c8..b5982f2 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -14,14 +14,17 @@ * @category Zend * @package Zend_Console */ -class Xterm256 { +class Xterm256 +{ public static $color; const FOREGROUND = 38; const BACKGROUND = 48; - private function __construct($color) { + private function __construct($color) + { static::$color = sprintf('%%s;5;%s', $color); } - public static function calculate($hexColor) { + public static function calculate($hexColor) + { $hex = str_split($hexColor, 2); if(count($hex) !== 3) { return null; @@ -39,4 +42,4 @@ public static function calculate($hexColor) { } return new static($x11); } -} \ No newline at end of file +} From 34f8da8ceb02a556306405090bf65e78ac751295 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Thu, 3 Jan 2013 22:28:21 +0100 Subject: [PATCH 03/10] cs fix --- src/Adapter/Posix.php | 6 ++++-- src/Color/Xterm256.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index 392ba03..e2ed629 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -372,18 +372,20 @@ protected function setTTYMode($mode) */ protected function getColorCode($color, $type = 'fg') { - if($color instanceof Xterm256) { + if ($color instanceof Xterm256) { $r = new \ReflectionClass($color); $code = $r->getStaticPropertyValue('color'); + return $type == 'fg' ? sprintf($code, $color::FOREGROUND) : sprintf($code, $color::BACKGROUND); } - elseif($color !== null) { + elseif ($color !== null) { if (!isset(static::$ansiColorMap[$type][$color])) { throw new Exception\BadMethodCallException(sprintf( 'Unknown color "%s". Please use one of the Zend\Console\ColorInterface constants or use Zend\Console\Color\Xterm256::calculate', $color )); } + return static::$ansiColorMap[$type][$color]; } else { diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index b5982f2..19b0fd4 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -26,20 +26,22 @@ private function __construct($color) public static function calculate($hexColor) { $hex = str_split($hexColor, 2); - if(count($hex) !== 3) { + if (count($hex) !== 3) { return null; } $ahex = array_map(function ($hex) { $val = round(((hexdec($hex) - 55)/40), 0); + return $val > 0 ? (int) $val : 0; }, $hex); $x11 = $ahex[0] * 36 + $ahex[1] * 6 + $ahex[2] + 16; - if($x11 >= 16 && $x11 <= 231) { + if ($x11 >= 16 && $x11 <= 231) { return new static($x11); } else { $x11 = 232 + floor(hexdec($hex[0])/10); } + return new static($x11); } } From e8bf5a16c0c09f634644cc0537d3e69aaa54d5b7 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Thu, 3 Jan 2013 23:09:21 +0100 Subject: [PATCH 04/10] cs fix --- src/Adapter/Posix.php | 6 ++---- src/Color/Xterm256.php | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index e2ed629..b1a3493 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -377,8 +377,7 @@ protected function getColorCode($color, $type = 'fg') $code = $r->getStaticPropertyValue('color'); return $type == 'fg' ? sprintf($code, $color::FOREGROUND) : sprintf($code, $color::BACKGROUND); - } - elseif ($color !== null) { + } elseif ($color !== null) { if (!isset(static::$ansiColorMap[$type][$color])) { throw new Exception\BadMethodCallException(sprintf( 'Unknown color "%s". Please use one of the Zend\Console\ColorInterface constants or use Zend\Console\Color\Xterm256::calculate', @@ -387,8 +386,7 @@ protected function getColorCode($color, $type = 'fg') } return static::$ansiColorMap[$type][$color]; - } - else { + } else { return null; } } diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index 19b0fd4..b9707ef 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -37,8 +37,7 @@ public static function calculate($hexColor) $x11 = $ahex[0] * 36 + $ahex[1] * 6 + $ahex[2] + 16; if ($x11 >= 16 && $x11 <= 231) { return new static($x11); - } - else { + } else { $x11 = 232 + floor(hexdec($hex[0])/10); } From f6f05a1c7b73a1b53c5db6e0105ce9e9d29656b7 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Fri, 4 Jan 2013 13:22:02 +0100 Subject: [PATCH 05/10] useless conditions --- src/Adapter/Posix.php | 7 ++++--- src/Color/Xterm256.php | 3 +-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index b1a3493..7df2b9b 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -377,7 +377,8 @@ protected function getColorCode($color, $type = 'fg') $code = $r->getStaticPropertyValue('color'); return $type == 'fg' ? sprintf($code, $color::FOREGROUND) : sprintf($code, $color::BACKGROUND); - } elseif ($color !== null) { + } + if ($color !== null) { if (!isset(static::$ansiColorMap[$type][$color])) { throw new Exception\BadMethodCallException(sprintf( 'Unknown color "%s". Please use one of the Zend\Console\ColorInterface constants or use Zend\Console\Color\Xterm256::calculate', @@ -386,8 +387,8 @@ protected function getColorCode($color, $type = 'fg') } return static::$ansiColorMap[$type][$color]; - } else { - return null; } + + return null; } } diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index b9707ef..fabab66 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -37,9 +37,8 @@ public static function calculate($hexColor) $x11 = $ahex[0] * 36 + $ahex[1] * 6 + $ahex[2] + 16; if ($x11 >= 16 && $x11 <= 231) { return new static($x11); - } else { - $x11 = 232 + floor(hexdec($hex[0])/10); } + $x11 = 232 + floor(hexdec($hex[0])/10); return new static($x11); } From 44162f894913c9d18491241624041043c54cc514 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Mon, 7 Jan 2013 15:30:05 +0100 Subject: [PATCH 06/10] added testsuite for xterm256, corrected algorithm for grayscale --- src/Adapter/Posix.php | 2 +- src/Color/Xterm256.php | 18 +-- test/Xterm256Test.php | 307 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 318 insertions(+), 9 deletions(-) create mode 100644 test/Xterm256Test.php diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index 7df2b9b..bde3748 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Console */ diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index fabab66..e95475f 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Console */ @@ -19,26 +19,28 @@ class Xterm256 public static $color; const FOREGROUND = 38; const BACKGROUND = 48; - private function __construct($color) + private function __construct($color = null) { - static::$color = sprintf('%%s;5;%s', $color); + static::$color = $color !== null ? sprintf('%%s;5;%s', $color) : null; } public static function calculate($hexColor) { $hex = str_split($hexColor, 2); - if (count($hex) !== 3) { - return null; + if (count($hex) !== 3 || !preg_match('#[0-9A-F]{6}#', $hexColor)) { + return new static(); } $ahex = array_map(function ($hex) { $val = round(((hexdec($hex) - 55)/40), 0); return $val > 0 ? (int) $val : 0; }, $hex); - $x11 = $ahex[0] * 36 + $ahex[1] * 6 + $ahex[2] + 16; - if ($x11 >= 16 && $x11 <= 231) { + $dhex = array_map('hexdec', $hex); + if (array_fill(0, 3, $dhex[0]) === $dhex && (int) substr($dhex[0], -1) === 8) { + $x11 = 232 + (int) floor($dhex[0]/10); + return new static($x11); } - $x11 = 232 + floor(hexdec($hex[0])/10); + $x11 = $ahex[0] * 36 + $ahex[1] * 6 + $ahex[2] + 16; return new static($x11); } diff --git a/test/Xterm256Test.php b/test/Xterm256Test.php new file mode 100644 index 0000000..61e9a26 --- /dev/null +++ b/test/Xterm256Test.php @@ -0,0 +1,307 @@ +getStaticPropertyValue('color'); + $this->assertNull($code); + } + } + public function testApproximateHexCodeInputs() { + $ahexs = array( + '000100' => 16, + 'FF33A0' => 199, + ); + foreach ($ahexs as $hex => $gcode) { + $color = Xterm256::calculate($hex); + $r = new \ReflectionClass($color); + $gcode = sprintf('%%s;5;%s', $gcode); + $this->assertEquals($gcode, $r->getStaticPropertyValue('color')); + } + } + public function testExactHexCodeInputs() + { + $ehexs = array( + '000000' => 16, + '00005F' => 17, + '000087' => 18, + '0000AF' => 19, + '0000D7' => 20, + '0000FF' => 21, + '005F00' => 22, + '005F5F' => 23, + '005F87' => 24, + '005FAF' => 25, + '005FD7' => 26, + '005FFF' => 27, + '008700' => 28, + '00875F' => 29, + '008787' => 30, + '0087AF' => 31, + '0087D7' => 32, + '0087FF' => 33, + '00AF00' => 34, + '00AF5F' => 35, + '00AF87' => 36, + '00AFAF' => 37, + '00AFD7' => 38, + '00AFFF' => 39, + '00D700' => 40, + '00D75F' => 41, + '00D787' => 42, + '00D7AF' => 43, + '00D7D7' => 44, + '00D7FF' => 45, + '00FF00' => 46, + '00FF5F' => 47, + '00FF87' => 48, + '00FFAF' => 49, + '00FFD7' => 50, + '00FFFF' => 51, + '5F0000' => 52, + '5F005F' => 53, + '5F0087' => 54, + '5F00AF' => 55, + '5F00D7' => 56, + '5F00FF' => 57, + '5F5F00' => 58, + '5F5F5F' => 59, + '5F5F87' => 60, + '5F5FAF' => 61, + '5F5FD7' => 62, + '5F5FFF' => 63, + '5F8700' => 64, + '5F875F' => 65, + '5F8787' => 66, + '5F87AF' => 67, + '5F87D7' => 68, + '5F87FF' => 69, + '5FAF00' => 70, + '5FAF5F' => 71, + '5FAF87' => 72, + '5FAFAF' => 73, + '5FAFD7' => 74, + '5FAFFF' => 75, + '5FD700' => 76, + '5FD75F' => 77, + '5FD787' => 78, + '5FD7AF' => 79, + '5FD7D7' => 80, + '5FD7FF' => 81, + '5FFF00' => 82, + '5FFF5F' => 83, + '5FFF87' => 84, + '5FFFAF' => 85, + '5FFFD7' => 86, + '5FFFFF' => 87, + '870000' => 88, + '87005F' => 89, + '870087' => 90, + '8700AF' => 91, + '8700D7' => 92, + '8700FF' => 93, + '875F00' => 94, + '875F5F' => 95, + '875F87' => 96, + '875FAF' => 97, + '875FD7' => 98, + '875FFF' => 99, + '878700' => 100, + '87875F' => 101, + '878787' => 102, + '8787AF' => 103, + '8787D7' => 104, + '8787FF' => 105, + '87AF00' => 106, + '87AF5F' => 107, + '87AF87' => 108, + '87AFAF' => 109, + '87AFD7' => 110, + '87AFFF' => 111, + '87D700' => 112, + '87D75F' => 113, + '87D787' => 114, + '87D7AF' => 115, + '87D7D7' => 116, + '87D7FF' => 117, + '87FF00' => 118, + '87FF5F' => 119, + '87FF87' => 120, + '87FFAF' => 121, + '87FFD7' => 122, + '87FFFF' => 123, + 'AF0000' => 124, + 'AF005F' => 125, + 'AF0087' => 126, + 'AF00AF' => 127, + 'AF00D7' => 128, + 'AF00FF' => 129, + 'AF5F00' => 130, + 'AF5F5F' => 131, + 'AF5F87' => 132, + 'AF5FAF' => 133, + 'AF5FD7' => 134, + 'AF5FFF' => 135, + 'AF8700' => 136, + 'AF875F' => 137, + 'AF8787' => 138, + 'AF87AF' => 139, + 'AF87D7' => 140, + 'AF87FF' => 141, + 'AFAF00' => 142, + 'AFAF5F' => 143, + 'AFAF87' => 144, + 'AFAFAF' => 145, + 'AFAFD7' => 146, + 'AFAFFF' => 147, + 'AFD700' => 148, + 'AFD75F' => 149, + 'AFD787' => 150, + 'AFD7AF' => 151, + 'AFD7D7' => 152, + 'AFD7FF' => 153, + 'AFFF00' => 154, + 'AFFF5F' => 155, + 'AFFF87' => 156, + 'AFFFAF' => 157, + 'AFFFD7' => 158, + 'AFFFFF' => 159, + 'D70000' => 160, + 'D7005F' => 161, + 'D70087' => 162, + 'D700AF' => 163, + 'D700D7' => 164, + 'D700FF' => 165, + 'D75F00' => 166, + 'D75F5F' => 167, + 'D75F87' => 168, + 'D75FAF' => 169, + 'D75FD7' => 170, + 'D75FFF' => 171, + 'D78700' => 172, + 'D7875F' => 173, + 'D78787' => 174, + 'D787AF' => 175, + 'D787D7' => 176, + 'D787FF' => 177, + 'D7AF00' => 178, + 'D7AF5F' => 179, + 'D7AF87' => 180, + 'D7AFAF' => 181, + 'D7AFD7' => 182, + 'D7AFFF' => 183, + 'D7D700' => 184, + 'D7D75F' => 185, + 'D7D787' => 186, + 'D7D7AF' => 187, + 'D7D7D7' => 188, + 'D7D7FF' => 189, + 'D7FF00' => 190, + 'D7FF5F' => 191, + 'D7FF87' => 192, + 'D7FFAF' => 193, + 'D7FFD7' => 194, + 'D7FFFF' => 195, + 'FF0000' => 196, + 'FF005F' => 197, + 'FF0087' => 198, + 'FF00AF' => 199, + 'FF00D7' => 200, + 'FF00FF' => 201, + 'FF5F00' => 202, + 'FF5F5F' => 203, + 'FF5F87' => 204, + 'FF5FAF' => 205, + 'FF5FD7' => 206, + 'FF5FFF' => 207, + 'FF8700' => 208, + 'FF875F' => 209, + 'FF8787' => 210, + 'FF87AF' => 211, + 'FF87D7' => 212, + 'FF87FF' => 213, + 'FFAF00' => 214, + 'FFAF5F' => 215, + 'FFAF87' => 216, + 'FFAFAF' => 217, + 'FFAFD7' => 218, + 'FFAFFF' => 219, + 'FFD700' => 220, + 'FFD75F' => 221, + 'FFD787' => 222, + 'FFD7AF' => 223, + 'FFD7D7' => 224, + 'FFD7FF' => 225, + 'FFFF00' => 226, + 'FFFF5F' => 227, + 'FFFF87' => 228, + 'FFFFAF' => 229, + 'FFFFD7' => 230, + 'FFFFFF' => 231, + '080808' => 232, + '121212' => 233, + '1C1C1C' => 234, + '262626' => 235, + '303030' => 236, + '3A3A3A' => 237, + '444444' => 238, + '4E4E4E' => 239, + '585858' => 240, + '626262' => 241, + '6C6C6C' => 242, + '767676' => 243, + '808080' => 244, + '8A8A8A' => 245, + '949494' => 246, + '9E9E9E' => 247, + 'A8A8A8' => 248, + 'B2B2B2' => 249, + 'BCBCBC' => 250, + 'C6C6C6' => 251, + 'D0D0D0' => 252, + 'DADADA' => 253, + 'E4E4E4' => 254, + 'EEEEEE' => 255, + ); + foreach ($ehexs as $hex => $gcode) { + $color = Xterm256::calculate($hex); + $r = new \ReflectionClass($color); + $gcode = sprintf('%%s;5;%s', $gcode); + $this->assertEquals($gcode, $r->getStaticPropertyValue('color')); + } + } +} From 1dcdcb4f2a6923635532bd54eda56e2097819a27 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Mon, 7 Jan 2013 15:36:01 +0100 Subject: [PATCH 07/10] preg_match fix mask ommission --- src/Color/Xterm256.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index e95475f..f018c9c 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -26,7 +26,7 @@ private function __construct($color = null) public static function calculate($hexColor) { $hex = str_split($hexColor, 2); - if (count($hex) !== 3 || !preg_match('#[0-9A-F]{6}#', $hexColor)) { + if (count($hex) !== 3 || !preg_match('#[0-9A-F]{6}#i', $hexColor)) { return new static(); } $ahex = array_map(function ($hex) { From fb7175598188229a9faaf674d1a5559249e11834 Mon Sep 17 00:00:00 2001 From: thomascantonnet Date: Mon, 7 Jan 2013 15:46:35 +0100 Subject: [PATCH 08/10] cs fix --- test/Xterm256Test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Xterm256Test.php b/test/Xterm256Test.php index 61e9a26..07329dc 100644 --- a/test/Xterm256Test.php +++ b/test/Xterm256Test.php @@ -41,7 +41,8 @@ public function testWrongHexCodeInputs() $this->assertNull($code); } } - public function testApproximateHexCodeInputs() { + public function testApproximateHexCodeInputs() + { $ahexs = array( '000100' => 16, 'FF33A0' => 199, From 574428a1f2254117f2a74eb5be7cdc2a7860b97d Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 7 Jan 2013 11:43:40 -0600 Subject: [PATCH 09/10] [zendframework/zf2#3339] CS and test cleanup - Various CS fixes/changes - Restructured tests to use PHPUnit data providers --- src/Adapter/Posix.php | 16 +- src/Color/Xterm256.php | 36 ++- test/Xterm256Test.php | 562 +++++++++++++++++++++-------------------- 3 files changed, 331 insertions(+), 283 deletions(-) diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index bde3748..30e504b 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -10,6 +10,7 @@ namespace Zend\Console\Adapter; +use ReflectionClass; use Zend\Console\Charset; use Zend\Console\Exception; use Zend\Console\Color\Xterm256; @@ -218,9 +219,9 @@ public function setPos($x, $y) */ public function colorize($string, $color = null, $bgColor = null) { - $color = $this->getColorCode($color, 'fg'); + $color = $this->getColorCode($color, 'fg'); $bgColor = $this->getColorCode($bgColor, 'bg'); - return ($color !== null ? "\x1b[" . $color . 'm' : '') + return ($color !== null ? "\x1b[" . $color . 'm' : '') . ($bgColor !== null ? "\x1b[" . $bgColor . 'm' : '') . $string . "\x1b[22;39m\x1b[0;49m"; @@ -373,11 +374,16 @@ protected function setTTYMode($mode) protected function getColorCode($color, $type = 'fg') { if ($color instanceof Xterm256) { - $r = new \ReflectionClass($color); + $r = new ReflectionClass($color); $code = $r->getStaticPropertyValue('color'); - - return $type == 'fg' ? sprintf($code, $color::FOREGROUND) : sprintf($code, $color::BACKGROUND); + if ($type == 'fg') { + $code = sprintf($code, $color::FOREGROUND); + } else { + $code = sprintf($code, $color::BACKGROUND); + } + return $code; } + if ($color !== null) { if (!isset(static::$ansiColorMap[$type][$color])) { throw new Exception\BadMethodCallException(sprintf( diff --git a/src/Color/Xterm256.php b/src/Color/Xterm256.php index f018c9c..dcb2618 100644 --- a/src/Color/Xterm256.php +++ b/src/Color/Xterm256.php @@ -16,30 +16,58 @@ */ class Xterm256 { - public static $color; + /** + * Foreground constant + */ const FOREGROUND = 38; + + /** + * Background constant + */ const BACKGROUND = 48; - private function __construct($color = null) + + /** + * @var string $color X11-formatted color value + */ + public static $color; + + /** + * Populate color property with X11-formatted equivalent + * + * @param mixed $color + * @return void + */ + protected function __construct($color = null) { static::$color = $color !== null ? sprintf('%%s;5;%s', $color) : null; } + + /** + * Calcluate the X11 color value of a hexadecimal color + * + * @param string $hexColor + * @return string + */ public static function calculate($hexColor) { $hex = str_split($hexColor, 2); if (count($hex) !== 3 || !preg_match('#[0-9A-F]{6}#i', $hexColor)) { + // Invalid/unknown color string return new static(); } + $ahex = array_map(function ($hex) { $val = round(((hexdec($hex) - 55)/40), 0); - return $val > 0 ? (int) $val : 0; }, $hex); + $dhex = array_map('hexdec', $hex); + if (array_fill(0, 3, $dhex[0]) === $dhex && (int) substr($dhex[0], -1) === 8) { $x11 = 232 + (int) floor($dhex[0]/10); - return new static($x11); } + $x11 = $ahex[0] * 36 + $ahex[1] * 6 + $ahex[2] + 16; return new static($x11); diff --git a/test/Xterm256Test.php b/test/Xterm256Test.php index 07329dc..4dacc9a 100644 --- a/test/Xterm256Test.php +++ b/test/Xterm256Test.php @@ -10,6 +10,7 @@ namespace ZendTest\Console; +use ReflectionClass; use Zend\Console\Color\Xterm256; /** @@ -20,289 +21,302 @@ */ class Xterm256Test extends \PHPUnit_Framework_TestCase { - public function setUp() + public function invalidHexCodes() { + return array( + 'too-long' => array('FFFFFF0'), + 'too-long-and-char-out-of-range' => array('ABCDEFG'), + 'too-long-digits' => array('01048212'), + 'too-long-and-invalid-enc' => array('ééààööüü'), + 'char-out-of-range' => array('FF00GG'), + 'null' => array(null), + ); + } + /** + * @dataProvider invalidHexCodes + */ + public function testWrongHexCodeInputs($hex) + { + $color = Xterm256::calculate($hex); + $r = new ReflectionClass($color); + $code = $r->getStaticPropertyValue('color'); + $this->assertNull($code); } - public function testWrongHexCodeInputs() + + public function approximateHexCodes() { - $whexs = array( - 'FFFFFF0', - 'ABCDEFG', - '01048212', - 'ééààööüü', - 'FF00GG', - null, + return array( + 'sixteen' => array('000100', 16), + 'one-ninety-nine' => array('FF33A0', 199), ); - foreach ($whexs as $hex) { - $color = Xterm256::calculate($hex); - $r = new \ReflectionClass($color); - $code = $r->getStaticPropertyValue('color'); - $this->assertNull($code); - } } - public function testApproximateHexCodeInputs() + + /** + * @dataProvider approximateHexCodes + */ + public function testApproximateHexCodeInputs($hex, $gcode) + { + $color = Xterm256::calculate($hex); + $r = new ReflectionClass($color); + $gcode = sprintf('%%s;5;%s', $gcode); + $this->assertEquals($gcode, $r->getStaticPropertyValue('color')); + } + + public function exactHexCodes() { - $ahexs = array( - '000100' => 16, - 'FF33A0' => 199, + return array( + '000000' => array('000000', 16), + '00005F' => array('00005F', 17), + '000087' => array('000087', 18), + '0000AF' => array('0000AF', 19), + '0000D7' => array('0000D7', 20), + '0000FF' => array('0000FF', 21), + '005F00' => array('005F00', 22), + '005F5F' => array('005F5F', 23), + '005F87' => array('005F87', 24), + '005FAF' => array('005FAF', 25), + '005FD7' => array('005FD7', 26), + '005FFF' => array('005FFF', 27), + '008700' => array('008700', 28), + '00875F' => array('00875F', 29), + '008787' => array('008787', 30), + '0087AF' => array('0087AF', 31), + '0087D7' => array('0087D7', 32), + '0087FF' => array('0087FF', 33), + '00AF00' => array('00AF00', 34), + '00AF5F' => array('00AF5F', 35), + '00AF87' => array('00AF87', 36), + '00AFAF' => array('00AFAF', 37), + '00AFD7' => array('00AFD7', 38), + '00AFFF' => array('00AFFF', 39), + '00D700' => array('00D700', 40), + '00D75F' => array('00D75F', 41), + '00D787' => array('00D787', 42), + '00D7AF' => array('00D7AF', 43), + '00D7D7' => array('00D7D7', 44), + '00D7FF' => array('00D7FF', 45), + '00FF00' => array('00FF00', 46), + '00FF5F' => array('00FF5F', 47), + '00FF87' => array('00FF87', 48), + '00FFAF' => array('00FFAF', 49), + '00FFD7' => array('00FFD7', 50), + '00FFFF' => array('00FFFF', 51), + '5F0000' => array('5F0000', 52), + '5F005F' => array('5F005F', 53), + '5F0087' => array('5F0087', 54), + '5F00AF' => array('5F00AF', 55), + '5F00D7' => array('5F00D7', 56), + '5F00FF' => array('5F00FF', 57), + '5F5F00' => array('5F5F00', 58), + '5F5F5F' => array('5F5F5F', 59), + '5F5F87' => array('5F5F87', 60), + '5F5FAF' => array('5F5FAF', 61), + '5F5FD7' => array('5F5FD7', 62), + '5F5FFF' => array('5F5FFF', 63), + '5F8700' => array('5F8700', 64), + '5F875F' => array('5F875F', 65), + '5F8787' => array('5F8787', 66), + '5F87AF' => array('5F87AF', 67), + '5F87D7' => array('5F87D7', 68), + '5F87FF' => array('5F87FF', 69), + '5FAF00' => array('5FAF00', 70), + '5FAF5F' => array('5FAF5F', 71), + '5FAF87' => array('5FAF87', 72), + '5FAFAF' => array('5FAFAF', 73), + '5FAFD7' => array('5FAFD7', 74), + '5FAFFF' => array('5FAFFF', 75), + '5FD700' => array('5FD700', 76), + '5FD75F' => array('5FD75F', 77), + '5FD787' => array('5FD787', 78), + '5FD7AF' => array('5FD7AF', 79), + '5FD7D7' => array('5FD7D7', 80), + '5FD7FF' => array('5FD7FF', 81), + '5FFF00' => array('5FFF00', 82), + '5FFF5F' => array('5FFF5F', 83), + '5FFF87' => array('5FFF87', 84), + '5FFFAF' => array('5FFFAF', 85), + '5FFFD7' => array('5FFFD7', 86), + '5FFFFF' => array('5FFFFF', 87), + '870000' => array('870000', 88), + '87005F' => array('87005F', 89), + '870087' => array('870087', 90), + '8700AF' => array('8700AF', 91), + '8700D7' => array('8700D7', 92), + '8700FF' => array('8700FF', 93), + '875F00' => array('875F00', 94), + '875F5F' => array('875F5F', 95), + '875F87' => array('875F87', 96), + '875FAF' => array('875FAF', 97), + '875FD7' => array('875FD7', 98), + '875FFF' => array('875FFF', 99), + '878700' => array('878700', 100), + '87875F' => array('87875F', 101), + '878787' => array('878787', 102), + '8787AF' => array('8787AF', 103), + '8787D7' => array('8787D7', 104), + '8787FF' => array('8787FF', 105), + '87AF00' => array('87AF00', 106), + '87AF5F' => array('87AF5F', 107), + '87AF87' => array('87AF87', 108), + '87AFAF' => array('87AFAF', 109), + '87AFD7' => array('87AFD7', 110), + '87AFFF' => array('87AFFF', 111), + '87D700' => array('87D700', 112), + '87D75F' => array('87D75F', 113), + '87D787' => array('87D787', 114), + '87D7AF' => array('87D7AF', 115), + '87D7D7' => array('87D7D7', 116), + '87D7FF' => array('87D7FF', 117), + '87FF00' => array('87FF00', 118), + '87FF5F' => array('87FF5F', 119), + '87FF87' => array('87FF87', 120), + '87FFAF' => array('87FFAF', 121), + '87FFD7' => array('87FFD7', 122), + '87FFFF' => array('87FFFF', 123), + 'AF0000' => array('AF0000', 124), + 'AF005F' => array('AF005F', 125), + 'AF0087' => array('AF0087', 126), + 'AF00AF' => array('AF00AF', 127), + 'AF00D7' => array('AF00D7', 128), + 'AF00FF' => array('AF00FF', 129), + 'AF5F00' => array('AF5F00', 130), + 'AF5F5F' => array('AF5F5F', 131), + 'AF5F87' => array('AF5F87', 132), + 'AF5FAF' => array('AF5FAF', 133), + 'AF5FD7' => array('AF5FD7', 134), + 'AF5FFF' => array('AF5FFF', 135), + 'AF8700' => array('AF8700', 136), + 'AF875F' => array('AF875F', 137), + 'AF8787' => array('AF8787', 138), + 'AF87AF' => array('AF87AF', 139), + 'AF87D7' => array('AF87D7', 140), + 'AF87FF' => array('AF87FF', 141), + 'AFAF00' => array('AFAF00', 142), + 'AFAF5F' => array('AFAF5F', 143), + 'AFAF87' => array('AFAF87', 144), + 'AFAFAF' => array('AFAFAF', 145), + 'AFAFD7' => array('AFAFD7', 146), + 'AFAFFF' => array('AFAFFF', 147), + 'AFD700' => array('AFD700', 148), + 'AFD75F' => array('AFD75F', 149), + 'AFD787' => array('AFD787', 150), + 'AFD7AF' => array('AFD7AF', 151), + 'AFD7D7' => array('AFD7D7', 152), + 'AFD7FF' => array('AFD7FF', 153), + 'AFFF00' => array('AFFF00', 154), + 'AFFF5F' => array('AFFF5F', 155), + 'AFFF87' => array('AFFF87', 156), + 'AFFFAF' => array('AFFFAF', 157), + 'AFFFD7' => array('AFFFD7', 158), + 'AFFFFF' => array('AFFFFF', 159), + 'D70000' => array('D70000', 160), + 'D7005F' => array('D7005F', 161), + 'D70087' => array('D70087', 162), + 'D700AF' => array('D700AF', 163), + 'D700D7' => array('D700D7', 164), + 'D700FF' => array('D700FF', 165), + 'D75F00' => array('D75F00', 166), + 'D75F5F' => array('D75F5F', 167), + 'D75F87' => array('D75F87', 168), + 'D75FAF' => array('D75FAF', 169), + 'D75FD7' => array('D75FD7', 170), + 'D75FFF' => array('D75FFF', 171), + 'D78700' => array('D78700', 172), + 'D7875F' => array('D7875F', 173), + 'D78787' => array('D78787', 174), + 'D787AF' => array('D787AF', 175), + 'D787D7' => array('D787D7', 176), + 'D787FF' => array('D787FF', 177), + 'D7AF00' => array('D7AF00', 178), + 'D7AF5F' => array('D7AF5F', 179), + 'D7AF87' => array('D7AF87', 180), + 'D7AFAF' => array('D7AFAF', 181), + 'D7AFD7' => array('D7AFD7', 182), + 'D7AFFF' => array('D7AFFF', 183), + 'D7D700' => array('D7D700', 184), + 'D7D75F' => array('D7D75F', 185), + 'D7D787' => array('D7D787', 186), + 'D7D7AF' => array('D7D7AF', 187), + 'D7D7D7' => array('D7D7D7', 188), + 'D7D7FF' => array('D7D7FF', 189), + 'D7FF00' => array('D7FF00', 190), + 'D7FF5F' => array('D7FF5F', 191), + 'D7FF87' => array('D7FF87', 192), + 'D7FFAF' => array('D7FFAF', 193), + 'D7FFD7' => array('D7FFD7', 194), + 'D7FFFF' => array('D7FFFF', 195), + 'FF0000' => array('FF0000', 196), + 'FF005F' => array('FF005F', 197), + 'FF0087' => array('FF0087', 198), + 'FF00AF' => array('FF00AF', 199), + 'FF00D7' => array('FF00D7', 200), + 'FF00FF' => array('FF00FF', 201), + 'FF5F00' => array('FF5F00', 202), + 'FF5F5F' => array('FF5F5F', 203), + 'FF5F87' => array('FF5F87', 204), + 'FF5FAF' => array('FF5FAF', 205), + 'FF5FD7' => array('FF5FD7', 206), + 'FF5FFF' => array('FF5FFF', 207), + 'FF8700' => array('FF8700', 208), + 'FF875F' => array('FF875F', 209), + 'FF8787' => array('FF8787', 210), + 'FF87AF' => array('FF87AF', 211), + 'FF87D7' => array('FF87D7', 212), + 'FF87FF' => array('FF87FF', 213), + 'FFAF00' => array('FFAF00', 214), + 'FFAF5F' => array('FFAF5F', 215), + 'FFAF87' => array('FFAF87', 216), + 'FFAFAF' => array('FFAFAF', 217), + 'FFAFD7' => array('FFAFD7', 218), + 'FFAFFF' => array('FFAFFF', 219), + 'FFD700' => array('FFD700', 220), + 'FFD75F' => array('FFD75F', 221), + 'FFD787' => array('FFD787', 222), + 'FFD7AF' => array('FFD7AF', 223), + 'FFD7D7' => array('FFD7D7', 224), + 'FFD7FF' => array('FFD7FF', 225), + 'FFFF00' => array('FFFF00', 226), + 'FFFF5F' => array('FFFF5F', 227), + 'FFFF87' => array('FFFF87', 228), + 'FFFFAF' => array('FFFFAF', 229), + 'FFFFD7' => array('FFFFD7', 230), + 'FFFFFF' => array('FFFFFF', 231), + '080808' => array('080808', 232), + '121212' => array('121212', 233), + '1C1C1C' => array('1C1C1C', 234), + '262626' => array('262626', 235), + '303030' => array('303030', 236), + '3A3A3A' => array('3A3A3A', 237), + '444444' => array('444444', 238), + '4E4E4E' => array('4E4E4E', 239), + '585858' => array('585858', 240), + '626262' => array('626262', 241), + '6C6C6C' => array('6C6C6C', 242), + '767676' => array('767676', 243), + '808080' => array('808080', 244), + '8A8A8A' => array('8A8A8A', 245), + '949494' => array('949494', 246), + '9E9E9E' => array('9E9E9E', 247), + 'A8A8A8' => array('A8A8A8', 248), + 'B2B2B2' => array('B2B2B2', 249), + 'BCBCBC' => array('BCBCBC', 250), + 'C6C6C6' => array('C6C6C6', 251), + 'D0D0D0' => array('D0D0D0', 252), + 'DADADA' => array('DADADA', 253), + 'E4E4E4' => array('E4E4E4', 254), + 'EEEEEE' => array('EEEEEE', 255), ); - foreach ($ahexs as $hex => $gcode) { - $color = Xterm256::calculate($hex); - $r = new \ReflectionClass($color); - $gcode = sprintf('%%s;5;%s', $gcode); - $this->assertEquals($gcode, $r->getStaticPropertyValue('color')); - } } - public function testExactHexCodeInputs() + + /** + * @dataProvider exactHexCodes + */ + public function testExactHexCodeInputs($hex, $gcode) { - $ehexs = array( - '000000' => 16, - '00005F' => 17, - '000087' => 18, - '0000AF' => 19, - '0000D7' => 20, - '0000FF' => 21, - '005F00' => 22, - '005F5F' => 23, - '005F87' => 24, - '005FAF' => 25, - '005FD7' => 26, - '005FFF' => 27, - '008700' => 28, - '00875F' => 29, - '008787' => 30, - '0087AF' => 31, - '0087D7' => 32, - '0087FF' => 33, - '00AF00' => 34, - '00AF5F' => 35, - '00AF87' => 36, - '00AFAF' => 37, - '00AFD7' => 38, - '00AFFF' => 39, - '00D700' => 40, - '00D75F' => 41, - '00D787' => 42, - '00D7AF' => 43, - '00D7D7' => 44, - '00D7FF' => 45, - '00FF00' => 46, - '00FF5F' => 47, - '00FF87' => 48, - '00FFAF' => 49, - '00FFD7' => 50, - '00FFFF' => 51, - '5F0000' => 52, - '5F005F' => 53, - '5F0087' => 54, - '5F00AF' => 55, - '5F00D7' => 56, - '5F00FF' => 57, - '5F5F00' => 58, - '5F5F5F' => 59, - '5F5F87' => 60, - '5F5FAF' => 61, - '5F5FD7' => 62, - '5F5FFF' => 63, - '5F8700' => 64, - '5F875F' => 65, - '5F8787' => 66, - '5F87AF' => 67, - '5F87D7' => 68, - '5F87FF' => 69, - '5FAF00' => 70, - '5FAF5F' => 71, - '5FAF87' => 72, - '5FAFAF' => 73, - '5FAFD7' => 74, - '5FAFFF' => 75, - '5FD700' => 76, - '5FD75F' => 77, - '5FD787' => 78, - '5FD7AF' => 79, - '5FD7D7' => 80, - '5FD7FF' => 81, - '5FFF00' => 82, - '5FFF5F' => 83, - '5FFF87' => 84, - '5FFFAF' => 85, - '5FFFD7' => 86, - '5FFFFF' => 87, - '870000' => 88, - '87005F' => 89, - '870087' => 90, - '8700AF' => 91, - '8700D7' => 92, - '8700FF' => 93, - '875F00' => 94, - '875F5F' => 95, - '875F87' => 96, - '875FAF' => 97, - '875FD7' => 98, - '875FFF' => 99, - '878700' => 100, - '87875F' => 101, - '878787' => 102, - '8787AF' => 103, - '8787D7' => 104, - '8787FF' => 105, - '87AF00' => 106, - '87AF5F' => 107, - '87AF87' => 108, - '87AFAF' => 109, - '87AFD7' => 110, - '87AFFF' => 111, - '87D700' => 112, - '87D75F' => 113, - '87D787' => 114, - '87D7AF' => 115, - '87D7D7' => 116, - '87D7FF' => 117, - '87FF00' => 118, - '87FF5F' => 119, - '87FF87' => 120, - '87FFAF' => 121, - '87FFD7' => 122, - '87FFFF' => 123, - 'AF0000' => 124, - 'AF005F' => 125, - 'AF0087' => 126, - 'AF00AF' => 127, - 'AF00D7' => 128, - 'AF00FF' => 129, - 'AF5F00' => 130, - 'AF5F5F' => 131, - 'AF5F87' => 132, - 'AF5FAF' => 133, - 'AF5FD7' => 134, - 'AF5FFF' => 135, - 'AF8700' => 136, - 'AF875F' => 137, - 'AF8787' => 138, - 'AF87AF' => 139, - 'AF87D7' => 140, - 'AF87FF' => 141, - 'AFAF00' => 142, - 'AFAF5F' => 143, - 'AFAF87' => 144, - 'AFAFAF' => 145, - 'AFAFD7' => 146, - 'AFAFFF' => 147, - 'AFD700' => 148, - 'AFD75F' => 149, - 'AFD787' => 150, - 'AFD7AF' => 151, - 'AFD7D7' => 152, - 'AFD7FF' => 153, - 'AFFF00' => 154, - 'AFFF5F' => 155, - 'AFFF87' => 156, - 'AFFFAF' => 157, - 'AFFFD7' => 158, - 'AFFFFF' => 159, - 'D70000' => 160, - 'D7005F' => 161, - 'D70087' => 162, - 'D700AF' => 163, - 'D700D7' => 164, - 'D700FF' => 165, - 'D75F00' => 166, - 'D75F5F' => 167, - 'D75F87' => 168, - 'D75FAF' => 169, - 'D75FD7' => 170, - 'D75FFF' => 171, - 'D78700' => 172, - 'D7875F' => 173, - 'D78787' => 174, - 'D787AF' => 175, - 'D787D7' => 176, - 'D787FF' => 177, - 'D7AF00' => 178, - 'D7AF5F' => 179, - 'D7AF87' => 180, - 'D7AFAF' => 181, - 'D7AFD7' => 182, - 'D7AFFF' => 183, - 'D7D700' => 184, - 'D7D75F' => 185, - 'D7D787' => 186, - 'D7D7AF' => 187, - 'D7D7D7' => 188, - 'D7D7FF' => 189, - 'D7FF00' => 190, - 'D7FF5F' => 191, - 'D7FF87' => 192, - 'D7FFAF' => 193, - 'D7FFD7' => 194, - 'D7FFFF' => 195, - 'FF0000' => 196, - 'FF005F' => 197, - 'FF0087' => 198, - 'FF00AF' => 199, - 'FF00D7' => 200, - 'FF00FF' => 201, - 'FF5F00' => 202, - 'FF5F5F' => 203, - 'FF5F87' => 204, - 'FF5FAF' => 205, - 'FF5FD7' => 206, - 'FF5FFF' => 207, - 'FF8700' => 208, - 'FF875F' => 209, - 'FF8787' => 210, - 'FF87AF' => 211, - 'FF87D7' => 212, - 'FF87FF' => 213, - 'FFAF00' => 214, - 'FFAF5F' => 215, - 'FFAF87' => 216, - 'FFAFAF' => 217, - 'FFAFD7' => 218, - 'FFAFFF' => 219, - 'FFD700' => 220, - 'FFD75F' => 221, - 'FFD787' => 222, - 'FFD7AF' => 223, - 'FFD7D7' => 224, - 'FFD7FF' => 225, - 'FFFF00' => 226, - 'FFFF5F' => 227, - 'FFFF87' => 228, - 'FFFFAF' => 229, - 'FFFFD7' => 230, - 'FFFFFF' => 231, - '080808' => 232, - '121212' => 233, - '1C1C1C' => 234, - '262626' => 235, - '303030' => 236, - '3A3A3A' => 237, - '444444' => 238, - '4E4E4E' => 239, - '585858' => 240, - '626262' => 241, - '6C6C6C' => 242, - '767676' => 243, - '808080' => 244, - '8A8A8A' => 245, - '949494' => 246, - '9E9E9E' => 247, - 'A8A8A8' => 248, - 'B2B2B2' => 249, - 'BCBCBC' => 250, - 'C6C6C6' => 251, - 'D0D0D0' => 252, - 'DADADA' => 253, - 'E4E4E4' => 254, - 'EEEEEE' => 255, - ); - foreach ($ehexs as $hex => $gcode) { - $color = Xterm256::calculate($hex); - $r = new \ReflectionClass($color); - $gcode = sprintf('%%s;5;%s', $gcode); - $this->assertEquals($gcode, $r->getStaticPropertyValue('color')); - } + $color = Xterm256::calculate($hex); + $r = new ReflectionClass($color); + $gcode = sprintf('%%s;5;%s', $gcode); + $this->assertEquals($gcode, $r->getStaticPropertyValue('color')); } } From c1273c6189e77dcf4e34487d1ccae60314c184f6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 21 Jan 2013 16:59:47 -0600 Subject: [PATCH 10/10] More catches from @Maks3w - Many of the classes had malformed docblocks: /** * @package ... * @subpackage ... */ - Some had subversion strings! --- src/Getopt.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Getopt.php b/src/Getopt.php index 36c34fd..e7de857 100644 --- a/src/Getopt.php +++ b/src/Getopt.php @@ -67,9 +67,6 @@ * Example: 'abc:' means options '-a', '-b', and '-c' * are legal, and the latter requires a string parameter. * - * @version Release: @package_version@ - * @since Class available since Release 0.6.0 - * * @todo Handle flags that implicitly print usage message, e.g. --help * * @todo Enable user to specify header and footer content in the help message.