diff --git a/src/Hex.php b/src/Hex.php index 9c0793c..165c128 100644 --- a/src/Hex.php +++ b/src/Hex.php @@ -10,110 +10,103 @@ class Hex { - private $hex; - private $brightnessModifier; - private $rgbConverter; - - const HEX_MIN_LENGTH = 3; - const HEX_MAX_LENGTH = 6; - - public function __construct( $hex ) - { - $this->hex = $hex; - $this->brightnessModifier = new HexBrightnessModifier; - $this->rgbConverter = new HexToRgb; - - $this->validateHex(); - } - - public function __toString() - { - return (string) $this->hex; - } - - /** - * @param int $percentage - * - * @return static - */ - public function lighten( $percentage = 0 ) - { - $this->validatePercentage( $percentage ); - - if ( $percentage === 0 ) - { - return new static($this->hex); - } - - return new static($this->brightnessModifier->adjustBrightness( $this->hex, $percentage )); - } - - /** - * @param int $percentage - * - * @return static - */ - public function darken( $percentage = 0 ) - { - $this->validatePercentage( $percentage ); - - if ( $percentage === 0 ) - { - return new static( $this->hex ); - } - - return new static( $this->brightnessModifier->adjustBrightness( $this->hex, $percentage * -1 ) ); - } - - /** - * @return array - */ - public function toRgb() - { - return $this->rgbConverter->convertToRgb( $this->hex ); - } - - /** - * @param integer $percentage - * - * @throws PercentageTooHighException - * @throws PercentageTooLowException - * @throws PercentageIsNotAnInteger - */ - private function validatePercentage( $percentage ) - { - if ( ! is_int( $percentage ) ) - { - throw new PercentageIsNotAnInteger( "The percentage ({$percentage}) is not an integer." ); - } - - if ( $percentage < 0 ) - { - throw new PercentageTooLowException( "The percentage ({$percentage}) is below zero (0)" ); - } - - if ( $percentage > 100 ) - { - throw new PercentageTooHighException( "The percentage ({$percentage}) is above 100" ); - } - } - - /** - * @throws HexTooLongException - * @throws HexTooShortException - */ - private function validateHex() - { - $hex = str_replace( '#', '', $this->hex ); - - if ( strlen( $hex ) < self::HEX_MIN_LENGTH ) - { - throw new HexTooShortException( "The hex ({$hex}) was too short. Minimum length is: " . self::HEX_MIN_LENGTH . ' characters, without hashtag.' ); - } - - if ( strlen( $hex ) > self::HEX_MAX_LENGTH ) - { - throw new HexTooLongException( "The hex ({$hex}) was too long. Maximum length is: " . self::HEX_MAX_LENGTH . ' characters, without hashtag.' ); - } - } + private $hex; + private $brightnessModifier; + private $rgbConverter; + + const HEX_MIN_LENGTH = 3; + const HEX_MAX_LENGTH = 6; + + public function __construct($hex) + { + $this->hex = $hex; + $this->brightnessModifier = new HexBrightnessModifier(); + $this->rgbConverter = new HexToRgb(); + + $this->validateHex(); + } + + public function __toString() + { + return (string) $this->hex; + } + + /** + * @param int $percentage + * + * @return static + */ + public function lighten($percentage = 0) + { + $this->validatePercentage($percentage); + + if ($percentage === 0) { + return new static($this->hex); + } + + return new static($this->brightnessModifier->adjustBrightness($this->hex, $percentage)); + } + + /** + * @param int $percentage + * + * @return static + */ + public function darken($percentage = 0) + { + $this->validatePercentage($percentage); + + if ($percentage === 0) { + return new static($this->hex); + } + + return new static($this->brightnessModifier->adjustBrightness($this->hex, $percentage * -1)); + } + + /** + * @return array + */ + public function toRgb() + { + return $this->rgbConverter->convertToRgb($this->hex); + } + + /** + * @param int $percentage + * + * @throws PercentageTooHighException + * @throws PercentageTooLowException + * @throws PercentageIsNotAnInteger + */ + private function validatePercentage($percentage) + { + if (!is_int($percentage)) { + throw new PercentageIsNotAnInteger("The percentage ({$percentage}) is not an integer."); + } + + if ($percentage < 0) { + throw new PercentageTooLowException("The percentage ({$percentage}) is below zero (0)"); + } + + if ($percentage > 100) { + throw new PercentageTooHighException("The percentage ({$percentage}) is above 100"); + } + } + + /** + * @throws HexTooLongException + * @throws HexTooShortException + */ + private function validateHex() + { + $hex = str_replace('#', '', $this->hex); + + if (strlen($hex) < self::HEX_MIN_LENGTH) { + throw new HexTooShortException("The hex ({$hex}) was too short. Minimum length is: ".self::HEX_MIN_LENGTH.' characters, without hashtag.'); + } + + if (strlen($hex) > self::HEX_MAX_LENGTH) { + throw new HexTooLongException("The hex ({$hex}) was too long. Maximum length is: ".self::HEX_MAX_LENGTH.' characters, without hashtag.'); + } + } } diff --git a/src/HexBrightnessModifier.php b/src/HexBrightnessModifier.php index e81d1b2..1107443 100644 --- a/src/HexBrightnessModifier.php +++ b/src/HexBrightnessModifier.php @@ -9,14 +9,14 @@ class HexBrightnessModifier const STEPS_MIN = -255; private $hadHashtag = false; - private $parser; + private $parser; - public function __construct() - { - $this->parser = new HexParser; - } + public function __construct() + { + $this->parser = new HexParser(); + } - /** + /** * @param string $hex * @param int $percentage * @@ -26,9 +26,9 @@ public function adjustBrightness($hex, $percentage) { $steps = $this->generateSteps($percentage); - $this->hadHashtag = $hex[0] === '#'; + $this->hadHashtag = $hex[0] === '#'; - $hex = $this->parser->parse($hex); + $hex = $this->parser->parse($hex); $hex = $this->adjustHex($hex, $steps); // Append hashtag if was inputted with a hashtag diff --git a/src/HexToRgb.php b/src/HexToRgb.php index 0a56308..0f8e4ab 100644 --- a/src/HexToRgb.php +++ b/src/HexToRgb.php @@ -4,38 +4,38 @@ class HexToRgb { - private $parser; + private $parser; - public function __construct() - { - $this->parser = new HexParser; - } + public function __construct() + { + $this->parser = new HexParser(); + } - /** - * @param string $hex - * - * @return array - */ - public function convertToRgb( $hex ) - { - $hex = $this->parser->parse( $hex ); + /** + * @param string $hex + * + * @return array + */ + public function convertToRgb($hex) + { + $hex = $this->parser->parse($hex); - return $this->generateRgb( $hex ); - } + return $this->generateRgb($hex); + } - /** - * @param string $hex - * - * @return array - */ - private function generateRgb( $hex ) - { - list($red, $green, $blue) = array_map('hexdec', str_split($hex, 2)); + /** + * @param string $hex + * + * @return array + */ + private function generateRgb($hex) + { + list($red, $green, $blue) = array_map('hexdec', str_split($hex, 2)); - return [ - 'r' => $red, - 'g' => $green, - 'b' => $blue, - ]; - } + return [ + 'r' => $red, + 'g' => $green, + 'b' => $blue, + ]; + } } diff --git a/tests/HexModifierTest.php b/tests/HexModifierTest.php index 13d4777..0c9e356 100644 --- a/tests/HexModifierTest.php +++ b/tests/HexModifierTest.php @@ -89,7 +89,7 @@ public function test_will_fail_with_too_low_percentage_input() $this->expectException(\LasseRafn\Hexer\Exceptions\PercentageTooLowException::class); $hex = new \LasseRafn\Hexer\Hex('#fff'); - (string) $hex->lighten(-10); + (string) $hex->lighten(-10); } public function test_will_fail_with_too_high_percentage_input() @@ -97,7 +97,7 @@ public function test_will_fail_with_too_high_percentage_input() $this->expectException(\LasseRafn\Hexer\Exceptions\PercentageTooHighException::class); $hex = new \LasseRafn\Hexer\Hex('#fff'); - (string) $hex->lighten(101); + (string) $hex->lighten(101); } public function test_will_fail_with_a_non_integer_percentage_input() @@ -105,7 +105,7 @@ public function test_will_fail_with_a_non_integer_percentage_input() $this->expectException(\LasseRafn\Hexer\Exceptions\PercentageIsNotAnInteger::class); $hex = new \LasseRafn\Hexer\Hex('#fff'); - (string) $hex->lighten('ab12'); + (string) $hex->lighten('ab12'); } public function test_will_return_initial_hex_with_zero_percentage() diff --git a/tests/HexToRgbTest.php b/tests/HexToRgbTest.php index 78ce46b..4321e6c 100644 --- a/tests/HexToRgbTest.php +++ b/tests/HexToRgbTest.php @@ -4,50 +4,51 @@ class HexToRgbTest extends TestCase { - public function test_can_convert_to_rgb() { - // Black - $hex = new \LasseRafn\Hexer\Hex('#000'); - - $this->assertEquals([ - 'r' => 0, - 'g' => 0, - 'b' => 0 - ], $hex->toRgb()); - - // White - $hex = new \LasseRafn\Hexer\Hex('#fff'); - - $this->assertEquals([ - 'r' => 255, - 'g' => 255, - 'b' => 255 - ], $hex->toRgb()); - - // 6 chars - $hex = new \LasseRafn\Hexer\Hex('#ff000'); - - $this->assertEquals([ - 'r' => 255, - 'g' => 0, - 'b' => 0 - ], $hex->toRgb()); - - // No hashtag - $hex = new \LasseRafn\Hexer\Hex('007F00'); - - $this->assertEquals([ - 'r' => 0, - 'g' => 127, - 'b' => 0 - ], $hex->toRgb()); - - // With brightness modified - $hex = new \LasseRafn\Hexer\Hex('007F00'); - - $this->assertEquals([ - 'r' => 128, - 'g' => 255, - 'b' => 128 - ], $hex->lighten(50)->toRgb()); + public function test_can_convert_to_rgb() + { + // Black + $hex = new \LasseRafn\Hexer\Hex('#000'); + + $this->assertEquals([ + 'r' => 0, + 'g' => 0, + 'b' => 0, + ], $hex->toRgb()); + + // White + $hex = new \LasseRafn\Hexer\Hex('#fff'); + + $this->assertEquals([ + 'r' => 255, + 'g' => 255, + 'b' => 255, + ], $hex->toRgb()); + + // 6 chars + $hex = new \LasseRafn\Hexer\Hex('#ff000'); + + $this->assertEquals([ + 'r' => 255, + 'g' => 0, + 'b' => 0, + ], $hex->toRgb()); + + // No hashtag + $hex = new \LasseRafn\Hexer\Hex('007F00'); + + $this->assertEquals([ + 'r' => 0, + 'g' => 127, + 'b' => 0, + ], $hex->toRgb()); + + // With brightness modified + $hex = new \LasseRafn\Hexer\Hex('007F00'); + + $this->assertEquals([ + 'r' => 128, + 'g' => 255, + 'b' => 128, + ], $hex->lighten(50)->toRgb()); } }