Colors   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 42
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEvenlyDistributedColors() 0 16 2
A getColors() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Barryvanveen\CCA\Generators;
6
7
use Barryvanveen\CCA\Config;
8
use Barryvanveen\CCA\Exceptions\InvalidColorsException;
9
use Phim\Color;
10
use Phim\Color\HsvColor;
11
12
class Colors
13
{
14
    const HSV_VALUE = 1.0;
15
16
    /**
17
     * @param Config $config
18
     *
19
     * @return Color\RgbColor[]
20
     *
21
     * @throws \Barryvanveen\CCA\Exceptions\InvalidColorsException
22
     */
23 9
    public static function getColors(Config $config): array
24
    {
25 9
        if ($config->imageColors() === null) {
26 3
            return self::getEvenlyDistributedColors($config->imageHue(), $config->states());
27
        }
28
29 6
        $colors = $config->imageColors();
30
31 6
        if (count($colors) !== $config->states()) {
32 3
            throw new InvalidColorsException("Not enough colors specified.");
33
        }
34
35 3
        return $colors;
36
    }
37
38 3
    protected static function getEvenlyDistributedColors(int $hue, int $numberOfColors): array
39
    {
40 3
        $saturationStepSize = 1/$numberOfColors;
41 3
        $saturationStart = $saturationStepSize/2;
42
43 3
        $colors = [];
44
45 3
        for ($i = 0; $i<$numberOfColors; $i++) {
46 3
            $saturation = ($saturationStart + ($i * $saturationStepSize));
47
48 3
            $hsv = new HsvColor($hue, $saturation, self::HSV_VALUE);
49
50 3
            $colors[] = $hsv->toRgb();
51
        }
52
53 3
        return $colors;
54
    }
55
}
56