From 2913c698a573ccf77f0e8c171d49d5972568aa70 Mon Sep 17 00:00:00 2001 From: Glynn Forrest Date: Thu, 23 Apr 2015 23:50:03 +0100 Subject: [PATCH] Add support for 3 character HTML colors --- library/ZendPdf/Color/Html.php | 20 ++++++++++---- tests/ZendPdf/Color/HtmlTest.php | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 tests/ZendPdf/Color/HtmlTest.php diff --git a/library/ZendPdf/Color/Html.php b/library/ZendPdf/Color/Html.php index 94bf783..a9839e4 100644 --- a/library/ZendPdf/Color/Html.php +++ b/library/ZendPdf/Color/Html.php @@ -72,16 +72,26 @@ public function getComponents() * Creates a ColorInterface object from the HTML representation. * * @param string $color May either be a hexidecimal number of the form - * #rrggbb or one of the 140 well-known names (black, white, blue, etc.) + * #rrggbb, #rgb or one of the 140 well-known names (black, white, blue, etc.) * @return ColorInterface */ public static function color($color) { - $pattern = '/^#([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})$/'; + $pattern = '/^#([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})$|^#([A-Fa-f0-9])([A-Fa-f0-9])([A-Fa-f0-9])$/'; if (preg_match($pattern, $color, $matches)) { - $r = round((hexdec($matches[1]) / 255), 3); - $g = round((hexdec($matches[2]) / 255), 3); - $b = round((hexdec($matches[3]) / 255), 3); + if (strlen($matches[1]) === 2) { + $r = $matches[1]; + $g = $matches[2]; + $b = $matches[3]; + } else { + $r = $matches[4].$matches[4]; + $g = $matches[5].$matches[5]; + $b = $matches[6].$matches[6]; + } + $r = round((hexdec($r) / 255), 3); + $g = round((hexdec($g) / 255), 3); + $b = round((hexdec($b) / 255), 3); + if (($r == $g) && ($g == $b)) { return new GrayScale($r); } else { diff --git a/tests/ZendPdf/Color/HtmlTest.php b/tests/ZendPdf/Color/HtmlTest.php new file mode 100644 index 0000000..825c338 --- /dev/null +++ b/tests/ZendPdf/Color/HtmlTest.php @@ -0,0 +1,46 @@ +assertSame($components, Html::color($color)->getComponents()); + } +}