nystudio107 /
craft-imageoptimize
| 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
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2017 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\imageoptimize\helpers; |
||
| 12 | |||
| 13 | /** |
||
|
0 ignored issues
–
show
|
|||
| 14 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 15 | * @package ImageOptimize |
||
|
0 ignored issues
–
show
|
|||
| 16 | * @since 1.5.7 |
||
|
0 ignored issues
–
show
|
|||
| 17 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 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
|
|||
| 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 |