|
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
|
|
|
$out = [ |
|
57
|
2 |
|
'hsl', |
|
58
|
2 |
|
($H < 0 ? $H + 6 : $H) * 60, |
|
|
|
|
|
|
59
|
2 |
|
$S * 100, |
|
60
|
2 |
|
$L * 100, |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
2 |
|
if (count($color) > 4) { |
|
64
|
1 |
|
$out[] = $color[4]; |
|
65
|
|
|
} // copy alpha |
|
66
|
|
|
|
|
67
|
2 |
|
return $out; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param double $comp |
|
72
|
|
|
* @param double $temp1 |
|
73
|
|
|
* @param double $temp2 |
|
74
|
|
|
* |
|
75
|
|
|
* @return double |
|
76
|
|
|
*/ |
|
77
|
1 |
|
protected function toRGBHelper($comp, $temp1, $temp2) |
|
78
|
|
|
{ |
|
79
|
1 |
|
if ($comp < 0) { |
|
80
|
1 |
|
$comp += 1.0; |
|
81
|
1 |
|
} elseif ($comp > 1) { |
|
82
|
1 |
|
$comp -= 1.0; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
View Code Duplication |
if (6 * $comp < 1) { |
|
|
|
|
|
|
86
|
1 |
|
return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
|
87
|
|
|
} |
|
88
|
1 |
|
if (2 * $comp < 1) { |
|
89
|
1 |
|
return $temp2; |
|
90
|
|
|
} |
|
91
|
1 |
View Code Duplication |
if (3 * $comp < 2) { |
|
|
|
|
|
|
92
|
1 |
|
return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return $temp1; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Converts a hsl array into a color value in rgb. |
|
100
|
|
|
* Expects H to be in range of 0 to 360, S and L in 0 to 100 |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $color |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function toRGB(array $color) |
|
107
|
|
|
{ |
|
108
|
2 |
|
if ($color[0] === 'color') { |
|
109
|
|
|
return $color; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
$H = $color[1] / 360; |
|
113
|
2 |
|
$S = $color[2] / 100; |
|
114
|
2 |
|
$L = $color[3] / 100; |
|
115
|
|
|
|
|
116
|
2 |
|
if ($S === 0) { |
|
117
|
2 |
|
$r = $g = $b = $L; |
|
118
|
|
|
} else { |
|
119
|
1 |
|
$temp2 = $L < 0.5 ? $L * (1.0 + $S) : $L + $S - $L * $S; |
|
120
|
1 |
|
$temp1 = 2.0 * $L - $temp2; |
|
121
|
|
|
|
|
122
|
1 |
|
$r = $this->toRGBHelper($H + 1 / 3, $temp1, $temp2); |
|
123
|
1 |
|
$g = $this->toRGBHelper($H, $temp1, $temp2); |
|
124
|
1 |
|
$b = $this->toRGBHelper($H - 1 / 3, $temp1, $temp2); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// $out = array('color', round($r*255), round($g*255), round($b*255)); |
|
|
|
|
|
|
128
|
2 |
|
$out = ['color', $r * 255, $g * 255, $b * 255]; |
|
129
|
2 |
|
if (count($color) > 4) { |
|
130
|
1 |
|
$out[] = $color[4]; |
|
131
|
|
|
} // copy alpha |
|
132
|
|
|
|
|
133
|
2 |
|
return $out; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param int $v |
|
138
|
|
|
* @param int $max |
|
139
|
|
|
* @param int $min |
|
140
|
|
|
* |
|
141
|
|
|
* @return mixed |
|
142
|
|
|
*/ |
|
143
|
2 |
|
public function clamp($v, $max = 1, $min = 0) |
|
144
|
|
|
{ |
|
145
|
2 |
|
return min($max, max($min, $v)); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
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: