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