implemented Color
This commit is contained in:
parent
bc3090325a
commit
4e58fd02a7
2 changed files with 226 additions and 0 deletions
114
src/Primitives/Color.php
Normal file
114
src/Primitives/Color.php
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ItsAMirko\RayTracer\Primitives;
|
||||
|
||||
/**
|
||||
* Class Color
|
||||
*
|
||||
* @package ItsAMirko\RayTracer\Primitives
|
||||
*/
|
||||
class Color
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $red;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $green;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $blue;
|
||||
|
||||
|
||||
/**
|
||||
* Color constructor.
|
||||
*
|
||||
* @param float $red
|
||||
* @param float $green
|
||||
* @param float $blue
|
||||
*/
|
||||
public function __construct(float $red, float $green, float $blue)
|
||||
{
|
||||
$this->red = $red;
|
||||
$this->green = $green;
|
||||
$this->blue = $blue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function red(): float
|
||||
{
|
||||
return $this->red;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function green(): float
|
||||
{
|
||||
return $this->green;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function blue(): float
|
||||
{
|
||||
return $this->blue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Color $color
|
||||
*
|
||||
* @return Color
|
||||
*/
|
||||
public function plusColor(Color $color): Color
|
||||
{
|
||||
return new Color($this->red + $color->red(), $this->green + $color->green(), $this->blue + $color->blue());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Color $color
|
||||
*
|
||||
* @return Color
|
||||
*/
|
||||
public function minusColor(Color $color): Color
|
||||
{
|
||||
return new Color($this->red - $color->red(), $this->green - $color->green(), $this->blue - $color->blue());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param float $multiplier
|
||||
*
|
||||
* @return Color
|
||||
*/
|
||||
public function multiplyWithScalar(float $multiplier): Color
|
||||
{
|
||||
return new Color($this->red * $multiplier, $this->green * $multiplier, $this->blue * $multiplier);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Color $color
|
||||
*
|
||||
* @return Color
|
||||
*/
|
||||
public function multiplyWithColor(Color $color): Color
|
||||
{
|
||||
return new Color($this->red * $color->red(), $this->green * $color->green(), $this->blue * $color->blue());
|
||||
}
|
||||
}
|
||||
112
tests/Primitives/ColorTest.php
Normal file
112
tests/Primitives/ColorTest.php
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ItsAMirko\RayTracer\Primitives;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use function random_int;
|
||||
|
||||
class ColorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $red;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $green;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $blue;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->red = random_int(0, 100) / 100;
|
||||
$this->green = random_int(0, 100) / 100;
|
||||
$this->blue = random_int(0, 100) / 100;
|
||||
}
|
||||
|
||||
|
||||
public function testProvidedColorValues(): void
|
||||
{
|
||||
$color = new Color($this->red, $this->green, $this->blue);
|
||||
|
||||
$this->assertSame($this->red, $color->red());
|
||||
$this->assertSame($this->green, $color->green());
|
||||
$this->assertSame($this->blue, $color->blue());
|
||||
}
|
||||
|
||||
|
||||
public function testCreatesNewColorByAddingAColor(): void
|
||||
{
|
||||
$color1 = new Color(1.0, 2.0, 3.0);
|
||||
$color2 = new Color($this->red, $this->green, $this->blue);
|
||||
|
||||
$actualColor = $color1->plusColor($color2);
|
||||
|
||||
$this->assertInstanceOf(Color::class, $actualColor);
|
||||
$this->assertSame(1 + $this->red, $actualColor->red());
|
||||
$this->assertSame(2 + $this->green, $actualColor->green());
|
||||
$this->assertSame(3 + $this->blue, $actualColor->blue());
|
||||
}
|
||||
|
||||
|
||||
public function testCreatesNewColorBySubtractingAColor(): void
|
||||
{
|
||||
$color1 = new Color(1.0, 2.0, 3.0);
|
||||
$color2 = new Color($this->red, $this->green, $this->blue);
|
||||
|
||||
$actualColor = $color1->minusColor($color2);
|
||||
|
||||
$this->assertInstanceOf(Color::class, $actualColor);
|
||||
$this->assertSame(1 - $this->red, $actualColor->red());
|
||||
$this->assertSame(2 - $this->green, $actualColor->green());
|
||||
$this->assertSame(3 - $this->blue, $actualColor->blue());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatesNewColorByMultiplyingWithAScalar(): void
|
||||
{
|
||||
$color = new Color($this->red, $this->green, $this->blue);
|
||||
$scalar = random_int(0, 100) / 100;
|
||||
|
||||
$actualColor = $color->multiplyWithScalar($scalar);
|
||||
|
||||
$this->assertInstanceOf(Color::class, $actualColor);
|
||||
$this->assertSame($scalar * $this->red, $actualColor->red());
|
||||
$this->assertSame($scalar * $this->green, $actualColor->green());
|
||||
$this->assertSame($scalar * $this->blue, $actualColor->blue());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatesNewColorByMultiplyingWithAColor(): void
|
||||
{
|
||||
$color1 = new Color(1.0, 2.0, 3.0);
|
||||
$color2 = new Color($this->red, $this->green, $this->blue);
|
||||
|
||||
$actualColor = $color1->multiplyWithColor($color2);
|
||||
|
||||
$this->assertInstanceOf(Color::class, $actualColor);
|
||||
$this->assertSame(1.0 * $this->red, $actualColor->red());
|
||||
$this->assertSame(2.0 * $this->green, $actualColor->green());
|
||||
$this->assertSame(3.0 * $this->blue, $actualColor->blue());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue