From 4e58fd02a738634e26219414d755f5ae909049ee Mon Sep 17 00:00:00 2001 From: Mirko Janssen Date: Sat, 21 Sep 2019 21:00:28 +0200 Subject: [PATCH] implemented Color --- src/Primitives/Color.php | 114 +++++++++++++++++++++++++++++++++ tests/Primitives/ColorTest.php | 112 ++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 src/Primitives/Color.php create mode 100644 tests/Primitives/ColorTest.php diff --git a/src/Primitives/Color.php b/src/Primitives/Color.php new file mode 100644 index 0000000..c88fb85 --- /dev/null +++ b/src/Primitives/Color.php @@ -0,0 +1,114 @@ +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()); + } +} \ No newline at end of file diff --git a/tests/Primitives/ColorTest.php b/tests/Primitives/ColorTest.php new file mode 100644 index 0000000..ff70a4b --- /dev/null +++ b/tests/Primitives/ColorTest.php @@ -0,0 +1,112 @@ +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()); + } +}