| Total Complexity | 9 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | class HSLToRGBConverter implements ConverterInterface |
||
| 13 | { |
||
| 14 | 65 | public function convert(Color $color): Color |
|
| 15 | { |
||
| 16 | /* @var HSL $color */ |
||
| 17 | 65 | Assert::isInstanceOf($color, HSL::class, sprintf('color should be an instance of [%s]', HSL::class)); |
|
| 18 | |||
| 19 | 65 | $hue = $color->getHue(); |
|
| 20 | 65 | $saturation = $color->getSaturation() / 100; |
|
| 21 | 65 | $lightness = $color->getLightness() / 100; |
|
| 22 | |||
| 23 | 65 | $c = (1 - abs(2 * $lightness - 1)) * $saturation; // 0.5 |
|
| 24 | 65 | $x = $c * (1 - abs(fmod($hue / 60, 2) - 1)); |
|
| 25 | 65 | $m = $lightness - $c / 2; // 0.25 |
|
| 26 | |||
| 27 | 65 | switch (floor($hue / 60)) { |
|
| 28 | 65 | case 0: |
|
| 29 | 15 | $red = $c; |
|
| 30 | 15 | $green = $x; |
|
| 31 | 15 | $blue = 0; |
|
| 32 | 15 | break; |
|
| 33 | 50 | case 1: |
|
| 34 | 10 | $red = $x; |
|
| 35 | 10 | $green = $c; |
|
| 36 | 10 | $blue = 0; |
|
| 37 | 10 | break; |
|
| 38 | 40 | case 2: |
|
| 39 | 10 | $red = 0; |
|
| 40 | 10 | $green = $c; |
|
| 41 | 10 | $blue = $x; |
|
| 42 | 10 | break; |
|
| 43 | 30 | case 3: |
|
| 44 | 10 | $red = 0; |
|
| 45 | 10 | $green = $x; |
|
| 46 | 10 | $blue = $c; |
|
| 47 | 10 | break; |
|
| 48 | 20 | case 4: |
|
| 49 | 10 | $red = $x; // >> 64 ??? |
|
| 50 | 10 | $green = 0; // >> 64 |
|
| 51 | 10 | $blue = $c; // 0.5 >> (0.25+0.5)*255 >> 191.25 >> 191 OK |
|
| 52 | 10 | break; |
|
| 53 | 10 | case 5: |
|
| 54 | 10 | $red = $c; |
|
| 55 | 10 | $green = 0; |
|
| 56 | 10 | $blue = $x; |
|
| 57 | 10 | break; |
|
| 58 | } |
||
| 59 | |||
| 60 | 65 | return new RGB((int) round(($red + $m) * 255), (int) round(($green + $m) * 255), (int) round(($blue + $m) * 255)); |
|
| 61 | } |
||
| 62 | |||
| 63 | 924 | public static function supportsFrom(): string |
|
| 64 | { |
||
| 65 | 924 | return HSL::class; |
|
| 66 | } |
||
| 67 | |||
| 68 | 924 | public static function supportsTo(): string |
|
| 71 | } |
||
| 72 | } |
||
| 73 |