Skip to content

Commit ba0e4cf

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents fecc3d0 + e2b7bee commit ba0e4cf

File tree

7 files changed

+238
-235
lines changed

7 files changed

+238
-235
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<?php namespace LasseRafn\Hexer\Exceptions;
1+
<?php
2+
3+
namespace LasseRafn\Hexer\Exceptions;
24

35
class HexTooLongException extends \Exception
46
{
5-
}
7+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<?php namespace LasseRafn\Hexer\Exceptions;
1+
<?php
2+
3+
namespace LasseRafn\Hexer\Exceptions;
24

35
class HexTooShortException extends \Exception
46
{
5-
}
7+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<?php namespace LasseRafn\Hexer\Exceptions;
1+
<?php
2+
3+
namespace LasseRafn\Hexer\Exceptions;
24

35
class PercentageTooHighException extends \Exception
46
{
5-
}
7+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<?php namespace LasseRafn\Hexer\Exceptions;
1+
<?php
2+
3+
namespace LasseRafn\Hexer\Exceptions;
24

35
class PercentageTooLowException extends \Exception
46
{
5-
}
7+
}

src/Hex.php

Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,73 @@
99

1010
class Hex
1111
{
12-
private $hex;
13-
private $brightnessModifier;
14-
15-
const HEX_MIN_LENGTH = 3;
16-
const HEX_MAX_LENGTH = 6;
17-
18-
public function __construct( $hex )
19-
{
20-
$this->hex = $hex;
21-
$this->brightnessModifier = new HexBrightnessModifier();
22-
23-
$this->validateHex();
24-
}
25-
26-
/**
27-
* @param integer $percentage
28-
*
29-
* @return string
30-
*/
31-
public function lighten( $percentage = 0 )
32-
{
33-
$this->validatePercentage($percentage);
34-
35-
if ( $percentage === 0 )
36-
{
37-
return $this->hex;
38-
}
39-
40-
return $this->brightnessModifier->adjustBrightness( $this->hex, $percentage );
41-
}
42-
43-
/**
44-
* @param integer $percentage
45-
*
46-
* @return string
47-
*/
48-
public function darken( $percentage = 0 )
49-
{
50-
$this->validatePercentage($percentage);
51-
52-
if ( $percentage === 0 )
53-
{
54-
return $this->hex;
55-
}
56-
57-
return $this->brightnessModifier->adjustBrightness( $this->hex, $percentage * -1 );
58-
}
59-
60-
private function validatePercentage( $percentage )
61-
{
62-
if ( $percentage < 0 )
63-
{
64-
throw new PercentageTooLowException( "The percentage ({$percentage}) is below zero (0)" );
65-
}
66-
67-
if ( $percentage > 100 )
68-
{
69-
throw new PercentageTooHighException( "The percentage ({$percentage}) is above 100" );
70-
}
71-
}
72-
73-
private function validateHex() {
74-
$hex = str_replace( '#', '', $this->hex );
75-
76-
if ( strlen( $hex ) < self::HEX_MIN_LENGTH )
77-
{
78-
throw new HexTooShortException( "The hex ({$hex}) was too short. Minimum length is: " . self::HEX_MIN_LENGTH . " characters, without hashtag." );
79-
}
80-
81-
if ( strlen( $hex ) > self::HEX_MAX_LENGTH )
82-
{
83-
throw new HexTooLongException( "The hex ({$hex}) was too long. Maximum length is: " . self::HEX_MAX_LENGTH . " characters, without hashtag." );
84-
}
85-
}
12+
private $hex;
13+
private $brightnessModifier;
14+
15+
const HEX_MIN_LENGTH = 3;
16+
const HEX_MAX_LENGTH = 6;
17+
18+
public function __construct($hex)
19+
{
20+
$this->hex = $hex;
21+
$this->brightnessModifier = new HexBrightnessModifier();
22+
23+
$this->validateHex();
24+
}
25+
26+
/**
27+
* @param int $percentage
28+
*
29+
* @return string
30+
*/
31+
public function lighten($percentage = 0)
32+
{
33+
$this->validatePercentage($percentage);
34+
35+
if ($percentage === 0) {
36+
return $this->hex;
37+
}
38+
39+
return $this->brightnessModifier->adjustBrightness($this->hex, $percentage);
40+
}
41+
42+
/**
43+
* @param int $percentage
44+
*
45+
* @return string
46+
*/
47+
public function darken($percentage = 0)
48+
{
49+
$this->validatePercentage($percentage);
50+
51+
if ($percentage === 0) {
52+
return $this->hex;
53+
}
54+
55+
return $this->brightnessModifier->adjustBrightness($this->hex, $percentage * -1);
56+
}
57+
58+
private function validatePercentage($percentage)
59+
{
60+
if ($percentage < 0) {
61+
throw new PercentageTooLowException("The percentage ({$percentage}) is below zero (0)");
62+
}
63+
64+
if ($percentage > 100) {
65+
throw new PercentageTooHighException("The percentage ({$percentage}) is above 100");
66+
}
67+
}
68+
69+
private function validateHex()
70+
{
71+
$hex = str_replace('#', '', $this->hex);
72+
73+
if (strlen($hex) < self::HEX_MIN_LENGTH) {
74+
throw new HexTooShortException("The hex ({$hex}) was too short. Minimum length is: ".self::HEX_MIN_LENGTH.' characters, without hashtag.');
75+
}
76+
77+
if (strlen($hex) > self::HEX_MAX_LENGTH) {
78+
throw new HexTooLongException("The hex ({$hex}) was too long. Maximum length is: ".self::HEX_MAX_LENGTH.' characters, without hashtag.');
79+
}
80+
}
8681
}

