Conditions | 3 |
Paths | 4 |
Total Lines | 23 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 2 |
1 | <?php namespace jlourenco\support\Helpers; |
||
6 | public static function adjustBrightness($hex, $steps) |
||
7 | { |
||
8 | // Steps should be between -255 and 255. Negative = darker, positive = lighter |
||
9 | $steps = max(-255, min(255, $steps)); |
||
10 | |||
11 | // Normalize into a six character long hex string |
||
12 | $hex = str_replace('#', '', $hex); |
||
13 | if (strlen($hex) == 3) { |
||
14 | $hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2); |
||
15 | } |
||
16 | |||
17 | // Split into three parts: R, G and B |
||
18 | $color_parts = str_split($hex, 2); |
||
19 | $return = '#'; |
||
20 | |||
21 | foreach ($color_parts as $color) { |
||
22 | $color = hexdec($color); // Convert to decimal |
||
23 | $color = max(0,min(255,$color + $steps)); // Adjust color |
||
24 | $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code |
||
25 | } |
||
26 | |||
27 | return $return; |
||
28 | } |
||
29 | |||
31 |