diff --git a/lib/Imagine/Color.php b/lib/Imagine/Color.php index 3861cad6c..6a787f3e4 100644 --- a/lib/Imagine/Color.php +++ b/lib/Imagine/Color.php @@ -90,6 +90,59 @@ public function getAlpha() return $this->alpha; } + /** + * Returns a copy of current color, incrementing the alpha channel by the + * given amount + * + * @param integer $alpha + * + * @return Imagine\Color + */ + public function dissolve($alpha) + { + return new Color((string) $this, $this->alpha + $alpha); + } + + /** + * Returns a copy of the current color, darkened by the specified number of + * shades + * + * @param integer $shade + * + * @retun Imagine\Color + */ + public function lighten($shade) + { + return new Color( + array( + min(255, $this->r + $shade), + min(255, $this->g + $shade), + min(255, $this->b + $shade), + ), + $this->alpha + ); + } + + /** + * Returns a copy of the current color, darkened by the specified number of + * shades + * + * @param integer $shade + * + * @retun Imagine\Color + */ + public function darken($shade) + { + return new Color( + array( + max(0, $this->r - $shade), + max(0, $this->g - $shade), + max(0, $this->b - $shade), + ), + $this->alpha + ); + } + /** * Internal * diff --git a/tests/Imagine/ColorTest.php b/tests/Imagine/ColorTest.php index 200279ca7..9dcf92a76 100644 --- a/tests/Imagine/ColorTest.php +++ b/tests/Imagine/ColorTest.php @@ -104,4 +104,28 @@ public function testShortColorNotationAndLongNotationProducesTheSameColor() $this->assertEquals(new Color('#fff'), new Color('ffffff')); $this->assertEquals(new Color('fff'), new Color('#ffffff')); } + + public function testShouldLigtenColor() + { + $color = new Color('000'); + + $this->assertEquals(new Color('222'), $color->lighten(34)); + $this->assertEquals(new Color('fff'), $color->lighten(300)); + } + + public function testShouldDarnenColor() + { + $color = new Color('fff'); + + $this->assertEquals(new Color('ddd'), $color->darken(34)); + $this->assertEquals(new Color('000'), $color->darken(300)); + } + + public function testShouldDissolveColor() + { + $color = new Color('fff'); + + $this->assertEquals(new Color('fff', 50), $color->dissolve(50)); + $this->assertEquals(new Color('fff'), $color->dissolve(100)->dissolve(-100)); + } }