src/HexBrightnessModifier.php

Lines changed: 81 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,85 @@
44

55
class HexBrightnessModifier
66
{
7-
const STEPS = 255;
8-
const STEPS_MAX = 255;
9-
const STEPS_MIN = -255;
10-
11-
private $hadHashtag = false;
12-
13-
/**
14-
* @param string $hex
15-
* @param integer $percentage
16-
*
17-
* @return string
18-
*/
19-
public function adjustBrightness( $hex, $percentage )
20-
{
21-
$steps = $this->generateSteps( $percentage );
22-
23-
$hex = $this->parseHex( $hex );
24-
$hex = $this->adjustHex( $hex, $steps );
25-
26-
// Append hashtag if was inputted with a hashtag
27-
if ( $this->hadHashtag )
28-
{
29-
$hex = "#{$hex}";
30-
}
31-
32-
return $hex;
33-
}
34-
35-
/**
36-
* @param integer $percentage
37-
*
38-
* @return integer
39-
*/
40-
private function generateSteps( $percentage )
41-
{
42-
$steps = (int) round( ( $percentage / 100 ) * self::STEPS );
43-
$steps = max( self::STEPS_MIN, min( self::STEPS_MAX, $steps ) );
44-
45-
return $steps;
46-
}
47-
48-
/**
49-
* @param string $hex
50-
*
51-
* @return mixed|string
52-
*/
53-
private function parseHex( $hex )
54-
{
55-
$this->hadHashtag = $hex[0] === '#';
56-
57-
$hex = str_replace( '#', '', $hex );
58-
$hex = strtolower( $hex );
59-
60-
if ( strlen( $hex ) === 3 )
61-
{
62-
$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
63-
}
64-
65-
return $hex;
66-
}
67-
68-
/**
69-
* @param string $hex
70-
* @param integer $steps
71-
*
72-
* @return string
73-
*/
74-
private function adjustHex( $hex, $steps )
75-
{
76-
$return = '';
77-
$parts = str_split( $hex, 2 );
78-
79-
foreach ( $parts as $color )
80-
{
81-
$color = hexdec( $color );
82-
$color = max( 0, min( 255, $color + $steps ) );
83-
84-
$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT );
85-
}
86-
87-
$return = strtolower( $return );
88-
89-
return $return;
90-
}
7+
const STEPS = 255;
8+
const STEPS_MAX = 255;
9+
const STEPS_MIN = -255;
10+
11+
private $hadHashtag = false;
12+
13+
/**
14+
* @param string $hex
15+
* @param int $percentage
16+
*
17+
* @return string
18+
*/
19+
public function adjustBrightness($hex, $percentage)
20+
{
21+
$steps = $this->generateSteps($percentage);
22+
23+
$hex = $this->parseHex($hex);
24+
$hex = $this->adjustHex($hex, $steps);
25+
26+
// Append hashtag if was inputted with a hashtag
27+
if ($this->hadHashtag) {
28+
$hex = "#{$hex}";
29+
}
30+
31+
return $hex;
32+
}
33+
34+
/**
35+
* @param int $percentage
36+
*
37+
* @return int
38+
*/
39+
private function generateSteps($percentage)
40+
{
41+
$steps = (int) round(($percentage / 100) * self::STEPS);
42+
$steps = max(self::STEPS_MIN, min(self::STEPS_MAX, $steps));
43+
44+
return $steps;
45+
}
46+
47+
/**
48+
* @param string $hex
49+
*
50+
* @return mixed|string
51+
*/
52+
private function parseHex($hex)
53+
{
54+
$this->hadHashtag = $hex[0] === '#';
55+
56+
$hex = str_replace('#', '', $hex);
57+
$hex = strtolower($hex);
58+
59+
if (strlen($hex) === 3) {
60+
$hex = str_repeat(substr($hex, 0, 1), 2).str_repeat(substr($hex, 1, 1), 2).str_repeat(substr($hex, 2, 1), 2);
61+
}
62+
63+
return $hex;
64+
}
65+
66+
/**
67+
* @param string $hex
68+
* @param int $steps
69+
*
70+
* @return string
71+
*/
72+
private function adjustHex($hex, $steps)
73+
{
74+
$return = '';
75+
$parts = str_split($hex, 2);
76+
77+
foreach ($parts as $color) {
78+
$color = hexdec($color);
79+
$color = max(0, min(255, $color + $steps));
80+
81+
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT);
82+
}
83+
84+
$return = strtolower($return);
85+
86+
return $return;
87+
}
9188
}

0 commit comments

Comments
 (0)