diff --git a/src/Primitives/Color.php b/src/Primitives/Color.php index c88fb85..efcd365 100644 --- a/src/Primitives/Color.php +++ b/src/Primitives/Color.php @@ -69,6 +69,39 @@ class Color } + /** + * @return int + */ + public function redAsHex(): int + { + $percentage = ($this->red < 0) ? 0 : (($this->red > 1) ? 1 : $this->red); + + return (int)round(255 * $percentage); + } + + + /** + * @return int + */ + public function greenAsHex(): int + { + $percentage = ($this->green < 0) ? 0 : (($this->green > 1) ? 1 : $this->green); + + return (int)round(255 * $percentage); + } + + + /** + * @return int + */ + public function blueAsHex(): int + { + $percentage = ($this->blue < 0) ? 0 : (($this->blue > 1) ? 1 : $this->blue); + + return (int)round(255 * $percentage); + } + + /** * @param Color $color * diff --git a/tests/Primitives/ColorTest.php b/tests/Primitives/ColorTest.php index ff70a4b..bc457a0 100644 --- a/tests/Primitives/ColorTest.php +++ b/tests/Primitives/ColorTest.php @@ -39,6 +39,32 @@ class ColorTest extends TestCase } + /** + * @dataProvider parametersForHexValues + * + * @param int $red + * @param int $green + * @param int $blue + * @param Color $color + */ + public function testProvidesHexValues(int $red, int $green, int $blue, Color $color): void + { + $this->assertSame($red, $color->redAsHex()); + $this->assertSame($green, $color->greenAsHex()); + $this->assertSame($blue, $color->blueAsHex()); + } + + + public function parametersForHexValues() + { + return [ + [255, 0, 0, new Color(1.5, 0.0, 0.0)], + [0, 128, 0, new Color(0.0, 0.5, 0.0)], + [0, 0, 255, new Color(-0.5, 0.0, 1.0)], + ]; + } + + public function testProvidedColorValues(): void { $color = new Color($this->red, $this->green, $this->blue);