Skip to content

Commit

Permalink
Merge pull request #1 from LasseRafn/analysis-XNpYde
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
LasseRafn authored Jun 16, 2017
2 parents 3b05622 + bc029d7 commit e2b7bee
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 235 deletions.
6 changes: 4 additions & 2 deletions src/Exceptions/HexTooLongException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace LasseRafn\Hexer\Exceptions;
<?php

namespace LasseRafn\Hexer\Exceptions;

class HexTooLongException extends \Exception
{
}
}
6 changes: 4 additions & 2 deletions src/Exceptions/HexTooShortException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace LasseRafn\Hexer\Exceptions;
<?php

namespace LasseRafn\Hexer\Exceptions;

class HexTooShortException extends \Exception
{
}
}
6 changes: 4 additions & 2 deletions src/Exceptions/PercentageTooHighException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace LasseRafn\Hexer\Exceptions;
<?php

namespace LasseRafn\Hexer\Exceptions;

class PercentageTooHighException extends \Exception
{
}
}
6 changes: 4 additions & 2 deletions src/Exceptions/PercentageTooLowException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace LasseRafn\Hexer\Exceptions;
<?php

namespace LasseRafn\Hexer\Exceptions;

class PercentageTooLowException extends \Exception
{
}
}
143 changes: 69 additions & 74 deletions src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,73 @@

class Hex
{
private $hex;
private $brightnessModifier;

const HEX_MIN_LENGTH = 3;
const HEX_MAX_LENGTH = 6;

public function __construct( $hex )
{
$this->hex = $hex;
$this->brightnessModifier = new HexBrightnessModifier();

$this->validateHex();
}

/**
* @param integer $percentage
*
* @return string
*/
public function lighten( $percentage = 0 )
{
$this->validatePercentage($percentage);

if ( $percentage === 0 )
{
return $this->hex;
}

return $this->brightnessModifier->adjustBrightness( $this->hex, $percentage );
}

/**
* @param integer $percentage
*
* @return string
*/
public function darken( $percentage = 0 )
{
$this->validatePercentage($percentage);

if ( $percentage === 0 )
{
return $this->hex;
}

return $this->brightnessModifier->adjustBrightness( $this->hex, $percentage * -1 );
}

private function validatePercentage( $percentage )
{
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" );
}
}

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;

const HEX_MIN_LENGTH = 3;
const HEX_MAX_LENGTH = 6;

public function __construct($hex)
{
$this->hex = $hex;
$this->brightnessModifier = new HexBrightnessModifier();

$this->validateHex();
}

/**
* @param int $percentage
*
* @return string
*/
public function lighten($percentage = 0)
{
$this->validatePercentage($percentage);

if ($percentage === 0) {
return $this->hex;
}

return $this->brightnessModifier->adjustBrightness($this->hex, $percentage);
}

/**
* @param int $percentage
*
* @return string
*/
public function darken($percentage = 0)
{
$this->validatePercentage($percentage);

if ($percentage === 0) {
return $this->hex;
}

return $this->brightnessModifier->adjustBrightness($this->hex, $percentage * -1);
}

private function validatePercentage($percentage)
{
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");
}
}

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.');
}
}
}
165 changes: 81 additions & 84 deletions src/HexBrightnessModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,85 @@

class HexBrightnessModifier
{
const STEPS = 255;
const STEPS_MAX = 255;
const STEPS_MIN = -255;

private $hadHashtag = false;

/**
* @param string $hex
* @param integer $percentage
*
* @return string
*/
public function adjustBrightness( $hex, $percentage )
{
$steps = $this->generateSteps( $percentage );

$hex = $this->parseHex( $hex );
$hex = $this->adjustHex( $hex, $steps );

// Append hashtag if was inputted with a hashtag
if ( $this->hadHashtag )
{
$hex = "#{$hex}";
}

return $hex;
}

/**
* @param integer $percentage
*
* @return integer
*/
private function generateSteps( $percentage )
{
$steps = (int) round( ( $percentage / 100 ) * self::STEPS );
$steps = max( self::STEPS_MIN, min( self::STEPS_MAX, $steps ) );

return $steps;
}

/**
* @param string $hex
*
* @return mixed|string
*/
private function parseHex( $hex )
{
$this->hadHashtag = $hex[0] === '#';

$hex = str_replace( '#', '', $hex );
$hex = strtolower( $hex );

if ( strlen( $hex ) === 3 )
{
$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
}

return $hex;
}

/**
* @param string $hex
* @param integer $steps
*
* @return string
*/
private function adjustHex( $hex, $steps )
{
$return = '';
$parts = str_split( $hex, 2 );

foreach ( $parts as $color )
{
$color = hexdec( $color );
$color = max( 0, min( 255, $color + $steps ) );

$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT );
}

$return = strtolower( $return );

return $return;
}
const STEPS = 255;
const STEPS_MAX = 255;
const STEPS_MIN = -255;

private $hadHashtag = false;

/**
* @param string $hex
* @param int $percentage
*
* @return string
*/
public function adjustBrightness($hex, $percentage)
{
$steps = $this->generateSteps($percentage);

$hex = $this->parseHex($hex);
$hex = $this->adjustHex($hex, $steps);

// Append hashtag if was inputted with a hashtag
if ($this->hadHashtag) {
$hex = "#{$hex}";
}

return $hex;
}

/**
* @param int $percentage
*
* @return int
*/
private function generateSteps($percentage)
{
$steps = (int) round(($percentage / 100) * self::STEPS);
$steps = max(self::STEPS_MIN, min(self::STEPS_MAX, $steps));

return $steps;
}

/**
* @param string $hex
*
* @return mixed|string
*/
private function parseHex($hex)
{
$this->hadHashtag = $hex[0] === '#';

$hex = str_replace('#', '', $hex);
$hex = strtolower($hex);

if (strlen($hex) === 3) {
$hex = str_repeat(substr($hex, 0, 1), 2).str_repeat(substr($hex, 1, 1), 2).str_repeat(substr($hex, 2, 1), 2);
}

return $hex;
}

/**
* @param string $hex
* @param int $steps
*
* @return string
*/
private function adjustHex($hex, $steps)
{
$return = '';
$parts = str_split($hex, 2);

foreach ($parts as $color) {
$color = hexdec($color);
$color = max(0, min(255, $color + $steps));

$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT);
}

$return = strtolower($return);

return $return;
}
}
Loading

0 comments on commit e2b7bee

Please sign in to comment.