From 69b4452e6a627e8e5f78ac793a7cef00aa6dae85 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 3 Feb 2013 16:21:41 +0100 Subject: [PATCH] added support for rgba output of pickColor method --- src/Intervention/Image/Image.php | 41 ++++++++++++++++++++++++-------- tests/ImageTest.php | 12 +++++++++- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index bf525901..be46f561 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -530,6 +530,11 @@ class Image $color = sprintf('rgb(%d, %d, %d)', $color['red'], $color['green'], $color['blue']); break; + case 'rgba': + $color = imagecolorsforindex($this->resource, $color); + $color = sprintf('rgba(%d, %d, %d, %.2f)', $color['red'], $color['green'], $color['blue'], $this->alpha2rgba($color['alpha'])); + break; + case 'hex': $color = imagecolorsforindex($this->resource, $color); $color = sprintf('#%02x%02x%02x', $color['red'], $color['green'], $color['blue']); @@ -585,16 +590,8 @@ class Image $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0; $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; $b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0; - - $range_input = range(1, 0, 1/127); - $range_output = range(0, 127); - - foreach ($range_input as $key => $value) { - if ($value <= ($matches[4])) { - $alpha = $range_output[$key]; - break; - } - } + $alpha = $this->alpha2gd($matches[4]); + } } @@ -625,6 +622,30 @@ class Image return $this; } + private function alpha2gd($input) + { + $range_input = range(1, 0, 1/127); + $range_output = range(0, 127); + + foreach ($range_input as $key => $value) { + if ($value <= $input) { + return $range_output[$key]; + } + } + } + + private function alpha2rgba($input) + { + $range_input = range(0, 127); + $range_output = range(1, 0, 1/127); + + foreach ($range_input as $key => $value) { + if ($value >= $input) { + return round($range_output[$key], 2); + } + } + } + /* public function render($type = 'jpg') { diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 243ecbc1..b32c9e49 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -263,6 +263,17 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertEquals($color['green'], 166); $this->assertInternalType('int', $color['blue']); $this->assertEquals($color['blue'], 0); + + // rgba color string + $color = $img->pickColor(799, 599, 'rgba'); + $this->assertInternalType('string', $color); + $this->assertEquals($color, 'rgba(255, 166, 0, 1.00)'); + $img = new Image(null, 100, 100); + $color = imagecolorallocatealpha($img->resource, 0, 0, 255, 64); + $img->fill($color); + $color = $img->pickColor(50, 50, 'rgba'); + $this->assertInternalType('string', $color); + $this->assertEquals($color, 'rgba(0, 0, 255, 0.50)'); } public function testParseColor() @@ -350,5 +361,4 @@ class ImageTest extends PHPUnit_Framework_Testcase $img->contrast(-50); $this->assertInstanceOf('Intervention\Image\Image', $img); } - } \ No newline at end of file