Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 99 additions & 106 deletions src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
}
16 changes: 8 additions & 8 deletions src/HexBrightnessModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down
58 changes: 29 additions & 29 deletions src/HexToRgb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
6 changes: 3 additions & 3 deletions tests/HexModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ 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()
{
$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()
{
$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()
Expand Down
Loading