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