1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LesserPhp\Color; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* lesserphp |
7
|
|
|
* https://www.maswaba.de/lesserphp |
8
|
|
|
* |
9
|
|
|
* LESS CSS compiler, adapted from http://lesscss.org |
10
|
|
|
* |
11
|
|
|
* Copyright 2013, Leaf Corcoran <[email protected]> |
12
|
|
|
* Copyright 2016, Marcus Schwarz <[email protected]> |
13
|
|
|
* Licensed under MIT or GPLv3, see LICENSE |
14
|
|
|
* @package LesserPhp |
15
|
|
|
*/ |
16
|
|
|
class Converter |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param array $color |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
2 |
|
public function toHSL($color) |
25
|
|
|
{ |
26
|
2 |
|
if ($color[0] === 'hsl') { |
27
|
|
|
return $color; |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
$r = $color[1] / 255; |
31
|
2 |
|
$g = $color[2] / 255; |
32
|
2 |
|
$b = $color[3] / 255; |
33
|
|
|
|
34
|
2 |
|
$min = min($r, $g, $b); |
35
|
2 |
|
$max = max($r, $g, $b); |
36
|
|
|
|
37
|
2 |
|
$L = ($min + $max) / 2; |
38
|
2 |
|
if ($min == $max) { |
39
|
2 |
|
$S = $H = 0; |
40
|
|
|
} else { |
41
|
1 |
|
if ($L < 0.5) { |
42
|
1 |
|
$S = ($max - $min) / ($max + $min); |
43
|
|
|
} else { |
44
|
1 |
|
$S = ($max - $min) / (2.0 - $max - $min); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if ($r == $max) { |
48
|
1 |
|
$H = ($g - $b) / ($max - $min); |
49
|
1 |
|
} elseif ($g == $max) { |
50
|
1 |
|
$H = 2.0 + ($b - $r) / ($max - $min); |
51
|
1 |
|
} elseif ($b == $max) { |
52
|
1 |
|
$H = 4.0 + ($r - $g) / ($max - $min); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$out = [ |
58
|
2 |
|
'hsl', |
59
|
2 |
|
($H < 0 ? $H + 6 : $H) * 60, |
|
|
|
|
60
|
2 |
|
$S * 100, |
61
|
2 |
|
$L * 100, |
62
|
|
|
]; |
63
|
|
|
|
64
|
2 |
|
if (count($color) > 4) { |
65
|
1 |
|
$out[] = $color[4]; |
66
|
|
|
} // copy alpha |
67
|
|
|
|
68
|
2 |
|
return $out; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param double $comp |
73
|
|
|
* @param double $temp1 |
74
|
|
|
* @param double $temp2 |
75
|
|
|
* |
76
|
|
|
* @return double |
77
|
|
|
*/ |
78
|
1 |
|
protected function toRGBHelper($comp, $temp1, $temp2) |
79
|
|
|
{ |
80
|
1 |
|
if ($comp < 0) { |
81
|
1 |
|
$comp += 1.0; |
82
|
1 |
|
} elseif ($comp > 1) { |
83
|
1 |
|
$comp -= 1.0; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
View Code Duplication |
if (6 * $comp < 1) { |
|
|
|
|
87
|
1 |
|
return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
88
|
|
|
} |
89
|
1 |
|
if (2 * $comp < 1) { |
90
|
1 |
|
return $temp2; |
91
|
|
|
} |
92
|
1 |
View Code Duplication |
if (3 * $comp < 2) { |
|
|
|
|
93
|
1 |
|
return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $temp1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Converts a hsl array into a color value in rgb. |
101
|
|
|
* Expects H to be in range of 0 to 360, S and L in 0 to 100 |
102
|
|
|
* |
103
|
|
|
* @param array $color |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
2 |
|
public function toRGB(array $color) |
108
|
|
|
{ |
109
|
2 |
|
if ($color[0] === 'color') { |
110
|
|
|
return $color; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
$H = $color[1] / 360; |
114
|
2 |
|
$S = $color[2] / 100; |
115
|
2 |
|
$L = $color[3] / 100; |
116
|
|
|
|
117
|
2 |
|
if ($S === 0) { |
118
|
2 |
|
$r = $g = $b = $L; |
119
|
|
|
} else { |
120
|
1 |
|
$temp2 = $L < 0.5 ? |
121
|
1 |
|
$L * (1.0 + $S) : |
122
|
1 |
|
$L + $S - $L * $S; |
123
|
|
|
|
124
|
1 |
|
$temp1 = 2.0 * $L - $temp2; |
125
|
|
|
|
126
|
1 |
|
$r = $this->toRGBHelper($H + 1 / 3, $temp1, $temp2); |
127
|
1 |
|
$g = $this->toRGBHelper($H, $temp1, $temp2); |
128
|
1 |
|
$b = $this->toRGBHelper($H - 1 / 3, $temp1, $temp2); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// $out = array('color', round($r*255), round($g*255), round($b*255)); |
|
|
|
|
132
|
2 |
|
$out = ['color', $r * 255, $g * 255, $b * 255]; |
133
|
2 |
|
if (count($color) > 4) { |
134
|
1 |
|
$out[] = $color[4]; |
135
|
|
|
} // copy alpha |
136
|
|
|
|
137
|
2 |
|
return $out; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param double $v |
142
|
|
|
* @param double $max |
143
|
|
|
* @param double $min |
144
|
|
|
* |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
2 |
|
public function clamp($v, $max = 1, $min = 0) |
148
|
|
|
{ |
149
|
2 |
|
return min($max, max($min, $v)); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: