ColorHelper   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 3
c 3
b 0
f 2
lcom 0
cbo 0
dl 0
loc 28
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A adjustBrightness() 0 23 3
1
<?php namespace jlourenco\support\Helpers;
2
3
class ColorHelper
4
{
5
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
30
}
31