Passed
Pull Request — master (#258)
by Dominik
05:19
created

ColorHelpers   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 7
Bugs 2 Features 3
Metric Value
wmc 11
eloc 43
c 7
b 2
f 3
dl 0
loc 107
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hexToRgba() 0 21 3
B rgbaToHsla() 0 43 7
A hexToHsla() 0 4 1
1
<?php
2
3
namespace Flynt\Utils;
4
5
class ColorHelpers
6
{
7
    /**
8
     * Converts a color from hex to rgba.
9
     *
10
     * @since 2.0
11
     *
12
     * @param string $color The color to convert.
13
     * @param int $opacity The color opacity.
14
     * @param string $returnType The return format, string or array.
15
     *
16
     * @return mixed
17
     */
18
    public static function hexToRgba($color, $opacity = 1, $returnType = 'array')
19
    {
20
        $color = str_replace('#', '', $color);
21
22
        if (strlen($color) === 3) {
23
            $r = hexdec(substr($color, 0, 1) . substr($color, 0, 1));
24
            $g = hexdec(substr($color, 1, 1) . substr($color, 1, 1));
25
            $b = hexdec(substr($color, 2, 1) . substr($color, 2, 1));
26
        } else {
27
            $r = hexdec(substr($color, 0, 2));
28
            $g = hexdec(substr($color, 2, 2));
29
            $b = hexdec(substr($color, 4, 2));
30
        }
31
32
        $rgba = [ $r, $g, $b, $opacity ];
33
34
        if ($returnType === 'string') {
35
            return 'rgba(' . implode(',', $rgba) . ')';
36
        }
37
38
        return $rgba;
39
    }
40
41
    /**
42
     * Converts a color from rgba to hsla.
43
     *
44
     * @since 2.0
45
     *
46
     * @param string $color The color to convert.
47
     * @param int $opacity The color opacity.
48
     * @param string $returnType The return format, string or array.
49
     *
50
     * @return mixed
51
     */
52
    public static function rgbaToHsla($color, $opacity = 1, $returnType = 'array')
53
    {
54
        $r = $color[0] / 255;
55
        $g = $color[1] / 255;
56
        $b = $color[2] / 255;
57
58
        $min = min($r, min($g, $b));
59
        $max = max($r, max($g, $b));
60
        $delta = $max - $min;
61
62
        $h = 0;
63
        $s = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $s is dead and can be removed.
Loading history...
64
        $l = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $l is dead and can be removed.
Loading history...
65
66
        if ($delta > 0) {
67
            if ($max === $r) {
68
                $h = fmod((($g - $b) / $delta), 6);
69
            } else if ($max === $g) {
70
                $h = ($b - $r) / $delta + 2;
71
            } else {
72
                $h = ($r - $g) / $delta + 4;
73
            }
74
        }
75
76
        $h = round($h * 60);
77
78
        if ($h < 0) {
79
            $h += 360;
80
        }
81
82
        $l = ($min + $max) / 2;
83
        $s = $delta == 0 ? 0 : $delta / (1 - abs(2 * $l - 1));
84
85
        $s = round($s * 100, 1);
86
        $l = round($l * 100, 1);
87
88
        $hsla = [ $h, "$s%", "$l%", $opacity ];
89
90
        if ($returnType === 'string') {
91
            return 'hsla(' . implode(',', $hsla) . ')';
92
        }
93
94
        return $hsla;
95
    }
96
97
    /**
98
     * Converts a color from hex to hsla.
99
     *
100
     * @since 2.0
101
     *
102
     * @param string $color The color to convert.
103
     * @param int $opacity The color opacity.
104
     * @param string $returnType The return format, string or array.
105
     *
106
     * @return mixed
107
     */
108
    public static function hexToHsla($color, $opacity = 1, $returnType = 'array')
109
    {
110
        $rgba = self::hexToRgba($color, $opacity);
111
        return self::rgbaToHsla($rgba, $opacity, $returnType);
0 ignored issues
show
Bug introduced by
$rgba of type array<integer,double|integer> is incompatible with the type string expected by parameter $color of Flynt\Utils\ColorHelpers::rgbaToHsla(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        return self::rgbaToHsla(/** @scrutinizer ignore-type */ $rgba, $opacity, $returnType);
Loading history...
112
    }
113
}
114