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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\helpers; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ImageOptimize Settings model |
15
|
|
|
* |
16
|
|
|
* @author nystudio107 |
|
|
|
|
17
|
|
|
* @package ImageOptimize |
|
|
|
|
18
|
|
|
* @since 1.5.7 |
|
|
|
|
19
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|