1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Color; |
4
|
|
|
|
5
|
|
|
class Convert |
6
|
|
|
{ |
7
|
|
|
public static function hexChannelToRgbChannel(string $hexValue): int |
8
|
|
|
{ |
9
|
|
|
return hexdec($hexValue); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public static function rgbChannelToHexChannel(int $rgbValue): string |
13
|
|
|
{ |
14
|
|
|
return str_pad(dechex($rgbValue), 2, '0', STR_PAD_LEFT); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function hslValueToRgb(float $hue, float $saturation, float $lightness): array |
18
|
|
|
{ |
19
|
|
|
$c = (1 - abs(2 * ($lightness / 100) - 1)) * ($saturation / 100); |
20
|
|
|
$x = $c * (1 - abs(fmod($hue / 60, 2) - 1)); |
21
|
|
|
$m = ($lightness / 100) - ($c / 2); |
22
|
|
|
|
23
|
|
|
$h = (360 + ($hue % 360)) % 360; // hue values can be less than 0 and greater than 360. This normalises them into the range 0-360. |
24
|
|
|
|
25
|
|
View Code Duplication |
if ($h > 0 && $h <= 60) { |
|
|
|
|
26
|
|
|
return [round(($c + $m) * 255), round(($x + $m) * 255), round($m * 255)]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
if ($h > 60 && $h <= 120) { |
|
|
|
|
30
|
|
|
return [round(($x + $m) * 255), round(($c + $m) * 255), round($m * 255)]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
if ($h > 120 && $h <= 180) { |
|
|
|
|
34
|
|
|
return [round($m * 255), round(($c + $m) * 255), round(($x + $m) * 255)]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
if ($h > 180 && $h <= 240) { |
|
|
|
|
38
|
|
|
return [round($m * 255), round(($x + $m) * 255), round(($c + $m) * 255)]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
if ($h > 240 && $h <= 300) { |
|
|
|
|
42
|
|
|
return [round(($x + $m) * 255), round($m * 255), round(($c + $m) * 255)]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
if ($h > 300 && $h <= 360) { |
|
|
|
|
46
|
|
|
return [round(($c + $m) * 255), round($m * 255), round(($x + $m) * 255)]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function rgbValueToHsl($red, $green, $blue) |
51
|
|
|
{ |
52
|
|
|
$r = $red / 255; |
53
|
|
|
$g = $green / 255; |
54
|
|
|
$b = $blue / 255; |
55
|
|
|
|
56
|
|
|
$cmax = max($r, $g, $b); |
57
|
|
|
$cmin = min($r, $g, $b); |
58
|
|
|
$delta = $cmax - $cmin; |
59
|
|
|
|
60
|
|
|
$hue = 0; |
61
|
|
|
if ($delta !== 0) { |
62
|
|
|
if ($r === $cmax) { |
63
|
|
|
$hue = 60 * fmod(($g - $b) / $delta, 6); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($g === $cmax) { |
67
|
|
|
$hue = 60 * ((($b - $r) / $delta) + 2); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($b === $cmax) { |
71
|
|
|
$hue = 60 * ((($r - $g) / $delta) + 4); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$lightness = ($cmax + $cmin) / 2; |
76
|
|
|
|
77
|
|
|
$saturation = $delta / (1 - abs((2 * $lightness) - 1)); |
78
|
|
|
|
79
|
|
|
return [$hue, min($saturation, 1) * 100, min($lightness, 1) * 100]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.