From c0972d7bbcbf8e6c23762d2cd335a1da0275dcda Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 3 Feb 2013 12:59:32 +0100 Subject: [PATCH] added pickColor method --- README.md | 1 + src/Intervention/Image/Image.php | 45 +++++++++++++++++++++++++++++--- tests/ImageTest.php | 40 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b2bb8208..e12f8763 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Add the facade of this package to the `$aliases` array. * Image::ellipse - Draw an ellipse centered at given coordinates * Image::circle - Draw a circle centered at given coordinates * Image::pixel - Set single pixel with given hexadecimal color at position x,y +* Image::pickColor - Picks and formats color at position in current image * Image::reset - Reset to original image resource * Image::save - Save image in filesystem diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 70c51cbf..45837dd3 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -486,9 +486,37 @@ class Image return $data; } + /** + * Picks and formats color at position + * + * @param int $x + * @param int $y + * @param string $format + * @return mixed + */ public function pickColor($x, $y, $format = null) { - $colorindex = imagecolorat($this->resource, $x, $y); + // pick color at postion + $color = imagecolorat($this->resource, $x, $y); + + // format color + switch (strtolower($format)) { + case 'rgb': + $color = imagecolorsforindex($this->resource, $color); + $color = sprintf('rgb(%d, %d, %d)', $color['red'], $color['green'], $color['blue']); + break; + + case 'hex': + $color = imagecolorsforindex($this->resource, $color); + $color = sprintf('#%02x%02x%02x', $color['red'], $color['green'], $color['blue']); + break; + + case 'array': + $color = imagecolorsforindex($this->resource, $color); + break; + } + + return $color; } /** @@ -499,19 +527,28 @@ class Image */ public function parseColor($value) { - if (is_array($value)) { // parse color array like: array(155, 155, 155) + if (is_array($value)) { + // parse color array like: array(155, 155, 155) list($r, $g, $b) = $value; - } elseif(is_string($value)) { // parse color in hexidecimal str like #cccccc or cccccc or ccc + } elseif(is_string($value)) { + // parse color string in hexidecimal format like #cccccc or cccccc or ccc if (preg_match('/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i', $value, $matches)) { $r = strlen($matches[1]) == '1' ? $matches[1].$matches[1] : $matches[1]; $g = strlen($matches[2]) == '1' ? $matches[2].$matches[2] : $matches[2]; $b = strlen($matches[3]) == '1' ? $matches[3].$matches[3] : $matches[3]; - } + // parse color string in format rgb(140, 140, 140) + } elseif (preg_match('/^rgb ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3})\)$/i', $value, $matches)) { + + $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0; + $g = ($matches[1] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; + $b = ($matches[1] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0; + + } } if (isset($r) && isset($g) && isset($b)) { diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 9f8c5dfd..cdc2a87a 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -235,6 +235,36 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertInternalType('string', $img); } + public function testPickColor() + { + $img = $this->getTestImage(); + + // int color + $color = $img->pickColor(100, 100); + $this->assertInternalType('int', $color); + $this->assertEquals($color, 16776956); + + // rgb color string + $color = $img->pickColor(799, 599, 'rgb'); + $this->assertInternalType('string', $color); + $this->assertEquals($color, 'rgb(255, 166, 0)'); + + // hex color string + $color = $img->pickColor(799, 599, 'hex'); + $this->assertInternalType('string', $color); + $this->assertEquals($color, '#ffa600'); + + // rgb color array + $color = $img->pickColor(799, 599, 'array'); + $this->assertInternalType('array', $color); + $this->assertInternalType('int', $color['red']); + $this->assertEquals($color['red'], 255); + $this->assertInternalType('int', $color['green']); + $this->assertEquals($color['green'], 166); + $this->assertInternalType('int', $color['blue']); + $this->assertEquals($color['blue'], 0); + } + public function testParseColor() { $img = $this->getTestImage(); @@ -252,6 +282,16 @@ class ImageTest extends PHPUnit_Framework_Testcase $color = $img->parseColor('ccc'); $this->assertInternalType('int', $color); + + $color = $img->parseColor('rgb(1, 14, 144)'); + $this->assertInternalType('int', $color); + + $color = $img->parseColor('rgb (255, 255, 255)'); + $this->assertInternalType('int', $color); + + $color = $img->parseColor('rgb(0,0,0)'); + $this->assertInternalType('int', $color); + } } \ No newline at end of file