1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The HSL Data Class |
4
|
|
|
* ================== |
5
|
|
|
* Contains a color as an HSL array |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace projectcleverweb\color; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The HSL Data Class |
12
|
|
|
* ================== |
13
|
|
|
* Contains a color as an HSL array |
14
|
|
|
*/ |
15
|
|
|
class hsl implements \ArrayAccess { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The HSL data |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $hsl; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The amount of accuracy to use when calculating HSL |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $accuracy; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Import a color as an HSL value |
31
|
|
|
* |
32
|
|
|
* @param array $rgb_array RGB color array to import |
33
|
|
|
* @param int $accuracy The amount of accuracy to use when calculating HSL |
34
|
|
|
*/ |
35
|
13 |
|
public function __construct(array $rgb_array, int $accuracy = 3) { |
36
|
13 |
|
$this->accuracy = $accuracy; |
37
|
13 |
|
$this->hsl = convert\rgb::to_hsl($rgb_array); |
38
|
13 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Shortcut for return the HSL array |
42
|
|
|
* |
43
|
|
|
* @return array The HSL array |
44
|
|
|
*/ |
45
|
3 |
|
public function __invoke() { |
46
|
3 |
|
return $this->hsl; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if offset exists in data array |
51
|
|
|
* |
52
|
|
|
* @param string $offset The offset to check |
53
|
|
|
* @return bool TRUE if the offset exist, FALSE otherwise |
54
|
|
|
*/ |
55
|
4 |
|
public function offsetExists($offset) :bool { |
56
|
4 |
|
return isset($this->hsl[$offset]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get an offset if it exists |
61
|
|
|
* |
62
|
|
|
* @param string $offset The offset to get |
63
|
|
|
* @return mixed The value of the offset |
64
|
|
|
*/ |
65
|
2 |
View Code Duplication |
public function offsetGet($offset) { |
|
|
|
|
66
|
2 |
|
if ($this->offsetExists($offset)) { |
67
|
1 |
|
return $this->hsl[$offset]; |
68
|
|
|
} |
69
|
1 |
|
return error::trigger(error::INVALID_ARGUMENT, sprintf( |
70
|
1 |
|
'The offset "%s" does not exist in %s', |
71
|
1 |
|
(string) $offset, |
72
|
1 |
|
__CLASS__ |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set a value in the data array |
78
|
|
|
* |
79
|
|
|
* @param string $offset The offset to set |
80
|
|
|
* @param mixed $value The value to set it to |
81
|
|
|
* @return mixed The result |
|
|
|
|
82
|
|
|
*/ |
83
|
2 |
View Code Duplication |
public function offsetSet($offset, $value) { |
|
|
|
|
84
|
2 |
|
if ($this->offsetExists($offset)) { |
85
|
1 |
|
return $this->hsl[$offset] = (float) $value; |
86
|
|
|
} |
87
|
1 |
|
return error::trigger(error::INVALID_ARGUMENT, sprintf( |
88
|
1 |
|
'The offset "%s" cannot be set in %s', |
89
|
1 |
|
(string) $offset, |
90
|
1 |
|
__CLASS__ |
91
|
|
|
)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Reset an offset to 0 |
96
|
|
|
* |
97
|
|
|
* @param string $offset The offset to unset |
98
|
|
|
* @return mixed The result |
|
|
|
|
99
|
|
|
*/ |
100
|
1 |
|
public function offsetUnset($offset) { |
101
|
1 |
|
if ($this->offsetExists($offset)) { |
102
|
1 |
|
return $this->hsl[$offset] = 0.0; |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.