Passed
Push — develop ( 95f65a...4503d2 )
by Andrew
04:41
created

Color   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 76
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A HTMLToRGB() 0 15 3
B RGBToHSL() 0 37 6
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS 3.x
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\helpers;
12
13
/**
14
 * ImageOptimize Settings model
15
 *
16
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
17
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
18
 * @since     1.5.7
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
19
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
class Color
21
{
22
    // Public Static Properties
23
    // =========================================================================
24
25
    // Public Static Methods
26
    // =========================================================================
27
28
    /**
29
     * Convert a HTML color (e.g. #FFFFFF or #FFF) to an array of RGB colors
30
     *
31
     * @param string $htmlCode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
32
     *
33
     * @return array
34
     */
35
    public static function HTMLToRGB(string $htmlCode): array
36
    {
37
        if (strpos($htmlCode, '#') === 0) {
38
            $htmlCode = substr($htmlCode, 1);
39
        }
40
41
        if (strlen($htmlCode) === 3) {
42
            $htmlCode = $htmlCode[0].$htmlCode[0].$htmlCode[1].$htmlCode[1].$htmlCode[2].$htmlCode[2];
43
        }
44
45
        $r = hexdec($htmlCode[0].$htmlCode[1]);
46
        $g = hexdec($htmlCode[2].$htmlCode[3]);
47
        $b = hexdec($htmlCode[4].$htmlCode[5]);
48
49
        return ['r' => $r, 'g' => $g, 'b' => $b];
50
    }
51
52
    /**
53
     * Convert an RGB color array to a HSL color array
54
     *
55
     * @param array $rgb
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     *
57
     * @return array
58
     */
59
    public static function RGBToHSL(array $rgb): array
60
    {
61
        $r = ((float)$rgb['r']) / 255.0;
62
        $g = ((float)$rgb['g']) / 255.0;
63
        $b = ((float)$rgb['b']) / 255.0;
64
65
        $maxC = max($r, $g, $b);
66
        $minC = min($r, $g, $b);
67
68
        $l = ($maxC + $minC) / 2.0;
69
70
        $s = 0;
71
        $h = 0;
72
        if ($maxC !== $minC) {
73
            if ($l < .5) {
74
                $s = ($maxC - $minC) / ($maxC + $minC);
75
            } else {
76
                $s = ($maxC - $minC) / (2.0 - $maxC - $minC);
77
            }
78
            if ($r === $maxC) {
79
                $h = ($g - $b) / ($maxC - $minC);
80
            }
81
            if ($g === $maxC) {
82
                $h = 2.0 + ($b - $r) / ($maxC - $minC);
83
            }
84
            if ($b === $maxC) {
85
                $h = 4.0 + ($r - $g) / ($maxC - $minC);
86
            }
87
88
            $h /= 6.0;
89
        }
90
91
        $h = (int)round(360.0 * $h);
92
        $s = (int)round(100.0 * $s);
93
        $l = (int)round(100.0 * $l);
94
95
        return ['h' => $h, 's' => $s, 'l' => $l];
96
    }
97
}
98