Skip to content

Commit

Permalink
added helper methods for color transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
avalanche123 committed Feb 27, 2011
1 parent f80c68a commit f28268b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/Imagine/Color.php
Expand Up @@ -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);
}

/**

This comment has been minimized.

Copy link
@henrikbjorn

henrikbjorn Feb 27, 2011

typo

This comment has been minimized.

Copy link
@avalanche123

avalanche123 Feb 27, 2011

Author Collaborator

fixed, thanks!

* 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
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Imagine/ColorTest.php
Expand Up @@ -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));
}
}

0 comments on commit f28268b

Please sign in to comment.