Color::HTMLToRGB()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS
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
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
14
 * @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...
15
 * @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...
16
 * @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...
17
 */
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...
18
class Color
19
{
20
    // Public Static Properties
21
    // =========================================================================
22
23
    // Public Static Methods
24
    // =========================================================================
25
26
    /**
27
     * Convert a HTML color (e.g. #FFFFFF or #FFF) to an array of RGB colors
28
     *
29
     * @param string $htmlCode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
30
     *
31
     * @return array
32
     */
33
    public static function HTMLToRGB(string $htmlCode): array
34
    {
35
        if (strpos($htmlCode, '#') === 0) {
36
            $htmlCode = substr($htmlCode, 1);
37
        }
38
39
        if (strlen($htmlCode) === 3) {
40
            $htmlCode = $htmlCode[0] . $htmlCode[0] . $htmlCode[1] . $htmlCode[1] . $htmlCode[2] . $htmlCode[2];
41
        }
42
43
        $r = hexdec($htmlCode[0] . $htmlCode[1]);
44
        $g = hexdec($htmlCode[2] . $htmlCode[3]);
45
        $b = hexdec($htmlCode[4] . $htmlCode[5]);
46
47
        return ['r' => $r, 'g' => $g, 'b' => $b];
48
    }
49
50
    /**
51
     * Convert an RGB color array to a HSL color array
52
     *
53
     * @param array $rgb
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     *
55
     * @return array
56
     */
57
    public static function RGBToHSL(array $rgb): array
58
    {
59
        $r = ((float)$rgb['r']) / 255.0;
60
        $g = ((float)$rgb['g']) / 255.0;
61
        $b = ((float)$rgb['b']) / 255.0;
62
63
        $maxC = max($r, $g, $b);
64
        $minC = min($r, $g, $b);
65
66
        $l = ($maxC + $minC) / 2.0;
67
68
        $s = 0;
69
        $h = 0;
70
        if ($maxC !== $minC) {
71
            if ($l < .5) {
72
                $s = ($maxC - $minC) / ($maxC + $minC);
73
            } else {
74
                $s = ($maxC - $minC) / (2.0 - $maxC - $minC);
75
            }
76
            if ($r === $maxC) {
77
                $h = ($g - $b) / ($maxC - $minC);
78
            }
79
            if ($g === $maxC) {
80
                $h = 2.0 + ($b - $r) / ($maxC - $minC);
81
            }
82
            if ($b === $maxC) {
83
                $h = 4.0 + ($r - $g) / ($maxC - $minC);
84
            }
85
86
            $h /= 6.0;
87
        }
88
89
        $h = (int)round(360.0 * $h);
90
        $s = (int)round(100.0 * $s);
91
        $l = (int)round(100.0 * $l);
92
93
        return ['h' => $h, 's' => $s, 'l' => $l];
94
    }
95
}
96