|
4 | 4 |
|
5 | 5 | class HexBrightnessModifier |
6 | 6 | { |
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 | + } |
91 | 88 | } |
0 commit comments