Completed
Push — develop ( 2fbff0...213046 )
by Barry
01:42
created

Colors::getEvenlyDistributedColors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Barryvanveen\CCA\Generators;
4
5
use Barryvanveen\CCA\Config;
6
use Phim\Color;
7
use Phim\Color\HsvColor;
8
9
class Colors
10
{
11
    const HSV_VALUE = 1.0;
12
13
    /**
14
     * @param Config $config
15
     *
16
     * @return Color\RgbColor[]
17
     *
18
     * @throws \Barryvanveen\CCA\Exceptions\InvalidColorException
19
     * @throws \Barryvanveen\CCA\Exceptions\InvalidHueException
20
     */
21
    public static function getColors(Config $config): array
22
    {
23
        if ($config->imageColors() === null) {
24
            return self::getEvenlyDistributedColors($config->imageHue(), $config->states());
25
        }
26
27
        return $config->imageColors();
28
    }
29
30
    protected static function getEvenlyDistributedColors(int $hue, int $numberOfColors): array
31
    {
32
        $saturationStepSize = 1/$numberOfColors;
33
        $saturationStart = $saturationStepSize/2;
34
35
        $colors = [];
36
37
        for ($i = 0; $i<$numberOfColors; $i++) {
38
            $saturation = ($saturationStart + ($i * $saturationStepSize));
39
40
            $hsv = new HsvColor($hue, $saturation, self::HSV_VALUE);
41
42
            $colors[] = $hsv->toRgb();
43
        }
44
45
        return $colors;
46
    }
47
}
48