Skip to content

Commit

Permalink
Added documentation for fadeIn and fadeOut
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Oct 11, 2020
1 parent c723d26 commit 517b880
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 42 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,37 @@ echo $hex->shade(50); // #808080
Set the absolute opacity of a color by a percent.

``` php
$hsl =new Hsl('90,90,50');
$hsl = new Hsl('90,90,50');
echo $hsl->fade(10); // hsla(90,90%,50%,0.1)

$rgb = new Rgb('128,242,13');
echo $rgb->fade(10); // rgba(128,242,13,0.1)
```

#### FadeIn

Increase the opacity of a color by a percent.

``` php
$hsla = new Hsla('90,90,50,0.3');
echo $hsla->fadeIn(10); // hsla(90,90%,50%,0.4)

$rgba = new Rgba('128,242,13,0.3');
echo $rgba->fadeIn(10); // rgba(128,242,13,0.4)
```

#### FadeOut

Decrease the opacity of a color by a percent.

``` php
$hsla = new Hsla('90,90,50,0.3');
echo $hsla->fadeOut(10); // hsla(90,90%,50%,0.2)

$rgba = new Rgba('128,242,13,0.3');
echo $rgba->fadeOut(10); // rgba(128,242,13,0.2)
```

#### Is light or dark

Determine if color is dark or light color.
Expand Down
80 changes: 48 additions & 32 deletions src/BaseColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public function mix(BaseColor $color, $percent = 50)
}

/**
* @link https://github.com/less/less.js/blob/master/packages/less/src/less/functions/color.js
*
* @param int $percent
*
* @return mixed
Expand All @@ -207,6 +209,8 @@ public function tint($percent = 50)
}

/**
* @link https://github.com/less/less.js/blob/master/packages/less/src/less/functions/color.js
*
* @param int $percent
*
* @return mixed
Expand All @@ -219,47 +223,49 @@ public function shade($percent = 50)
}

/**
* @param number $percent
* @link https://github.com/less/less.js/blob/master/packages/less/src/less/functions/color.js
*
* @param $percent
*
* @return OzdemirBurak\Iris\Color\Rgba|\OzdemirBurak\Iris\Color\Hsla;
* @return float|\OzdemirBurak\Iris\Color\Hsla|\OzdemirBurak\Iris\Color\Rgba
*/
public function fade($percent)
{
$percent = $this->clamp($percent / 100);
if ($this instanceof \OzdemirBurak\Iris\Color\Hsl) {
return $this->toHsla()->alpha($percent);
}
return $this->toRgba()->alpha($percent);
}
public function fade($percent)
{
[$model, $percent] = [$this->getColorModelName($this), $this->clamp($percent / 100)];
if ($model === 'Hsl') {
return $this->toHsla()->alpha($percent);
}
return $this->toRgba()->alpha($percent);
}

/**
* @param number $percent
* @param $percent
*
* @return OzdemirBurak\Iris\Color\Rgba|\OzdemirBurak\Iris\Color\Hsla;
* @return float|\OzdemirBurak\Iris\Color\Hsla|\OzdemirBurak\Iris\Color\Rgba
*/
public function fadeIn($percent)
{
$percent = $percent / 100;
if ($this instanceof \OzdemirBurak\Iris\Color\Hsla || $this instanceof \OzdemirBurak\Iris\Color\Rgba) {
return $this->alpha($this->clamp($this->alpha() + $percent));
} elseif ($this instanceof \OzdemirBurak\Iris\Color\Hsl) {
$hsla = $this->toHsla();
return $hsla->alpha($this->clamp($hsla->alpha() + $percent));
}
$rgba = $this->toRgba();
return $rgba->alpha($this->clamp($rgba->alpha() + $percent));
}
public function fadeIn($percent)
{
[$model, $percent] = [$this->getColorModelName($this), $percent / 100];
if ($model === 'Hsla' || $model === 'Rgba') {
return $this->alpha($this->clamp($this->alpha() + $percent));
}
if ($model === 'Hsl') {
$hsla = $this->toHsla();
return $hsla->alpha($this->clamp($hsla->alpha() + $percent));
}
$rgba = $this->toRgba();
return $rgba->alpha($this->clamp($rgba->alpha() + $percent));
}

/**
* @param number $percent
* @param $percent
*
* @return OzdemirBurak\Iris\Color\Rgba|\OzdemirBurak\Iris\Color\Hsla;
* @return float|\OzdemirBurak\Iris\Color\Hsla|\OzdemirBurak\Iris\Color\Rgba
*/
public function fadeOut($percent)
{
return $this->fadeIn(-1 * $percent);
}

public function fadeOut($percent)
{
return $this->fadeIn(-1 * $percent);
}

/**
* @param $value
Expand All @@ -278,7 +284,7 @@ protected function clamp($value)
*/
protected function back(BaseColor $color)
{
return $this->{'to' . substr(strrchr(get_class($color), '\\'), 1)}();
return $this->{'to' . $this->getColorModelName($color)}();
}

/**
Expand All @@ -288,4 +294,14 @@ protected function getExceptionMessage()
{
return 'Invalid ' . strtoupper(substr(static::class, strrpos(static::class, '\\') + 1)) . ' value';
}

/**
* @param \OzdemirBurak\Iris\BaseColor $color
*
* @return false|string
*/
protected function getColorModelName(BaseColor $color)
{
return substr(strrchr(get_class($color), '\\'), 1);
}
}
18 changes: 9 additions & 9 deletions tests/OperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,27 @@ public function testShade()
/**
* @group operations-fade
*/
public function testFade()
{
public function testFade()
{
$this->assertEquals(new Hsla('90,90,50,0.1'), (new Hsl('90,90,50'))->fade(10));
$this->assertEquals(new Rgba('128,242,13,0.1'), (new Rgb('128,242,13'))->fade(10));
}
}

/**
* @group operations-fadeIn
*/
public function testFadeIn()
{
public function testFadeIn()
{
$this->assertEquals(new Hsla('90,90,50,0.4'), (new Hsla('90,90,50,0.3'))->fadeIn(10));
$this->assertEquals(new Rgba('128,242,13,0.4'), (new Rgba('128,242,13,0.3'))->fadeIn(10));
}
}

/**
* @group operations-fadeOut
*/
public function testFadeOut()
{
public function testFadeOut()
{
$this->assertEquals(new Hsla('90,90,50,0.2'), (new Hsla('90,90,50,0.3'))->fadeOut(10));
$this->assertEquals(new Rgba('128,242,13,0.2'), (new Rgba('128,242,13,0.3'))->fadeOut(10));
}
}
}

0 comments on commit 517b880

Please sign in to comment.