1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 17:41:58 +02:00

added support for rgba output of pickColor method

This commit is contained in:
Oliver Vogel
2013-02-03 16:21:41 +01:00
parent 928055ac99
commit 69b4452e6a
2 changed files with 42 additions and 11 deletions

View File

@@ -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;
$alpha = $this->alpha2gd($matches[4]);
$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;
}
}
}
}
@@ -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')
{

View File

@@ -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);
}
}