1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Artack\Color\Converter; |
||
6 | |||
7 | use Artack\Color\Color\Color; |
||
8 | use Artack\Color\Color\HSV; |
||
9 | use Artack\Color\Color\RGB; |
||
10 | use Webmozart\Assert\Assert; |
||
11 | |||
12 | class HSVToRGBConverter implements Convertible |
||
13 | { |
||
14 | public function convert(Color $color): Color |
||
15 | { |
||
16 | /* @var HSV $color */ |
||
17 | Assert::isInstanceOf($color, HSV::class, sprintf('color should be an instance of [%s]', HSV::class)); |
||
18 | |||
19 | $h = $color->getHue(); |
||
20 | $s = $color->getSaturation() / 100; |
||
21 | $v = $color->getValue() / 100; |
||
22 | |||
23 | $hF = floor($h / 60); |
||
24 | $f = $h / 60 - $hF; |
||
25 | |||
26 | $p = $v * (1 - $s); |
||
27 | $q = $v * (1 - $s * $f); |
||
28 | $t = $v * (1 - $s * (1 - $f)); |
||
29 | |||
30 | switch ($hF) { |
||
31 | case 0: |
||
32 | $r = $v; |
||
33 | $g = $t; |
||
34 | $b = $p; |
||
35 | break; |
||
36 | case 1: |
||
37 | $r = $q; |
||
38 | $g = $v; |
||
39 | $b = $p; |
||
40 | break; |
||
41 | case 2: |
||
42 | $r = $p; |
||
43 | $g = $v; |
||
44 | $b = $t; |
||
45 | break; |
||
46 | case 3: |
||
47 | $r = $p; |
||
48 | $g = $q; |
||
49 | $b = $v; |
||
50 | break; |
||
51 | case 4: |
||
52 | $r = $t; |
||
53 | $g = $p; |
||
54 | $b = $v; |
||
55 | break; |
||
56 | case 5: |
||
57 | $r = $v; |
||
58 | $g = $p; |
||
59 | $b = $q; |
||
60 | break; |
||
61 | } |
||
62 | |||
63 | return new RGB((int) round($r * 255), (int) round($g * 255), (int) round($b * 255)); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
64 | } |
||
65 | |||
66 | public static function supportsFrom(): string |
||
67 | { |
||
68 | return HSV::class; |
||
69 | } |
||
70 | |||
71 | public static function supportsTo(): string |
||
72 | { |
||
73 | return RGB::class; |
||
74 | } |
||
75 | } |
||
76 |