1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-20 20:51:20 +02:00

added support of rgba

This commit is contained in:
Oliver Vogel
2013-02-03 14:59:51 +01:00
parent 486743dfcf
commit 0f36ff6562
2 changed files with 57 additions and 7 deletions

View File

@@ -551,6 +551,8 @@ class Image
*/ */
public function parseColor($value) public function parseColor($value)
{ {
$alpha = 0;
if (is_array($value)) { if (is_array($value)) {
// parse color array like: array(155, 155, 155) // parse color array like: array(155, 155, 155)
@@ -561,23 +563,39 @@ class Image
// parse color string in hexidecimal format like #cccccc or cccccc or ccc // 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)) { 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]; $r = strlen($matches[1]) == '1' ? '0x'.$matches[1].$matches[1] : '0x'.$matches[1];
$g = strlen($matches[2]) == '1' ? $matches[2].$matches[2] : $matches[2]; $g = strlen($matches[2]) == '1' ? '0x'.$matches[2].$matches[2] : '0x'.$matches[2];
$b = strlen($matches[3]) == '1' ? $matches[3].$matches[3] : $matches[3]; $b = strlen($matches[3]) == '1' ? '0x'.$matches[3].$matches[3] : '0x'.$matches[3];
// parse color string in format rgb(140, 140, 140) // 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)) { } 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; $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$g = ($matches[1] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$b = ($matches[1] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 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)) { 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 { } else {

View File

@@ -292,6 +292,38 @@ class ImageTest extends PHPUnit_Framework_Testcase
$color = $img->parseColor('rgb(0,0,0)'); $color = $img->parseColor('rgb(0,0,0)');
$this->assertInternalType('int', $color); $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() public function testBrightnessImage()