1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Color; |
4
|
|
|
|
5
|
|
|
class Hsla implements Color |
6
|
|
|
{ |
7
|
|
|
/** @var float */ |
8
|
|
|
protected $hue, $saturation, $lightness, $alpha; |
9
|
|
|
|
10
|
|
|
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 1.0) |
11
|
|
|
{ |
12
|
|
|
Validate::hslValue($saturation, 'saturation'); |
13
|
|
|
Validate::hslValue($lightness, 'lightness'); |
14
|
|
|
Validate::alphaChannelValue($alpha); |
15
|
|
|
|
16
|
|
|
$this->hue = $hue; |
17
|
|
|
$this->saturation = $saturation; |
18
|
|
|
$this->lightness = $lightness; |
19
|
|
|
$this->alpha = $alpha; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public static function fromString(string $string) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
Validate::hslaColorString($string); |
25
|
|
|
|
26
|
|
|
$matches = null; |
27
|
|
|
preg_match('/hsla\( *(\d{1,3}) *, *(\d{1,3})%? *, *(\d{1,3})%? *, *([0-1](\.\d{1,2})?) *\)/i', $string, $matches); |
28
|
|
|
|
29
|
|
|
return new static($matches[1], $matches[2], $matches[3], $matches[4]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function hue(): float |
33
|
|
|
{ |
34
|
|
|
return $this->hue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function saturation(): float |
38
|
|
|
{ |
39
|
|
|
return $this->saturation; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function lightness(): float |
43
|
|
|
{ |
44
|
|
|
return $this->lightness; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function red(): int |
48
|
|
|
{ |
49
|
|
|
return Convert::hslValueToRgb($this->hue, $this->saturation, $this->lightness)[0]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function green(): int |
53
|
|
|
{ |
54
|
|
|
return Convert::hslValueToRgb($this->hue, $this->saturation, $this->lightness)[1]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function blue(): int |
58
|
|
|
{ |
59
|
|
|
return Convert::hslValueToRgb($this->hue, $this->saturation, $this->lightness)[2]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function alpha(): float |
63
|
|
|
{ |
64
|
|
|
return $this->alpha; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function toHex(): Hex |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return new Hex( |
70
|
|
|
Convert::rgbChannelToHexChannel($this->red()), |
71
|
|
|
Convert::rgbChannelToHexChannel($this->green()), |
72
|
|
|
Convert::rgbChannelToHexChannel($this->blue()) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function toHsla(float $alpha = 1): self |
77
|
|
|
{ |
78
|
|
|
return new self($this->hue(), $this->saturation(), $this->lightness(), $alpha); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function toHsl(): Hsl |
82
|
|
|
{ |
83
|
|
|
return new Hsl($this->hue(), $this->saturation(), $this->lightness()); |
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): Rgba |
92
|
|
|
{ |
93
|
|
|
return new Rgba($this->red(), $this->green(), $this->blue(), $alpha); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function __toString(): string |
97
|
|
|
{ |
98
|
|
|
$hue = round($this->hue); |
99
|
|
|
$saturation = round($this->saturation); |
100
|
|
|
$lightness = round($this->lightness); |
101
|
|
|
$alpha = round($this->alpha, 2); |
102
|
|
|
|
103
|
|
|
return "hsla({$hue},{$saturation}%,{$lightness}%,{$alpha})"; |
104
|
|
|
} |
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.