diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index f2a868dc..e695de03 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -551,6 +551,8 @@ class Image */ public function parseColor($value) { + $alpha = 0; + if (is_array($value)) { // parse color array like: array(155, 155, 155) @@ -561,24 +563,40 @@ class Image // 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]; + $r = strlen($matches[1]) == '1' ? '0x'.$matches[1].$matches[1] : '0x'.$matches[1]; + $g = strlen($matches[2]) == '1' ? '0x'.$matches[2].$matches[2] : '0x'.$matches[2]; + $b = strlen($matches[3]) == '1' ? '0x'.$matches[3].$matches[3] : '0x'.$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; + $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; + $b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0; + // parse color string in format rgba(255, 0, 0, 0.5) + } elseif (preg_match('/^rgba ?\(([0-9]{1,3}), ?([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[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; + } + } } } if (isset($r) && isset($g) && isset($b)) { - return imagecolorallocate($this->resource, '0x'.$r, '0x'.$g, '0x'.$b); - + return imagecolorallocatealpha($this->resource, $r, $g, $b, $alpha); + } else { throw new Exception("Error parsing color [{$value}]"); diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 2a0f7c65..1b42f825 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -292,6 +292,38 @@ class ImageTest extends PHPUnit_Framework_Testcase $color = $img->parseColor('rgb(0,0,0)'); $this->assertInternalType('int', $color); + $color = $img->parseColor('rgba(0,0,0,0.5)'); + $this->assertInternalType('int', $color); + + $color = $img->parseColor('rgba(255, 0, 0, 0.5)'); + $this->assertInternalType('int', $color); + } + + public function testAdvancedColors() + { + $img = new Image(null, 100, 100); + $img->fill('rgb(255, 0, 0)'); + + $checkColor = $img->pickColor(50, 50,'array'); + $this->assertEquals($checkColor['red'], 255); + $this->assertEquals($checkColor['green'], 0); + $this->assertEquals($checkColor['blue'], 0); + $this->assertEquals($checkColor['alpha'], 0); + + $img->rectangle('rgba(0,0,0,0.5)', 0, 0, 100, 100); + $checkColor = $img->pickColor(50, 50,'array'); + $this->assertEquals($checkColor['red'], 128); + $this->assertEquals($checkColor['green'], 0); + $this->assertEquals($checkColor['blue'], 0); + $this->assertEquals($checkColor['alpha'], 0); + + $img = new Image(null, 100, 100); + $img->fill('rgba(0,0,0,0.5)'); + $checkColor = $img->pickColor(50, 50,'array'); + $this->assertEquals($checkColor['red'], 0); + $this->assertEquals($checkColor['green'], 0); + $this->assertEquals($checkColor['blue'], 0); + $this->assertEquals($checkColor['alpha'], 64); } public function testBrightnessImage()