|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Color; |
|
4
|
|
|
|
|
5
|
|
|
class Rgba implements Color |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var int */ |
|
8
|
|
|
protected $red, $green, $blue; |
|
9
|
|
|
|
|
10
|
|
|
/** @var float */ |
|
11
|
|
|
protected $alpha; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(int $red, int $green, int $blue, float $alpha) |
|
14
|
|
|
{ |
|
15
|
|
|
Validate::rgbChannelValue($red, 'red'); |
|
16
|
|
|
Validate::rgbChannelValue($green, 'green'); |
|
17
|
|
|
Validate::rgbChannelValue($blue, 'blue'); |
|
18
|
|
|
Validate::alphaChannelValue($alpha); |
|
19
|
|
|
|
|
20
|
|
|
$this->red = $red; |
|
21
|
|
|
$this->green = $green; |
|
22
|
|
|
$this->blue = $blue; |
|
23
|
|
|
$this->alpha = $alpha; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function fromString(string $string) |
|
27
|
|
|
{ |
|
28
|
|
|
Validate::rgbaColorString($string); |
|
29
|
|
|
|
|
30
|
|
|
$matches = null; |
|
31
|
|
|
preg_match('/rgba\( *(\d{1,3} *, *\d{1,3} *, *\d{1,3} *, *[0-1](\.\d{1,2})?) *\)/i', $string, $matches); |
|
32
|
|
|
|
|
33
|
|
|
$channels = explode(',', $matches[1]); |
|
34
|
|
|
[$red, $green, $blue, $alpha] = array_map('trim', $channels); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
return new static($red, $green, $blue, $alpha); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function red(): int |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->red; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function green(): int |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->green; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function blue(): int |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->blue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function alpha(): float |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->alpha; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function toHex(): Hex |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->toRgb()->toHex(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function toHsl(): Hsl |
|
65
|
|
|
{ |
|
66
|
|
|
[$hue, $saturation, $lightness] = Convert::rgbValueToHsl( |
|
|
|
|
|
|
67
|
|
|
$this->red, |
|
68
|
|
|
$this->green, |
|
69
|
|
|
$this->blue |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return new Hsl($hue, $saturation, $lightness); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function toHsla(float $alpha = 1): Hsla |
|
76
|
|
|
{ |
|
77
|
|
|
[$hue, $saturation, $lightness] = Convert::rgbValueToHsl( |
|
|
|
|
|
|
78
|
|
|
$this->red, |
|
79
|
|
|
$this->green, |
|
80
|
|
|
$this->blue |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
return new Hsla($hue, $saturation, $lightness, $alpha); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function toRgb(): Rgb |
|
87
|
|
|
{ |
|
88
|
|
|
return new Rgb($this->red, $this->green, $this->blue); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function toRgba(float $alpha = 1): self |
|
92
|
|
|
{ |
|
93
|
|
|
return new self($this->red, $this->green, $this->blue, $alpha); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function __toString(): string |
|
97
|
|
|
{ |
|
98
|
|
|
$alpha = number_format($this->alpha, 2); |
|
99
|
|
|
|
|
100
|
|
|
return "rgba({$this->red},{$this->green},{$this->blue},{$alpha})"; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.