Completed
Push — develop ( ebc20b...a36a32 )
by Barry
01:39
created

Image::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Barryvanveen\CCA\Generators;
4
5
use Barryvanveen\CCA\Config;
6
use Barryvanveen\CCA\Coordinate;
7
use Barryvanveen\CCA\State;
8
9
abstract class Image
10
{
11
    /** @var Config */
12
    protected $config;
13
14
    /** @var array */
15
    protected $grid;
16
17
    /** @var mixed */
18
    protected $image;
19
20
    /** @var int[] */
21
    protected $colors;
22
23 18
    protected function __construct(Config $config, State $state)
24
    {
25 18
        $this->config = $config;
26
27 18
        $this->grid = $state->toArray();
28
29 18
        $this->initImage();
30
31 18
        $this->initColors();
32
33 18
        $this->createImage();
34 18
    }
35
36
    abstract public static function createFromState(Config $config, State $state);
37
38 18
    protected function initImage()
39
    {
40 18
        $this->image = imagecreatetruecolor($this->getImageWidth(), $this->getImageHeight());
41 18
    }
42
43 18
    protected function getImageWidth(): int
44
    {
45 18
        return $this->config->columns() * $this->config->imageCellSize();
46
    }
47
48 18
    protected function getImageHeight(): int
49
    {
50 18
        return $this->config->rows() * $this->config->imageCellSize();
51
    }
52
53 18
    protected function initColors()
54
    {
55 18
        $colors = Colors::getColors($this->config);
56
57 18
        foreach ($colors as $color) {
58 18
            $this->colors[] = imagecolorallocate($this->image, $color->getRed(), $color->getGreen(), $color->getBlue());
59
        }
60 18
    }
61
62 18
    protected function createImage()
63
    {
64 18
        $this->fillBackground();
65
66 18
        for ($row = 0; $row < $this->config->rows(); $row++) {
67 18
            for ($column = 0; $column < $this->config->columns(); $column++) {
68 18
                $state = $this->getStateFromGrid($row, $column);
69
70 18
                if ($state === 0) {
71 15
                    continue;
72
                }
73
74 18
                $this->fillCell($row, $column, $state);
75
            }
76
        }
77 18
    }
78
79 18
    protected function fillBackground()
80
    {
81 18
        imagefilledrectangle(
82 18
            $this->image,
83 18
            0,
84 18
            0,
85 18
            $this->getImageWidth(),
86 18
            $this->getImageHeight(),
87 18
            $this->colors[0]
88
        );
89 18
    }
90
91 18
    protected function getStateFromGrid(int $row, int $column): int
92
    {
93 18
        $coordinate = new Coordinate($row, $column, $this->config->columns());
94
95 18
        return $this->grid[$coordinate->position()];
96
    }
97
98 18
    protected function fillCell(int $row, int $column, int $state)
99
    {
100 18
        list($x1, $y1) = $this->getCellTopLeft($row, $column);
101
102 18
        list($x2, $y2) = $this->getCellBottomRight($row, $column);
103
104 18
        $color = $this->getColorFromState($state);
105
106 18
        imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $color);
107 18
    }
108
109 18
    protected function getCellTopLeft(int $row, int $column): array
110
    {
111 18
        $x = $column * $this->config->imageCellSize();
112 18
        $y = $row * $this->config->imageCellSize();
113
114 18
        return [$x, $y];
115
    }
116
117 18
    protected function getCellBottomRight(int $row, int $column): array
118
    {
119 18
        list($x, $y) = $this->getCellTopLeft($row, $column);
120
121 18
        $x += ($this->config->imageCellSize() - 1);
122 18
        $y += ($this->config->imageCellSize() - 1);
123
124 18
        return [$x, $y];
125
    }
126
127 18
    protected function getColorFromState(int $state): int
128
    {
129 18
        return $this->colors[$state];
130
    }
131
132 9
    public function get()
133
    {
134 9
        return $this->image;
135
    }
136
137
    abstract public function save();
138
}
139