|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tubalmartin\CssMin; |
|
4
|
|
|
|
|
5
|
|
|
class Utils |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Clamps a number between a minimum and a maximum value. |
|
9
|
|
|
* @param int|float $n the number to clamp |
|
10
|
|
|
* @param int|float $min the lower end number allowed |
|
11
|
|
|
* @param int|float $max the higher end number allowed |
|
12
|
|
|
* @return int|float |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function clampNumber($n, $min, $max) |
|
15
|
|
|
{ |
|
16
|
|
|
return min(max($n, $min), $max); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Clamps a RGB color number outside the sRGB color space |
|
21
|
|
|
* @param int|float $n the number to clamp |
|
22
|
|
|
* @return int|float |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function clampNumberSrgb($n) |
|
25
|
|
|
{ |
|
26
|
|
|
return self::clampNumber($n, 0, 255); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Converts a HSL color into a RGB color |
|
31
|
|
|
* @param array $hslValues |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function hslToRgb($hslValues) |
|
35
|
|
|
{ |
|
36
|
|
|
$h = floatval($hslValues[0]); |
|
37
|
|
|
$s = floatval(str_replace('%', '', $hslValues[1])); |
|
38
|
|
|
$l = floatval(str_replace('%', '', $hslValues[2])); |
|
39
|
|
|
|
|
40
|
|
|
// Wrap and clamp, then fraction! |
|
41
|
|
|
$h = ((($h % 360) + 360) % 360) / 360; |
|
42
|
|
|
$s = self::clampNumber($s, 0, 100) / 100; |
|
43
|
|
|
$l = self::clampNumber($l, 0, 100) / 100; |
|
44
|
|
|
|
|
45
|
|
|
if ($s == 0) { |
|
46
|
|
|
$r = $g = $b = self::roundNumber(255 * $l); |
|
47
|
|
|
} else { |
|
48
|
|
|
$v2 = $l < 0.5 ? $l * (1 + $s) : ($l + $s) - ($s * $l); |
|
49
|
|
|
$v1 = (2 * $l) - $v2; |
|
50
|
|
|
$r = self::roundNumber(255 * self::hueToRgb($v1, $v2, $h + (1/3))); |
|
51
|
|
|
$g = self::roundNumber(255 * self::hueToRgb($v1, $v2, $h)); |
|
52
|
|
|
$b = self::roundNumber(255 * self::hueToRgb($v1, $v2, $h - (1/3))); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return array($r, $g, $b); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Tests and selects the correct formula for each RGB color channel |
|
60
|
|
|
* @param $v1 |
|
61
|
|
|
* @param $v2 |
|
62
|
|
|
* @param $vh |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function hueToRgb($v1, $v2, $vh) |
|
66
|
|
|
{ |
|
67
|
|
|
$vh = $vh < 0 ? $vh + 1 : ($vh > 1 ? $vh - 1 : $vh); |
|
68
|
|
|
|
|
69
|
|
|
if ($vh * 6 < 1) { |
|
70
|
|
|
return $v1 + ($v2 - $v1) * 6 * $vh; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($vh * 2 < 1) { |
|
74
|
|
|
return $v2; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($vh * 3 < 2) { |
|
78
|
|
|
return $v1 + ($v2 - $v1) * ((2 / 3) - $vh) * 6; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $v1; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Convert strings like "64M" or "30" to int values |
|
86
|
|
|
* @param mixed $size |
|
87
|
|
|
* @return int |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function normalizeInt($size) |
|
90
|
|
|
{ |
|
91
|
|
|
if (is_string($size)) { |
|
92
|
|
|
$letter = substr($size, -1); |
|
93
|
|
|
$size = intval($size); |
|
94
|
|
|
switch ($letter) { |
|
95
|
|
|
case 'M': |
|
96
|
|
|
case 'm': |
|
97
|
|
|
return (int) $size * 1048576; |
|
98
|
|
|
case 'K': |
|
99
|
|
|
case 'k': |
|
100
|
|
|
return (int) $size * 1024; |
|
101
|
|
|
case 'G': |
|
102
|
|
|
case 'g': |
|
103
|
|
|
return (int) $size * 1073741824; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
return (int) $size; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Converts a string containing and RGB percentage value into a RGB integer value i.e. '90%' -> 229.5 |
|
111
|
|
|
* @param $rgbPercentage |
|
112
|
|
|
* @return int |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function rgbPercentageToRgbInteger($rgbPercentage) |
|
115
|
|
|
{ |
|
116
|
|
|
if (strpos($rgbPercentage, '%') !== false) { |
|
117
|
|
|
$rgbPercentage = self::roundNumber(floatval(str_replace('%', '', $rgbPercentage)) * 2.55); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return intval($rgbPercentage, 10); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Converts a RGB color into a HEX color |
|
125
|
|
|
* @param array $rgbColors |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function rgbToHex($rgbColors) |
|
129
|
|
|
{ |
|
130
|
|
|
$hexColors = array(); |
|
131
|
|
|
|
|
132
|
|
|
// Values outside the sRGB color space should be clipped (0-255) |
|
133
|
|
|
for ($i = 0, $l = count($rgbColors); $i < $l; $i++) { |
|
134
|
|
|
$hexColors[$i] = sprintf("%02x", self::clampNumberSrgb(self::rgbPercentageToRgbInteger($rgbColors[$i]))); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $hexColors; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Rounds a number to its closest integer |
|
142
|
|
|
* @param $n |
|
143
|
|
|
* @return int |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function roundNumber($n) |
|
146
|
|
|
{ |
|
147
|
|
|
return intval(round(floatval($n)), 10); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|