1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Color; |
4
|
|
|
|
5
|
|
|
class Hex implements Color |
6
|
|
|
{ |
7
|
|
|
/** @var string */ |
8
|
|
|
protected $red, $green, $blue; |
9
|
|
|
|
10
|
|
|
public function __construct(string $red, string $green, string $blue) |
11
|
|
|
{ |
12
|
|
|
Validate::hexChannelValue($red, 'red'); |
|
|
|
|
13
|
|
|
Validate::hexChannelValue($green, 'green'); |
|
|
|
|
14
|
|
|
Validate::hexChannelValue($blue, 'blue'); |
|
|
|
|
15
|
|
|
|
16
|
|
|
$this->red = strtolower($red); |
17
|
|
|
$this->green = strtolower($green); |
18
|
|
|
$this->blue = strtolower($blue); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function fromString(string $string) |
22
|
|
|
{ |
23
|
|
|
Validate::hexColorString($string); |
24
|
|
|
|
25
|
|
|
[$red, $green, $blue] = str_split(ltrim($string, '#'), 2); |
|
|
|
|
26
|
|
|
|
27
|
|
|
return new static($red, $green, $blue); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function red(): string |
31
|
|
|
{ |
32
|
|
|
return $this->red; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function green(): string |
36
|
|
|
{ |
37
|
|
|
return $this->green; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function blue(): string |
41
|
|
|
{ |
42
|
|
|
return $this->blue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function toHex(): self |
46
|
|
|
{ |
47
|
|
|
return new self($this->red, $this->green, $this->blue); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function toHsl(): Hsl |
51
|
|
|
{ |
52
|
|
|
[$hue, $saturation, $lightness] = Convert::rgbValueToHsl( |
|
|
|
|
53
|
|
|
Convert::hexChannelToRgbChannel($this->red), |
54
|
|
|
Convert::hexChannelToRgbChannel($this->green), |
55
|
|
|
Convert::hexChannelToRgbChannel($this->blue) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return new Hsl($hue, $saturation, $lightness); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function toHsla(float $alpha = 1): Hsla |
62
|
|
|
{ |
63
|
|
|
[$hue, $saturation, $lightness] = Convert::rgbValueToHsl( |
|
|
|
|
64
|
|
|
Convert::hexChannelToRgbChannel($this->red), |
65
|
|
|
Convert::hexChannelToRgbChannel($this->green), |
66
|
|
|
Convert::hexChannelToRgbChannel($this->blue) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
return new Hsla($hue, $saturation, $lightness, $alpha); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function toRgb(): Rgb |
73
|
|
|
{ |
74
|
|
|
return new Rgb( |
75
|
|
|
Convert::hexChannelToRgbChannel($this->red), |
76
|
|
|
Convert::hexChannelToRgbChannel($this->green), |
77
|
|
|
Convert::hexChannelToRgbChannel($this->blue) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function toRgba(float $alpha = 1): Rgba |
82
|
|
|
{ |
83
|
|
|
return $this->toRgb()->toRgba($alpha); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function __toString(): string |
87
|
|
|
{ |
88
|
|
|
return "#{$this->red}{$this->green}{$this->blue}"; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.